一体机登录功能开发
This commit is contained in:
158
utils/loginHelper.js
Normal file
158
utils/loginHelper.js
Normal file
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* 登录帮助工具类
|
||||
* 用于根据平台类型跳转到对应的登录页面
|
||||
*/
|
||||
|
||||
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()}`
|
||||
: '';
|
||||
|
||||
uni.navigateTo({
|
||||
url: `${loginPage}${queryString}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查登录状态,如果未登录则跳转到对应登录页面
|
||||
* @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 idCardBase64 = urlObj.searchParams.get('idCardBase64');
|
||||
if (idCardBase64) {
|
||||
params.idCardBase64 = idCardBase64;
|
||||
}
|
||||
|
||||
// 获取重定向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
|
||||
};
|
||||
Reference in New Issue
Block a user