hookehuyr

使用vant提供函数调整倒计时

...@@ -22,9 +22,11 @@ ...@@ -22,9 +22,11 @@
22 <van-field v-model="code" center clearable name="code" label="短信验证码" placeholder="请输入短信验证码" 22 <van-field v-model="code" center clearable name="code" label="短信验证码" placeholder="请输入短信验证码"
23 :formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]"> 23 :formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]">
24 <template #button> 24 <template #button>
25 - <van-button @click="sendCode" size="small" type="primary" :disabled="disabled"> 25 + <van-button @click="sendCode" v-if="countDown.current.value.total === limit" size="small" type="primary" :disabled="disabled">
26 - <span v-if="countDown === 60">发送验证码</span> 26 + <span>发送验证码</span>
27 - <span v-else>{{ countDown }} 秒重新发送</span> 27 + </van-button>
28 + <van-button v-else size="small" type="primary" :disabled="disabled">
29 + <span>{{ countDown.current.value.seconds }} 秒重新发送</span>
28 </van-button> 30 </van-button>
29 </template> 31 </template>
30 </van-field> 32 </van-field>
...@@ -46,6 +48,9 @@ import { useRoute, useRouter } from 'vue-router' ...@@ -46,6 +48,9 @@ import { useRoute, useRouter } from 'vue-router'
46 import axios from '@/utils/axios' 48 import axios from '@/utils/axios'
47 import logo_image from '@images/zhengshu-banner@2x.png' 49 import logo_image from '@images/zhengshu-banner@2x.png'
48 import { wxInfo } from '@/utils/tools'; 50 import { wxInfo } from '@/utils/tools';
51 +
52 +import { useCountDown } from '@vant/use';
53 +
49 const $route = useRoute(); 54 const $route = useRoute();
50 const $router = useRouter(); 55 const $router = useRouter();
51 56
...@@ -104,20 +109,32 @@ const onBlur = () => { // 数字键盘失焦回调 ...@@ -104,20 +109,32 @@ const onBlur = () => { // 数字键盘失焦回调
104 }; 109 };
105 110
106 // 设置发送短信倒计时 111 // 设置发送短信倒计时
107 -const countDown = ref(60); 112 +// const countDown = ref(60);
108 -const countDownHandle = () => { 113 +// const countDownHandle = () => {
109 - // 倒计时 114 +// // 倒计时
110 - if (countDown.value === 0) { 115 +// if (countDown.value === 0) {
111 - countDown.value = 60; 116 +// countDown.value = 60;
112 - } else { 117 +// } else {
113 - countDown.value--; 118 +// countDown.value--;
114 - setTimeout(() => { 119 +// setTimeout(() => {
115 - countDownHandle() 120 +// countDownHandle()
116 - }, 1000) 121 +// }, 1000)
122 +// }
123 +// }
124 +
125 +// 设置发送短信倒计时
126 +// TAG: vant 自带倒计时函数
127 +const limit = ref(60000); // 配置倒计时秒数
128 +const countDown = useCountDown({
129 + // 倒计时 24 小时
130 + time: limit.value,
131 + onFinish: () => {
132 + countDown.reset();
117 } 133 }
118 -} 134 +});
135 +
119 const sendCode = () => { // 发送验证码 136 const sendCode = () => { // 发送验证码
120 - if (countDown.value === 60) { 137 + countDown.start();
121 // TODO: 验证码接口 138 // TODO: 验证码接口
122 axios.post('/srv/?a=bind_phone&t=get_code', { 139 axios.post('/srv/?a=bind_phone&t=get_code', {
123 phone: phone.value 140 phone: phone.value
...@@ -125,7 +142,6 @@ const sendCode = () => { // 发送验证码 ...@@ -125,7 +142,6 @@ const sendCode = () => { // 发送验证码
125 .then(res => { 142 .then(res => {
126 if (res.data.code === 1) { 143 if (res.data.code === 1) {
127 Toast.success('发送成功'); 144 Toast.success('发送成功');
128 - countDownHandle()
129 } else { 145 } else {
130 console.warn(res.data); 146 console.warn(res.data);
131 Toast({ 147 Toast({
...@@ -137,7 +153,6 @@ const sendCode = () => { // 发送验证码 ...@@ -137,7 +153,6 @@ const sendCode = () => { // 发送验证码
137 .catch(err => { 153 .catch(err => {
138 console.error(err); 154 console.error(err);
139 }) 155 })
140 - }
141 }; 156 };
142 157
143 const disabled = ref(true); 158 const disabled = ref(true);
......