diff --git a/pages.json b/pages.json index 6f60db0f87281f48bf72c6a7b2d542520154d7f3..8ca282f4eaadd104ca3981d292eadb0dcdfa1722 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 1c1d075810f163e2ad0feadc309f89f7c2972c7e..3a43b1aa7a7db7912fb2bf696356f15597e83a2b 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 dcf9194e29f7f868d1142f8c688417f98393ddd7..b03a2bf94ab8e55b989ec33db81932052dbff939 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 f5888c42247739b60ef3be81774e445d5536db06..a23dee489e413771ee85b769402714870f445497 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 4d694418040f9c646df5f42fb0b87b0d73187941..8a062225bb487b214b31e4a3f6254f4869da12c6 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 d0e5d557a2fb78ab03eb8deec75470d8df1c5ede..8ead4f641acb40d02a55e34b0ecb426b8c142f2d 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 821e8ca4992062553f7f2f06e885186afb7eb921..5a5385876ca17b2e6d112f71eb59f2cfad636cf6 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 f43cd273ecf3db93a98aabadb3c0f9d2e659b438..b4e06d6fae2cdc76032a5a83b578d9d25d67568a 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 0000000000000000000000000000000000000000..f33c6f4698352780d2e9fdfeb61dd5e3ae910bf2 --- /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 + } +}