优化登录,修改bug

This commit is contained in:
zhouyanli
2026-07-29 17:47:10 +08:00
parent 9be76c6386
commit a6243cf466
24 changed files with 533 additions and 1012 deletions

View File

@@ -1,72 +1,82 @@
<template>
<view class="login-page">
<view class="status_bar"></view>
<view class="status_bar"></view>
<!-- 登录标题 -->
<view class="login-title">手机验证码登录</view>
<view class="login-title">{{ loginMode === 'sms' ? '手机验证码登录' : '账号密码登录' }}</view>
<!-- 手机号输入框 -->
<view class="input-item">
<view class="input-icon">
<!-- <uni-icons type="phone" size="24" color="#999"></uni-icons> -->
<image src="https://wuyeadmin.bugtc.com/images/login/phone.png" mode="" style="height: 100%;width: 100%;"></image>
<!-- ====== 验证码登录模式 ====== -->
<template v-if="loginMode === 'sms'">
<!-- 手机号输入框 -->
<view class="input-item">
<view class="input-icon">
<image src="https://wuyeadmin.bugtc.com/images/login/phone.png" mode="" style="height: 100%;width: 100%;"></image>
</view>
<uni-easyinput class="input-content" type="number" v-model="phoneNumber"
@input="phoneNumber = phoneNumber.replace(/\D/g, '')"
placeholder="请输入手机号" :inputBorder="false" maxlength="11" :placeholderStyle="placeholderStyle">
</uni-easyinput>
</view>
<uni-easyinput class="input-content" type="number" v-model="phoneNumber"
placeholder="请输入手机号" :inputBorder="false" maxlength="11" :placeholderStyle="placeholderStyle">
</uni-easyinput>
</view>
<!-- 验证码输入框 + 获取验证码按钮 -->
<view class="input-item verify-item">
<view class="input-icon">
<image src="https://wuyeadmin.bugtc.com/images/login/code.png" mode="" style="height: 100%;width: 100%;"></image>
<!-- 验证码输入框 + 获取验证码按钮 -->
<view class="input-item verify-item">
<view class="input-icon">
<image src="https://wuyeadmin.bugtc.com/images/login/code.png" mode="" style="height: 100%;width: 100%;"></image>
</view>
<uni-easyinput class="input-content verify-input" type="number" v-model="verifyCode"
@input="verifyCode = verifyCode.replace(/\D/g, '')"
placeholder="请输入验证码" :inputBorder="false" maxlength="6" :placeholderStyle="placeholderStyle">
</uni-easyinput>
<button class="get-verify-btn" :disabled="!canGetVerify || countDown > 0" @click="getVerifyCode">
{{ countDown > 0 ? `${countDown}s后重新获取` : '获取验证码' }}
</button>
</view>
<uni-easyinput class="input-content verify-input" type="number" v-model="verifyCode"
placeholder="请输入验证码" :inputBorder="false" maxlength="6" :placeholderStyle="placeholderStyle">
</uni-easyinput>
<button
class="get-verify-btn"
:disabled="!canGetVerify || countDown > 0"
@click="getVerifyCode"
>
{{ countDown > 0 ? `${countDown}s后重新获取` : '获取验证码' }}
</button>
</view>
</template>
<!-- 协议勾选 -->
<view class="agreement-item" >
<view @click="toggleAgreement">
<uni-icons
type="checkbox-filled"
size="24"
color="#007aff"
v-if="agreeAgreement"
></uni-icons>
<uni-icons
type="checkbox"
size="24"
color="#999"
v-else
></uni-icons>
</view>
<text class="agreement-text" >我已阅读并同意
<text class="agreePrivacy" @click.stop="goToUserAgreement">用户协议</text>
<text class="agreePrivacy" @click.stop="goToPrivacy">隐私政策</text>
</text>
<!-- ====== 账号密码登录模式 ====== -->
<template v-if="loginMode === 'pwd'">
<!-- 账号输入框 -->
<view class="input-item">
<view class="input-icon">
<image src="https://wuyeadmin.bugtc.com/images/login/phone.png" mode="" style="height: 100%;width: 100%;"></image>
</view>
<uni-easyinput class="input-content" type="text" v-model="account"
placeholder="请输入帐号" :inputBorder="false" maxlength="16" :placeholderStyle="placeholderStyle">
</uni-easyinput>
</view>
<!-- 密码输入框 -->
<view class="input-item">
<view class="input-icon">
<image src="https://wuyeadmin.bugtc.com/images/login/password.png" mode="" style="height: 100%;width: 100%;"></image>
</view>
<uni-easyinput class="input-content" type="password" v-model="password"
placeholder="请输入密码" :inputBorder="false" maxlength="16" :placeholderStyle="placeholderStyle">
</uni-easyinput>
</view>
</template>
<!-- 协议勾选 + 忘记密码 -->
<view class="agreement-wrap">
<view class="agreement-item">
<view @click="toggleAgreement" class="checkbox-icon">
<uni-icons type="checkbox-filled" size="24" color="#007aff" v-if="agreeAgreement"></uni-icons>
<uni-icons type="checkbox" size="24" color="#999" v-else></uni-icons>
</view>
<text class="agreement-text">我已阅读并同意
<text class="agreePrivacy" @click.stop="goToUserAgreement">用户协议</text>
<text class="agreePrivacy" @click.stop="goToPrivacy">隐私政策</text>
</text>
</view>
<text class="forget-pwd" v-if="loginMode === 'pwd'" @click="goForgetPwd">忘记密码</text>
</view>
<!-- 登录按钮 -->
<button
class="login-btn"
@click="handleLogin"
>
登录
</button>
<button class="login-btn" @click="handleLogin">登录</button>
<!-- 底部链接 -->
<view class="bottom-link">
<navigator class="link-item" url="/pageSubPack/loginSub/pwd-login">账号密码登录</navigator>
<text class="link-item" @click="switchMode">{{ loginMode === 'sms' ? '账号密码登录' : '验证码登录' }}</text>
<navigator class="link-item" url="/pageSubPack/loginSub/register-new-user">注册新用户</navigator>
</view>
@@ -77,51 +87,50 @@
<script setup>
import { ref, computed, onUnmounted } from 'vue';
import {getLoginBySms,getSendCode} from '../api/api.js'
import { onLoad } from '@dcloudio/uni-app'
import { getLoginBySms, getLoginPassword, getSendCode } from '../api/api.js'
import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue'
// ========== 模式切换 ==========
// 'sms' = 手机验证码登录, 'pwd' = 账号密码登录
const loginMode = ref('pwd')
// ========== 响应式数据 ==========
// 手机号
const phoneNumber = ref('');
// 验证码
const verifyCode = ref('');
const phoneNumber = ref('') // 短信登录:手机号
const verifyCode = ref('') // 短信登录:验证码
const account = ref('') // 密码登录:账号
const password = ref('') // 密码登录:密码
const agreeAgreement = ref(false)
const placeholderStyle = ref('font-size:24rpx')
// 是否同意协议
const agreeAgreement = ref(false); // 默认不勾选
// 倒计时(秒)
const countDown = ref(0);
// 定时器
let timer = null;
// 验证码倒计时
const countDown = ref(0)
let timer = null
// 隐私弹窗
const showPrivacyPopup = ref(false)
const pendingAction = ref('')
// ========== onLoad: 读取 mode 参数 ==========
onLoad((options) => {
if (options && options.mode === 'sms') {
loginMode.value = 'sms'
} else if (options && options.mode === 'pwd') {
loginMode.value = 'pwd'
}
})
// ========== 计算属性 ==========
// 是否可获取验证码(手机号格式正确)
// 短信模式:是否可获取验证码
const canGetVerify = computed(() => {
// 手机号正则11位数字以1开头
const phoneReg = /^1[3-9]\d{9}$/;
return phoneReg.test(phoneNumber.value);
});
if (loginMode.value !== 'sms') return false
const phoneReg = /^1[3-9]\d{9}$/
return phoneReg.test(phoneNumber.value)
})
// 是否可以登录(手机号+验证码+协议都满足)
const canLogin = computed(() => {
const phoneReg = /^1[3-9]\d{9}$/;
const verifyReg = /^\d{6}$/;
return phoneReg.test(phoneNumber.value) && verifyReg.test(verifyCode.value) && agreeAgreement.value;
});
// ========== 共享方法 ==========
const toggleAgreement = () => { agreeAgreement.value = !agreeAgreement.value }
// ========== 方法 ==========
// 检查表单状态
const checkForm = () => {
// 无需额外逻辑,计算属性已实时监听
};
// 切换协议勾选状态
const toggleAgreement = () => {
agreeAgreement.value = !agreeAgreement.value;
};
// 打开用户协议
const goToUserAgreement = () => {
// #ifdef APP-PLUS
plus.runtime.openURL('https://wuye.ckdzkj.com/parivw/ownler_xieyi.html')
@@ -131,7 +140,6 @@ const goToUserAgreement = () => {
// #endif
}
// 打开隐私政策
const goToPrivacy = () => {
// #ifdef APP-PLUS
plus.runtime.openURL('https://wuye.ckdzkj.com/parivw/owner.html')
@@ -140,13 +148,12 @@ const goToPrivacy = () => {
window.open('https://wuye.ckdzkj.com/parivw/owner.html')
// #endif
}
// 打开隐私弹窗
const openPrivacyPopup = (action) => {
pendingAction.value = action || ''
showPrivacyPopup.value = true
}
// 同意隐私协议
const agreePrivacy = () => {
showPrivacyPopup.value = false
agreeAgreement.value = true
@@ -156,155 +163,140 @@ const agreePrivacy = () => {
pendingAction.value = ''
}
// 不同意隐私协议
const disagreePrivacy = () => {
showPrivacyPopup.value = false
pendingAction.value = ''
}
// 获取验证码
// 切换登录模式
const switchMode = () => {
if (loginMode.value === 'sms') {
loginMode.value = 'pwd'
} else {
loginMode.value = 'sms'
}
// 重置相关字段
agreeAgreement.value = false
if (timer) { clearInterval(timer); timer = null }
countDown.value = 0
}
// 忘记密码跳转
const goForgetPwd = () => {
uni.navigateTo({ url: '/pageSubPack/loginSub/forget-pwd' })
}
// ========== 验证码登录:获取验证码 ==========
const getVerifyCode = () => {
if (!canGetVerify.value) return;
let params = {
phone:phoneNumber.value,
scene:'login'
}
uni.showLoading({ title: '发送中...', mask: true });
getSendCode(params).then(res =>{
if(res.code === 200){
uni.hideLoading();
uni.showToast({
title: '验证码已发送',
icon: 'success'
});
// 开始60秒倒计时
countDown.value = 60;
timer = setInterval(() => {
countDown.value--;
if (countDown.value <= 0) {
clearInterval(timer);
timer = null;
}
}, 1000);
}else{
uni.showToast({
title: '验证码发送失败',
icon: 'none'
});
}
if (!canGetVerify.value) return
uni.showLoading({ title: '发送中...', mask: true })
getSendCode({ phone: phoneNumber.value, scene: 'login' }).then(res => {
uni.hideLoading()
if (res.code === 200) {
uni.showToast({ title: '验证码已发送', icon: 'success' })
countDown.value = 60
timer = setInterval(() => {
countDown.value--
if (countDown.value <= 0) {
clearInterval(timer)
timer = null
}
}, 1000)
} else {
uni.showToast({ title: '验证码发送失败', icon: 'none' })
}
}).catch(err => {
uni.hideLoading();
uni.showToast({
title: err.msg||'网络异常',
icon: 'none'
});
uni.hideLoading()
uni.showToast({ title: err.msg || '网络异常', icon: 'none' })
})
}
};
// 登录处理
// ========== 登录处理(分发) ==========
const handleLogin = () => {
if (!phoneNumber.value) {
uni.showToast({ title: '请输入手机号', icon: 'none' });
return;
}
if (!/^1[3-9]\d{9}$/.test(phoneNumber.value)) {
uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
return;
}
if (!verifyCode.value) {
uni.showToast({ title: '请输入验证码', icon: 'none' });
return;
}
if (!/^\d{6}$/.test(verifyCode.value)) {
uni.showToast({ title: '验证码为 6 位数字', icon: 'none' });
return;
}
if (!agreeAgreement.value) {
openPrivacyPopup('login')
return;
if (loginMode.value === 'sms') {
handleSmsLogin()
} else {
handlePwdLogin()
}
}
// ========== 验证码登录 ==========
const handleSmsLogin = () => {
if (!phoneNumber.value) { uni.showToast({ title: '请输入手机号', icon: 'none' }); return }
if (!/^1[3-9]\d{9}$/.test(phoneNumber.value)) { uni.showToast({ title: '请输入正确的手机号', icon: 'none' }); return }
if (!verifyCode.value) { uni.showToast({ title: '请输入验证码', icon: 'none' }); return }
if (!/^\d{6}$/.test(verifyCode.value)) { uni.showToast({ title: '验证码为 6 位数字', icon: 'none' }); return }
if (!agreeAgreement.value) { openPrivacyPopup('login'); return }
doLogin()
};
}
// ========== 密码登录 ==========
const handlePwdLogin = () => {
if (!account.value) { uni.showToast({ title: '请输入账号', icon: 'none' }); return }
if (account.value.length < 4 || account.value.length > 20) { uni.showToast({ title: '账号长度需为 4-20 位', icon: 'none' }); return }
if (!password.value) { uni.showToast({ title: '请输入密码', icon: 'none' }); return }
if (password.value.length < 6) { uni.showToast({ title: '密码至少输入 6 位', icon: 'none' }); return }
if (password.value.length > 16) { uni.showToast({ title: '密码不能超过 16 位', icon: 'none' }); return }
if (!agreeAgreement.value) { openPrivacyPopup('login'); return }
doLogin()
}
// ========== 执行登录 ==========
const doLogin = () => {
uni.showLoading({ title: '登录中...', mask: true });
const push_cid = uni.getStorageSync('push_cid')
let params = {
phone:phoneNumber.value,
smsCode:verifyCode.value,
scene:'login',
cid:push_cid
uni.showLoading({ title: '登录中...', mask: true })
const push_cid = uni.getStorageSync('push_cid')
if (loginMode.value === 'sms') {
// 验证码登录
getLoginBySms({ phone: phoneNumber.value, smsCode: verifyCode.value, scene: 'login', cid: push_cid })
.then(onLoginSuccess).catch(onLoginError)
} else {
// 密码登录
getLoginPassword({ username: account.value, password: password.value, cid: push_cid })
.then(onLoginSuccess).catch(onLoginError)
}
getLoginBySms(params).then(res =>{
if(res.code === 200){
uni.hideLoading();
if(res.data){
uni.setStorageSync('token', res.data.token);
// 存用户ID
if(res.data.userId) {
uni.setStorageSync('userId', res.data.userId);
}
// 存当前绑定的小区ID0 表示未绑定
const currentVillageId = res.data.currentVillageId || 0;
uni.setStorageSync('currentVillageId', currentVillageId);
uni.hideLoading();
uni.showToast({ title: '登录成功', icon: 'success' });
}
setTimeout(() =>{
uni.switchTab({ url: '/pages/index/index' });
},1500)
}else{
uni.showToast({ title: '登录失败', icon: 'none' });
}
const onLoginSuccess = (res) => {
if (res.code === 200 && res.data) {
uni.setStorageSync('token', res.data.token)
if (res.data.userId) { uni.setStorageSync('userId', res.data.userId) }
const currentVillageId = res.data.currentVillageId || 0
uni.setStorageSync('currentVillageId', currentVillageId)
uni.hideLoading()
uni.showToast({ title: '登录成功', icon: 'success' })
setTimeout(() => { uni.switchTab({ url: '/pages/index/index' }) }, 1500)
} else {
uni.hideLoading()
uni.showToast({ title: res.msg || '登录失败', icon: 'none' })
}
}
}else{
uni.showToast({ title:'登录失败', icon: 'none' });
}
}).catch(err => {
uni.hideLoading();
uni.showToast({
title: err.msg || '网络异常,请重试',
icon: 'none'
});
})
// 登录成功后上报推送 CID
// #ifdef APP-PLUS
const cid = uni.getStorageSync('push_cid')
if (cid) {
reportPushCID(cid)
}
// #endif
// setTimeout(() =>{
// uni.switchTab({ url: '/pages/index/index' });
// },1500)
const onLoginError = (err) => {
uni.hideLoading()
};
uni.showToast({ title: err.msg || '网络异常,请重试', icon: 'none' })
}
// ========== 生命周期 ==========
// 页面卸载时清除定时器
onUnmounted(() => {
if (timer) {
clearInterval(timer);
}
});
if (timer) { clearInterval(timer) }
})
</script>
<style scoped>
/* 页面容器 */
.login-page {
padding: 80rpx 40rpx 40rpx;
/* background-color: #f8f9fa; */
min-height: 100vh;
height: 100vh;
overflow: hidden;
box-sizing: border-box;
background: repeating-linear-gradient(to bottom, #E2F0FF, #F6FAFF );
}
.status_bar {
height: 160px;
width: 100%;
}
.status_bar {
height: 160px;
width: 100%;
}
/* 登录标题 */
.login-title {
@@ -335,10 +327,9 @@ onUnmounted(() => {
/* 输入框图标 */
.input-icon {
width: 50rpx;
height: 50rpx;
width: 50rpx;
height: 50rpx;
margin-right: 20rpx;
color: #999;
}
/* 输入框内容 */
@@ -349,7 +340,7 @@ onUnmounted(() => {
height: 100%;
}
/* 深度覆盖 uni-easyinput 内部默认样式,解决高度偏移 */
/* 深度覆盖 uni-easyinput 内部默认样式 */
.input-content :deep(.uni-easyinput__content) {
height: 100% !important;
min-height: unset !important;
@@ -386,36 +377,56 @@ onUnmounted(() => {
margin: 0;
}
/* 获取验证码按钮禁用状态 */
.get-verify-btn:disabled {
background-color: #ccc;
color: #fff;
border-radius: 50px;
}
.get-verify-btn::after{
border-radius: 50px;
.get-verify-btn::after {
border-radius: 50px;
}
/* 协议和忘记密码行 */
.agreement-wrap {
display: flex;
align-items: flex-start;
justify-content: space-between;
margin: 70rpx 0 28rpx 0;
padding: 0 10rpx;
}
/* 协议勾选项 */
.agreement-item {
display: flex;
align-items: center;
margin: 70rpx 0 28rpx 0;
padding-left: 10rpx;
align-items: flex-start;
flex: 1;
min-width: 0;
}
.checkbox-icon {
flex-shrink: 0;
margin-top: 2rpx;
}
/* 协议文本 */
.agreement-text {
font-size: 24rpx;
color: #666;
margin-left: 10rpx;
line-height: 1.5;
}
.agreePrivacy{
color: #2F77FD;
.agreePrivacy {
color: #2F77FD;
}
/* .agreePrivacy.active{
color: #2F77FD;
} */
.forget-pwd {
font-size: 24rpx;
color: #666;
flex-shrink: 0;
margin-left: 16rpx;
line-height: 1.5;
}
/* 登录按钮 */
.login-btn {
width: 100%;
@@ -428,15 +439,13 @@ onUnmounted(() => {
margin-bottom: 60rpx;
}
/* 登录按钮禁用状态 */
.login-btn:disabled {
background-color: #99c8ff;
color: #fff;
/* border-radius: 50px; */
}
/* 禁用按钮边框 */
button:after{
border-radius: 8px;
button:after {
border-radius: 8px;
}
/* 底部链接 */
@@ -446,7 +455,6 @@ button:after{
padding: 0 20rpx;
}
/* 链接项 */
.link-item {
font-size: 28rpx;
color: #007aff;