功能:重建指尖管理师规划系统
This commit is contained in:
+147
-3
@@ -44,12 +44,38 @@ const navigation = require('./utils/navigation')
|
||||
*/
|
||||
const file = require('./utils/file')
|
||||
const { IDLE_TIMEOUT_MS, IDLE_WARNING_MS } = require('./utils/config')
|
||||
const adminAuth = require('./admin/utils/auth')
|
||||
const adminPermission = require('./admin/utils/permission')
|
||||
const adminIdleSession = require('./admin/utils/idle-session')
|
||||
const { resetLoading: resetAdminLoading } = require('./admin/utils/loading')
|
||||
const adminPermissionApi = require('./admin/api/permission')
|
||||
const adminAuthApi = require('./admin/api/auth')
|
||||
const {
|
||||
IDLE_TIMEOUT_MS: ADMIN_IDLE_TIMEOUT_MS,
|
||||
IDLE_WARNING_MS: ADMIN_IDLE_WARNING_MS
|
||||
} = require('./admin/utils/config')
|
||||
|
||||
function buildAdminCurrentShop(userInfo) {
|
||||
if (!userInfo || !userInfo.shopId) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
id: userInfo.shopId,
|
||||
shopName: userInfo.shopName || ''
|
||||
}
|
||||
}
|
||||
|
||||
App({
|
||||
globalData: {
|
||||
userInfo: null,
|
||||
isLoggedIn: false,
|
||||
cartCount: 0,
|
||||
currentShop: null,
|
||||
permissions: [],
|
||||
permissionCodes: [],
|
||||
pageDirtyFlags: {},
|
||||
activeSessionMode: '',
|
||||
shopTheme: theme.DEFAULT_SHOP_THEME,
|
||||
pendingProductFilter: null,
|
||||
pendingWechatMiniLogin: null,
|
||||
@@ -101,8 +127,15 @@ App({
|
||||
isLoggedIn: () => auth.isLoggedIn(),
|
||||
onLogout: () => this.handleIdleLogout()
|
||||
})
|
||||
adminIdleSession.init({
|
||||
timeoutMs: ADMIN_IDLE_TIMEOUT_MS,
|
||||
warningMs: ADMIN_IDLE_WARNING_MS,
|
||||
isLoggedIn: () => adminAuth.isLoggedIn(),
|
||||
onLogout: () => this.handleAdminIdleLogout()
|
||||
})
|
||||
this.bootstrapSession()
|
||||
if (!auth.isLoggedIn()) {
|
||||
this.bootstrapAdminSession()
|
||||
if (!auth.isLoggedIn() && !adminAuth.isLoggedIn()) {
|
||||
this.tryWechatMiniAutoLogin()
|
||||
}
|
||||
},
|
||||
@@ -113,6 +146,7 @@ App({
|
||||
*/
|
||||
onShow() {
|
||||
idleSession.onAppShow()
|
||||
adminIdleSession.onAppShow()
|
||||
this._appShownOnce = true
|
||||
this.checkPopupMessages()
|
||||
},
|
||||
@@ -123,6 +157,7 @@ App({
|
||||
*/
|
||||
onHide() {
|
||||
idleSession.onAppHide()
|
||||
adminIdleSession.onAppHide()
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -134,6 +169,7 @@ App({
|
||||
const isLoggedIn = auth.isLoggedIn()
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = isLoggedIn
|
||||
this.globalData.activeSessionMode = isLoggedIn ? 'customer' : this.globalData.activeSessionMode
|
||||
this.refreshShopTheme(userInfo)
|
||||
|
||||
if (userInfo && userInfo.shopId) {
|
||||
@@ -149,6 +185,109 @@ App({
|
||||
this.clearCartBadge()
|
||||
},
|
||||
|
||||
bootstrapAdminSession() {
|
||||
resetAdminLoading()
|
||||
|
||||
if (!adminAuth.isLoggedIn()) {
|
||||
this.globalData.currentShop = null
|
||||
this.setAdminPermissionState([])
|
||||
return
|
||||
}
|
||||
|
||||
const userInfo = adminAuth.getStoredUserInfo()
|
||||
const currentShop = adminAuth.getCurrentShop() || buildAdminCurrentShop(userInfo)
|
||||
const permissions = adminAuth.getStoredPermissions()
|
||||
|
||||
if (currentShop) {
|
||||
adminAuth.setCurrentShop(currentShop)
|
||||
}
|
||||
|
||||
this.globalData.activeSessionMode = 'admin'
|
||||
this.globalData.isLoggedIn = true
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.currentShop = currentShop
|
||||
this.setAdminPermissionState(permissions)
|
||||
|
||||
if (!permissions.length) {
|
||||
this.loadCurrentPermissions().catch(() => undefined)
|
||||
}
|
||||
},
|
||||
|
||||
setAdminPermissionState(permissions) {
|
||||
const list = Array.isArray(permissions) ? permissions : []
|
||||
this.globalData.permissions = list
|
||||
this.globalData.permissionCodes = adminPermission.getPermissionCodes(list)
|
||||
},
|
||||
|
||||
async setAdminSession(payload) {
|
||||
adminAuth.setSession(payload)
|
||||
this.bootstrapAdminSession()
|
||||
await this.loadCurrentPermissions().catch(() => [])
|
||||
},
|
||||
|
||||
async loadCurrentPermissions() {
|
||||
if (!adminAuth.isLoggedIn()) {
|
||||
adminAuth.clearPermissions()
|
||||
this.setAdminPermissionState([])
|
||||
return []
|
||||
}
|
||||
|
||||
const permissions = await adminPermissionApi.getCurrentUserPermissions()
|
||||
adminAuth.setPermissions(permissions)
|
||||
this.setAdminPermissionState(permissions)
|
||||
return permissions
|
||||
},
|
||||
|
||||
refreshAdminUserInfo(userInfo) {
|
||||
adminAuth.updateStoredUserInfo(userInfo)
|
||||
|
||||
if (userInfo && userInfo.shopId) {
|
||||
adminAuth.setCurrentShop({
|
||||
id: userInfo.shopId,
|
||||
shopName: userInfo.shopName || ''
|
||||
})
|
||||
}
|
||||
|
||||
this.bootstrapAdminSession()
|
||||
},
|
||||
|
||||
clearAdminSession() {
|
||||
adminAuth.clearSession()
|
||||
if (this.globalData.activeSessionMode === 'admin' || !auth.isLoggedIn()) {
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.userInfo = null
|
||||
this.globalData.activeSessionMode = ''
|
||||
}
|
||||
this.globalData.currentShop = null
|
||||
this.globalData.pageDirtyFlags = {}
|
||||
this.setAdminPermissionState([])
|
||||
},
|
||||
|
||||
async handleAdminIdleLogout() {
|
||||
if (!adminAuth.isLoggedIn()) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
await adminAuthApi.logout()
|
||||
} catch (error) {}
|
||||
this.clearAdminSession()
|
||||
wx.showToast({
|
||||
title: '长时间未操作,已退出后台登录',
|
||||
icon: 'none'
|
||||
})
|
||||
setTimeout(() => {
|
||||
wx.reLaunch({
|
||||
url: '/admin/pages/login/index'
|
||||
})
|
||||
}, 260)
|
||||
},
|
||||
|
||||
async switchAdminShop(shopId) {
|
||||
const payload = await adminAuthApi.switchShop(shopId)
|
||||
await this.setAdminSession(payload)
|
||||
return payload
|
||||
},
|
||||
|
||||
async tryWechatMiniAutoLogin() {
|
||||
if (auth.isLoggedIn()) {
|
||||
return true
|
||||
@@ -287,6 +426,7 @@ App({
|
||||
auth.setSession(payload)
|
||||
this.globalData.userInfo = auth.getStoredUserInfo()
|
||||
this.globalData.isLoggedIn = auth.isLoggedIn()
|
||||
this.globalData.activeSessionMode = 'customer'
|
||||
this.refreshShopTheme(this.globalData.userInfo)
|
||||
|
||||
if (this.globalData.userInfo && this.globalData.userInfo.shopId) {
|
||||
@@ -305,6 +445,7 @@ App({
|
||||
auth.updateStoredUserInfo(userInfo)
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = auth.isLoggedIn()
|
||||
this.globalData.activeSessionMode = auth.isLoggedIn() ? 'customer' : ''
|
||||
this.refreshShopTheme(userInfo)
|
||||
|
||||
if (userInfo && userInfo.shopId) {
|
||||
@@ -318,8 +459,11 @@ App({
|
||||
*/
|
||||
clearUserSession() {
|
||||
auth.clearSession()
|
||||
this.globalData.userInfo = null
|
||||
this.globalData.isLoggedIn = false
|
||||
if (this.globalData.activeSessionMode === 'customer' || !adminAuth.isLoggedIn()) {
|
||||
this.globalData.userInfo = null
|
||||
this.globalData.isLoggedIn = false
|
||||
this.globalData.activeSessionMode = ''
|
||||
}
|
||||
this.setShopTheme(null)
|
||||
this.clearCartBadge()
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user