Files
JayuCloud/YumiNail-Admin-Program/app.js
T

193 lines
6.5 KiB
JavaScript

/**
* Purpose: auth supports app.js by holding or executing the auth page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const auth = require('./utils/auth')
/**
* Purpose: permission supports app.js by holding or executing the permission page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const permission = require('./utils/permission')
const { resetLoading } = require('./utils/loading')
/**
* Purpose: idleSession supports app.js by holding or executing the idle session page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const idleSession = require('./utils/idle-session')
const { IDLE_TIMEOUT_MS, IDLE_WARNING_MS } = require('./utils/config')
/**
* Purpose: permissionApi supports app.js by holding or executing the permission api page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const permissionApi = require('./api/permission')
/**
* Purpose: authApi supports app.js by holding or executing the auth api page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const authApi = require('./api/auth')
/**
* Purpose: buildCurrentShop supports app.js by holding or executing the build current shop page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function buildCurrentShop(userInfo) {
if (!userInfo || !userInfo.shopId) {
return null
}
return {
id: userInfo.shopId,
shopName: userInfo.shopName || ''
}
}
App({
globalData: {
isLoggedIn: false,
userInfo: null,
currentShop: null,
permissions: [],
permissionCodes: [],
pageDirtyFlags: {}
},
/**
* Purpose: onLaunch supports app.js by holding or executing the on launch page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
onLaunch() {
idleSession.init({
timeoutMs: IDLE_TIMEOUT_MS,
warningMs: IDLE_WARNING_MS,
isLoggedIn: () => auth.isLoggedIn(),
onLogout: () => this.handleIdleLogout()
})
this.bootstrapSession()
},
/**
* Purpose: onShow supports app.js by holding or executing the on show page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
onShow() {
idleSession.onAppShow()
},
/**
* Purpose: onHide supports app.js by holding or executing the on hide page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
onHide() {
idleSession.onAppHide()
},
/**
* Purpose: bootstrapSession supports app.js by holding or executing the bootstrap session page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
bootstrapSession() {
resetLoading()
const userInfo = auth.getStoredUserInfo()
const currentShop = auth.getCurrentShop() || buildCurrentShop(userInfo)
const permissions = auth.getStoredPermissions()
if (currentShop) {
auth.setCurrentShop(currentShop)
}
this.globalData.isLoggedIn = auth.isLoggedIn()
this.globalData.userInfo = userInfo
this.globalData.currentShop = currentShop
this.setPermissionState(permissions)
if (this.globalData.isLoggedIn && !permissions.length) {
this.loadCurrentPermissions().catch(() => undefined)
}
},
/**
* Purpose: setPermissionState supports app.js by holding or executing the set permission state page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
setPermissionState(permissions) {
const list = Array.isArray(permissions) ? permissions : []
this.globalData.permissions = list
this.globalData.permissionCodes = permission.getPermissionCodes(list)
},
async setAdminSession(payload) {
auth.setSession(payload)
this.bootstrapSession()
await this.loadCurrentPermissions().catch(() => [])
},
async loadCurrentPermissions() {
if (!auth.isLoggedIn()) {
auth.clearPermissions()
this.setPermissionState([])
return []
}
const permissions = await permissionApi.getCurrentUserPermissions()
auth.setPermissions(permissions)
this.setPermissionState(permissions)
return permissions
},
/**
* Purpose: refreshUserInfo supports app.js by holding or executing the refresh user info page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
refreshUserInfo(userInfo) {
auth.updateStoredUserInfo(userInfo)
if (userInfo && userInfo.shopId) {
auth.setCurrentShop({
id: userInfo.shopId,
shopName: userInfo.shopName || ''
})
}
this.bootstrapSession()
},
/**
* Purpose: clearAdminSession supports app.js by holding or executing the clear admin session page or API role.
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
*/
clearAdminSession() {
auth.clearSession()
this.globalData.isLoggedIn = false
this.globalData.userInfo = null
this.globalData.currentShop = null
this.globalData.pageDirtyFlags = {}
this.setPermissionState([])
},
async handleIdleLogout() {
if (!auth.isLoggedIn()) {
return
}
try {
await authApi.logout()
} catch (error) {}
this.clearAdminSession()
wx.showToast({
title: '长时间未操作,已退出登录',
icon: 'none'
})
setTimeout(() => {
wx.reLaunch({
url: '/pages/login/index'
})
}, 260)
},
async switchAdminShop(shopId) {
const payload = await authApi.switchShop(shopId)
await this.setAdminSession(payload)
return payload
}
})