mix-code.vue
2.2 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
<template>
<view class="mix-get-code" @click="getCode">
<view v-if="loading" class="loading">
<mix-icon-loading size="28rpx" color="#0083ff"></mix-icon-loading>
</view>
<text class="text" :class="{disabled: timeDown > 0}">
{{ timeDown > 0 ? '重新获取 ' + timeDown + 's' : '获取验证码' }}
</text>
</view>
</template>
<script>
/**
* 手机验证码
* @prop mobile 手机号
* @prop templateCode 短信模版id
*/
import {checkStr} from '@/common/js/util'
export default {
//获取手机验证码
name: 'MixMobileCode',
data() {
return {
loading: false,
timeDown: ''
}
},
props: {
mobile: {
type: String,
default: ''
},
templateCode: {
type: String,
default: ''
},
action: {
type: String,
default: '用户注册' //设置支付密码
}
},
methods: {
//获取验证码
async getCode(){
if(this.timeDown > 0){
return;
}
this.$util.throttle(()=>{
const mobile = this.mobile || this.$store.state.userInfo.username;;
if(!checkStr(mobile, 'mobile')){
this.$util.msg('手机号码格式不正确');
return;
}
this.loading = true;
this.$request('smsCode', 'send', {
mobile,
action: this.action, //uni短信必填
TemplateCode: this.templateCode, //阿里云必填
}).then(response=>{
this.$util.msg(response.msg);
this.loading = false;
if(response.status === 1){
this.countDown(60);
}
}).catch(err=>{
this.$util.msg('验证码发送失败');
this.loading = false;
console.log(err);
})
}, 2000)
},
//倒计时
countDown(timer){
timer --;
this.timeDown = timer;
if(timer > 0){
setTimeout(()=>{
this.countDown(timer);
}, 1000)
}
},
}
}
</script>
<style scoped lang="scss">
.mix-get-code{
flex-shrink: 0;
display: flex;
justify-content: space-between;
align-items: center;
height: 36rpx;
&:before{
content: '';
width: 0;
height: 40;
border-right: 1px solid #f0f0f0;
}
.loading{
margin-right: 8rpx;
}
.text{
line-height: 28rpx;
font-size: 26rpx;
color: #40a2ff;
&.disabled{
color: #ccc;
}
}
}
</style>