v0.0.4
This commit is contained in:
24
cloudfunctions/draftSave/index.js
Normal file
24
cloudfunctions/draftSave/index.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
|
||||
exports.main = async event => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
const now = Date.now()
|
||||
const data = {
|
||||
openid: OPENID,
|
||||
draft: event.draft || {},
|
||||
updatedAt: now
|
||||
}
|
||||
const existed = await db.collection('drafts').where({ openid: OPENID }).limit(1).get()
|
||||
|
||||
if (existed.data.length) {
|
||||
await db.collection('drafts').doc(existed.data[0]._id).update({ data })
|
||||
return { draftId: existed.data[0]._id, updatedAt: now }
|
||||
}
|
||||
|
||||
const result = await db.collection('drafts').add({ data })
|
||||
return { draftId: result._id, updatedAt: now }
|
||||
}
|
||||
|
||||
9
cloudfunctions/draftSave/package.json
Normal file
9
cloudfunctions/draftSave/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "draftSave",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"wx-server-sdk": "latest"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user