var util = require('../../../utils/util.js'); var api = require('../../../config/api.js'); var app = getApp(); Page({ /** * 页面的初始数据 */ data: { options: '', couponList: [], scrollTop: 0, typeList: [{ name: "全部", type: 0 }, { name: "立减券", type: 1 }, { name: "折扣券", type: 2 }, ], fraId: '', recCode: '', addressInfo: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (this.data.options == '') { this.setData({ options: options }); } console.info(options); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { if (app.globalData.hasLogin == false) { wx.navigateTo({ url: "/pages/auth/login/login" }); return; } setTimeout(() => { this.getLocation(); }, 500); }, getCouponList() { let that = this; let options = that.data.options; that.setData({ options: '' }) let scene = decodeURIComponent(options.scene); var fraId, recCode; //两种格式 一种加盟商推荐fraId: 一种服务者推荐:recCode: if (scene && scene.indexOf("fraId:") >= 0){ fraId = scene.split(":")[1]; wx.setStorageSync('fraId',fraId); } if (scene && scene.indexOf("recCode:") >= 0) recCode = scene.split(":")[1]; that.setData({ fraId: fraId, recCode: recCode }); util.request(api.CouponReceiveList, { city: that.data.addressInfo.city }).then(function (res) { if (res.errno === 0) { that.setData({ couponList: res.data, }); console.info(that.data.couponList) } }); }, goReceive(e) { //点击领取按钮 let that = this; let id = e.currentTarget.dataset.id; let coupon = null; let coupons = this.data.couponList; coupons.forEach(item => { if (item.id == id) coupon = item; }) let goodsvalue = coupon.goodsValue; util.request(api.CouponReceive, { couponId: id, fraId: that.data.fraId, recCode: that.data.recCode }, "POST").then(res => { if (res.errno === 0) { that.goBuy(goodsvalue,id); } else { util.showErrorToast(res.errmsg); } }).catch(res => { util.showErrorToast(res.errmsg) }) }, goBuy(goodsvalue,couponId) { // if (goodsvalue.length == 1) { // wx.navigateTo({ // url: '/pages/goods/goods?id=' + goodsvalue[0], // }) // } else if (goodsvalue.length >= 1) { wx.navigateTo({ url: '/pages/ucenter/couponGoods/couponGoods?id=' + couponId }) } else { wx.switchTab({ url: '/pages/tabBar/catalog/catalog', }) } }, getLocation() { let that = this //获取地理位置 缓存中是否有地址 /** * 分2重判断 * 1:缓存中是否有地址 * 2:未登录 取当前地理位置判断 */ var addressInfo = wx.getStorageSync("addressInfo"); console.info(addressInfo); if (addressInfo) { that.setData({ addressInfo: addressInfo }) that.getCouponList(); } else { map.getCity().then(res => { that.setData({ addressInfo: res }) that.getCouponList(); }).catch(res => { //reject内容 1:未授权 2:不在服务范围 3:异常解析 wx.showModal({ title: '提醒', showCancel: false, content: '您未授权或非业务城市,无法获取优惠券信息' }) }); } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })