import { axios } from "../../js/libs"; const app = getApp(); Page({ data: { showModal: false, shareTitle: "", mall: null, userCouponNum: 0, mallCode: null, couponCode: null, refresherTriggered: false, }, async onLoad({ q }) { if (q) { const { mallCode, couponCode } = decodeURIComponent(q) .split("?") .pop() .replace("q=", "") .split("&") .map((kv) => kv.split("=")) .reduce( (acc, nxt) => Object.assign(acc, { [nxt[0]]: nxt[1], }), {} ); this.setData({ mallCode, couponCode, }); } const db = wx.cloud.database(); const { data: { value: shareTitle }, } = await db.collection("config").doc("shareTitle").get(); this.setData({ shareTitle, }); }, async refreshList() { const mall = this.data.mall; const { coupons } = getApp(); if (mall) { const codes = await coupons.getMallCodes(mall.code); mall.coupons = codes; this.setData({ mall, }); } this.setData({ refresherTriggered: false, }); }, async onShow() { const { malls, coupons } = getApp(); const mall = this.data.mallCode ? await malls.getMallByCode(this.data.mallCode) : await malls.getCurrentMall(); const mallCode = this.data.mallCode; if (this.data.mallCode) { this.setData({ mallCode: null, }); } if (!mall.coupons) { const codes = await coupons.getMallCodes(mall.code, mall.name); mall.coupons = codes; } this.setData({ mall, }); const userCodes = await coupons.getUserCodes(-1); this.setData({ userCouponNum: userCodes.length, }); if (this.data.couponCode) { this.doShowQrcodeModal({ mallCode, couponCode: this.data.couponCode, }); this.setData({ couponCode: null, }); } }, async doShowQrcodeModal({ mallCode, couponCode }) { const { getOpenid } = getApp(); wx.showLoading(); try { const openid = await getOpenid(); await axios.get("/Api/Coupon/GetCoupon", { couponCode, mallCode, userCode: openid, }); this.setData({ couponName: "直播券", showModal: true, }); } catch (error) { wx.showToast({ icon: "none", title: JSON.stringify(error), }); } finally { wx.hideLoading(); } }, async doShowModal({ detail: { code } }) { const { getOpenid, coupons } = getApp(); wx.showLoading(); try { const openid = await getOpenid(); const coupon = coupons.get(code); await axios.get("/Api/Coupon/GetCoupon", { couponCode: coupon.code, mallCode: coupon.mallCode, userCode: openid, }); coupons.set({ ...coupon, received: true, }); this.setData({ mall: { ...this.data.mall, coupons: [...this.data.mall.coupons], }, couponName: "优惠券", showModal: true, }); } catch (error) { wx.showToast({ icon: "none", title: JSON.stringify(error), }); } finally { wx.hideLoading(); } }, hideModal() { this.setData({ showModal: false, }); }, onShareAppMessage() { return { title: this.data.shareTitle, path: "pages/index/index", imageUrl: "./shareImg.png", }; }, });