VerifyCode.vue
3.77 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
<template>
<div>
<div :class="className" @click="requestSMS"><span></span>{{count}}{{message}}</div>
<el-dialog :append-to-body="true" title="图形验证码" :visible.sync="dialogVisible" width="300px">
<el-form ref="form" inline :model="form" :rules="rules">
<el-form-item prop="code">
<el-input v-model="form.code" auto-complete="off" placeholder="验证码" style="width: 63%">
<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
</el-input>
<div class="login-code">
<img :src="codeUrl" @click="getCode" class="login-code-img" />
</div>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<!-- <el-button @click="dialogVisible = false">取 消</el-button> -->
<el-button type="primary" @click="confirmFn">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import { getImageCode, getCodeInPhone } from '@/api/login'
export default {
name: 'verify-code',
props: {
// 用户电话
telNumber: {
required: true
}
},
data() {
return {
form: {
code: ''
},
codeUrl: '',
dialogVisible: false,
className: 'clickable',
message: '发送验证码',
count: '',
send: true,
timer: null,
reg: /^((1[0-9][0-9]))\d{8}$/,
rules: {
code: [
{ required: true, trigger: 'blur', message: '请输入验证码' }
]
}
}
},
methods: {
confirmFn() {
this.$refs.form.validate(vali => {
if (vali) {
getCodeInPhone({
account: this.telNumber,
imgCode: this.form.code,
uuid: this.form.uuid
}).then(res => {
if (res.code === 200) {
this.msgSuccess('短信发送成功')
this.send = false
this.className = 'unclickable'
this.message = 's后重发'
const TOTAL_TIME = 60
if (!this.timer) {
this.count = TOTAL_TIME
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TOTAL_TIME) {
this.count--
} else {
clearInterval(this.timer)
this.timer = null
this.count = ''
this.message = '重新发送'
this.className = 'clickable'
this.send = true
}
}, 1000)
}
} else {
this.getCode()
}
this.dialogVisible = false
})
}
})
},
getCode() {
getImageCode().then(res => {
if (res.code === 200) {
this.codeUrl = 'data:image/gif;base64,' + res.img
this.form.uuid = res.uuid
}
})
},
requestSMS() {
if (this.send) {
if (this.$props.telNumber === '' || this.$props.telNumber === undefined) {
this.msgError('请输手机号码')
} else if (!this.reg.test(this.$props.telNumber)) {
// 核对手机号格式
this.msgError('请输正确的手机号码')
} else {
this.getCode()
this.dialogVisible = true
}
}
}
}
}
</script>
<style scoped lang="scss">
.clickable {
font-size: 12px;
color: #5474c2;
letter-spacing: 0;
text-align: center;
cursor: pointer;
}
.unclickable {
font-size: 12px;
color: #b5bbc4;
letter-spacing: 0;
text-align: center;
}
span {
color: #b5bbc4;
margin-right: 5px;
}
.login-code {
width: 33%;
height: 38px;
float: right;
img {
cursor: pointer;
vertical-align: middle;
}
}
.login-code-img {
height: 38px;
}
</style>