authPhoneDialog.vue
4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<template>
<baseDialog
:is-show.sync="dialogShow"
:dialog-config="dialogConfig.config"
class="detail-content-warn"
@handleClick="submit"
>
<el-form
class="form"
label-width="150px"
size="small"
>
<el-form-item
label="手机号:"
required
:error="phoneError"
>
<el-input
v-model="phone"
class="input-width"
disabled
></el-input>
</el-form-item>
<el-form-item
label="验证码:"
required
:error="smsError"
>
<div class="sms-code flex">
<el-input
v-model="smsCode"
class="input-width"
:maxlength="6"
placeholder="请输入验证码"
@input="handleInput"
>
clearable
>
</el-input>
<el-button
ref="smsCodeBtn"
type="primary"
:plain="countDownStatus"
:disabled="countDownStatus"
class="get-sms-code"
@click="getSmsCode"
>{{smsBtnText}}</el-button>
</div>
</el-form-item>
</el-form>
</basedialog>
</template>
<script>
import { sendSmsCode, verifySmsCode } from "@api/usedCarManagement"
export default {
name: "AuthPhoneDialog",
inject: ['userInfo'],
props: {
visible: {
type: Boolean,
default: false
},
},
computed: {
dialogShow: {
get () {
return this.visible
},
set (val) {
this.$emit('dialogClose')
}
},
phone () {
return this.userInfo.loginAccount.phone || ''
}
},
watch: {
visible: {
handler (val) {
if (val) {
this.phoneError = ''
this.smsError = ''
this.smsCode = ''
}
},
immediate: true,
deep: true
}
},
mounted () {
},
data () {
return {
phoneError: '',
smsError: '',
smsBtnText: '获取验证码',
count: 60,
smsCode: '',
countDownStatus: false,
dialogConfig: {
config: {
title: "短信验证",
width: "640px"
}
},
}
},
methods: {
handleInput (value) {
this.smsCode = value.replace(/\D/g, '')
},
async getSmsCode (e) {
try {
let { phone } = this
if (!phone) return this.phoneError = '手机号不能为空'
if (!/^1[3456789]\d{9}$/.test(phone)) return this.phoneError = '手机号格式错误'
this.countDownStatus = true
let data = await sendSmsCode({
phone
})
if (data) {
this.countDown()
let target = e.target
if (target.nodeName == 'SPAN') target = e.target.parentNode
target.blur()
this.notice.okTips('发送短信成功')
} else {
this.countDownStatus = false
this.notice.errorTips('发送短信失败')
}
} catch (error) {
this.countDownStatus = false
}
},
countDown () {
this.count--
let count = this.count
this.smsBtnText = `获取验证码(${count})`
if (count <= 0 || !this.countDownStatus) {
this.count = 60
this.countDownStatus = false
this.smsBtnText = '获取验证码'
return
}
setTimeout(() => {
this.countDown()
}, 1e3)
},
async submit () {
this.smsError = ''
this.phoneError = ''
let { phone, smsCode } = this
if (!phone) return this.phoneError = '手机号不能为空'
if (!/^1[3456789]\d{9}$/.test(phone)) return this.phoneError = '手机号格式错误'
if (!smsCode) return this.smsError = '验证码不能为空'
if (smsCode.length < 6) return this.smsError = '验证码错误,请重试!'
let { code } = await verifySmsCode({
phone,
smsCode
})
if (code == 0) {
this.$emit('loginSuccess')
this.count = 60
this.countDownStatus = false
this.smsBtnText = '获取验证码'
this.smsCode = ''
} else this.smsError = '验证码错误,请重试!'
}
}
}
</script>
<style lang="scss" scoped>
.flex {
display: flex;
align-items: center;
}
.detail-content-warn {
::v-deep .el-dialog {
height: 360px;
.el-dialog__body {
border-bottom: 1px solid #e6e6e6;
}
}
.form {
height: 195px;
padding: 17px 22px 0 0;
box-sizing: border-box;
}
.sms-code {
gap: 8px;
.get-sms-code {
width: 160px;
}
}
}
</style>