From 446c33145b8a6280439d869b9246dfb1c27a033b Mon Sep 17 00:00:00 2001 From: hzj <1304805162@qq.com> Date: Tue, 4 Nov 2025 15:14:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=BC=93=E5=AD=98=E9=97=AE=E9=A2=98):=20?= =?UTF-8?q?=E5=9C=A8=E8=8E=B7=E5=8F=96=E4=BF=A1=E6=81=AF=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E4=B8=AD=E6=B7=BB=E5=8A=A0=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E7=9A=84=E6=A0=87=E5=BF=97=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=A4=84=E7=90=86=E5=88=87=E6=8D=A2=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E5=90=8E=E7=94=A8=E7=9A=84=E6=98=AF=E4=B8=8A=E4=B8=AA=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E7=9A=84=E4=BF=A1=E6=81=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/usePageInitialization.js | 87 ++++++++++++++++++------------ src/views/AgencyCenterView.vue | 3 +- src/views/HostCenterView.vue | 1 + 3 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/utils/usePageInitialization.js b/src/utils/usePageInitialization.js index b200820..e383653 100644 --- a/src/utils/usePageInitialization.js +++ b/src/utils/usePageInitialization.js @@ -4,12 +4,22 @@ import { getBankBalance, getMemberProfile, getTeamMemberWorkStatistics, - getTeamEntry + getTeamEntry, } from '../api/wallet.js' -import { connectApplication, parseAccessOrigin, parseHeader, setHttpHeaders, isInApp } from '../utils/appBridge.js' -import { appConnectionManager, isAppConnected, getAppHeaderInfo } from '../utils/appConnectionManager.js' +import { + connectApplication, + parseAccessOrigin, + parseHeader, + setHttpHeaders, + isInApp, +} from '../utils/appBridge.js' +import { + appConnectionManager, + isAppConnected, + getAppHeaderInfo, +} from '../utils/appConnectionManager.js' import { checkAndRedirect } from '../utils/routeGuard.js' -import {getTeamInfo, getUserId, getUserProfile, setTeamInfo} from '../utils/userStore.js' +import { getTeamInfo, getUserId, getUserProfile, setTeamInfo } from '../utils/userStore.js' import { showError } from '../utils/toast.js' /** @@ -20,6 +30,7 @@ import { showError } from '../utils/toast.js' * @param {boolean} options.needsBankBalance 是否需要获取银行余额 (默认: true) * @param {boolean} options.needsWorkStatistics 是否需要获取工作统计 (默认: true) * @param {boolean} options.needsRouteGuard 是否需要路由守卫检查 (默认: true) + * @param {boolean} options.forceRefresh 是否强制刷新数据 (默认: false) * @param {Function} options.onDataLoaded 数据加载完成后的回调函数 * @param {Function} options.onConnectionSuccess APP连接成功后的回调函数 * @param {Function} options.onConnectionFailed APP连接失败后的回调函数 @@ -30,9 +41,10 @@ export function usePageInitialization(options = {}) { needsWorkStatistics = true, needsTeamInfo = false, needsRouteGuard = true, + forceRefresh = false, // 强制刷新数据 onDataLoaded, onConnectionSuccess, - onConnectionFailed + onConnectionFailed, } = options const router = useRouter() @@ -49,13 +61,13 @@ export function usePageInitialization(options = {}) { id: '1001', userAvatar: '', userNickname: 'user', - account: '1234567890' + account: '1234567890', }) // 收入信息 const salaryInfo = reactive({ - hostSalary: 0.00, - currentSalary: 0.00 + hostSalary: 0.0, + currentSalary: 0.0, }) // 任务信息 @@ -63,7 +75,7 @@ export function usePageInitialization(options = {}) { anchorType: 'Chat', minutesProgress: 0, minutesTotal: 120, - giftTask: 0 + giftTask: 0, }) /** @@ -165,9 +177,7 @@ export function usePageInitialization(options = {}) { // 设置连接状态 appConnectionManager.setConnecting(connectionPromise) - connectionPromise - .then(resolve) - .catch(reject) + connectionPromise.then(resolve).catch(reject) }) console.debug('✅ Connection completed successfully') @@ -179,7 +189,6 @@ export function usePageInitialization(options = {}) { } return connectionResult - } catch (error) { console.error('❌ Connection failed:', error) appConnected.value = false @@ -192,7 +201,7 @@ export function usePageInitialization(options = {}) { // 连接失败的特殊处理 if (error.message && error.message.includes('parse access')) { - router.push({ path: '/not_app', query: { message: error.message }}) + router.push({ path: '/not_app', query: { message: error.message } }) } return { success: false, error: error.message } @@ -200,18 +209,23 @@ export function usePageInitialization(options = {}) { } const fetchUserInfo = async () => { - const userProfile = getUserProfile(); - if (!userProfile) { - const res = await getMemberProfile() - if (res.status && res.body) { - userInfo.id = res.body.account - userInfo.userNickname = res.body.userNickname - userInfo.userAvatar = res.body.userAvatar + if (!forceRefresh) { + const userProfile = getUserProfile() + if (userProfile) { + // 使用缓存数据 + userInfo.id = userProfile.account + userInfo.userNickname = userProfile.userNickname + userInfo.userAvatar = userProfile.userAvatar + return } - } else { - userInfo.id = userProfile.account - userInfo.userNickname = userProfile.userNickname - userInfo.userAvatar = userProfile.userAvatar + } + + // 强制从服务器获取数据 + const res = await getMemberProfile() + if (res.status && res.body) { + userInfo.id = res.body.account + userInfo.userNickname = res.body.userNickname + userInfo.userAvatar = res.body.userAvatar } } @@ -221,8 +235,14 @@ export function usePageInitialization(options = {}) { const fetchTeamInfo = async () => { if (!needsTeamInfo) return - const teamInfo = getTeamInfo() - if (teamInfo) return teamInfo + // 如果不是强制刷新,则尝试使用缓存 + if (!forceRefresh) { + const teamInfo = getTeamInfo() + if (teamInfo) { + console.log('teamInfo:', teamInfo) + return teamInfo + } + } try { console.debug('🏢 Fetching team information...') @@ -275,11 +295,11 @@ export function usePageInitialization(options = {}) { // 格式化余额显示,保留2位小数 salaryInfo.currentSalary = response.body.toFixed(2) } else { - salaryInfo.currentSalary = 0.00 + salaryInfo.currentSalary = 0.0 } } catch (error) { console.error('Failed to fetch bank balance:', error) - salaryInfo.currentSalary = 0.00 + salaryInfo.currentSalary = 0.0 // 可以显示错误提示 showError('Failed to load balance. Please try again.') } finally { @@ -301,10 +321,10 @@ export function usePageInitialization(options = {}) { const ownTime = Number(response.body.ownTime) || 0 const otherTime = Number(response.body.otherTime) || 0 const totalTime = ownTime + otherTime - taskInfo.minutesProgress = totalTime >= taskInfo.minutesTotal ? taskInfo.minutesTotal : totalTime + taskInfo.minutesProgress = + totalTime >= taskInfo.minutesTotal ? taskInfo.minutesTotal : totalTime taskInfo.giftTask = response.body.acceptGiftValue || 0 // acceptGiftValue } - } catch (error) { console.error('获取工作统计失败:', error) } @@ -347,12 +367,11 @@ export function usePageInitialization(options = {}) { userProfile: userProfile.value, userInfo: userInfo, salaryInfo: salaryInfo, - taskInfo: taskInfo + taskInfo: taskInfo, }) } console.debug('✅ Data initialization completed') - } catch (error) { console.error('❌ Data initialization failed:', error) } @@ -424,6 +443,6 @@ export function usePageInitialization(options = {}) { initializePage, // 手动初始化方法(如果需要重新初始化) - reinitialize: initializePage + reinitialize: initializePage, } } diff --git a/src/views/AgencyCenterView.vue b/src/views/AgencyCenterView.vue index e609c8c..f92ccb2 100644 --- a/src/views/AgencyCenterView.vue +++ b/src/views/AgencyCenterView.vue @@ -584,7 +584,8 @@ const fetchTeamBillData = async () => { const { userInfo, salaryInfo, taskInfo, handleImageError, getAvatarPlaceholder } = usePageInitializationWithConfig('AGENCY_CENTER', { needsTeamInfo: true, - onDataLoaded: (data) => { + forceRefresh: true, // 强制刷新 + onDataLoaded: () => { fetchTeamMemberCount() fetchTeamBillData() // 新增:获取团队账单数据 fetchMemberWorkData() // 新增:获取成员工作数据 diff --git a/src/views/HostCenterView.vue b/src/views/HostCenterView.vue index 5a1475a..cec2f36 100644 --- a/src/views/HostCenterView.vue +++ b/src/views/HostCenterView.vue @@ -249,6 +249,7 @@ const transfer = () => { const { appConnected, userInfo, salaryInfo, taskInfo, handleImageError, getAvatarPlaceholder } = usePageInitializationWithConfig('HOST_CENTER', { needsTeamInfo: true, + forceRefresh: true, // 强制刷新 onDataLoaded: () => { fetchMemberWorkData() // 新增:获取成员工作数据 },