功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"usingComponents": {
|
||||
"navigation-bar": "/components/navigation-bar/navigation-bar"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<navigation-bar title="工作台" back="{{false}}" color="#f6efe7" background="#081518"></navigation-bar>
|
||||
|
||||
<view class="page-shell">
|
||||
<view class="page-stack">
|
||||
<view class="hero-panel">
|
||||
<text class="hero-kicker">{{userInfo.nickname || userInfo.account || '后台账号'}}</text>
|
||||
<text class="hero-title">{{currentShop ? currentShop.shopName : '无店铺登录工作台'}}</text>
|
||||
<text class="hero-desc">当前身份:{{userInfo ? userInfo.type === 4 ? '平台管理员' : userInfo.type === 2 ? '店长' : '美甲师' : '--'}}。已同步 {{permissionCount}} 项后台权限。</text>
|
||||
<view class="badge-row">
|
||||
<text class="badge">{{currentShop ? '当前店铺已锁定' : '当前无店铺上下文'}}</text>
|
||||
<text class="soft-badge">{{shoplessAdmin ? '先完成门店初始化' : '移动端工作流已就绪'}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{shoplessAdmin}}" class="surface-card">
|
||||
<text class="section-title">先创建第一家店铺</text>
|
||||
<text class="section-desc">当前是无店铺管理员登录状态。为避免误进入强店铺范围页面,首版仅建议先完成店铺初始化,再继续订单、商品和用户管理。</text>
|
||||
<view class="action-row">
|
||||
<button class="primary-btn" bindtap="handleOpenShops">进入店铺管理</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view wx:else class="surface-card">
|
||||
<text class="section-title">今日概览</text>
|
||||
<text class="section-desc">把高频动作和最关键的经营数字集中到一屏,方便在手机上快速判断接下来该处理什么。</text>
|
||||
<view class="stats-grid">
|
||||
<view class="stat-card">
|
||||
<text class="stat-label">待处理订单</text>
|
||||
<text class="stat-value">{{pendingOrders}}</text>
|
||||
<text class="stat-note">优先处理预约确认与履约</text>
|
||||
</view>
|
||||
<view class="stat-card">
|
||||
<text class="stat-label">当前权限</text>
|
||||
<text class="stat-value">{{permissionCount}}</text>
|
||||
<text class="stat-note">前端仅做显隐,服务端仍会兜底校验</text>
|
||||
</view>
|
||||
<view class="stat-card" wx:if="{{stats}}">
|
||||
<text class="stat-label">用户总量</text>
|
||||
<text class="stat-value">{{stats.totalUsers || 0}}</text>
|
||||
<text class="stat-note">按当前角色与店铺范围计算</text>
|
||||
</view>
|
||||
<view class="stat-card" wx:if="{{stats}}">
|
||||
<text class="stat-label">作品总量</text>
|
||||
<text class="stat-value">{{stats.totalProducts || 0}}</text>
|
||||
<text class="stat-note">支持后续直接跳去作品管理</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="action-row">
|
||||
<button class="secondary-btn" bindtap="handleOpenOrders">查看订单</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="surface-card">
|
||||
<text class="section-title">快捷入口</text>
|
||||
<text class="section-desc">只展示当前账号真实可进入的模块,避免手机端一屏堆太多无权限入口。</text>
|
||||
<block wx:for="{{shortcutModules}}" wx:key="key">
|
||||
<view class="list-card" data-path="{{item.path}}" bindtap="handleOpenModule">
|
||||
<view class="list-head">
|
||||
<view>
|
||||
<text class="list-title">{{item.title}}</text>
|
||||
<text class="list-subtitle">{{item.description}}</text>
|
||||
</view>
|
||||
<text class="badge">进入</text>
|
||||
</view>
|
||||
</view>
|
||||
</block>
|
||||
<view wx:if="{{!shortcutModules.length}}" class="empty-state">
|
||||
<text class="empty-title">暂无可用快捷入口</text>
|
||||
<text class="empty-desc">当前账号没有可直接打开的后台模块,请先检查权限或切换店铺。</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,3 @@
|
||||
.page-shell {
|
||||
background: transparent;
|
||||
}
|
||||
Reference in New Issue
Block a user