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
<template>
<div class="app-preset-smsverification">
<div class="content">
<i-input
size="default"
type="text"
v-model="phoneNumber"
:placeholder="$t('components.login.phoneplaceholder')"
@on-blur="veriPhoneNumber"
></i-input>
<el-button
:disabled="disabled"
size="mini"
type="primary"
@click="getVeriCode()"
>{{ disabled ? `${countDown}s ${$t('components.login.getcodeafter')}` : `${$t('components.login.getcode')}` }}</el-button
>
</div>
<div class="code">
<i-input
size="default"
type="text"
:value="currentValue"
@input="codeChange"
:placeholder="$t('components.login.codeplaceholder')"
></i-input>
</div>
</div>
</template>
<script lang='ts'>
import { Vue, Component, Prop, Model } from "vue-property-decorator";
import { Http } from "ibiz-core";
@Component({})
export default class AppPresetSmsVerification extends Vue {
/**
*名称
*
* @type {string}
* @memberof AppPresetSmsVerification
*/
@Prop() public name!: string;
/**
*验证码值
*
* @type {string}
* @memberof AppPresetSmsVerification
*/
@Prop() public value!: string;
/**
*验证码当前值
*
* @type {string}
* @memberof AppPresetSmsVerification
*/
get currentValue(): string {
return this.value;
}
/**
* 手机号
*
* @type {*}
* @memberof AppPresetSmsVerification
*/
public phoneNumber: string = '';
/**
* 倒计时
*
* @type {*}
* @memberof AppPresetSmsVerification
*/
public countDown: number = 60;
/**
* 错误提示
* @type {*}
* @memberof AppPresetSmsVerification
*/
public phoneError = false;
/**
* 是否禁用获取验证码按钮
*
* @type {*}
* @memberof AppPresetSmsVerification
*/
public disabled: boolean = false;
/**
* @description 网络请求接口对象
* @type {IHttp}
* @memberof AppPresetSmsVerification
*/
public http: any = Http.getInstance();
/**
* 定时器
*
* @type {*}
* @memberof AppPresetSmsVerification
*/
public timer: any;
/**
* 组件创建时触发
*
* @type {*}
* @memberof AppPresetSmsVerification
*/
public created() {
this.$emit("valueChange", { name: this.name, value: '' });
}
/**
* @description 设置倒计时
* @memberof AppPresetSmsVerification
*/
public setCountDown() {
if (this.countDown > 0) {
this.countDown--;
} else {
this.countDown = 60;
this.disabled = false;
clearInterval(this.timer);
}
this.$forceUpdate();
}
/**
* @description 验证码输入变化
* @memberof AppPresetSmsVerification
*/
public codeChange(value: string) {
this.$emit("valueChange", { name: this.name, value });
}
/**
* @description 校验手机号
* @memberof AppPresetSmsVerification
*/
public veriPhoneNumber(): boolean {
this.phoneError = !/^1[3-9]\d{9}$/.test(this.phoneNumber);
if(this.phoneError) {
this.$emit("error", this.$t('components.login.phonefailed'));
} else {
this.$emit("error", '');
}
return this.phoneError;
}
/**
* @description 获取验证码
* @memberof AppPresetSmsVerification
*/
public getVeriCode() {
if(this.phoneError) return;
this.http.post(``, {}).then((response: any) => {
if (response.status == 200) {
this.disabled = true;
this.timer = setInterval(() => {
this.setCountDown();
}, 1000);
}
});
}
}
</script>
<style lang='less'>
@import "./app-preset-smsverification.less";
</style>