2025-11-14 18:10:59 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 登录帮助工具类
|
|
|
|
|
|
* 用于根据平台类型跳转到对应的登录页面
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import useUserStore from '@/stores/useUserStore';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查当前平台类型
|
|
|
|
|
|
* @returns {string} 平台类型:'mp-weixin' | 'h5' | 'app'
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function getPlatformType() {
|
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
|
return 'mp-weixin';
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
|
return 'h5';
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
|
|
return 'app';
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
return 'h5'; // 默认返回H5
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 跳转到对应平台的登录页面
|
|
|
|
|
|
* @param {Object} options 跳转选项
|
|
|
|
|
|
* @param {string} options.redirectUrl 登录成功后跳转的URL
|
|
|
|
|
|
* @param {string} options.loginType 登录类型:'wechat' | 'account' | 'idCard'
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function navigateToLoginPage(options = {}) {
|
|
|
|
|
|
const { redirectUrl, loginType = 'account' } = options;
|
|
|
|
|
|
const platform = getPlatformType();
|
|
|
|
|
|
|
|
|
|
|
|
let loginPage = '';
|
|
|
|
|
|
let params = {};
|
|
|
|
|
|
|
|
|
|
|
|
if (redirectUrl) {
|
|
|
|
|
|
params.redirectUrl = redirectUrl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (platform) {
|
|
|
|
|
|
case 'mp-weixin':
|
|
|
|
|
|
// 小程序端使用微信授权登录,直接显示微信授权弹窗
|
|
|
|
|
|
uni.$emit('showLoginModal', { loginType: 'wechat' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
case 'h5':
|
|
|
|
|
|
if (loginType === 'idCard') {
|
|
|
|
|
|
// H5端身份证号码登录
|
|
|
|
|
|
loginPage = '/pages/login/id-card-login';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// H5端账号密码登录
|
|
|
|
|
|
loginPage = '/pages/login/h5-login';
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'app':
|
|
|
|
|
|
// App端使用微信授权登录
|
|
|
|
|
|
uni.$emit('showLoginModal', { loginType: 'wechat' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
loginPage = '/pages/login/h5-login';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (loginPage) {
|
|
|
|
|
|
const queryString = Object.keys(params).length > 0
|
|
|
|
|
|
? `?${new URLSearchParams(params).toString()}`
|
|
|
|
|
|
: '';
|
|
|
|
|
|
|
2025-12-05 16:29:47 +08:00
|
|
|
|
const finalUrl = `${loginPage}${queryString}`;
|
|
|
|
|
|
const pages = getCurrentPages();
|
|
|
|
|
|
|
|
|
|
|
|
if (pages.length >= 10) {
|
|
|
|
|
|
// 页面栈已满,使用redirectTo替代
|
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
|
url: finalUrl,
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
console.error('页面跳转失败:', err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
|
url: finalUrl,
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
console.error('页面跳转失败:', err);
|
|
|
|
|
|
// 失败后尝试redirectTo
|
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
|
url: finalUrl,
|
|
|
|
|
|
fail: (err2) => {
|
|
|
|
|
|
console.error('redirectTo也失败:', err2);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-11-14 18:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查登录状态,如果未登录则跳转到对应登录页面
|
|
|
|
|
|
* @param {Object} options 选项
|
|
|
|
|
|
* @param {string} options.redirectUrl 登录成功后跳转的URL
|
|
|
|
|
|
* @param {string} options.loginType 登录类型
|
|
|
|
|
|
* @returns {boolean} 是否已登录
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function checkLoginAndNavigate(options = {}) {
|
|
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
|
|
|
|
|
|
|
if (userStore.hasLogin) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 未登录,跳转到对应登录页面
|
|
|
|
|
|
navigateToLoginPage(options);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理身份证号码登录的URL参数
|
|
|
|
|
|
* @param {string} url 包含参数的URL
|
|
|
|
|
|
* @returns {Object} 解析后的参数对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function parseIdCardLoginParams(url) {
|
|
|
|
|
|
const urlObj = new URL(url, window.location.origin);
|
|
|
|
|
|
const params = {};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取身份证号码base64参数
|
2025-11-14 19:05:03 +08:00
|
|
|
|
const idCard = urlObj.searchParams.get('idCard');
|
|
|
|
|
|
if (idCard) {
|
|
|
|
|
|
params.idCard = idCard;
|
2025-11-14 18:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取重定向URL
|
|
|
|
|
|
const redirectUrl = urlObj.searchParams.get('redirectUrl');
|
|
|
|
|
|
if (redirectUrl) {
|
|
|
|
|
|
params.redirectUrl = redirectUrl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return params;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Base64解码身份证号码
|
|
|
|
|
|
* @param {string} base64Str base64编码的字符串
|
|
|
|
|
|
* @returns {string} 解码后的身份证号码
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function decodeIdCardBase64(base64Str) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
|
return atob(base64Str);
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
|
const result = uni.base64ToArrayBuffer(base64Str);
|
|
|
|
|
|
return String.fromCharCode.apply(null, new Uint8Array(result));
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
|
|
return plus.base64.decode(base64Str);
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
// 默认使用H5方式
|
|
|
|
|
|
return atob(base64Str);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('Base64解码失败:', error);
|
|
|
|
|
|
throw new Error('身份证号码解码失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
getPlatformType,
|
|
|
|
|
|
navigateToLoginPage,
|
|
|
|
|
|
checkLoginAndNavigate,
|
|
|
|
|
|
parseIdCardLoginParams,
|
|
|
|
|
|
decodeIdCardBase64
|
|
|
|
|
|
};
|