功能:收口小程序前后台安全与防重复提交
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'
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user