From 9fe0a3c9baa9c87a069075a3cda255651f34a5be Mon Sep 17 00:00:00 2001 From: jiannibang <271381693@qq.com> Date: Mon, 8 Aug 2022 10:59:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4getLocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/pages/index/index.js | 204 +++++++++++++------------------ 1 file changed, 88 insertions(+), 116 deletions(-) diff --git a/miniprogram/pages/index/index.js b/miniprogram/pages/index/index.js index 8d02bbd..ca7c4f8 100644 --- a/miniprogram/pages/index/index.js +++ b/miniprogram/pages/index/index.js @@ -1,12 +1,10 @@ -import { - axios -} from '../../js/libs' -const app = getApp() +import { axios } from "../../js/libs"; +const app = getApp(); Page({ data: { showModal: false, - shareTitle: '', + shareTitle: "", mall: null, userCouponNum: 0, mallCode: null, @@ -14,174 +12,148 @@ Page({ refresherTriggered: false, }, - async onLoad({ - q - }) { + 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] - }), {}) + 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 - }) + couponCode, + }); } - const db = wx.cloud.database() + const db = wx.cloud.database(); const { - data: { - value: shareTitle - } - } = await db.collection('config').doc('shareTitle').get() + data: { value: shareTitle }, + } = await db.collection("config").doc("shareTitle").get(); this.setData({ - shareTitle - }) - - }, - getLocation() { - return new Promise(resolve => { - wx.getLocation({ - type: 'wgs84', - success: resolve - }) - }) - + shareTitle, + }); }, async refreshList() { - const mall = this.data.mall - const { - coupons, - } = getApp() + const mall = this.data.mall; + const { coupons } = getApp(); if (mall) { - const codes = await coupons.getMallCodes(mall.code) - mall.coupons = codes + const codes = await coupons.getMallCodes(mall.code); + mall.coupons = codes; this.setData({ - mall - }) + mall, + }); } this.setData({ - refresherTriggered: false - }) + refresherTriggered: false, + }); }, async onShow() { - const { - malls, - coupons, - } = getApp() + const { malls, coupons } = getApp(); - const mall = this.data.mallCode ? await malls.getMallByCode(this.data.mallCode) : await malls.getCurrentMall() - const mallCode = this.data.mallCode + 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 - }) + mallCode: null, + }); } if (!mall.coupons) { - const codes = await coupons.getMallCodes(mall.code, mall.name) - mall.coupons = codes + const codes = await coupons.getMallCodes(mall.code, mall.name); + mall.coupons = codes; } - this.setData({ - mall - }) - const userCodes = await coupons.getUserCodes(-1) + mall, + }); + const userCodes = await coupons.getUserCodes(-1); this.setData({ - userCouponNum: userCodes.length - }) + userCouponNum: userCodes.length, + }); if (this.data.couponCode) { - this.doShowQrcodeModal({ mallCode, - couponCode: this.data.couponCode - }) + couponCode: this.data.couponCode, + }); this.setData({ - couponCode: null - }) + couponCode: null, + }); } }, - async doShowQrcodeModal({ - mallCode, - couponCode - }) { - const { - getOpenid, - } = getApp() - wx.showLoading() + async doShowQrcodeModal({ mallCode, couponCode }) { + const { getOpenid } = getApp(); + wx.showLoading(); try { - const openid = await getOpenid() - await axios.get('/Api/Coupon/GetCoupon', { + const openid = await getOpenid(); + await axios.get("/Api/Coupon/GetCoupon", { couponCode, mallCode, - "userCode": openid - }) + userCode: openid, + }); this.setData({ - couponName: '直播券', - showModal: true - }) + couponName: "直播券", + showModal: true, + }); } catch (error) { wx.showToast({ - icon: 'none', + icon: "none", title: JSON.stringify(error), - }) + }); } finally { - wx.hideLoading() + wx.hideLoading(); } - }, - async doShowModal({ - detail: { - code - } - }) { - const { - getOpenid, - coupons - } = getApp() - wx.showLoading() + 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 - }) + 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 - }) + received: true, + }); this.setData({ mall: { ...this.data.mall, - coupons: [...this.data.mall.coupons] + coupons: [...this.data.mall.coupons], }, - couponName: '优惠券', - showModal: true - }) + couponName: "优惠券", + showModal: true, + }); } catch (error) { wx.showToast({ - icon: 'none', + icon: "none", title: JSON.stringify(error), - }) + }); } finally { - wx.hideLoading() + wx.hideLoading(); } - }, hideModal() { this.setData({ - showModal: false - }) + showModal: false, + }); }, onShareAppMessage() { return { title: this.data.shareTitle, path: "pages/index/index", - imageUrl: "./shareImg.png" - } - } -}) \ No newline at end of file + imageUrl: "./shareImg.png", + }; + }, +});