hookehuyr

✨ feat: 联调发送验证码相关功能页

1 +/*
2 + * @Date: 2022-06-17 15:03:03
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2022-06-17 15:05:04
5 + * @FilePath: /tswj/src/api/B/login.js
6 + * @Description: 文件描述
7 + */
8 +import { fn, fetch } from '@/api/fn';
9 +
10 +const Api = {
11 + B_LOGIN: '/srv/?a=b_login',
12 +}
13 +
14 +/**
15 + * @description 登录接口
16 + * @param {*} phone 手机号
17 + * @param {*} pin 验证码
18 + */
19 +export const b_LoginAPI = (params) => fn(fetch.get(Api.B_LOGIN, params));
1 +/*
2 + * @Date: 2022-06-17 14:54:29
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2022-06-17 14:58:17
5 + * @FilePath: /tswj/src/api/common.js
6 + * @Description: 通用接口
7 + */
8 +import { fn, fetch } from '@/api/fn';
9 +
10 +const Api = {
11 + SMS: '/srv/?a=sms',
12 +}
13 +
14 +/**
15 + * @description: 发送验证码
16 + * @param {*} phone 手机号码
17 + * @returns
18 + */
19 +export const smsAPI = (params) => fn(fetch.post(Api.SMS, params));
...@@ -47,12 +47,14 @@ import { styleColor } from '@/constant.js'; ...@@ -47,12 +47,14 @@ import { styleColor } from '@/constant.js';
47 import { ref, onMounted } from 'vue'; 47 import { ref, onMounted } from 'vue';
48 import { Toast } from 'vant' 48 import { Toast } from 'vant'
49 import { useRouter } from 'vue-router' 49 import { useRouter } from 'vue-router'
50 -import axios from '@/utils/axios'
51 import logo_image from '@images/denglu-top@2x.png' 50 import logo_image from '@images/denglu-top@2x.png'
52 import { wxInfo } from '@/utils/tools'; 51 import { wxInfo } from '@/utils/tools';
53 52
54 import { useCountDown } from '@vant/use'; 53 import { useCountDown } from '@vant/use';
55 54
55 +import { smsAPI } from '@/api/common'
56 +import { b_LoginAPI } from '@/api/B/login'
57 +
56 const $router = useRouter(); 58 const $router = useRouter();
57 59
58 // 判断是否显示控件 60 // 判断是否显示控件
...@@ -157,27 +159,13 @@ const countDown = useCountDown({ ...@@ -157,27 +159,13 @@ const countDown = useCountDown({
157 } 159 }
158 }); 160 });
159 161
160 -const sendCode = () => { // 发送验证码 162 +const sendCode = async () => { // 发送验证码
161 countDown.start(); 163 countDown.start();
162 - // TODO: 验证码接口 164 + // 验证码接口
163 - axios.post('/srv/?a=bind_phone&t=get_code', { 165 + const { code } = await smsAPI({ phone: phone.value });
164 - phone: phone.value 166 + if (code === 1) {
165 - })
166 - .then(res => {
167 - if (res.data.code === 1) {
168 Toast.success('发送成功'); 167 Toast.success('发送成功');
169 - } else {
170 - console.warn(res.data);
171 - if (!res.data.show) return false;
172 - Toast({
173 - message: res.data.msg,
174 - icon: 'close',
175 - });
176 } 168 }
177 - })
178 - .catch(err => {
179 - console.error(err);
180 - })
181 }; 169 };
182 170
183 const disabled = ref(true); 171 const disabled = ref(true);
...@@ -189,27 +177,13 @@ const formatter = (value) => value.substring(0, 4); ...@@ -189,27 +177,13 @@ const formatter = (value) => value.substring(0, 4);
189 * @param {*} values 177 * @param {*} values
190 */ 178 */
191 179
192 -const onSubmit = (values) => { 180 +const onSubmit = async (values) => {
193 - axios.post('/srv/?a=b_login', { 181 + const { code } = await b_LoginAPI({ phone: values.phone, pin: values.code })
194 - phone: values.phone, 182 + if (code === 1) {
195 - pin: values.code,
196 - })
197 - .then(res => {
198 - if (res.data.code === 1) {
199 $router.push({ 183 $router.push({
200 path: '/business/index' 184 path: '/business/index'
201 }); 185 });
202 - } else {
203 - console.warn(res.data);
204 - Toast({
205 - message: res.data.msg,
206 - icon: 'close',
207 - });
208 } 186 }
209 - })
210 - .catch(err => {
211 - console.error(err);
212 - })
213 }; 187 };
214 188
215 const themeVars = { 189 const themeVars = {
......
...@@ -19,9 +19,11 @@ ...@@ -19,9 +19,11 @@
19 <van-field v-model="pin" center clearable name="pin" type="digit" label="短信验证码" placeholder="请输入短信验证码" 19 <van-field v-model="pin" center clearable name="pin" type="digit" label="短信验证码" placeholder="请输入短信验证码"
20 :formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]"> 20 :formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]">
21 <template #button> 21 <template #button>
22 - <van-button @click="sendCode" size="small" type="primary" :disabled="disabled"> 22 + <van-button v-if="countDown.current.value.total === limit" size="small" type="primary" :disabled="disabled" @click="sendCode">
23 - <span v-if="countDown === 60">发送验证码</span> 23 + <span>发送验证码</span>
24 - <span v-else>{{ countDown }} 秒重新发送</span> 24 + </van-button>
25 + <van-button v-else size="small" type="primary" :disabled="disabled">
26 + <span>{{ countDown.current.value.seconds }} 秒重新发送</span>
25 </van-button> 27 </van-button>
26 </template> 28 </template>
27 </van-field> 29 </van-field>
...@@ -48,6 +50,8 @@ import { useRoute, useRouter } from 'vue-router' ...@@ -48,6 +50,8 @@ import { useRoute, useRouter } from 'vue-router'
48 import axios from '@/utils/axios'; 50 import axios from '@/utils/axios';
49 import { Toast } from 'vant'; 51 import { Toast } from 'vant';
50 import { styleColor } from '@/constant.js'; 52 import { styleColor } from '@/constant.js';
53 +import { smsAPI } from '@/api/common'
54 +import { useCountDown } from '@vant/use';
51 55
52 const $route = useRoute(); 56 const $route = useRoute();
53 const $router = useRouter(); 57 const $router = useRouter();
...@@ -86,39 +90,36 @@ const onBlur = () => { // 数字键盘失焦回调 ...@@ -86,39 +90,36 @@ const onBlur = () => { // 数字键盘失焦回调
86 90
87 /******************** 发送验证码模块 START ********************/ 91 /******************** 发送验证码模块 START ********************/
88 92
89 -// TODO:发送验证码接口待调,验证码写默认8888 93 +// const countDown = ref(60);
90 -const countDown = ref(60); 94 +// const countDownHandle = () => {
91 -const countDownHandle = () => { 95 +// // 倒计时
92 - // 倒计时 96 +// if (countDown.value === 0) {
93 - if (countDown.value === 0) { 97 +// countDown.value = 60;
94 - countDown.value = 60; 98 +// } else {
95 - } else { 99 +// countDown.value--;
96 - countDown.value--; 100 +// setTimeout(() => {
97 - setTimeout(() => { 101 +// countDownHandle()
98 - countDownHandle() 102 +// }, 1000)
99 - }, 1000) 103 +// }
104 +// }
105 +
106 +// 设置发送短信倒计时
107 +// TAG: vant 自带倒计时函数
108 +const limit = ref(60000); // 配置倒计时秒数
109 +const countDown = useCountDown({
110 + // 倒计时 24 小时
111 + time: limit.value,
112 + onFinish: () => {
113 + countDown.reset();
100 } 114 }
101 -} 115 +});
102 -const sendCode = () => { // 发送验证码 116 +
103 - if (countDown.value === 60) { 117 +const sendCode = async () => { // 发送验证码
104 - axios.post('/srv/?a=bind_phone&t=get_code', { 118 + countDown.start();
105 - phone: phone.value 119 + // 验证码接口
106 - }) 120 + const { code } = await smsAPI({ phone: phone.value });
107 - .then(res => { 121 + if (code === 1) {
108 - if (res.data.code === 1) {
109 Toast.success('发送成功'); 122 Toast.success('发送成功');
110 - countDownHandle()
111 - } else {
112 - console.warn(res.data);
113 - Toast({
114 - message: res.data.msg,
115 - icon: 'close',
116 - });
117 - }
118 - })
119 - .catch(err => {
120 - console.error(err);
121 - })
122 } 123 }
123 }; 124 };
124 125
......