feat(缓存问题): 在获取信息的工具类中添加强制刷新的标志,用于处理切换账号后用的是上个账号的信息问题

This commit is contained in:
hzj 2025-11-04 15:14:04 +08:00
parent 7718a74691
commit 446c33145b
3 changed files with 56 additions and 35 deletions

View File

@ -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,
}
}

View File

@ -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() //

View File

@ -249,6 +249,7 @@ const transfer = () => {
const { appConnected, userInfo, salaryInfo, taskInfo, handleImageError, getAvatarPlaceholder } =
usePageInitializationWithConfig('HOST_CENTER', {
needsTeamInfo: true,
forceRefresh: true, //
onDataLoaded: () => {
fetchMemberWorkData() //
},