const dashboardApi = require('../../api/dashboard') const orderApi = require('../../api/order') const navigation = require('../../utils/navigation') const permission = require('../../utils/permission') const cache = require('../../utils/cache') const { CACHE_TTL } = require('../../utils/config') const { consumeDirty } = require('../../utils/page-state') Page({ data: { userInfo: null, currentShop: null, permissionCount: 0, shortcutModules: [], stats: null, pendingOrders: 0, shoplessAdmin: false, loading: false, initialized: false }, onShow() { if (!navigation.ensureLogin('进入工作台')) { return } this.syncGlobalState() const dirty = consumeDirty('admin-workbench') if (!this.data.initialized || dirty) { this.loadDashboard({ force: dirty }) } }, syncGlobalState() { const app = getApp() const userInfo = app.globalData.userInfo const currentShop = app.globalData.currentShop const permissionCodes = app.globalData.permissionCodes || [] const modules = permission.getManageModules(userInfo, permissionCodes, currentShop) this.setData({ userInfo, currentShop, permissionCount: permissionCodes.length, shortcutModules: modules.filter(item => item.allowed).slice(0, 4), shoplessAdmin: permission.isPlatformAdmin(userInfo) && !currentShop }) }, async loadDashboard(options) { const requestId = (this._loadRequestId || 0) + 1 this._loadRequestId = requestId const force = Boolean(options && options.force) const app = getApp() const permissionCodes = app.globalData.permissionCodes || [] const currentShopId = this.data.currentShop && this.data.currentShop.id ? String(this.data.currentShop.id) : 'none' const statsCacheKey = `workbench:stats:${currentShopId}` const pendingCacheKey = `workbench:pending:${currentShopId}` this.setData({ loading: true }) if (this.data.shoplessAdmin) { if (requestId === this._loadRequestId) { this.setData({ stats: null, pendingOrders: 0, loading: false, initialized: true }) } return } if (permission.hasPermission(permissionCodes, 'dashboard:view')) { try { if (force) { cache.removeCache(statsCacheKey) } const stats = force ? await dashboardApi.getStats() : await cache.remember(statsCacheKey, CACHE_TTL.workbench, () => dashboardApi.getStats()) if (requestId !== this._loadRequestId) { return } this.setData({ stats, pendingOrders: Number(stats && stats.todayOrders) || 0, loading: false, initialized: true }) return } catch (error) {} } if (permission.hasPermission(permissionCodes, 'order:view')) { try { if (force) { cache.removeCache(pendingCacheKey) } const result = force ? await orderApi.listOrders({ page: 1, size: 20, status: 1 }) : await cache.remember(pendingCacheKey, CACHE_TTL.workbench, () => orderApi.listOrders({ page: 1, size: 20, status: 1 })) if (requestId !== this._loadRequestId) { return } this.setData({ stats: null, pendingOrders: Number(result && result.total) || 0, loading: false, initialized: true }) } catch (error) { if (requestId === this._loadRequestId) { this.setData({ stats: null, pendingOrders: 0, loading: false, initialized: true }) } } return } if (requestId === this._loadRequestId) { this.setData({ stats: null, pendingOrders: 0, loading: false, initialized: true }) } }, handleOpenModule(event) { const path = event.currentTarget.dataset.path if (!path) { return } if (path === '/pages/orders/index') { navigation.switchTab(path) return } navigation.navigate(path).catch(() => undefined) }, handleOpenShops() { navigation.navigate('/pages/shops/index').catch(() => undefined) }, handleOpenOrders() { navigation.switchTab('/pages/orders/index').catch(() => undefined) } })