/** * Purpose: productApi supports index.js by holding or executing the product 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 productApi = require('../../api/product') /** * 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: format supports index.js by holding or executing the format 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 format = require('../../utils/format') const { setTempRecord, clearTempRecord } = require('../../utils/storage') const { consumeDirty } = require('../../utils/page-state') /** * Purpose: PAGE_KEY supports index.js by holding or executing the page key 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 PAGE_KEY = 'admin-products' 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: keyword supports index.js by holding or executing the keyword 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. */ keyword: '', /** * Purpose: list supports index.js by holding or executing the list 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. */ list: [], /** * 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: canCreate supports index.js by holding or executing the can create 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. */ canCreate: 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 } const permissionCodes = getApp().globalData.permissionCodes || [] this.setData({ /** * Purpose: canCreate supports index.js by holding or executing the can create 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. */ canCreate: permissionCodes.includes('product:create') }) if (!this.data.initialized || consumeDirty(PAGE_KEY)) { this.loadProducts() } }, /** * Purpose: handleKeywordInput supports index.js by holding or executing the handle keyword input 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. */ handleKeywordInput(event) { this.setData({ /** * Purpose: keyword supports index.js by holding or executing the keyword 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. */ keyword: event.detail.value }) }, /** * Purpose: handleSearch supports index.js by holding or executing the handle search 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. */ handleSearch() { this.loadProducts() }, async loadProducts() { const requestId = (this._loadRequestId || 0) + 1 this._loadRequestId = requestId 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 }) try { const result = await productApi.listProducts({ /** * 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: keyword supports index.js by holding or executing the keyword 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. */ keyword: this.data.keyword || undefined }) if (requestId !== this._loadRequestId) { return } this.setData({ /** * Purpose: list supports index.js by holding or executing the list 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. */ list: (Array.isArray(result && result.list) ? result.list : []).map(item => ({ ...item, /** * Purpose: displayPrice supports index.js by holding or executing the display price 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. */ displayPrice: format.formatPrice(item.price), /** * Purpose: displayShopName supports index.js by holding or executing the display shop name 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. */ displayShopName: item.shopName || '当前店铺', /** * Purpose: displayDuration supports index.js by holding or executing the display duration 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. */ displayDuration: item.approximateDuration || '未设置时长', /** * Purpose: displayTagText supports index.js by holding or executing the display tag text 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. */ displayTagText: Array.isArray(item.tagList) ? item.tagList.join(' / ') : (item.tags || '未设置标签') })), /** * 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) { return } wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title 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. */ title: error.message || '作品列表加载失败', /** * Purpose: icon supports index.js by holding or executing the icon 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. */ icon: 'none' }) } finally { if (requestId === this._loadRequestId) { 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: false }) } } }, /** * Purpose: handleCreate supports index.js by holding or executing the handle create 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. */ handleCreate() { clearTempRecord('currentProduct') navigation.navigate('/pages/product-form/index', { /** * Purpose: mode supports index.js by holding or executing the mode 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. */ mode: 'create' }).catch(() => undefined) }, /** * Purpose: handleEdit supports index.js by holding or executing the handle edit 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. */ handleEdit(event) { const product = event.currentTarget.dataset.product setTempRecord('currentProduct', product) navigation.navigate('/pages/product-form/index', { /** * Purpose: mode supports index.js by holding or executing the mode 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. */ mode: 'edit', /** * Purpose: id supports index.js by holding or executing the id 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. */ id: product.id }).catch(() => undefined) }, /** * Purpose: handleDelete supports index.js by holding or executing the handle delete 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. */ handleDelete(event) { const product = event.currentTarget.dataset.product wx.showModal({ /** * Purpose: title supports index.js by holding or executing the title 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. */ title: '删除作品', /** * Purpose: content supports index.js by holding or executing the content 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. */ content: `确认删除作品“${product.title}”吗?`, /** * Purpose: success supports index.js by holding or executing the success 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. */ success: async result => { if (!result.confirm) { return } try { await productApi.deleteProduct(product.id) wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title 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. */ title: '作品已删除', /** * Purpose: icon supports index.js by holding or executing the icon 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. */ icon: 'none' }) this.loadProducts() } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title 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. */ title: error.message || '作品删除失败', /** * Purpose: icon supports index.js by holding or executing the icon 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. */ icon: 'none' }) } } }) } })