init
This commit is contained in:
93
pages/home/index.js
Normal file
93
pages/home/index.js
Normal file
@@ -0,0 +1,93 @@
|
||||
const { getRuntimeConfig } = require('../../config/env')
|
||||
const { sessionStore } = require('../../stores')
|
||||
const { maskToken } = require('../../utils/util')
|
||||
|
||||
const QUICK_ENTRIES = [
|
||||
{
|
||||
title: '登录主包',
|
||||
description: '演示主包内的认证入口、全局会话同步和基础 UI 组件封装。',
|
||||
badge: '主包',
|
||||
actionText: '打开登录页',
|
||||
actionPath: '/pages/login/index'
|
||||
},
|
||||
{
|
||||
title: '业务工作台',
|
||||
description: '演示分包页面,只在需要时加载,保持主包轻量和首页启动稳定。',
|
||||
badge: '分包',
|
||||
actionText: '进入工作台',
|
||||
actionPath: '/packages/demo/pages/workbench/index'
|
||||
}
|
||||
]
|
||||
|
||||
function buildSessionView(state) {
|
||||
const userName = state.userInfo?.nickname || state.userInfo?.name || '访客'
|
||||
|
||||
return {
|
||||
statusLabel: state.isLoggedIn ? '已登录' : '未登录',
|
||||
userName,
|
||||
tokenLabel: maskToken(state.token),
|
||||
permissionsLabel: state.permissions.length ? state.permissions.join(' / ') : '暂无权限'
|
||||
}
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
quickEntries: QUICK_ENTRIES,
|
||||
envName: '',
|
||||
apiBaseUrl: '',
|
||||
isLoggedIn: false,
|
||||
sessionView: buildSessionView(sessionStore.getState())
|
||||
},
|
||||
onLoad() {
|
||||
const runtimeConfig = getRuntimeConfig()
|
||||
|
||||
this.unsubscribe = sessionStore.subscribe(nextState => {
|
||||
this.syncSession(nextState)
|
||||
})
|
||||
|
||||
this.setData({
|
||||
envName: runtimeConfig.name.toUpperCase(),
|
||||
apiBaseUrl: runtimeConfig.baseURL
|
||||
})
|
||||
this.syncSession(sessionStore.getState())
|
||||
},
|
||||
onUnload() {
|
||||
this.unsubscribe?.()
|
||||
},
|
||||
syncSession(state) {
|
||||
this.setData({
|
||||
isLoggedIn: state.isLoggedIn,
|
||||
sessionView: buildSessionView(state)
|
||||
})
|
||||
},
|
||||
handlePrimaryAction() {
|
||||
if (this.data.isLoggedIn) {
|
||||
wx.navigateTo({
|
||||
url: '/packages/demo/pages/workbench/index'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
wx.navigateTo({
|
||||
url: '/pages/login/index'
|
||||
})
|
||||
},
|
||||
handleLogout() {
|
||||
sessionStore.clearSession()
|
||||
wx.showToast({
|
||||
title: '已清理登录态',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
handleEntryAction(event) {
|
||||
const { path } = event.detail
|
||||
|
||||
if (!path) {
|
||||
return
|
||||
}
|
||||
|
||||
wx.navigateTo({
|
||||
url: path
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user