登录验证提示

This commit is contained in:
FengHui
2026-04-10 12:17:53 +08:00
parent 4d8403609f
commit 7c409e8528

View File

@@ -1,13 +1,13 @@
<template> <template>
<view class="wx-login-page"> <view class="wx-login-page">
<!-- 顶部导航栏 --> <!-- 顶部导航栏 -->
<view class="nav-bar"> <!-- <view class="nav-bar">
<view class="nav-back" @click="goBack"> <view class="nav-back" @click="goBack">
<uni-icons type="arrowleft" size="24" color="#333"></uni-icons> <uni-icons type="arrowleft" size="24" color="#333"></uni-icons>
</view> </view>
<view class="nav-title">登录</view> <view class="nav-title">登录</view>
<view class="nav-placeholder"></view> <view class="nav-placeholder"></view>
</view> </view> -->
<!-- 页面内容 --> <!-- 页面内容 -->
<view class="page-content"> <view class="page-content">
@@ -83,8 +83,7 @@
<button <button
class="auth-btn primary" class="auth-btn primary"
open-type="getPhoneNumber" open-type="getPhoneNumber"
@getphonenumber="getPhoneNumber" @getphonenumber="onWxGetPhoneNumber"
:disabled="!canSubmit"
> >
<uni-icons type="phone" size="20" color="#FFFFFF"></uni-icons> <uni-icons type="phone" size="20" color="#FFFFFF"></uni-icons>
<text>手机号快捷登录</text> <text>手机号快捷登录</text>
@@ -93,7 +92,7 @@
<!-- H5和App使用普通按钮 --> <!-- H5和App使用普通按钮 -->
<!-- #ifndef MP-WEIXIN --> <!-- #ifndef MP-WEIXIN -->
<button class="auth-btn primary" @click="wxLogin" :disabled="!canSubmit"> <button class="auth-btn primary" @click="wxLogin">
<uni-icons type="phone" size="20" color="#FFFFFF"></uni-icons> <uni-icons type="phone" size="20" color="#FFFFFF"></uni-icons>
<text>手机号快捷登录</text> <text>手机号快捷登录</text>
</button> </button>
@@ -101,7 +100,7 @@
<!-- 测试登录按钮仅开发环境 --> <!-- 测试登录按钮仅开发环境 -->
<!-- #ifdef APP-PLUS || H5 --> <!-- #ifdef APP-PLUS || H5 -->
<button class="auth-btn secondary" @click="testLogin" :disabled="!canSubmit"> <button class="auth-btn secondary" @click="testLogin">
<text>测试账号登录</text> <text>测试账号登录</text>
</button> </button>
<!-- #endif --> <!-- #endif -->
@@ -198,15 +197,69 @@ const goBack = () => {
uni.navigateBack(); uni.navigateBack();
}; };
// 微信获取手机号 // 通用的验证函数
const getPhoneNumber = async (e) => { const validateForm = () => {
console.log('获取手机号:', e); if (userType.value === null) {
uni.showToast({
title: '请先选择您的角色(个人或单位)',
icon: 'none',
duration: 2000
});
return false;
}
if (!canSubmit.value) { if (userType.value === 0 && orgType.value === null) {
$api.msg('请先完成上述选择并同意隐私协议'); uni.showToast({
title: '请选择机构类型',
icon: 'none',
duration: 2000
});
return false;
}
if (!agreedToAgreement.value) {
uni.showToast({
title: '请先阅读并同意隐私协议',
icon: 'none',
duration: 2000
});
return false;
}
return true;
};
// 微信小程序授权前的检查
const checkBeforeWxAuth = (e) => {
console.log('checkBeforeWxAuth called, canSubmit:', canSubmit.value);
console.log('userType:', userType.value, 'agreedToAgreement:', agreedToAgreement.value);
// 验证表单
if (!validateForm()) {
console.log('Validation failed, preventing wx auth');
// 阻止微信授权流程
if (e && e.preventDefault) {
e.preventDefault();
}
return false;
} else {
console.log('Validation passed, allowing wx auth');
}
};
// 微信获取手机号
const onWxGetPhoneNumber = async (e) => {
console.log('onWxGetPhoneNumber called, event:', e);
console.log('userType:', userType.value, 'agreedToAgreement:', agreedToAgreement.value);
// 使用通用验证函数
if (!validateForm()) {
console.log('Validation failed in onWxGetPhoneNumber');
return; return;
} }
console.log('Validation passed, proceeding with wx auth');
if (e.detail.errMsg === 'getPhoneNumber:ok') { if (e.detail.errMsg === 'getPhoneNumber:ok') {
uni.login({ uni.login({
provider: 'weixin', provider: 'weixin',
@@ -265,23 +318,41 @@ const getPhoneNumber = async (e) => {
}, },
fail: (err) => { fail: (err) => {
console.error('获取微信登录code失败', err); console.error('获取微信登录code失败', err);
$api.msg('获取登录信息失败,请重试'); uni.showToast({
title: '获取登录信息失败,请重试',
icon: 'none',
duration: 2000
});
} }
}); });
} else if (e.detail.errMsg === 'getPhoneNumber:fail user deny') { } else if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
$api.msg('您取消了授权'); uni.showToast({
title: '您取消了授权',
icon: 'none',
duration: 2000
});
} else { } else {
$api.msg('获取手机号失败'); uni.showToast({
title: '获取手机号失败',
icon: 'none',
duration: 2000
});
} }
}; };
// H5/App 微信登录(暂保持原有逻辑,后续可调整) // H5/App 微信登录(暂保持原有逻辑,后续可调整)
const wxLogin = () => { const wxLogin = () => {
if (!canSubmit.value) { console.log('wxLogin called');
$api.msg('请先完成上述选择并同意隐私协议'); console.log('userType:', userType.value, 'agreedToAgreement:', agreedToAgreement.value);
// 使用通用验证函数
if (!validateForm()) {
console.log('Validation failed in wxLogin');
return; return;
} }
console.log('Validation passed, proceeding with H5/App login');
// #ifdef H5 // #ifdef H5
// H5端跳转到H5登录页面 // H5端跳转到H5登录页面
uni.navigateTo({ uni.navigateTo({
@@ -316,7 +387,11 @@ const wxLogin = () => {
uni.setStorageSync('userInfo', userInfo); uni.setStorageSync('userInfo', userInfo);
} }
$api.msg('登录成功'); uni.showToast({
title: '登录成功',
icon: 'success',
duration: 2000
});
// 登录成功后返回上一页 // 登录成功后返回上一页
uni.navigateBack(); uni.navigateBack();
}); });
@@ -325,7 +400,11 @@ const wxLogin = () => {
}, },
fail: (err) => { fail: (err) => {
console.error('微信登录失败:', err); console.error('微信登录失败:', err);
$api.msg('微信登录失败'); uni.showToast({
title: '微信登录失败',
icon: 'none',
duration: 2000
});
} }
}); });
} }
@@ -336,11 +415,17 @@ const wxLogin = () => {
// 测试账号登录(仅开发环境) // 测试账号登录(仅开发环境)
const testLogin = () => { const testLogin = () => {
if (!canSubmit.value) { console.log('testLogin called');
$api.msg('请先完成上述选择并同意隐私协议'); console.log('userType:', userType.value, 'agreedToAgreement:', agreedToAgreement.value);
// 使用通用验证函数
if (!validateForm()) {
console.log('Validation failed in testLogin');
return; return;
} }
console.log('Validation passed, proceeding with test login');
uni.showLoading({ title: '登录中...' }); uni.showLoading({ title: '登录中...' });
const params = { const params = {
@@ -359,15 +444,27 @@ const testLogin = () => {
uni.setStorageSync('userInfo', userInfo); uni.setStorageSync('userInfo', userInfo);
} }
$api.msg('测试登录成功'); uni.showToast({
title: '测试登录成功',
icon: 'success',
duration: 2000
});
// 登录成功后返回上一页 // 登录成功后返回上一页
uni.navigateBack(); uni.navigateBack();
}).catch(() => { }).catch(() => {
$api.msg('获取用户信息失败'); uni.showToast({
title: '获取用户信息失败',
icon: 'none',
duration: 2000
});
}); });
}).catch((err) => { }).catch((err) => {
uni.hideLoading(); uni.hideLoading();
$api.msg(err.msg || '登录失败'); uni.showToast({
title: err.msg || '登录失败',
icon: 'none',
duration: 2000
});
}); });
}; };
@@ -547,6 +644,8 @@ onLoad(() => {
&:disabled &:disabled
opacity: 0.5 opacity: 0.5
cursor: not-allowed cursor: not-allowed
background: #CCCCCC !important
box-shadow: none !important
text text
margin-left: 12rpx margin-left: 12rpx