Showing
2 changed files
with
267 additions
and
0 deletions
src/components/LoginBox/index.vue
0 → 100644
| 1 | +<template> | ||
| 2 | + <div class="login-section"> | ||
| 3 | + <van-config-provider :theme-vars="themeVars"> | ||
| 4 | + <van-form ref="form" @submit="onSubmit"> | ||
| 5 | + <van-cell-group inset style="border: 1px solid #EAEAEA;"> | ||
| 6 | + <van-field v-if="use_widget" v-model="phone" name="phone" label="手机号" placeholder="手机号" readonly clickable | ||
| 7 | + :rules="[{ validator, message: '请输入正确手机号' }]" | ||
| 8 | + @touchstart.stop="showKeyboard" /> | ||
| 9 | + <van-field v-else v-model="phone" name="phone" label="手机号" placeholder="手机号" | ||
| 10 | + :rules="[{ validator, message: '请输入正确手机号' }]" /> | ||
| 11 | + <van-field v-model="code" center clearable name="code" type="digit" label="短信验证码" placeholder="请输入短信验证码" | ||
| 12 | + :formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]"> | ||
| 13 | + <template #button> | ||
| 14 | + <van-button @click="sendCode" v-if="countDown.current.value.total === limit" size="small" type="primary" | ||
| 15 | + :disabled="disabled"> | ||
| 16 | + <span>发送验证码</span> | ||
| 17 | + </van-button> | ||
| 18 | + <van-button v-else size="small" type="primary" :disabled="disabled"> | ||
| 19 | + <span>{{ countDown.current.value.seconds }} 秒重新发送</span> | ||
| 20 | + </van-button> | ||
| 21 | + </template> | ||
| 22 | + </van-field> | ||
| 23 | + </van-cell-group> | ||
| 24 | + </van-form> | ||
| 25 | + </van-config-provider> | ||
| 26 | + </div> | ||
| 27 | + | ||
| 28 | + <van-number-keyboard v-model="phone" :show="keyboard_show" :maxlength="11" @blur="onBlur" /> | ||
| 29 | + | ||
| 30 | + <!-- 图片滑块验证 --> | ||
| 31 | + <image-slider-verify :isShow="sliderShow" @done="handleConfirm" @on-close="handleClose"> | ||
| 32 | + </image-slider-verify> | ||
| 33 | +</template> | ||
| 34 | + | ||
| 35 | +<script setup> | ||
| 36 | +import ImageSliderVerify from '@/components/ImageSliderVerify/index.vue' | ||
| 37 | + | ||
| 38 | +import { ref, onMounted } from 'vue' | ||
| 39 | +import { useRoute, useRouter } from 'vue-router' | ||
| 40 | +import { useCountDown } from '@vant/use'; | ||
| 41 | +import { wxInfo } from '@/utils/tools'; | ||
| 42 | + | ||
| 43 | +import { Cookies, $, _, axios, storeToRefs, mainStore, Toast, useTitle } from '@/utils/generatePackage.js' | ||
| 44 | +//import { } from '@/utils/generateModules.js' | ||
| 45 | +//import { } from '@/utils/generateIcons.js' | ||
| 46 | +//import { } from '@/composables' | ||
| 47 | +const $route = useRoute(); | ||
| 48 | +const $router = useRouter(); | ||
| 49 | + | ||
| 50 | +const emit = defineEmits(['on-submit']) | ||
| 51 | + | ||
| 52 | +const form = ref(null); | ||
| 53 | + | ||
| 54 | +const submit = () => { | ||
| 55 | + let valid = form.value.validate(); | ||
| 56 | + valid | ||
| 57 | + .then(() => { | ||
| 58 | + form.value.submit(); | ||
| 59 | + }) | ||
| 60 | + .catch(error => { | ||
| 61 | + console.error(error); | ||
| 62 | + Toast({ | ||
| 63 | + message: '请检查后再次提交', | ||
| 64 | + icon: 'cross', | ||
| 65 | + }); | ||
| 66 | + }) | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +defineExpose({ | ||
| 70 | + submit | ||
| 71 | +}) | ||
| 72 | + | ||
| 73 | +const themeVars = { | ||
| 74 | + buttonPrimaryBackground: '#F9D95C', | ||
| 75 | + buttonPrimaryBorderColor: '#F9D95C', | ||
| 76 | + buttonPrimaryColor: '#713610', | ||
| 77 | + CellVerticalPadding: '14px' | ||
| 78 | +}; | ||
| 79 | + | ||
| 80 | +const onSubmit = () => { | ||
| 81 | + emit('on-submit', { | ||
| 82 | + phone: phone.value, | ||
| 83 | + code: code.value, | ||
| 84 | + }) | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +// 判断是否显示控件 | ||
| 88 | +let use_widget = ref(true); | ||
| 89 | +/** | ||
| 90 | + * 手机号码校验 | ||
| 91 | + * 函数返回 true 表示校验通过,false 表示不通过 | ||
| 92 | + * @param {*} val | ||
| 93 | + */ | ||
| 94 | +const validator = (val) => { | ||
| 95 | + let flag = false; | ||
| 96 | + // 简单判断手机号位数 | ||
| 97 | + if (/1\d{10}/.test(val) && phone.value.length === 11) { | ||
| 98 | + disabled.value = false; | ||
| 99 | + flag = true; | ||
| 100 | + } else { | ||
| 101 | + disabled.value = true; | ||
| 102 | + flag = false; | ||
| 103 | + } | ||
| 104 | + return flag | ||
| 105 | +}; | ||
| 106 | + | ||
| 107 | + | ||
| 108 | +const phone = ref(''); | ||
| 109 | +const code = ref(''); | ||
| 110 | +// TAG: 开发环境测试数据 | ||
| 111 | +if (import.meta.env.DEV) { | ||
| 112 | + phone.value = import.meta.env.VITE_ID | ||
| 113 | + code.value = import.meta.env.VITE_PIN | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +onMounted(() => { | ||
| 117 | + /** | ||
| 118 | + * 判断微信环境看是否弹出控件框 | ||
| 119 | + * 桌面微信直接输入 | ||
| 120 | + * 其他环境弹出输入框 | ||
| 121 | + */ | ||
| 122 | + if (wxInfo().isiOS || wxInfo().isAndroid) { | ||
| 123 | + use_widget.value = true; | ||
| 124 | + } else { | ||
| 125 | + use_widget.value = false; | ||
| 126 | + } | ||
| 127 | + // 判断微信授权状态,进入页面时未授权需要授权跳转 | ||
| 128 | + if (!Cookies.get('PHPSESSID')) { | ||
| 129 | + $router.replace({ | ||
| 130 | + path: '/auth', | ||
| 131 | + query: { | ||
| 132 | + href: location.hash, | ||
| 133 | + userType: 'b' | ||
| 134 | + } | ||
| 135 | + }); | ||
| 136 | + } | ||
| 137 | +}) | ||
| 138 | + | ||
| 139 | +// 手机号输入控件控制 | ||
| 140 | +const keyboard_show = ref(false); | ||
| 141 | +const showKeyboard = () => { // 弹出数字弹框 | ||
| 142 | + keyboard_show.value = true; | ||
| 143 | +}; | ||
| 144 | +const onBlur = () => { // 数字键盘失焦回调 | ||
| 145 | + keyboard_show.value = false; | ||
| 146 | + if (phone.value.length === 11) { | ||
| 147 | + disabled.value = false; | ||
| 148 | + } | ||
| 149 | +}; | ||
| 150 | + | ||
| 151 | +// 设置发送短信倒计时 | ||
| 152 | +// TAG: vant 自带倒计时函数 | ||
| 153 | +const limit = ref(60000); // 配置倒计时秒数 | ||
| 154 | +const countDown = useCountDown({ | ||
| 155 | + // 倒计时 24 小时 | ||
| 156 | + time: limit.value, | ||
| 157 | + onFinish: () => { | ||
| 158 | + countDown.reset(); | ||
| 159 | + } | ||
| 160 | +}); | ||
| 161 | + | ||
| 162 | +const sendCode = () => { // 发送验证码 | ||
| 163 | + countDown.start(); | ||
| 164 | + // TODO: 验证码接口 | ||
| 165 | + axios.post('/srv/?a=bind_phone&t=get_code', { | ||
| 166 | + phone: phone.value | ||
| 167 | + }) | ||
| 168 | + .then(res => { | ||
| 169 | + if (res.data.code === 1) { | ||
| 170 | + Toast.success('发送成功'); | ||
| 171 | + } else { | ||
| 172 | + console.warn(res.data); | ||
| 173 | + if (!res.data.show) return false; | ||
| 174 | + Toast({ | ||
| 175 | + message: res.data.msg, | ||
| 176 | + icon: 'close', | ||
| 177 | + }); | ||
| 178 | + } | ||
| 179 | + }) | ||
| 180 | + .catch(err => { | ||
| 181 | + console.error(err); | ||
| 182 | + }) | ||
| 183 | +}; | ||
| 184 | + | ||
| 185 | +const disabled = ref(true); | ||
| 186 | +// 过滤输入的数字 只能四位 | ||
| 187 | +const formatter = (value) => value.substring(0, 4); | ||
| 188 | + | ||
| 189 | +// 滑块验证成功后回调 | ||
| 190 | +const sliderShow = ref(false); | ||
| 191 | +const handleConfirm = (val) => { | ||
| 192 | + sliderShow.value = false | ||
| 193 | + console.warn('验证成功'); | ||
| 194 | +} | ||
| 195 | +const handleClose = () => { | ||
| 196 | + sliderShow.value = false | ||
| 197 | +} | ||
| 198 | +const imageVerify = () => { | ||
| 199 | + sliderShow.value = true; | ||
| 200 | +} | ||
| 201 | +</script> | ||
| 202 | + | ||
| 203 | +<style lang="less" scoped> | ||
| 204 | +</style> |
src/components/LoginBox/test.vue
0 → 100644
| 1 | +<template> | ||
| 2 | + <login-box ref="form" @on-submit="onSubmit"></login-box> | ||
| 3 | + <div class="btn" @click="submit"> | ||
| 4 | + 登 录 | ||
| 5 | + </div> | ||
| 6 | +</template> | ||
| 7 | + | ||
| 8 | +<script setup> | ||
| 9 | +import LoginBox from '@/components/LoginBox' | ||
| 10 | +import { onMounted, ref } from 'vue'; | ||
| 11 | +import { useRoute, useRouter } from 'vue-router'; | ||
| 12 | + | ||
| 13 | +import { | ||
| 14 | + Cookies, | ||
| 15 | + $, | ||
| 16 | + _, | ||
| 17 | + axios, | ||
| 18 | + storeToRefs, | ||
| 19 | + mainStore, | ||
| 20 | + Toast, | ||
| 21 | + useTitle, | ||
| 22 | +} from '@/utils/generatePackage.js'; | ||
| 23 | +//import { } from '@/utils/generateModules.js' | ||
| 24 | +//import { } from '@/utils/generateIcons.js' | ||
| 25 | +//import { } from '@/composables' | ||
| 26 | +const $route = useRoute(); | ||
| 27 | +const $router = useRouter(); | ||
| 28 | +useTitle($route.meta.title); | ||
| 29 | + | ||
| 30 | +const form = ref(null); | ||
| 31 | + | ||
| 32 | +const submit = () => { | ||
| 33 | + form.value.submit(); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +const onSubmit = (values) => { | ||
| 37 | + axios.post('/srv/?a=b_login', { | ||
| 38 | + phone: values.phone, | ||
| 39 | + pin: values.code, | ||
| 40 | + }) | ||
| 41 | + .then(res => { | ||
| 42 | + if (res.data.code === 1) { | ||
| 43 | + $router.push({ | ||
| 44 | + path: '/business/index' | ||
| 45 | + }); | ||
| 46 | + } else { | ||
| 47 | + console.warn(res.data); | ||
| 48 | + Toast({ | ||
| 49 | + message: res.data.msg, | ||
| 50 | + icon: 'close', | ||
| 51 | + }); | ||
| 52 | + } | ||
| 53 | + }) | ||
| 54 | + .catch(err => { | ||
| 55 | + console.error(err); | ||
| 56 | + }) | ||
| 57 | +}; | ||
| 58 | +</script> | ||
| 59 | + | ||
| 60 | +<style | ||
| 61 | + lang="less" | ||
| 62 | + scoped> | ||
| 63 | +</style> |
-
Please register or login to post a comment