SendCode.uvue
1.17 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
<template>
<cl-button @tap="sendCode">
{{ isSendRes ? `${number }s 后重试` : text }}
</cl-button>
</template>
<script setup lang="uts">
import { useUi } from "@/uni_modules/cool-unix/hooks";
const props = defineProps({
email: {
type: String,
default: ""
}
});
const ui = useUi();
const number = ref(60);
const text = ref("发送验证码");
// 是否发送成功
const isSendRes = ref(false);
const reg = /^[A-Z0-9\u4E00-\u9FA5]+@[\w-]+(\.[\w-]+)+$/i;
// 操作倒计时数字
const timer = ref<null|number>(null);
const handleNumber = () => {
timer.value = setInterval(() => {
number.value = number.value - 1;
if (number.value == 0) {
if (timer.value != null) {
clearInterval(timer.value as number);
}
isSendRes.value = false;
}
}, 1000) as number;
};
const sendCode = () => {
if (isSendRes.value) return ui.showToast({
message: "验证码已发送,请稍后重试"
});
if (props.email == "") {
ui.showToast({
message: "请输入邮箱地址"
});
} else if (reg.test(props.email)) {
isSendRes.value = true;
handleNumber();
} else {
ui.showToast(({
message: "请输入正确的邮箱地址"
}));
}
};
</script>
<style>
</style>