giftpackbuy.js 10.1 KB
var i = require("../../../utils/util.js");
var e = getApp(),
  a = e.globalData.setting,
  os = a,
  t = e.request,
  d = e.globalData;
Page({
  data: {
    url: a.url, //接口网址
    iurl: a.imghost, //图片前缀网址
    getStorageID: '',
    getUserID: '',
    wareCard: [],
    // page: 0,
    // pageSize: 10,
    isEmpty: false,
    loadingType: 0, //定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
    contentText: {
      contentdown: '加载更多',
      contentrefresh: '加载中...',
      contentnomore: '已加载全部'
    },
    ismore: 0, //数据是否全部加载完成
    is_read: 0, //是否查询过我的礼包接口
    curpage: 1, //当前分页数
    pageSize: 10, //页大小
    total: 0, //总数量
    ad_data: null,
    first_leader: e.globalData.first_leader || 0, // 推荐人ID
  },
  onLoad: function (options) {
    var th = this;
    var my_confirm = th.selectComponent("#my_confirm"); //组件的id
    my_confirm.open_cancel(0);
    th.setData({
      getStorageID: a.stoid,
      getUserID: d.user_id
    });
    //-- 获取分享人的ID --
    var first_leader = options.first_leader || getApp().globalData.first_leader;
    if (first_leader) {
      this.setData({
        first_leader,
      })
      //--  user_id代过来免登录 --
      getApp().globalData.first_leader = first_leader;
      //调用接口判断是不是会员
      getApp().request.promiseGet("/api/weshop/shoppingGuide/get/" + os.stoid + "/" + first_leader, {}).then(res => {
        if (res.data.code == 0) {
          getApp().globalData.guide_id = res.data.data.id;
        }
      })
    }

    getApp().request.promiseGet("/api/weshop/ad/page?pid=901&store_id=" + os.stoid, {
      data: {
        enabled: 1
      }
    }).then(res => {
      if (res.data.code == 0 && res.data.data && res.data.data.pageData && res.data.data.pageData.length > 0) {
        var a = res.data.data.pageData;
        var tt = {
          'ad_code': os.imghost + a[0].ad_code,
          'media_link': '',
          'ad_weapplink': a[0].ad_weapplink,
          'bgcolor': a[0].bgcolor,
        };
        th.setData({
          ad_data: tt
        });
      }
    })


  },
  onShow: function () {
    //--先判断会员状态--
    var user_info = getApp().globalData.userInfo;
    if (user_info == null || user_info.mobile == undefined || user_info.mobile == "" || user_info.mobile == null) {
      getApp().goto('/pages/togoin/togoin');
      return false;
    }
    this.getList();

  },

  //购买礼包
  GetBuyPrice: function (e) {
    var that = this.data;
    var th = this;
    var id = e.currentTarget.dataset.id; //活动id
    var my_confirm = th.selectComponent("#my_confirm"); //组件的id
    var order_sn = e.currentTarget.dataset.order_sn; //订单号
    var money = e.currentTarget.dataset.money; //支付金额
    my_confirm.open(
      "是否确定购买该礼包",
      "取消",
      "确定",
      function () {
        my_confirm.open_cancel(0);
      },
      function () {
        my_confirm.open_cancel(0);
        var json = {
          "actId": '', //活动Id
          "actType": '', //活动类型 1:新人礼、2:评价有礼、3:节日营销、4:生日营销
          "buyType": '2', //1=积分兑换 2=余额购买
          "giftBagId": id, //礼包Id
          "storeId": that.getStorageID, //商家Id
          "userId": that.getUserID, //用户ID
          "buyFrom": 2,
          "first_leader": that.first_leader,
        };

        //-- 分享导购要记录 --
        if (getApp().globalData.guide_id) {
          json.guide_id = getApp().globalData.guide_id;
        }

        var data = JSON.stringify(json);
        var url = that.url + "/api/weshop/marketing/buy/receive/gift/record/insert";
        getApp().request.json_post(url, json,
          function (res) {
            if (res.data.code == 0 && res.data.data) {
              var order_sn = res.data.data.orderSn;
              res = res.data.data.result;
              if (!res) return false;
              var url = "/pages/giftpack/payment/payment?money=" + money + "&order_sn=" + order_sn;
              wx.requestPayment({
                timeStamp: String(res.timeStamp),
                nonceStr: res.nonceStr,
                package: res.packageValue,
                signType: res.signType,
                paySign: res.paySign,
                success: function (res) {
                  getApp().goto(url);
                },
                fail: function (res) {
                  getApp().my_warnning("取消支付", 0, th);
                }
              });
            } else {
              getApp().my_warnning(res.data.msg, 0, th);
            }
          },
          function (res) {

          }

        )


      }

    )


  },
  // 积分兑换
  GetBuyIntegral: function (e) {
    var that = this.data;
    var th = this;
    var id = e.currentTarget.dataset.id; //活动id
    var my_confirm = th.selectComponent("#my_confirm"); //组件的id

    my_confirm.open(
      "是否确定兑换该礼包",
      "取消",
      "确定",
      function () {
        my_confirm.open_cancel(0);
      },
      function () {
        my_confirm.open_cancel(0);
        var json = {
          "actId": '', //活动Id
          "actType": '', //活动类型 1:新人礼、2:评价有礼、3:节日营销、4:生日营销
          "buyType": '1', //1=积分兑换 2=余额购买  3=免费领取
          "giftBagId": id, //礼包Id
          "storeId": that.getStorageID, //商家Id
          "userId": that.getUserID, //用户ID
          "buyFrom": 2,
          "first_leader": that.first_leader
        };

        //-- 分享导购要记录 --
        if (getApp().globalData.guide_id) {
          json.guide_id = getApp().globalData.guide_id;
        }
        console.log(json,'json');
        var data = JSON.stringify(json);
        var url = that.url + "/api/weshop/marketing/buy/receive/gift/record/insert";
        getApp().request.json_post(url, json,
          function (res) {
            if (res.data.code == 0) {
              getApp().my_warnning("兑换成功!", 1, th);
            } else {
              getApp().my_warnning(res.data.msg, 0, th);
            }
          },
          function (res) {})

      })

  },
  //免费领取
  GetFree: function (e) {
    var that = this.data;
    var th = this;
    var id = e.currentTarget.dataset.id; //活动id
    var my_confirm = th.selectComponent("#my_confirm"); //组件的id
    my_confirm.open(
      "是否确定领取该礼包",
      "取消",
      "确定",
      function () {
        my_confirm.open_cancel(0);
      },
      function () {
        my_confirm.open_cancel(0);
        var json = {
          "actId": '', //活动Id
          "actType": '', //活动类型 1:新人礼、2:评价有礼、3:节日营销、4:生日营销
          "buyType": '3', //1=积分兑换 2=余额购买  3=免费领取
          "giftBagId": id, //礼包Id
          "storeId": that.getStorageID, //商家Id
          "userId": that.getUserID, //用户ID
          "buyFrom": 2,
          "first_leader": that.first_leader
        };
        //-- 分享导购要记录 --
        if (getApp().globalData.guide_id) {
          json.guide_id = getApp().globalData.guide_id;
        }
        var data = JSON.stringify(json);
        var url = that.url + "/api/weshop/marketing/buy/receive/gift/record/insert";
        getApp().request.json_post(url, json,
          function (res) {
            if (res.data.code == 0) {
              getApp().my_warnning("领取成功!", 1, th);
            } else {
              getApp().my_warnning(res.data.msg, 0, th);
            }
          },
          function (res) {})
      })
  },

  getList: function (e) {
    var th = this;
    getApp().request.get('/api/weshop/marketing/giftbag/page', {
      isShowLoading: true,
      data: {
        storeId: th.data.getStorageID, //商家ID
        userId: th.data.getUserID, //用户ID
        page: th.data.curpage,
        pageSize: th.data.pageSize
      },
      success: function (res) {
        if (res.data.code == 0) {
          th.data.curpage++;
          var arr1 = th.data.wareCard;
          var arr2 = res.data.data.pageData;
          var arr3 = [...arr1, ...arr2];
          var ismore = 0;
          if (arr3.length == res.data.data.total) ismore = 1
          th.setData({
            wareCard: arr3,
            total: res.data.data.total,
            ismore: ismore,
            is_read: 1
          }), wx.stopPullDownRefresh(); //停止下拉刷新
        } else {
          th.setData({
            is_read: 1,
            ismore: 1
          })
        }
      }
    })

  },
  //销毁界面跳转
  redirectTo: function (e) {
    var th = this;
    var url = e.currentTarget.dataset.url;
    //销毁跳转
    wx.redirectTo({
      url: url
    });
  },
  //不销毁界面跳转
  navigateTo: function (e) {
    var th = this;
    var url = e.currentTarget.dataset.url;
    getApp().goto(url);

  },
  //下拉事件
  onReachBottom: function () {
    var th = this;
    if (th.data.total <= th.data.pageSize) return;
    if (th.data.ismore) return;

    th.getList();
  },
  //图片失败,默认图片
  bind_bnerr2: function (e) {
    var _errImg = e.target.dataset.errorimg;
    var _errObj = {};
    _errObj[_errImg] = "/miniapp/images/default_g_img.gif";
    this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ; 
  },
  goto: function (e) {
    var th = this;
    var url = e.currentTarget.dataset.url;
    getApp().goto(url);
  },

  onShareAppMessage: function (e) {
    var curPage = this;
    var pagePath = curPage.route; //当前页面url
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    pagePath += "?first_leader="+this.data.getUserID;
    return {
      title: "专享礼包",
      path: pagePath,
    }
  },

  // 分享朋友圈
  onShareTimeline() {
    var curPage = this;
    var pagePath = curPage.route; //当前页面url
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    pagePath += "?first_leader="+this.data.getUserID;
    return {
      title: '专享礼包',
      imageUrl: this.data.ad_data.ad_code,
      path: pagePath,
    }
  },

})