211 lines
5.4 KiB
JavaScript
211 lines
5.4 KiB
JavaScript
/**
|
||
* 登录帮助工具类
|
||
* 用于根据平台类型跳转到对应的登录页面
|
||
*/
|
||
|
||
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':
|
||
case 'app':
|
||
case 'h5':
|
||
// 跳转到微信登录页面
|
||
loginPage = '/pages/login/wx-login';
|
||
break;
|
||
|
||
default:
|
||
loginPage = '/pages/login/h5-login';
|
||
}
|
||
|
||
if (loginPage) {
|
||
const queryString = Object.keys(params).length > 0
|
||
? `?${new URLSearchParams(params).toString()}`
|
||
: '';
|
||
|
||
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);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检查登录状态,如果未登录则跳转到对应登录页面
|
||
* @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参数
|
||
const idCard = urlObj.searchParams.get('idCard');
|
||
if (idCard) {
|
||
params.idCard = idCard;
|
||
}
|
||
|
||
// 获取重定向URL
|
||
const redirectUrl = urlObj.searchParams.get('redirectUrl');
|
||
if (redirectUrl) {
|
||
params.redirectUrl = redirectUrl;
|
||
}
|
||
|
||
return params;
|
||
}
|
||
|
||
/**
|
||
* Base64解码身份证号码
|
||
* @param {string} base64Str base64编码的字符串
|
||
* @returns {string} 解码后的身份证号码
|
||
*/
|
||
/** 预取的 wx.login code,须在 getPhoneNumber 之前获取,授权后勿再 login */
|
||
let cachedWxLoginCode = '';
|
||
|
||
/**
|
||
* 预取 wx.login 的 code(与后续 getPhoneNumber 的 encryptedData 使用同一 session)
|
||
*/
|
||
export function refreshWxLoginCode() {
|
||
return new Promise((resolve, reject) => {
|
||
// #ifdef MP-WEIXIN
|
||
uni.login({
|
||
provider: 'weixin',
|
||
success: (res) => {
|
||
if (res.code) {
|
||
cachedWxLoginCode = res.code;
|
||
resolve(res.code);
|
||
} else {
|
||
reject(new Error('未获取到微信登录凭证'));
|
||
}
|
||
},
|
||
fail: (err) => reject(err),
|
||
});
|
||
// #endif
|
||
// #ifndef MP-WEIXIN
|
||
resolve('');
|
||
// #endif
|
||
});
|
||
}
|
||
|
||
export function getWxLoginCode() {
|
||
return cachedWxLoginCode;
|
||
}
|
||
|
||
/** 取出并清空缓存(微信 code 仅能使用一次) */
|
||
export function takeWxLoginCode() {
|
||
const code = cachedWxLoginCode;
|
||
cachedWxLoginCode = '';
|
||
return code;
|
||
}
|
||
|
||
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,
|
||
refreshWxLoginCode,
|
||
getWxLoginCode,
|
||
takeWxLoginCode,
|
||
};
|