v0.0.4
This commit is contained in:
28
cloudfunctions/profileUpdate/index.js
Normal file
28
cloudfunctions/profileUpdate/index.js
Normal file
@@ -0,0 +1,28 @@
|
||||
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()
|
||||
if (!OPENID) throw new Error('no openid in wx context')
|
||||
const patch = event.patch || {}
|
||||
delete patch.openid
|
||||
delete patch._id
|
||||
|
||||
const users = await db.collection('users').where({ openid: OPENID }).limit(1).get()
|
||||
if (!users.data.length) throw new Error('user not found')
|
||||
|
||||
// Setting a real avatar means the user has completed WeChat profile authorization
|
||||
if (patch.avatarUrl) patch.profileCompleted = true
|
||||
|
||||
await db.collection('users').doc(users.data[0]._id).update({
|
||||
data: {
|
||||
...patch,
|
||||
updatedAt: Date.now()
|
||||
}
|
||||
})
|
||||
const user = await db.collection('users').doc(users.data[0]._id).get()
|
||||
return { user: user.data }
|
||||
}
|
||||
|
||||
9
cloudfunctions/profileUpdate/package.json
Normal file
9
cloudfunctions/profileUpdate/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "profileUpdate",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"wx-server-sdk": "latest"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user