功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
const productApi = require('../../api/product')
|
||||
const navigation = require('../../utils/navigation')
|
||||
const format = require('../../utils/format')
|
||||
const { setTempRecord, clearTempRecord } = require('../../utils/storage')
|
||||
const { consumeDirty } = require('../../utils/page-state')
|
||||
|
||||
const PAGE_KEY = 'admin-products'
|
||||
|
||||
Page({
|
||||
data: {
|
||||
keyword: '',
|
||||
list: [],
|
||||
loading: false,
|
||||
canCreate: false,
|
||||
initialized: false
|
||||
},
|
||||
|
||||
onShow() {
|
||||
if (!navigation.ensureLogin('进入作品管理')) {
|
||||
return
|
||||
}
|
||||
|
||||
const permissionCodes = getApp().globalData.permissionCodes || []
|
||||
this.setData({
|
||||
canCreate: permissionCodes.includes('product:create')
|
||||
})
|
||||
|
||||
if (!this.data.initialized || consumeDirty(PAGE_KEY)) {
|
||||
this.loadProducts()
|
||||
}
|
||||
},
|
||||
|
||||
handleKeywordInput(event) {
|
||||
this.setData({
|
||||
keyword: event.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
handleSearch() {
|
||||
this.loadProducts()
|
||||
},
|
||||
|
||||
async loadProducts() {
|
||||
const requestId = (this._loadRequestId || 0) + 1
|
||||
this._loadRequestId = requestId
|
||||
this.setData({
|
||||
loading: true
|
||||
})
|
||||
|
||||
try {
|
||||
const result = await productApi.listProducts({
|
||||
page: 1,
|
||||
size: 20,
|
||||
keyword: this.data.keyword || undefined
|
||||
})
|
||||
if (requestId !== this._loadRequestId) {
|
||||
return
|
||||
}
|
||||
|
||||
this.setData({
|
||||
list: (Array.isArray(result && result.list) ? result.list : []).map(item => ({
|
||||
...item,
|
||||
displayPrice: format.formatPrice(item.price),
|
||||
displayShopName: item.shopName || '当前店铺',
|
||||
displayDuration: item.approximateDuration || '未设置时长',
|
||||
displayTagText: Array.isArray(item.tagList) ? item.tagList.join(' / ') : (item.tags || '未设置标签')
|
||||
})),
|
||||
initialized: true
|
||||
})
|
||||
} catch (error) {
|
||||
if (requestId !== this._loadRequestId) {
|
||||
return
|
||||
}
|
||||
wx.showToast({
|
||||
title: error.message || '作品列表加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
if (requestId === this._loadRequestId) {
|
||||
this.setData({
|
||||
loading: false
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
handleCreate() {
|
||||
clearTempRecord('currentProduct')
|
||||
navigation.navigate('/pages/product-form/index', {
|
||||
mode: 'create'
|
||||
}).catch(() => undefined)
|
||||
},
|
||||
|
||||
handleEdit(event) {
|
||||
const product = event.currentTarget.dataset.product
|
||||
setTempRecord('currentProduct', product)
|
||||
navigation.navigate('/pages/product-form/index', {
|
||||
mode: 'edit',
|
||||
id: product.id
|
||||
}).catch(() => undefined)
|
||||
},
|
||||
|
||||
handleDelete(event) {
|
||||
const product = event.currentTarget.dataset.product
|
||||
wx.showModal({
|
||||
title: '删除作品',
|
||||
content: `确认删除作品“${product.title}”吗?`,
|
||||
success: async result => {
|
||||
if (!result.confirm) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
await productApi.deleteProduct(product.id)
|
||||
wx.showToast({
|
||||
title: '作品已删除',
|
||||
icon: 'none'
|
||||
})
|
||||
this.loadProducts()
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '作品删除失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"usingComponents": {
|
||||
"navigation-bar": "/components/navigation-bar/navigation-bar"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<navigation-bar title="作品管理" back="{{true}}" color="#f6efe7" background="#081518"></navigation-bar>
|
||||
|
||||
<view class="page-shell">
|
||||
<view class="page-stack">
|
||||
<view class="surface-card">
|
||||
<text class="section-title">作品列表</text>
|
||||
<view class="search-row">
|
||||
<input class="field-input search-input" placeholder="按标题搜索作品" placeholder-class="placeholder" value="{{keyword}}" bindinput="handleKeywordInput" confirm-type="search" bindconfirm="handleSearch"></input>
|
||||
<button class="secondary-btn" bindtap="handleSearch">搜索</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<block wx:for="{{list}}" wx:key="id">
|
||||
<view class="list-card">
|
||||
<view class="list-head">
|
||||
<view>
|
||||
<text class="list-title">{{item.title}}</text>
|
||||
<text class="list-subtitle">{{item.displayShopName}} · {{item.displayDuration}}</text>
|
||||
</view>
|
||||
<text class="badge">¥{{item.displayPrice}}</text>
|
||||
</view>
|
||||
<view class="meta-grid">
|
||||
<view class="meta-item">
|
||||
<text class="meta-label">标签</text>
|
||||
<text class="meta-value">{{item.displayTagText}}</text>
|
||||
</view>
|
||||
<view class="meta-item">
|
||||
<text class="meta-label">点赞 / 收藏</text>
|
||||
<text class="meta-value">{{item.likeCount || 0}} / {{item.collectCount || 0}}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="list-actions">
|
||||
<view class="mini-btn primary" data-product="{{item}}" bindtap="handleEdit">编辑</view>
|
||||
<view class="mini-btn danger" data-product="{{item}}" bindtap="handleDelete">删除</view>
|
||||
</view>
|
||||
</view>
|
||||
</block>
|
||||
|
||||
<view wx:if="{{loading && !list.length}}" class="empty-state">
|
||||
<text class="empty-title">正在加载作品列表</text>
|
||||
</view>
|
||||
|
||||
<view wx:elif="{{!list.length}}" class="empty-state">
|
||||
<text class="empty-title">还没有作品</text>
|
||||
<text class="empty-desc">先上传图片、绑定标签和美甲师后,就可以把完整作品发布到门店里。</text>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{canCreate}}" class="fab" bindtap="handleCreate">+</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,3 @@
|
||||
.page-shell {
|
||||
background: transparent;
|
||||
}
|
||||
Reference in New Issue
Block a user