116 lines
2.6 KiB
JavaScript
116 lines
2.6 KiB
JavaScript
const shopApi = require('../../api/shop')
|
|
const navigation = require('../../utils/navigation')
|
|
const { setTempRecord, clearTempRecord } = require('../../utils/storage')
|
|
const { consumeDirty } = require('../../utils/page-state')
|
|
|
|
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('shop:create')
|
|
})
|
|
|
|
if (!this.data.initialized || consumeDirty('admin-shops')) {
|
|
this.loadShops()
|
|
}
|
|
},
|
|
|
|
handleKeywordInput(event) {
|
|
this.setData({
|
|
keyword: event.detail.value
|
|
})
|
|
},
|
|
|
|
handleSearch() {
|
|
this.loadShops()
|
|
},
|
|
|
|
async loadShops() {
|
|
const requestId = (this._loadRequestId || 0) + 1
|
|
this._loadRequestId = requestId
|
|
this.setData({
|
|
loading: true
|
|
})
|
|
|
|
try {
|
|
const result = await shopApi.listShops({
|
|
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,
|
|
displayStatusText: Number(item.status) === 0 ? '已停用' : '启用中'
|
|
})),
|
|
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('currentShopRecord')
|
|
navigation.navigate('/pages/shop-form/index', {
|
|
mode: 'create'
|
|
}).catch(() => undefined)
|
|
},
|
|
|
|
handleEdit(event) {
|
|
const shop = event.currentTarget.dataset.shop
|
|
setTempRecord('currentShopRecord', shop)
|
|
navigation.navigate('/pages/shop-form/index', {
|
|
mode: 'edit',
|
|
id: shop.id
|
|
}).catch(() => undefined)
|
|
},
|
|
|
|
handleStatus(event) {
|
|
const shop = event.currentTarget.dataset.shop
|
|
const nextStatus = Number(shop.status) === 0 ? 1 : 0
|
|
shopApi.updateShopStatus(shop.id, nextStatus)
|
|
.then(() => {
|
|
wx.showToast({
|
|
title: '店铺状态已更新',
|
|
icon: 'none'
|
|
})
|
|
this.loadShops()
|
|
})
|
|
.catch(error => {
|
|
wx.showToast({
|
|
title: error.message || '状态更新失败',
|
|
icon: 'none'
|
|
})
|
|
})
|
|
}
|
|
})
|