hookehuyr

✨ feat(登录模块): 优化逻辑条件,授权条件调整

1 /* 1 /*
2 * @Date: 2022-06-22 00:07:42 2 * @Date: 2022-06-22 00:07:42
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2022-06-22 01:28:42 4 + * @LastEditTime: 2022-06-22 08:51:37
5 * @FilePath: /tswj/src/composables/useLogin.js 5 * @FilePath: /tswj/src/composables/useLogin.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
...@@ -17,12 +17,12 @@ export const useLogin = () => { ...@@ -17,12 +17,12 @@ export const useLogin = () => {
17 const phone = ref(''); 17 const phone = ref('');
18 const code = ref('') 18 const code = ref('')
19 19
20 - const form = ref(null); 20 + const refForm = ref(null);
21 - const submit = () => { 21 + const validateForm = () => {
22 - let valid = form.value.validate(); 22 + const valid = refForm.value.validate();
23 valid 23 valid
24 .then(() => { 24 .then(() => {
25 - form.value.submit(); 25 + refForm.value.submit();
26 }) 26 })
27 .catch(error => { 27 .catch(error => {
28 console.error(error); 28 console.error(error);
...@@ -41,27 +41,27 @@ export const useLogin = () => { ...@@ -41,27 +41,27 @@ export const useLogin = () => {
41 let use_widget = ref(true); 41 let use_widget = ref(true);
42 use_widget.value = !!(wxInfo().isiOS || wxInfo().isAndroid); 42 use_widget.value = !!(wxInfo().isiOS || wxInfo().isAndroid);
43 43
44 - const disabled = ref(true);
45 /** 44 /**
46 * 手机号码校验 45 * 手机号码校验
47 * 函数返回 true 表示校验通过,false 表示不通过 46 * 函数返回 true 表示校验通过,false 表示不通过
48 * @param {*} val 47 * @param {*} val
49 */ 48 */
50 - const validator = (val) => { 49 + const sms_disabled = ref(true);
50 + const phoneValidator = (val) => {
51 let flag = false; 51 let flag = false;
52 // 简单判断手机号位数 52 // 简单判断手机号位数
53 if (/1\d{10}/.test(val) && phone.value.length === 11) { 53 if (/1\d{10}/.test(val) && phone.value.length === 11) {
54 - disabled.value = false; 54 + sms_disabled.value = false;
55 flag = true; 55 flag = true;
56 } else { 56 } else {
57 - disabled.value = true; 57 + sms_disabled.value = true;
58 flag = false; 58 flag = false;
59 } 59 }
60 return flag 60 return flag
61 }; 61 };
62 62
63 /** 63 /**
64 - * 数字弹框 64 + * 手机号数字弹框
65 */ 65 */
66 const keyboard_show = ref(false); 66 const keyboard_show = ref(false);
67 const refPhone = ref(null) 67 const refPhone = ref(null)
...@@ -93,11 +93,12 @@ export const useLogin = () => { ...@@ -93,11 +93,12 @@ export const useLogin = () => {
93 }; 93 };
94 94
95 // 过滤输入的数字 只能四位 95 // 过滤输入的数字 只能四位
96 - const formatter = (value) => value.substring(0, 4); 96 + const smsFormatter = (value) => value.substring(0, 4);
97 97
98 /** 98 /**
99 * 用户登录 99 * 用户登录
100 - * @param {*} values 100 + * @param {*} phone
101 + * @param {*} pin
101 */ 102 */
102 const $router = useRouter(); 103 const $router = useRouter();
103 const onSubmit = async (values) => { 104 const onSubmit = async (values) => {
...@@ -114,15 +115,15 @@ export const useLogin = () => { ...@@ -114,15 +115,15 @@ export const useLogin = () => {
114 code, 115 code,
115 onSubmit, 116 onSubmit,
116 use_widget, 117 use_widget,
117 - disabled, 118 + sms_disabled,
118 - validator, 119 + phoneValidator,
119 keyboardBlur, 120 keyboardBlur,
120 keyboard_show, 121 keyboard_show,
121 showKeyboard, 122 showKeyboard,
122 refPhone, 123 refPhone,
123 - form, 124 + refForm,
124 - submit, 125 + validateForm,
125 - formatter, 126 + smsFormatter,
126 limitSeconds, 127 limitSeconds,
127 countDown, 128 countDown,
128 sendCode, 129 sendCode,
......
...@@ -11,20 +11,21 @@ ...@@ -11,20 +11,21 @@
11 </div> 11 </div>
12 <div class="login-section"> 12 <div class="login-section">
13 <van-config-provider :theme-vars="loginTheme"> 13 <van-config-provider :theme-vars="loginTheme">
14 - <van-form ref="form" @submit="onSubmit"> 14 + <van-form ref="refForm" @submit="onSubmit">
15 <van-cell-group inset style="border: 1px solid #EAEAEA;"> 15 <van-cell-group inset style="border: 1px solid #EAEAEA;">
16 <van-field v-if="use_widget" ref="refPhone" v-model="phone" name="phone" label="手机号" placeholder="手机号" 16 <van-field v-if="use_widget" ref="refPhone" v-model="phone" name="phone" label="手机号" placeholder="手机号"
17 - readonly clickable :rules="[{ validator, message: '请输入正确手机号' }]" @touchstart.stop="showKeyboard" /> 17 + readonly clickable :rules="[{ validator: phoneValidator, message: '请输入正确手机号' }]"
18 + @touchstart.stop="showKeyboard" />
18 <van-field v-else v-model="phone" name="validator" label="手机号" placeholder="手机号" 19 <van-field v-else v-model="phone" name="validator" label="手机号" placeholder="手机号"
19 - :rules="[{ validator, message: '请输入正确手机号', trigger: 'onBlur' }]" /> 20 + :rules="[{ validator: phoneValidator, message: '请输入正确手机号', trigger: 'onBlur' }]" />
20 <van-field v-model="code" center clearable name="code" type="digit" label="短信验证码" placeholder="请输入短信验证码" 21 <van-field v-model="code" center clearable name="code" type="digit" label="短信验证码" placeholder="请输入短信验证码"
21 - :formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]"> 22 + :formatter="smsFormatter" :rules="[{ required: true, message: '请填写验证码' }]">
22 <template #button> 23 <template #button>
23 <van-button v-if="countDown.current.value.total === limitSeconds" size="small" type="primary" 24 <van-button v-if="countDown.current.value.total === limitSeconds" size="small" type="primary"
24 - :disabled="disabled" @click="sendCode"> 25 + :disabled="sms_disabled" @click="sendCode">
25 <span>发送验证码</span> 26 <span>发送验证码</span>
26 </van-button> 27 </van-button>
27 - <van-button v-else size="small" type="primary" :disabled="disabled"> 28 + <van-button v-else size="small" type="primary" :disabled="sms_disabled">
28 <span>{{ countDown.current.value.seconds }} 秒重新发送</span> 29 <span>{{ countDown.current.value.seconds }} 秒重新发送</span>
29 </van-button> 30 </van-button>
30 </template> 31 </template>
...@@ -33,7 +34,7 @@ ...@@ -33,7 +34,7 @@
33 </van-form> 34 </van-form>
34 </van-config-provider> 35 </van-config-provider>
35 </div> 36 </div>
36 - <div class="btn" @click="submit"> 37 + <div class="btn" @click="validateForm">
37 登&nbsp;录 38 登&nbsp;录
38 </div> 39 </div>
39 40
...@@ -44,16 +45,13 @@ ...@@ -44,16 +45,13 @@
44 </template> 45 </template>
45 46
46 <script setup> 47 <script setup>
47 -import Cookies from 'js-cookie'
48 import ImageSliderVerify from '@/components/ImageSliderVerify/index.vue' 48 import ImageSliderVerify from '@/components/ImageSliderVerify/index.vue'
49 import { loginTheme } from '@/theme-vars.js'; 49 import { loginTheme } from '@/theme-vars.js';
50 -import { ref, onMounted } from 'vue'; 50 +import { ref } from 'vue';
51 -import { useRouter } from 'vue-router'
52 import logo_image from '@images/denglu-top@2x.png' 51 import logo_image from '@images/denglu-top@2x.png'
53 import { useLogin } from '@/composables/useLogin'; 52 import { useLogin } from '@/composables/useLogin';
54 53
55 -const { phone, code, onSubmit, use_widget, validator, keyboardBlur, disabled, keyboard_show, refPhone, showKeyboard, form, submit, formatter, limitSeconds, countDown, sendCode, } = useLogin(); 54 +const { phone, code, onSubmit, use_widget, phoneValidator, keyboardBlur, sms_disabled, keyboard_show, refPhone, showKeyboard, refForm, validateForm, smsFormatter, limitSeconds, countDown, sendCode, } = useLogin();
56 -const $router = useRouter();
57 55
58 // TAG: 开发环境测试数据 56 // TAG: 开发环境测试数据
59 if (import.meta.env.DEV) { 57 if (import.meta.env.DEV) {
...@@ -61,19 +59,6 @@ if (import.meta.env.DEV) { ...@@ -61,19 +59,6 @@ if (import.meta.env.DEV) {
61 code.value = import.meta.env.VITE_PIN 59 code.value = import.meta.env.VITE_PIN
62 } 60 }
63 61
64 -onMounted(() => {
65 - // 判断微信授权状态,进入页面时未授权需要授权跳转
66 - if (!Cookies.get('PHPSESSID')) {
67 - $router.replace({
68 - path: '/auth',
69 - query: {
70 - href: location.hash,
71 - userType: 'b'
72 - }
73 - });
74 - }
75 -})
76 -
77 // 滑块验证成功后回调 62 // 滑块验证成功后回调
78 const sliderShow = ref(false); 63 const sliderShow = ref(false);
79 const handleConfirm = (val) => { 64 const handleConfirm = (val) => {
......