From a879c958b62ae48f682f9893853f0d5bd9c3d04f Mon Sep 17 00:00:00 2001 From: chenruilong Date: Wed, 8 Feb 2023 11:43:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(uni-id-co):=20=E6=96=B0=E5=A2=9E=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E7=94=A8=E6=88=B7=E8=81=94=E7=99=BB=E5=90=8E=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 25308acdb3625c26c3f65655ef27fb930504b1b6) --- .../module/external/update-user-info.js | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/external/update-user-info.js diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/external/update-user-info.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/external/update-user-info.js new file mode 100644 index 0000000..f23a916 --- /dev/null +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/external/update-user-info.js @@ -0,0 +1,161 @@ +const { userCollection, EXTERNAL_DIRECT_CONNECT_PROVIDER } = require('../../common/constants') +const { ERROR } = require('../../common/error') +const { findUser } = require('../../lib/utils/account') +const PasswordUtils = require('../../lib/utils/password') + +/** + * 使用 uid 或 externalUid 获取用户信息 + * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#external-update-userinfo + * @param {object} params + * @param {string} params.uid uni-id体系的用户id + * @param {string} params.externalUid 业务系统的用户id + * @param {string} params.nickname 昵称 + * @param {string} params.gender 性别 + * @param {string} params.avatar 头像 + * @returns {object} + */ +module.exports = async function (params = {}) { + const schema = { + uid: { + required: false, + type: 'string' + }, + externalUid: { + required: false, + type: 'string' + }, + username: { + required: false, + type: 'string' + }, + password: { + required: false, + type: 'password' + }, + authorizedApp: { + required: false, + type: 'array' + }, // 指定允许登录的app,传空数组或不传时表示可以不可以在任何端登录 + nickname: { + required: false, + type: 'nickname' + }, + role: { + require: false, + type: 'array' + }, + mobile: { + required: false, + type: 'mobile' + }, + email: { + required: false, + type: 'email' + }, + tags: { + required: false, + type: 'array' + }, + status: { + required: false, + type: 'number' + } + } + + this.middleware.validate(params, schema) + + const { + uid, + externalUid, + username, + password, + authorizedApp, + nickname, + role, + mobile, + email, + tags, + status + } = params + + if (!uid && !externalUid) { + throw { + errCode: ERROR.PARAM_REQUIRED, + errMsgVal: { + param: 'uid or externalUid' + } + } + } + + let query + if (uid) { + query = { + _id: uid + } + } else { + query = { + identities: { + provider: EXTERNAL_DIRECT_CONNECT_PROVIDER, + uid: externalUid + } + } + } + + // 更新的用户数据字段 + const data = { + username, + dcloud_appid: authorizedApp, + nickname, + role, + mobile, + email, + tags, + status + } + + const realData = Object.keys(data).reduce((res, key) => { + const item = data[key] + if (item !== undefined) { + res[key] = item + } + return res + }, {}) + + // 更新用户名时验证用户名是否重新 + if (username) { + const { + userMatched + } = await findUser({ + userQuery: { + username + }, + authorizedApp + }) + if (userMatched.filter(user => user._id !== uid).length) { + throw { + errCode: ERROR.ACCOUNT_EXISTS + } + } + } + if (password) { + const passwordUtils = new PasswordUtils({ + clientInfo: this.getUniversalClientInfo(), + passwordSecret: this.config.passwordSecret + }) + const { + passwordHash, + version + } = passwordUtils.generatePasswordHash({ + password + }) + + realData.password = passwordHash + realData.password_secret_version = version + } + + await userCollection.where(query).update(realData) + + return { + errCode: 0 + } +} -- GitLab