hookehuyr

登录页功能优化

1 .nut-input { 1 .nut-input {
2 border-radius: 50rpx; 2 border-radius: 50rpx;
3 + padding-top: 25rpx;
3 } 4 }
......
1 <!-- 1 <!--
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2023-12-18 18:00:07 4 + * @LastEditTime: 2023-12-20 10:25:15
5 * @FilePath: /meihuaApp/src/pages/login/index.vue 5 * @FilePath: /meihuaApp/src/pages/login/index.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -9,12 +9,23 @@ ...@@ -9,12 +9,23 @@
9 <view> 9 <view>
10 <view style="color: #6A4925; font-size: 56rpx; text-align: center;">登&nbsp;录</view> 10 <view style="color: #6A4925; font-size: 56rpx; text-align: center;">登&nbsp;录</view>
11 <view style="border: 1px solid #F1EBDF; width: 90%; margin: 0 auto; margin-top: 50rpx; border-radius: 50rpx; padding: 5rpx 0;"> 11 <view style="border: 1px solid #F1EBDF; width: 90%; margin: 0 auto; margin-top: 50rpx; border-radius: 50rpx; padding: 5rpx 0;">
12 - <nut-input v-model="tel" placeholder="请输入手机号" :border="false" type="number" /> 12 + <nut-input v-model="tel" placeholder="请输入手机号" :border="false" type="number" max-length="11" />
13 </view> 13 </view>
14 <view style="border: 1px solid #F1EBDF; width: 90%; margin: 0 auto; margin-top: 20rpx; border-radius: 50rpx;"> 14 <view style="border: 1px solid #F1EBDF; width: 90%; margin: 0 auto; margin-top: 20rpx; border-radius: 50rpx;">
15 - <nut-input v-model="code" placeholder="请输入验证码" :border="false" type="number" > 15 + <!-- <nut-input v-model="code" placeholder="请输入验证码" :border="false" type="number" >
16 <template #right> <nut-button size="small" color="#6A4925">获取验证码</nut-button> </template> 16 <template #right> <nut-button size="small" color="#6A4925">获取验证码</nut-button> </template>
17 - </nut-input> 17 + </nut-input> -->
18 + <nut-row>
19 + <nut-col span="14">
20 + <nut-input v-model="code" placeholder="请输入验证码" :border="false" type="number" :formatter="codeFormatter" max-length="4"></nut-input>
21 + </nut-col>
22 + <nut-col span="10">
23 + <view style="padding: 25rpx 10rpx; line-height: 40rpx; background-color: #6A4925; color: #fff; border-top-right-radius: 50rpx; border-bottom-right-radius: 50rpx; font-size: 28rpx;text-align: center;">
24 + <view @tap="getCode" v-if="!time_remaining">获取验证码</view>
25 + <text v-else>{{ time_remaining }}秒后重新发送</text>
26 + </view>
27 + </nut-col>
28 + </nut-row>
18 </view> 29 </view>
19 <view style="color: #7B7B7B; font-size: 28rpx; margin: 20rpx 50rpx;">未注册手机号验证后自动创建新账号</view> 30 <view style="color: #7B7B7B; font-size: 28rpx; margin: 20rpx 50rpx;">未注册手机号验证后自动创建新账号</view>
20 <view style="margin: 50rpx; margin-bottom: 10rpx;"> 31 <view style="margin: 50rpx; margin-bottom: 10rpx;">
...@@ -31,8 +42,49 @@ ...@@ -31,8 +42,49 @@
31 <script setup> 42 <script setup>
32 import Taro from '@tarojs/taro' 43 import Taro from '@tarojs/taro'
33 import { IconFont } from '@nutui/icons-vue-taro'; 44 import { IconFont } from '@nutui/icons-vue-taro';
34 -import { ref } from "vue"; 45 +import { ref, nextTick } from "vue";
46 +
47 +let countdownIntervalId; // 用于存储倒计时的计时器 ID
48 +
49 +const startCountdown = (timeInSeconds, updateCallback, resetCallback) => {
50 + if (countdownIntervalId) {
51 + // 如果已经有倒计时在进行中,则先清除之前的计时器
52 + clearInterval(countdownIntervalId);
53 + }
54 +
55 + let timeLeft = timeInSeconds;
56 +
57 + // 更新倒计时显示的函数
58 + const updateCountdown = () => {
59 + if (timeLeft <= 0) {
60 + // 倒计时结束
61 + clearInterval(countdownIntervalId);
62 + resetCallback(); // 调用重置函数
63 + } else {
64 + // 更新倒计时显示
65 + updateCallback(timeLeft);
66 + timeLeft--;
67 + }
68 + }
69 +
70 + // 立即执行一次更新倒计时显示函数
71 + updateCountdown();
72 +
73 + // 每秒调用一次更新倒计时显示函数
74 + countdownIntervalId = setInterval(updateCountdown, 1000);
75 +}
76 +
77 +const resetCountdown = () => {
78 + if (countdownIntervalId) {
79 + // 清除倒计时计时器
80 + clearInterval(countdownIntervalId);
81 + countdownIntervalId = undefined;
82 + console.log("倒计时已重置");
83 + time_remaining.value = 0;
84 + }
85 +}
35 86
87 +const time_remaining = ref(0);
36 const tel = ref(''); 88 const tel = ref('');
37 const code = ref(''); 89 const code = ref('');
38 90
...@@ -42,12 +94,65 @@ const tapRight = () => { ...@@ -42,12 +94,65 @@ const tapRight = () => {
42 showRight.value = true; 94 showRight.value = true;
43 } 95 }
44 96
97 +const codeFormatter = (value) => {
98 + return value.replace(/\D/g, '').substring(0, 4);
99 +}
100 +
101 +const isValidTel = (tel) => {
102 + return /^1\d{10}$/.test(tel);
103 +}
104 +
45 const login = () => { 105 const login = () => {
106 + console.warn(tel.value);
107 + console.warn(code.value);
108 + if (!isValidTel(tel.value) ||!code.value) {
109 + Taro.showToast({
110 + title: '请检查输入项',
111 + icon: 'error',
112 + duration: 2000
113 + });
114 + return;
115 + } else {
46 Taro.showToast({ 116 Taro.showToast({
47 title: '登录成功', 117 title: '登录成功',
48 icon: 'success', 118 icon: 'success',
49 - duration: 2000 119 + duration: 2000,
120 + success: () => {
121 + setTimeout(() => {
122 + Taro.reLaunch({
123 + url: '/pages/index/index'
50 }) 124 })
125 + }, 1000);
126 + }
127 + });
128 + }
129 +}
130 +
131 +const updateDisplay = (timeLeft) => {
132 + // 倒计时剩余时间
133 + time_remaining.value = timeLeft;
134 +}
135 +
136 +const resetDisplay = () => {
137 + // 在此处进行重置后的展示操作
138 + resetCountdown();
139 +}
140 +
141 +const getCode = () => {
142 + if (isValidTel(tel.value)) {
143 + Taro.showToast({
144 + title: '验证码已发送',
145 + icon:'success',
146 + duration: 2000
147 + });
148 + startCountdown(10, updateDisplay, resetDisplay);
149 + } else {
150 + Taro.showToast({
151 + title: '请检查手机号',
152 + icon: 'error',
153 + duration: 2000
154 + });
155 + }
51 } 156 }
52 </script> 157 </script>
53 158
......