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, getBankBalance,
getMemberProfile, getMemberProfile,
getTeamMemberWorkStatistics, getTeamMemberWorkStatistics,
getTeamEntry getTeamEntry,
} from '../api/wallet.js' } from '../api/wallet.js'
import { connectApplication, parseAccessOrigin, parseHeader, setHttpHeaders, isInApp } from '../utils/appBridge.js' import {
import { appConnectionManager, isAppConnected, getAppHeaderInfo } from '../utils/appConnectionManager.js' connectApplication,
parseAccessOrigin,
parseHeader,
setHttpHeaders,
isInApp,
} from '../utils/appBridge.js'
import {
appConnectionManager,
isAppConnected,
getAppHeaderInfo,
} from '../utils/appConnectionManager.js'
import { checkAndRedirect } from '../utils/routeGuard.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' import { showError } from '../utils/toast.js'
/** /**
@ -20,6 +30,7 @@ import { showError } from '../utils/toast.js'
* @param {boolean} options.needsBankBalance 是否需要获取银行余额 (默认: true) * @param {boolean} options.needsBankBalance 是否需要获取银行余额 (默认: true)
* @param {boolean} options.needsWorkStatistics 是否需要获取工作统计 (默认: true) * @param {boolean} options.needsWorkStatistics 是否需要获取工作统计 (默认: true)
* @param {boolean} options.needsRouteGuard 是否需要路由守卫检查 (默认: true) * @param {boolean} options.needsRouteGuard 是否需要路由守卫检查 (默认: true)
* @param {boolean} options.forceRefresh 是否强制刷新数据 (默认: false)
* @param {Function} options.onDataLoaded 数据加载完成后的回调函数 * @param {Function} options.onDataLoaded 数据加载完成后的回调函数
* @param {Function} options.onConnectionSuccess APP连接成功后的回调函数 * @param {Function} options.onConnectionSuccess APP连接成功后的回调函数
* @param {Function} options.onConnectionFailed APP连接失败后的回调函数 * @param {Function} options.onConnectionFailed APP连接失败后的回调函数
@ -30,9 +41,10 @@ export function usePageInitialization(options = {}) {
needsWorkStatistics = true, needsWorkStatistics = true,
needsTeamInfo = false, needsTeamInfo = false,
needsRouteGuard = true, needsRouteGuard = true,
forceRefresh = false, // 强制刷新数据
onDataLoaded, onDataLoaded,
onConnectionSuccess, onConnectionSuccess,
onConnectionFailed onConnectionFailed,
} = options } = options
const router = useRouter() const router = useRouter()
@ -49,13 +61,13 @@ export function usePageInitialization(options = {}) {
id: '1001', id: '1001',
userAvatar: '', userAvatar: '',
userNickname: 'user', userNickname: 'user',
account: '1234567890' account: '1234567890',
}) })
// 收入信息 // 收入信息
const salaryInfo = reactive({ const salaryInfo = reactive({
hostSalary: 0.00, hostSalary: 0.0,
currentSalary: 0.00 currentSalary: 0.0,
}) })
// 任务信息 // 任务信息
@ -63,7 +75,7 @@ export function usePageInitialization(options = {}) {
anchorType: 'Chat', anchorType: 'Chat',
minutesProgress: 0, minutesProgress: 0,
minutesTotal: 120, minutesTotal: 120,
giftTask: 0 giftTask: 0,
}) })
/** /**
@ -165,9 +177,7 @@ export function usePageInitialization(options = {}) {
// 设置连接状态 // 设置连接状态
appConnectionManager.setConnecting(connectionPromise) appConnectionManager.setConnecting(connectionPromise)
connectionPromise connectionPromise.then(resolve).catch(reject)
.then(resolve)
.catch(reject)
}) })
console.debug('✅ Connection completed successfully') console.debug('✅ Connection completed successfully')
@ -179,7 +189,6 @@ export function usePageInitialization(options = {}) {
} }
return connectionResult return connectionResult
} catch (error) { } catch (error) {
console.error('❌ Connection failed:', error) console.error('❌ Connection failed:', error)
appConnected.value = false appConnected.value = false
@ -192,7 +201,7 @@ export function usePageInitialization(options = {}) {
// 连接失败的特殊处理 // 连接失败的特殊处理
if (error.message && error.message.includes('parse access')) { 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 } return { success: false, error: error.message }
@ -200,18 +209,23 @@ export function usePageInitialization(options = {}) {
} }
const fetchUserInfo = async () => { const fetchUserInfo = async () => {
const userProfile = getUserProfile(); if (!forceRefresh) {
if (!userProfile) { const userProfile = getUserProfile()
const res = await getMemberProfile() if (userProfile) {
if (res.status && res.body) { // 使用缓存数据
userInfo.id = res.body.account userInfo.id = userProfile.account
userInfo.userNickname = res.body.userNickname userInfo.userNickname = userProfile.userNickname
userInfo.userAvatar = res.body.userAvatar 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 () => { const fetchTeamInfo = async () => {
if (!needsTeamInfo) return if (!needsTeamInfo) return
const teamInfo = getTeamInfo() // 如果不是强制刷新,则尝试使用缓存
if (teamInfo) return teamInfo if (!forceRefresh) {
const teamInfo = getTeamInfo()
if (teamInfo) {
console.log('teamInfo:', teamInfo)
return teamInfo
}
}
try { try {
console.debug('🏢 Fetching team information...') console.debug('🏢 Fetching team information...')
@ -275,11 +295,11 @@ export function usePageInitialization(options = {}) {
// 格式化余额显示保留2位小数 // 格式化余额显示保留2位小数
salaryInfo.currentSalary = response.body.toFixed(2) salaryInfo.currentSalary = response.body.toFixed(2)
} else { } else {
salaryInfo.currentSalary = 0.00 salaryInfo.currentSalary = 0.0
} }
} catch (error) { } catch (error) {
console.error('Failed to fetch bank balance:', 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.') showError('Failed to load balance. Please try again.')
} finally { } finally {
@ -301,10 +321,10 @@ export function usePageInitialization(options = {}) {
const ownTime = Number(response.body.ownTime) || 0 const ownTime = Number(response.body.ownTime) || 0
const otherTime = Number(response.body.otherTime) || 0 const otherTime = Number(response.body.otherTime) || 0
const totalTime = ownTime + otherTime 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 taskInfo.giftTask = response.body.acceptGiftValue || 0 // acceptGiftValue
} }
} catch (error) { } catch (error) {
console.error('获取工作统计失败:', error) console.error('获取工作统计失败:', error)
} }
@ -347,12 +367,11 @@ export function usePageInitialization(options = {}) {
userProfile: userProfile.value, userProfile: userProfile.value,
userInfo: userInfo, userInfo: userInfo,
salaryInfo: salaryInfo, salaryInfo: salaryInfo,
taskInfo: taskInfo taskInfo: taskInfo,
}) })
} }
console.debug('✅ Data initialization completed') console.debug('✅ Data initialization completed')
} catch (error) { } catch (error) {
console.error('❌ Data initialization failed:', error) console.error('❌ Data initialization failed:', error)
} }
@ -424,6 +443,6 @@ export function usePageInitialization(options = {}) {
initializePage, initializePage,
// 手动初始化方法(如果需要重新初始化) // 手动初始化方法(如果需要重新初始化)
reinitialize: initializePage reinitialize: initializePage,
} }
} }

View File

@ -584,7 +584,8 @@ const fetchTeamBillData = async () => {
const { userInfo, salaryInfo, taskInfo, handleImageError, getAvatarPlaceholder } = const { userInfo, salaryInfo, taskInfo, handleImageError, getAvatarPlaceholder } =
usePageInitializationWithConfig('AGENCY_CENTER', { usePageInitializationWithConfig('AGENCY_CENTER', {
needsTeamInfo: true, needsTeamInfo: true,
onDataLoaded: (data) => { forceRefresh: true, //
onDataLoaded: () => {
fetchTeamMemberCount() fetchTeamMemberCount()
fetchTeamBillData() // fetchTeamBillData() //
fetchMemberWorkData() // fetchMemberWorkData() //

View File

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