From 4017f8e691fb5a5c93928b9f45c35e06c4a92f40 Mon Sep 17 00:00:00 2001 From: chenruilong Date: Wed, 23 Nov 2022 16:44:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(uni-id-co):=20=E6=96=B0=E5=A2=9E=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=AF=86=E7=A0=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages.json | 6 ++ uni_modules/uni-id-pages/common/store.js | 78 ++++++++++------- uni_modules/uni-id-pages/config.js | 13 ++- .../uni-id-co/common/constants.js | 6 +- .../uni-id-co/config/permission.js | 3 + .../cloudfunctions/uni-id-co/index.obj.js | 1 + .../uni-id-co/lib/utils/register.js | 3 +- .../uni-id-co/module/account/index.js | 1 + .../uni-id-co/module/account/set-pwd.js | 83 +++++++++++++++++++ 9 files changed, 160 insertions(+), 34 deletions(-) create mode 100644 uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/set-pwd.js diff --git a/pages.json b/pages.json index 6f60db0..8ca282f 100644 --- a/pages.json +++ b/pages.json @@ -91,6 +91,12 @@ "navigationBarTitleText": "修改密码", "enablePullDownRefresh": false } + },{ + "path": "uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd", + "style": { + "navigationBarTitleText": "设置密码", + "enablePullDownRefresh": false + } } ], "globalStyle": { diff --git a/uni_modules/uni-id-pages/common/store.js b/uni_modules/uni-id-pages/common/store.js index 1c1d075..3a43b1a 100644 --- a/uni_modules/uni-id-pages/common/store.js +++ b/uni_modules/uni-id-pages/common/store.js @@ -1,4 +1,6 @@ import pagesJson from '@/pages.json' +import config from '@/uni_modules/uni-id-pages/config.js' + const uniIdCo = uniCloud.importObject("uni-id-co") const db = uniCloud.database(); const usersTable = db.collection('uni-id-users') @@ -69,9 +71,44 @@ export const mutations = { uni.$emit('uni-id-pages-logout') this.setUserInfo({},{cover:true}) }, + + loginBack (e = {}) { + const {uniIdRedirectUrl = ''} = e + let delta = 0; //判断需要返回几层 + let pages = getCurrentPages(); + // console.log(pages); + pages.forEach((page, index) => { + if (pages[pages.length - index - 1].route.split('/')[3] == 'login') { + delta++ + } + }) + // console.log('判断需要返回几层:', delta); + if (uniIdRedirectUrl) { + return uni.reLaunch({ + url: uniIdRedirectUrl + }) + } + // #ifdef H5 + if (e.loginType == 'weixin') { + console.log('window.history', window.history); + return window.history.go(-3) + } + // #endif + + if (delta) { + const page = pagesJson.pages[0] + return uni.reLaunch({ + url: `/${page.path}` + }) + } + + uni.navigateBack({ + delta + }) + }, loginSuccess(e = {}){ const { - showToast = true, toastText = '登录成功', autoBack = true, uniIdRedirectUrl = '' + showToast = true, toastText = '登录成功', autoBack = true, uniIdRedirectUrl = '', passwordConfirmed } = e console.log({ toastText, @@ -84,39 +121,20 @@ export const mutations = { }); } this.updateUserInfo() + uni.$emit('uni-id-pages-login-success') - if (autoBack) { - let delta = 0; //判断需要返回几层 - let pages = getCurrentPages(); - // console.log(pages); - pages.forEach((page, index) => { - if (pages[pages.length - index - 1].route.split('/')[3] == 'login') { - delta++ + + if (config.setPasswordAfterLogin && !passwordConfirmed) { + return uni.redirectTo({ + url: uniIdRedirectUrl ? `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?uniIdRedirectUrl=${uniIdRedirectUrl}&loginType=${e.loginType}`: `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?loginType=${e.loginType}`, + fail: (err) => { + console.log(err) } }) - // console.log('判断需要返回几层:', delta); - if (uniIdRedirectUrl) { - return uni.reLaunch({ - url: uniIdRedirectUrl - }) - } - // #ifdef H5 - if (e.loginType == 'weixin') { - console.log('window.history', window.history); - return window.history.go(-3) - } - // #endif - - if (delta) { - const page = pagesJson.pages[0] - return uni.reLaunch({ - url: `/${page.path}` - }) - } + } - uni.navigateBack({ - delta - }) + if (autoBack) { + this.loginBack(uniIdRedirectUrl) } } diff --git a/uni_modules/uni-id-pages/config.js b/uni_modules/uni-id-pages/config.js index dcf9194..b03a2bf 100644 --- a/uni_modules/uni-id-pages/config.js +++ b/uni_modules/uni-id-pages/config.js @@ -52,5 +52,16 @@ export default { * weak(弱:密码必须包含字母和数字,长度范围:6-16位之间) * 为空或false则不验证密码强度 */ - "passwordStrength":"medium" + "passwordStrength":"medium", + /** + * 登录后允许用户设置密码(只针对未设置密码得用户) + * 开启此功能将 setPasswordAfterLogin 设置为 true 即可 + * "setPasswordAfterLogin": false + * + * 如果允许用户跳过设置密码 将 allowSkip 设置为 true + * "setPasswordAfterLogin": { + * "allowSkip": true + * } + * */ + "setPasswordAfterLogin": false } diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/constants.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/constants.js index f5888c4..a23dee4 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/constants.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/common/constants.js @@ -41,7 +41,8 @@ const CAPTCHA_SCENE = { RESET_PWD_BY_EMAIL: 'reset-pwd-by-email', SEND_SMS_CODE: 'send-sms-code', SEND_EMAIL_CODE: 'send-email-code', - BIND_MOBILE_BY_SMS: 'bind-mobile-by-sms' + BIND_MOBILE_BY_SMS: 'bind-mobile-by-sms', + SET_PWD_BY_SMS: 'set-pwd-by-sms' } const LOG_TYPE = { @@ -64,7 +65,8 @@ const LOG_TYPE = { const SMS_SCENE = { LOGIN_BY_SMS: 'login-by-sms', RESET_PWD_BY_SMS: 'reset-pwd-by-sms', - BIND_MOBILE_BY_SMS: 'bind-mobile-by-sms' + BIND_MOBILE_BY_SMS: 'bind-mobile-by-sms', + SET_PWD_BY_SMS: 'set-pwd-by-sms' } const EMAIL_SCENE = { diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/config/permission.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/config/permission.js index 4d69441..8a06222 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/config/permission.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/config/permission.js @@ -74,5 +74,8 @@ module.exports = { }, unbindApple: { auth: true + }, + setPwd: { + auth: true } } diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js index d0e5d55..8ead4f6 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/index.obj.js @@ -50,6 +50,7 @@ const { unbindApple } = require('./module/relate/index') const { + setPwd, updatePwd, resetPwdBySms, resetPwdByEmail, diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/register.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/register.js index 821e8ca..5a53858 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/register.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/lib/utils/register.js @@ -201,7 +201,8 @@ async function postRegister (params = {}) { } }) : {} - ) + ), + passwordConfirmed: !!userRecord.password } } diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/index.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/index.js index f43cd27..b4e06d6 100644 --- a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/index.js +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/index.js @@ -1,4 +1,5 @@ module.exports = { + setPwd: require('./set-pwd'), updatePwd: require('./update-pwd'), resetPwdBySms: require('./reset-pwd-by-sms'), resetPwdByEmail: require('./reset-pwd-by-email'), diff --git a/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/set-pwd.js b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/set-pwd.js new file mode 100644 index 0000000..f33c6f4 --- /dev/null +++ b/uni_modules/uni-id-pages/uniCloud/cloudfunctions/uni-id-co/module/account/set-pwd.js @@ -0,0 +1,83 @@ +const { userCollection, SMS_SCENE, LOG_TYPE, CAPTCHA_SCENE } = require('../../common/constants') +const { ERROR } = require('../../common/error') +const { verifyMobileCode } = require('../../lib/utils/verify-code') +const PasswordUtils = require('../../lib/utils/password') +const { getNeedCaptcha, verifyCaptcha } = require('../../lib/utils/captcha') + +module.exports = async function (params = {}) { + const schema = { + password: 'password', + code: 'string', + captcha: { + required: false, + type: 'string' + } + } + this.middleware.validate(params, schema) + + const { password, code, captcha } = params + const uid = this.authInfo.uid + const getUserRes = await userCollection.doc(uid).get() + const userRecord = getUserRes.data[0] + if (!userRecord) { + throw { + errCode: ERROR.ACCOUNT_NOT_EXISTS + } + } + + const needCaptcha = await getNeedCaptcha.call(this, { + mobile: userRecord.mobile + }) + + if (needCaptcha) { + await verifyCaptcha.call(this, { + captcha, + scene: CAPTCHA_SCENE.SET_PWD_BY_SMS + }) + } + + try { + // 验证手机号验证码,验证不通过时写入失败日志 + await verifyMobileCode({ + mobile: userRecord.mobile, + code, + scene: SMS_SCENE.SET_PWD_BY_SMS + }) + } catch (error) { + await this.middleware.uniIdLog({ + data: { + mobile: userRecord.mobile + }, + type: LOG_TYPE.SET_PWD_BY_SMS, + success: false + }) + throw error + } + + const { + passwordHash, + version + } = new PasswordUtils({ + clientInfo: this.getUniversalClientInfo(), + passwordSecret: this.config.passwordSecret + }).generatePasswordHash({ + password + }) + + // 更新用户密码 + await userCollection.doc(uid).update({ + password: passwordHash, + password_secret_version: version + }) + + await this.middleware.uniIdLog({ + data: { + mobile: userRecord.mobile + }, + type: LOG_TYPE.SET_PWD_BY_SMS + }) + + return { + errCode: 0 + } +} -- GitLab