blob: b3079f4232e74bd4b93f7fa899f4ef52f877b798 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view class="u-code-wrap">
3 <!-- 此组件功能由js完成,无需写html逻辑 -->
4 </view>
5</template>
6
7<script>
8 /**
9 * verificationCode 验证码输入框
10 * @description 考虑到用户实际发送验证码的场景,可能是一个按钮,也可能是一段文字,提示语各有不同,所以本组件 不提供界面显示,只提供提示语,由用户将提示语嵌入到具体的场景
11 * @tutorial https://www.uviewui.com/components/verificationCode.html
12 * @property {Number String} seconds 倒计时所需的秒数(默认60)
13 * @property {String} start-text 开始前的提示语,见官网说明(默认获取验证码)
14 * @property {String} change-text 倒计时期间的提示语,必须带有字母"x",见官网说明(默认X秒重新获取)
15 * @property {String} end-text 倒计结束的提示语,见官网说明(默认重新获取)
16 * @property {Boolean} keep-running 是否在H5刷新或各端返回再进入时继续倒计时(默认false)
17 * @event {Function} change 倒计时期间,每秒触发一次
18 * @event {Function} start 开始倒计时触发
19 * @event {Function} end 结束倒计时触发
20 * @example <u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode"
21 */
22 export default {
23 name: "u-verification-code",
24 props: {
25 // 倒计时总秒数
26 seconds: {
27 type: [String, Number],
28 default: 60
29 },
30 // 尚未开始时提示
31 startText: {
32 type: String,
33 default: '获取验证码'
34 },
35 // 正在倒计时中的提示
36 changeText: {
37 type: String,
38 default: 'X秒重新获取'
39 },
40 // 倒计时结束时的提示
41 endText: {
42 type: String,
43 default: '重新获取'
44 },
45 // 是否在H5刷新或各端返回再进入时继续倒计时
46 keepRunning: {
47 type: Boolean,
48 default: false
49 },
50 // 为了区分多个页面,或者一个页面多个倒计时组件本地存储的继续倒计时变了
51 uniqueKey: {
52 type: String,
53 default: ''
54 }
55 },
56 data() {
57 return {
58 secNum: this.seconds,
59 timer: null,
60 canGetCode: true, // 是否可以执行验证码操作
61 }
62 },
63 mounted() {
64 this.checkKeepRunning();
65 },
66 watch: {
67 seconds: {
68 immediate: true,
69 handler(n) {
70 this.secNum = n;
71 }
72 }
73 },
74 methods: {
75 checkKeepRunning() {
76 // 获取上一次退出页面(H5还包括刷新)时的时间戳,如果没有上次的保存,此值可能为空
77 let lastTimestamp = Number(uni.getStorageSync(this.uniqueKey + '_$uCountDownTimestamp'));
78 if(!lastTimestamp) return this.changeEvent(this.startText);
79 // 当前秒的时间戳
80 let nowTimestamp = Math.floor((+ new Date()) / 1000);
81 // 判断当前的时间戳,是否小于上一次的本该按设定结束,却提前结束的时间戳
82 if(this.keepRunning && lastTimestamp && lastTimestamp > nowTimestamp) {
83 // 剩余尚未执行完的倒计秒数
84 this.secNum = lastTimestamp - nowTimestamp;
85 // 清除本地保存的变量
86 uni.removeStorageSync(this.uniqueKey + '_$uCountDownTimestamp');
87 // 开始倒计时
88 this.start();
89 } else {
90 // 如果不存在需要继续上一次的倒计时,执行正常的逻辑
91 this.changeEvent(this.startText);
92 }
93 },
94 // 开始倒计时
95 start() {
96 // 防止快速点击获取验证码的按钮而导致内部产生多个定时器导致混乱
97 if(this.timer) {
98 clearInterval(this.timer);
99 this.timer = null;
100 }
101 this.$emit('start');
102 this.canGetCode = false;
103 // 这里放这句,是为了一开始时就提示,否则要等setInterval的1秒后才会有提示
104 this.changeEvent(this.changeText.replace(/x|X/, this.secNum));
105 this.setTimeToStorage();
106 this.timer = setInterval(() => {
107 if (--this.secNum) {
108 // 用当前倒计时的秒数替换提示字符串中的"x"字母
109 this.changeEvent(this.changeText.replace(/x|X/, this.secNum));
110 } else {
111 clearInterval(this.timer);
112 this.timer = null;
113 this.changeEvent(this.endText);
114 this.secNum = this.seconds;
115 this.$emit('end');
116 this.canGetCode = true;
117 }
118 }, 1000);
119 },
120 // 重置,可以让用户再次获取验证码
121 reset() {
122 this.canGetCode = true;
123 clearInterval(this.timer);
124 this.secNum = this.seconds;
125 this.changeEvent(this.endText);
126 },
127 changeEvent(text) {
128 this.$emit('change', text);
129 },
130 // 保存时间戳,为了防止倒计时尚未结束,H5刷新或者各端的右上角返回上一页再进来
131 setTimeToStorage() {
132 if(!this.keepRunning || !this.timer) return;
133 // 记录当前的时间戳,为了下次进入页面,如果还在倒计时内的话,继续倒计时
134 // 倒计时尚未结束,结果大于0;倒计时已经开始,就会小于初始值,如果等于初始值,说明没有开始倒计时,无需处理
135 if(this.secNum > 0 && this.secNum <= this.seconds) {
136 // 获取当前时间戳(+ new Date()为特殊写法),除以1000变成秒,再去除小数部分
137 let nowTimestamp = Math.floor((+ new Date()) / 1000);
138 // 将本该结束时候的时间戳保存起来 => 当前时间戳 + 剩余的秒数
139 uni.setStorage({
140 key: this.uniqueKey + '_$uCountDownTimestamp',
141 data: nowTimestamp + Number(this.secNum)
142 })
143 }
144 }
145 },
146 // 组件销毁的时候,清除定时器,否则定时器会继续存在,系统不会自动清除
147 beforeDestroy() {
148 this.setTimeToStorage();
149 clearTimeout(this.timer);
150 this.timer = null;
151 }
152 }
153</script>
154
155<style lang="scss" scoped>
156 @import "../../libs/css/style.components.scss";
157
158 .u-code-wrap {
159 width: 0;
160 height: 0;
161 position: fixed;
162 z-index: -1;
163 }
164</style>