369 lines
18 KiB
JavaScript
369 lines
18 KiB
JavaScript
/**
|
|
* Purpose: dashboardApi supports index.js by holding or executing the dashboard 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 dashboardApi = require('../../api/dashboard')
|
|
/**
|
|
* Purpose: orderApi supports index.js by holding or executing the order 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 orderApi = require('../../api/order')
|
|
/**
|
|
* Purpose: navigation supports index.js by holding or executing the navigation 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 navigation = require('../../utils/navigation')
|
|
/**
|
|
* Purpose: permission supports index.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')
|
|
/**
|
|
* Purpose: cache supports index.js by holding or executing the cache 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 cache = require('../../utils/cache')
|
|
const { CACHE_TTL } = require('../../utils/config')
|
|
const { consumeDirty } = require('../../utils/page-state')
|
|
|
|
Page({
|
|
/**
|
|
* Purpose: data supports index.js by holding or executing the data page or API role.
|
|
* Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
data: {
|
|
/**
|
|
* Purpose: userInfo supports index.js by holding or executing the user info page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
userInfo: null,
|
|
/**
|
|
* Purpose: currentShop supports index.js by holding or executing the current shop page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
currentShop: null,
|
|
/**
|
|
* Purpose: permissionCount supports index.js by holding or executing the permission count page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
permissionCount: 0,
|
|
/**
|
|
* Purpose: shortcutModules supports index.js by holding or executing the shortcut modules page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
shortcutModules: [],
|
|
/**
|
|
* Purpose: stats supports index.js by holding or executing the stats page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
stats: null,
|
|
/**
|
|
* Purpose: pendingOrders supports index.js by holding or executing the pending orders page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
pendingOrders: 0,
|
|
/**
|
|
* Purpose: shoplessAdmin supports index.js by holding or executing the shopless admin page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
shoplessAdmin: false,
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false,
|
|
/**
|
|
* Purpose: initialized supports index.js by holding or executing the initialized page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
initialized: false
|
|
},
|
|
|
|
/**
|
|
* Purpose: onShow supports index.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() {
|
|
if (!navigation.ensureLogin('进入工作台')) {
|
|
return
|
|
}
|
|
|
|
this.syncGlobalState()
|
|
const dirty = consumeDirty('admin-workbench')
|
|
if (!this.data.initialized || dirty) {
|
|
this.loadDashboard({
|
|
/**
|
|
* Purpose: force supports index.js by holding or executing the force page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
force: dirty
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: syncGlobalState supports index.js by holding or executing the sync global 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.
|
|
*/
|
|
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,
|
|
/**
|
|
* Purpose: permissionCount supports index.js by holding or executing the permission count page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
permissionCount: permissionCodes.length,
|
|
/**
|
|
* Purpose: shortcutModules supports index.js by holding or executing the shortcut modules page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
shortcutModules: modules.filter(item => item.allowed).slice(0, 4),
|
|
/**
|
|
* Purpose: shoplessAdmin supports index.js by holding or executing the shopless admin page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
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({
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: true
|
|
})
|
|
|
|
if (this.data.shoplessAdmin) {
|
|
if (requestId === this._loadRequestId) {
|
|
this.setData({
|
|
/**
|
|
* Purpose: stats supports index.js by holding or executing the stats page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
stats: null,
|
|
/**
|
|
* Purpose: pendingOrders supports index.js by holding or executing the pending orders page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
pendingOrders: 0,
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false,
|
|
/**
|
|
* Purpose: initialized supports index.js by holding or executing the initialized page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
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,
|
|
/**
|
|
* Purpose: pendingOrders supports index.js by holding or executing the pending orders page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
pendingOrders: Number(stats && stats.todayOrders) || 0,
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false,
|
|
/**
|
|
* Purpose: initialized supports index.js by holding or executing the initialized page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
initialized: true
|
|
})
|
|
return
|
|
} catch (error) {}
|
|
}
|
|
|
|
if (permission.hasPermission(permissionCodes, 'order:view')) {
|
|
try {
|
|
if (force) {
|
|
cache.removeCache(pendingCacheKey)
|
|
}
|
|
const result = force
|
|
? await orderApi.listOrders({
|
|
/**
|
|
* Purpose: page supports index.js by holding or executing the page page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
page: 1,
|
|
/**
|
|
* Purpose: size supports index.js by holding or executing the size page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
size: 20,
|
|
/**
|
|
* Purpose: status supports index.js by holding or executing the status page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
status: 1
|
|
})
|
|
: await cache.remember(pendingCacheKey, CACHE_TTL.workbench, () => orderApi.listOrders({
|
|
/**
|
|
* Purpose: page supports index.js by holding or executing the page page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
page: 1,
|
|
/**
|
|
* Purpose: size supports index.js by holding or executing the size page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
size: 20,
|
|
/**
|
|
* Purpose: status supports index.js by holding or executing the status page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
status: 1
|
|
}))
|
|
if (requestId !== this._loadRequestId) {
|
|
return
|
|
}
|
|
this.setData({
|
|
/**
|
|
* Purpose: stats supports index.js by holding or executing the stats page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
stats: null,
|
|
/**
|
|
* Purpose: pendingOrders supports index.js by holding or executing the pending orders page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
pendingOrders: Number(result && result.total) || 0,
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false,
|
|
/**
|
|
* Purpose: initialized supports index.js by holding or executing the initialized page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
initialized: true
|
|
})
|
|
} catch (error) {
|
|
if (requestId === this._loadRequestId) {
|
|
this.setData({
|
|
/**
|
|
* Purpose: stats supports index.js by holding or executing the stats page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
stats: null,
|
|
/**
|
|
* Purpose: pendingOrders supports index.js by holding or executing the pending orders page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
pendingOrders: 0,
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false,
|
|
/**
|
|
* Purpose: initialized supports index.js by holding or executing the initialized page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
initialized: true
|
|
})
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
if (requestId === this._loadRequestId) {
|
|
this.setData({
|
|
/**
|
|
* Purpose: stats supports index.js by holding or executing the stats page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
stats: null,
|
|
/**
|
|
* Purpose: pendingOrders supports index.js by holding or executing the pending orders page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
pendingOrders: 0,
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false,
|
|
/**
|
|
* Purpose: initialized supports index.js by holding or executing the initialized page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
initialized: true
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: handleOpenModule supports index.js by holding or executing the handle open module 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.
|
|
*/
|
|
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)
|
|
},
|
|
|
|
/**
|
|
* Purpose: handleOpenShops supports index.js by holding or executing the handle open shops 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.
|
|
*/
|
|
handleOpenShops() {
|
|
navigation.navigate('/pages/shops/index').catch(() => undefined)
|
|
},
|
|
|
|
/**
|
|
* Purpose: handleOpenOrders supports index.js by holding or executing the handle open orders 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.
|
|
*/
|
|
handleOpenOrders() {
|
|
navigation.switchTab('/pages/orders/index').catch(() => undefined)
|
|
}
|
|
})
|