/** * Purpose: couponApi supports index.js by holding or executing the coupon 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 couponApi = require('../../api/coupon') /** * 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') /** * 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') const { setTempRecord, clearTempRecord } = require('../../utils/storage') 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: 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: 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: 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('coupon:create') }) this.loadCoupons() }, async loadCoupons() { try { const result = await couponApi.listCoupons({ /** * 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 }) 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: displayTypeText supports index.js by holding or executing the display type 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. */ displayTypeText: format.getCouponTypeText(item.couponType), /** * Purpose: displayValueText supports index.js by holding or executing the display value 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. */ displayValueText: format.getCouponValueText(item), /** * 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 || '当前店铺' })) }) } 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' }) } }, /** * 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('currentCoupon') navigation.navigate('/pages/coupon-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 coupon = event.currentTarget.dataset.coupon setTempRecord('currentCoupon', coupon) navigation.navigate('/pages/coupon-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: coupon.id }).catch(() => undefined) }, /** * Purpose: handleDistribute supports index.js by holding or executing the handle distribute 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. */ handleDistribute(event) { const coupon = event.currentTarget.dataset.coupon setTempRecord('currentCoupon', coupon) navigation.navigate('/pages/coupon-distribute/index', { /** * 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: coupon.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 coupon = event.currentTarget.dataset.coupon 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: `确认删除优惠券“${coupon.couponName}”吗?`, /** * 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 couponApi.deleteCoupon(coupon.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.loadCoupons() } 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' }) } } }) } })