完善后端联调链路与模拟器多通道支持
This commit is contained in:
127
miniprogram/pages/home/home.ts
Normal file
127
miniprogram/pages/home/home.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { clearBackendAuthTokens, loadBackendAuthTokens, loadBackendBaseUrl } from '../../utils/backendAuth'
|
||||
import { getEntryHome, type BackendCardResult, type BackendEntryHomeResult } from '../../utils/backendApi'
|
||||
|
||||
const DEFAULT_CHANNEL_CODE = 'mini-demo'
|
||||
const DEFAULT_CHANNEL_TYPE = 'wechat_mini'
|
||||
|
||||
type HomePageData = {
|
||||
loading: boolean
|
||||
statusText: string
|
||||
userNameText: string
|
||||
tenantText: string
|
||||
channelText: string
|
||||
ongoingSessionText: string
|
||||
recentSessionText: string
|
||||
cards: BackendCardResult[]
|
||||
}
|
||||
|
||||
function requireAuthToken(): string | null {
|
||||
const app = getApp<IAppOption>()
|
||||
const tokens = app.globalData && app.globalData.backendAuthTokens
|
||||
? app.globalData.backendAuthTokens
|
||||
: loadBackendAuthTokens()
|
||||
return tokens && tokens.accessToken ? tokens.accessToken : null
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
loading: false,
|
||||
statusText: '准备加载首页',
|
||||
userNameText: '--',
|
||||
tenantText: '--',
|
||||
channelText: '--',
|
||||
ongoingSessionText: '无',
|
||||
recentSessionText: '无',
|
||||
cards: [],
|
||||
} as HomePageData,
|
||||
|
||||
onLoad() {
|
||||
this.loadEntryHome()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.loadEntryHome()
|
||||
},
|
||||
|
||||
async loadEntryHome() {
|
||||
const accessToken = requireAuthToken()
|
||||
if (!accessToken) {
|
||||
wx.redirectTo({ url: '/pages/login/login' })
|
||||
return
|
||||
}
|
||||
|
||||
this.setData({
|
||||
loading: true,
|
||||
statusText: '正在加载首页聚合',
|
||||
})
|
||||
|
||||
try {
|
||||
const result = await getEntryHome({
|
||||
baseUrl: loadBackendBaseUrl(),
|
||||
accessToken,
|
||||
channelCode: DEFAULT_CHANNEL_CODE,
|
||||
channelType: DEFAULT_CHANNEL_TYPE,
|
||||
})
|
||||
this.applyEntryHomeResult(result)
|
||||
} catch (error) {
|
||||
const message = error && (error as { message?: string }).message ? (error as { message: string }).message : '未知错误'
|
||||
this.setData({
|
||||
loading: false,
|
||||
statusText: `首页加载失败:${message}`,
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
applyEntryHomeResult(result: BackendEntryHomeResult) {
|
||||
this.setData({
|
||||
loading: false,
|
||||
statusText: '首页加载完成',
|
||||
userNameText: result.user.nickname || result.user.publicId || result.user.id,
|
||||
tenantText: `${result.tenant.name} (${result.tenant.code})`,
|
||||
channelText: `${result.channel.displayName} / ${result.channel.code}`,
|
||||
ongoingSessionText: result.ongoingSession
|
||||
? `${result.ongoingSession.eventName || result.ongoingSession.eventDisplayName || result.ongoingSession.eventId || result.ongoingSession.id || result.ongoingSession.sessionId} / ${result.ongoingSession.status || result.ongoingSession.sessionStatus}`
|
||||
: '无',
|
||||
recentSessionText: result.recentSession
|
||||
? `${result.recentSession.eventName || result.recentSession.eventDisplayName || result.recentSession.eventId || result.recentSession.id || result.recentSession.sessionId} / ${result.recentSession.status || result.recentSession.sessionStatus}`
|
||||
: '无',
|
||||
cards: result.cards || [],
|
||||
})
|
||||
},
|
||||
|
||||
handleRefresh() {
|
||||
this.loadEntryHome()
|
||||
},
|
||||
|
||||
handleOpenCard(event: WechatMiniprogram.TouchEvent) {
|
||||
const eventId = event.currentTarget.dataset.eventId as string | undefined
|
||||
if (!eventId) {
|
||||
wx.showToast({
|
||||
title: '该卡片暂无活动入口',
|
||||
icon: 'none',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
wx.navigateTo({
|
||||
url: `/pages/event/event?eventId=${encodeURIComponent(eventId)}`,
|
||||
})
|
||||
},
|
||||
|
||||
handleOpenRecentResult() {
|
||||
wx.navigateTo({
|
||||
url: '/pages/result/result',
|
||||
})
|
||||
},
|
||||
|
||||
handleLogout() {
|
||||
clearBackendAuthTokens()
|
||||
const app = getApp<IAppOption>()
|
||||
if (app.globalData) {
|
||||
app.globalData.backendAuthTokens = null
|
||||
}
|
||||
wx.redirectTo({
|
||||
url: '/pages/login/login',
|
||||
})
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user