提交 4017f8e6 编写于 作者: C chenruilong

feat(uni-id-co): 新增设置密码接口

上级 15c6de13
......@@ -91,6 +91,12 @@
"navigationBarTitleText": "修改密码",
"enablePullDownRefresh": false
}
},{
"path": "uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd",
"style": {
"navigationBarTitleText": "设置密码",
"enablePullDownRefresh": false
}
}
],
"globalStyle": {
......
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)
}
}
......
......@@ -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
}
......@@ -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 = {
......
......@@ -74,5 +74,8 @@ module.exports = {
},
unbindApple: {
auth: true
},
setPwd: {
auth: true
}
}
......@@ -50,6 +50,7 @@ const {
unbindApple
} = require('./module/relate/index')
const {
setPwd,
updatePwd,
resetPwdBySms,
resetPwdByEmail,
......
......@@ -201,7 +201,8 @@ async function postRegister (params = {}) {
}
})
: {}
)
),
passwordConfirmed: !!userRecord.password
}
}
......
module.exports = {
setPwd: require('./set-pwd'),
updatePwd: require('./update-pwd'),
resetPwdBySms: require('./reset-pwd-by-sms'),
resetPwdByEmail: require('./reset-pwd-by-email'),
......
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
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册