2025-10-20 22:41:56 +08:00

236 lines
8.4 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup>
import { onLaunch, onShow, onError } from '@dcloudio/uni-app';
import { ShoproInit } from './sheep';
import sheep from '@/sheep';
// 防止重复登录的标志
let isLoggingIn = false;
onLaunch(() => {
console.log('[App] 应用启动...');
// 隐藏原生导航栏 使用自定义底部导航
uni.hideTabBar({
fail: () => {},
});
// 加载Shopro底层依赖
ShoproInit();
// 检测建行生活登录参数
checkCCBLogin();
});
onError((err) => {
console.log('AppOnError:', err);
});
onShow(() => {
// #ifdef APP-PLUS
// 获取urlSchemes参数
const args = plus.runtime.arguments;
if (args) {
}
// 获取剪贴板
uni.getClipboardData({
success: (res) => {},
});
// #endif
});
/**
* 检测URL中的建行登录参数并自动登录
* 建行跳转URL格式url?platform=ccblife&channel=mbs&ccbParamSJ=xxx&CITYID=330100&USERCITYID=440100
*
* 逻辑:
* 1. 解析URL参数获取ccbParamSJ
* 2. 与本地存储的lastCcbParamSJ比较
* 3. 如果一致则无需重新登录
* 4. 如果不一致则需要切换用户(退出当前用户后重新登录)
*/
const checkCCBLogin = () => {
// #ifdef H5
console.log('[App] ========== 开始检测建行登录参数 ==========');
console.log('[App] 完整URL:', window.location.href);
console.log('[App] search:', window.location.search);
try {
let ccbParamSJ = null;
let platform = null;
let cityid = null;
// 优先从search中提取参数建行标准格式url?platform=ccblife&ccbParamSJ=xxx
if (window.location.search) {
const urlParams = new URLSearchParams(window.location.search);
ccbParamSJ = urlParams.get('ccbParamSJ');
platform = urlParams.get('platform');
cityid = urlParams.get('CITYID') || urlParams.get('cityid');
console.log('[App] 从search中解析参数');
}
// 备用从hash中提取参数UniApp内部跳转可能使用
if (!ccbParamSJ && window.location.hash.includes('?')) {
const hashParts = window.location.hash.split('?');
if (hashParts.length > 1) {
const hashParams = new URLSearchParams(hashParts[1]);
ccbParamSJ = hashParams.get('ccbParamSJ');
platform = hashParams.get('platform');
cityid = hashParams.get('CITYID') || hashParams.get('cityid');
console.log('[App] 从hash中解析参数');
}
}
console.log('[App] ====== 参数解析结果 ======');
console.log('[App] platform:', platform);
console.log('[App] CITYID:', cityid);
console.log('[App] 当前ccbParamSJ:', ccbParamSJ ? ccbParamSJ.substring(0, 50) + '...' : 'null');
// 获取上次保存的ccbParamSJ
const lastCcbParamSJ = uni.getStorageSync('lastCcbParamSJ') || null;
console.log('[App] 上次ccbParamSJ:', lastCcbParamSJ ? lastCcbParamSJ.substring(0, 50) + '...' : 'null');
// 判断参数是否变化
const isParamChanged = ccbParamSJ && (lastCcbParamSJ !== ccbParamSJ);
const isFirstTime = ccbParamSJ && !lastCcbParamSJ;
console.log('[App] 是否首次登录:', isFirstTime);
console.log('[App] 参数是否变化:', isParamChanged);
console.log('[App] ===========================');
// 如果有建行参数
if (ccbParamSJ && platform === 'ccblife') {
// 如果参数未变化,无需重新登录
if (!isParamChanged && !isFirstTime) {
console.log('[App] ccbParamSJ未变化无需重新登录跳过');
// 清除URL参数
const cleanUrl = window.location.origin + window.location.pathname + '#/pages/index/index';
window.history.replaceState({}, '', cleanUrl);
console.log('[App] ========== 检测完成 ==========');
return;
}
// 参数变化了,需要切换用户
if (isParamChanged) {
console.log('[App] 🔄🔄🔄 检测到ccbParamSJ变化需要切换用户');
} else {
console.log('[App] 🆕 首次登录');
}
// 🔒 防止重复调用:检查是否正在登录中
if (isLoggingIn) {
console.log('[App] ⚠️ 已有登录流程正在执行,跳过重复调用');
return;
}
// 设置登录中标志
isLoggingIn = true;
console.log('[App] 🔒 设置登录锁定标志');
// 延迟执行登录,确保依赖已初始化
setTimeout(async () => {
try {
console.log('[App] 开始执行登录流程...');
// 如果是切换用户,先退出当前用户
const userStore = sheep.$store('user');
if (isParamChanged && userStore && userStore.isLogin) {
console.log('[App] 退出当前用户,准备切换...');
await userStore.logout();
uni.removeStorageSync('token');
uni.removeStorageSync('userInfo');
console.log('[App] 当前用户已退出');
}
// 显示loading
const loadingTitle = isParamChanged ? '切换用户中...' : '建行登录中...';
uni.showLoading({
title: loadingTitle,
mask: true
});
// 调用建行登录API只传三个参数
console.log('[App] 调用登录API参数:', {
ccbParamSJ: ccbParamSJ.substring(0, 30) + '...',
cityid: cityid || '360100',
CITYID: cityid || '360100'
});
const result = await sheep.$api.third.ccbLogin({
ccbParamSJ: ccbParamSJ,
cityid: cityid || '360100',
CITYID: cityid || '360100'
});
console.log('[App] 登录API返回:', result);
uni.hideLoading();
if (result.code === 1) {
console.log('[App] ✅✅✅ 建行登录成功!');
console.log('[App] Token:', result.data.token?.substring(0, 20) + '...');
console.log('[App] 用户信息:', result.data.user_info);
// 设置token和用户信息
sheep.$store('user').setToken(result.data.token);
uni.setStorageSync('userInfo', result.data.user_info);
// 🔑 关键保存当前的ccbParamSJ到本地存储
uni.setStorageSync('lastCcbParamSJ', ccbParamSJ);
console.log('[App] ✅ 已保存ccbParamSJ到本地存储');
// 显示欢迎提示
uni.showToast({
title: `登录成功`,
icon: 'success',
duration: 2000
});
// 清除URL中的敏感参数保留干净的URL
const cleanUrl = window.location.origin + window.location.pathname + '#/pages/index/index';
window.history.replaceState({}, '', cleanUrl);
console.log('[App] URL参数已清除');
} else {
throw new Error(result.msg || '登录失败');
}
} catch (error) {
console.error('[App] ❌❌❌ 建行登录失败:', error);
uni.hideLoading();
uni.showModal({
title: isParamChanged ? '切换用户失败' : '登录失败',
content: error.message || error.msg || '请检查网络连接',
showCancel: false,
complete: () => {
// 登录失败后也清除URL参数
const cleanUrl = window.location.origin + window.location.pathname + '#/pages/index/index';
window.history.replaceState({}, '', cleanUrl);
}
});
} finally {
// 🔓 无论成功失败,都释放登录锁
isLoggingIn = false;
console.log('[App] 🔓 释放登录锁定标志');
}
}, 1200); // 延迟1200ms确保Shopro和API已加载完成
} else {
console.log('[App] 未检测到建行登录参数');
if (ccbParamSJ && platform !== 'ccblife') {
console.warn('[App] ⚠️ 发现ccbParamSJ但platform不是ccblife:', platform);
}
}
} catch (error) {
console.error('[App] ❌ 参数解析错误:', error);
}
console.log('[App] ========== 检测完成 ==========');
// #endif
};
</script>
<style lang="scss">
@import '@/sheep/scss/index.scss';
</style>