Commit c2db39b8 by liimin

新增登录短信验证流程

1 parent 2badd452
......@@ -30,6 +30,18 @@ const updatePassword = data => {
data.method = 'scrm.api.sso.user.updatePassword'
return http({ data, returnAll: true })
}
const verifyPhone = data => {
data.method = 'scrm.api.sso.user.login.account.phone.get'
return http({ data, returnAll: true })
}
const getsmscode = data => {
data.method = 'scrm.api.sso.user.sms.get.code'
return http({ data, returnAll: true })
}
const smscodecheck = data => {
data.method = 'scrm.api.sso.user.sms.code.check'
return http({ data, returnAll: true })
}
export default {
login,
logout,
......@@ -37,5 +49,8 @@ export default {
exchange,
permission,
code,
updatePassword
updatePassword,
verifyPhone,
getsmscode,
smscodecheck
}
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2021-08-19 11:16:08
* @LastEditors: limin
* @LastEditTime: 2022-07-25 17:46:03
* @LastEditTime: 2022-11-07 21:30:55
* @Description:
-->
<template>
......@@ -217,6 +217,7 @@ export default {
&.el-button--default {
&:hover,
&:focus {
border-color: #7777f3;
color: $main-color !important;
}
}
......
......@@ -72,6 +72,7 @@
</div>
</div>
</div>
<LoginSMSCodeCheck :visible.sync="smsCodeDialogVisible" :checkProps="smsCheckProps" @onSuccess="afterVerify"></LoginSMSCodeCheck>
</div>
</template>
......@@ -82,6 +83,7 @@ import api from '@/api'
import md5 from 'md5'
import { Message } from '@/plugins/element'
import { constants } from '@/common'
import LoginSMSCodeCheck from './components/LoginSMSCodeCheck.vue'
const genRules = field => [{ required: true, message: `请输入${field}`, trigger: 'blur' }]
const RULES = Object.freeze({
userName: genRules('用户名'),
......@@ -98,6 +100,9 @@ export default {
this.getCode()
this.clearAmimate()
},
components: {
[LoginSMSCodeCheck.name]: LoginSMSCodeCheck
},
data() {
return {
loginForm: {
......@@ -114,7 +119,23 @@ export default {
isDoAnimate: true,
showTip: false,
isHasMutilMerchants: false,
rules: RULES
smsCodeDialogVisible: false,
rules: RULES,
//手机号验证组件所需参数
smsCheckProps: {
sessionId: '',
mobile: '13800138000'
},
//登录成功后,短信验证前保存的登录数据
loginSuccessData: {}
}
},
watch: {
smsCodeDialogVisible: function (val) {
if (!val) {
this.isLogin = false
this.getCode()
}
}
},
computed: {
......@@ -141,7 +162,8 @@ export default {
* 1.校验输入
* 2.MD5加密密码
* 3.调登录接口登录
* 4.判断是否有多个商户(有:弹出选择商户的界面 没有:获取菜单信息)
* 4.调接口判断是否要验证短信验证码 插入流程 2022-11-07
* 5.判断是否有多个商户(有:弹出选择商户的界面 没有:获取菜单信息)
*/
submitForm: utils.debounce(function () {
this.errMsg = ''
......@@ -152,22 +174,31 @@ export default {
const loginData = Object.assign({ loginSource, productCode }, this.loginForm, { password: md5(this.loginForm.password).toLowerCase() })
let { code, data, message } = await api.app.login(loginData)
if (code === response.code_success_value) {
const { loginList: merchants, loginToken } = data
if (merchants.length > 1) {
this.loginForm.loginToken = loginToken
this.merchants = merchants
this.doAnimate()
this.isHasMutilMerchants = true
} else {
this._permission(merchants[0])
}
//将登录成功信息存起来。当短信验证完后,需要用来走后面的流程
this.loginSuccessData = data
//调接口判断是否要验证短信验证码
this.verifyPhone(data)
} else {
this.error(message)
}
}
})
}, 500),
/**
* 验证完短信,判断是否绑定多个商户。如果有多个,则打开选择界面
* 如果只有一个,则直接获取权限信息
*/
afterVerify() {
const { loginList: merchants, loginToken } = this.loginSuccessData
if (merchants.length > 1) {
this.loginForm.loginToken = loginToken
this.merchants = merchants
this.doAnimate()
this.isHasMutilMerchants = true
} else {
this._permission(merchants[0])
}
},
/**
* 获取商户下的菜单信息
*/
......@@ -223,12 +254,41 @@ export default {
clearAmimate() {
setTimeout(() => (this.isDoAnimate = false), 500)
},
/**
* 选择商户界面返回
*/
forback() {
this.isLogin = false
this.doAnimate()
this.getCode()
this.isHasMutilMerchants = false
},
/**
* 登录成功,验证本账号是否需要短信验证登录
* 如果不需要验证,直接调取是否选择门店方法
* 如果需要验证,弹出验证窗口
*/
async verifyPhone() {
const loginData = this.loginSuccessData
let { response } = this.$config
try {
const loginAccount = this.loginForm.userName
const sessionId = loginData.loginList[0]?.sessionId
const { code, message, data: mobile } = await api.app.verifyPhone({ loginAccount, sessionId })
if (code == response.code_success_value) {
if (mobile) {
this.smsCodeDialogVisible = true
Object.assign(this.smsCheckProps, { sessionId, mobile })
} else {
this.afterVerify()
}
} else {
this.error(message)
}
} catch (error) {
this.error(error.message, ERROR_TYPE.MESSAGE)
}
},
...mapActions('app', ['login']),
...mapActions('auth', ['permission', 'setSession']),
...mapActions('track', ['setTracker'])
......@@ -395,7 +455,7 @@ $border-radius: 20px;
border-radius: 0 $border-radius $border-radius 0;
.el-alert {
position: absolute;
top: 20px;
top: 12px;
width: 345px;
background: #fff1f0;
border-radius: 4px;
......
<!--
* @Author: limin
* @Date: 2022-11-07 11:16:08
* @LastEditors: limin
* @LastEditTime: 2022-11-07 21:30:08
* @Description:
-->
<template>
<div class="com-login-sms-code-check">
<el-dialog title="手机号验证" :visible.sync="dialogVisible" top="32vh" width="576px" height="292px" :destroy-on-close="true" :close-on-click-modal="false">
<span class="notify">为了您的账号安全,请输入发送至 +86 {{ checkProps.mobile }} 的6位验证码</span>
<el-form :model="smsCodeCheckForm" @keyup.enter.native="submitForm" :rules="rules" ref="smsCodeCheckForm" class="smsCodeCheckForm">
<el-form-item prop="smscode">
<el-input clearable v-model.trim="smsCodeCheckForm.smscode" placeholder="请输入短信验证码" maxlength="6" onkeyup="value=value.replace(/[^\d]/g,' ')"></el-input>
<el-button type="text" @click="getSmsCode">获取验证码</el-button>
<span class="time-counter" v-show="isShowtimeCounter">{{ counter }}</span>
</el-form-item>
</el-form>
<div slot="footer">
<div class="btn-wrapper">
<el-button @click="cancle">取消</el-button>
<el-button type="primary" @click="submitForm" :loading="loading">提交</el-button>
</div>
</div>
</el-dialog>
</div>
</template>
<script>
import { utils } from 'glsx-common'
import api from '@/api'
import { Message } from '@/plugins/element'
const counter = 60
export default {
name: 'LoginSMSCodeCheck',
props: {
visible: {
type: Boolean,
default: false
},
checkProps: {
type: Object,
default: null
}
},
data() {
return {
smsCodeCheckForm: {
smscode: ''
},
isShowtimeCounter: false,
counter,
timer: null,
loading: false,
rules: {
smscode: [{ validator: this.checkSmsCode, trigger: 'blur' }]
}
}
},
computed: {
dialogVisible: {
get() {
return this.visible
},
set(v) {
this.$emit('update:visible', v)
}
}
},
methods: {
/**
* 点击提交按钮,开启表单验证
* 1.验证码是否为空
* 2.异步验证验证码是否正确
* 以上两步均通过验证,关闭验证弹窗,触发验证成功事件
*/
submitForm: utils.debounce(function () {
this.$refs['smsCodeCheckForm'].validate(async valid => {
this.loading = false
if (valid) {
this.cancle()
this.$emit('onSuccess')
}
})
}, 500),
/**
* 获取短信验证码
*/
async getSmsCode() {
try {
//打开计时器倒计时60秒
this.startTimer()
const { code, message } = await api.app.getsmscode(Object.assign({}, this.checkProps))
let { response } = this.$config
if (code == response.code_success_value) {
Message.success('获取短信验证码成功')
} else {
Message.error(`获取短信验证码失败:${message}`)
}
} catch (error) {
Message.error(`获取短信验证码失败:${error.message}`)
}
},
/**
* 异步验证短信验证码
* callback
*/
async smsCodeCheck(callback) {
try {
const { code, message } = await api.app.smscodecheck(Object.assign({}, this.smsCodeCheckForm, this.checkProps))
let { response } = this.$config
if (code == response.code_success_value) {
callback()
} else {
callback(new Error(message))
}
} catch (error) {
callback(new Error(error.message))
}
},
/**
* 自定义验证码验证规则
*/
checkSmsCode(rule, value, callback) {
if (!value) {
//不能为空
return callback(new Error('请输入短信验证码'))
} else {
//异步验证
return this.smsCodeCheck(callback)
}
},
/**
* 开启定时器 计时60秒
*/
startTimer() {
this.isShowtimeCounter = true
this.timer = setInterval(() => {
this.counter--
if (this.counter === 0) {
this.clearTimer()
}
}, 1000)
},
/**
* 关闭定时器
*/
clearTimer() {
if (this.timer) {
clearInterval(this.timer)
this.timer = null
this.isShowtimeCounter = false
this.counter = counter
}
},
/**
* 关闭短信验证码弹窗
* 重置验证码输入框
*/
cancle() {
this.dialogVisible = false
this.$refs['smsCodeCheckForm'].resetFields()
}
}
}
</script>
<style lang="scss" scoped>
.com-login-sms-code-check {
/deep/.el-dialog {
box-shadow: 0px 10px 20px 0px rgba(88, 80, 255, 0.1);
.notify {
width: 488px;
height: 24px;
font-size: 16px;
color: #333;
line-height: 24px;
margin-bottom: 24px;
display: block;
margin-top: 32px;
}
&__header {
padding: 0;
background-color: #fff;
border-bottom: 1px solid #e5e5e5;
}
&__title {
color: #333;
font-weight: 600;
height: 60px;
line-height: 60px;
text-align: center;
padding-left: 20px;
}
&__close {
font-size: 18px;
font-weight: 600;
&:hover {
color: $main-color;
}
}
&__body {
width: 488px;
margin: 0 auto;
text-align: center;
.el-form {
&-item {
margin-bottom: 18px;
&__label {
line-height: 30px;
float: none;
}
&__content {
width: 284px;
margin: 0 auto;
.el-input__inner {
&:focus {
border-color: $main-color;
}
}
.el-button--text {
color: $main-color;
position: absolute;
right: 10px;
}
}
.time-counter {
display: block;
position: absolute;
right: 0;
top: 0;
text-align: center;
width: 6em;
letter-spacing: 1px;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
font-weight: 600;
border-radius: 0 4px 4px 0;
}
}
}
}
&__footer {
text-align: center;
border-top: 1px solid #e5e5e5;
margin-top: 48px;
.btn-wrapper {
margin: 0 auto;
width: 284px;
height: 64px;
display: flex;
justify-content: center;
align-items: center;
.el-button {
min-width: 60px;
height: 32px;
padding: 6px 16px;
&.el-button--primary {
background-color: $main-color;
border-color: $main-color;
&:hover,
&:focus {
border-color: #7777f3;
background-color: #7777f3;
}
}
&.el-button--default {
&:hover,
&:focus {
border-color: #7777f3;
color: $main-color !important;
}
}
}
}
}
}
}
</style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!