groupBuy.js 8.3 KB
  // pages/groupBuy/groupBuy.js
  const app = getApp()
  const time=require('../../../utils/util.js');
  Page({

    /**
     * 页面的初始数据
     */
    data: {
      current: 0,
      chargenum: "",
      group_page_number: 1,
      record_page_number: 1,
      group_has_more: true,
      record_has_more: true,
      groupList: [],
      recordList: [],
      pay_info: [],
      is_agree: false
    },

    //tab切换
    tabCharge(e) {
      var index = e.currentTarget.dataset.current
      var current = this.data.current
      if (index == current) {
        return false
      } else {
        this.setData({
          current: index
        })
      }
    },

    //同意协议按钮
    agree() {
      this.setData({
        is_agree: !this.data.is_agree
      })
    },

    //服务协议
    serviceProtocol() {
      wx.navigateTo({
        url: '../protocol/protocol',
      })
    },

    //查看详情
    listDetail(e) {
      var id = e.currentTarget.dataset.id
      wx.setStorageSync('id', id)
      wx.navigateTo({
        url: '../groupBuyDetail/groupBuyDetail?id=' + id,
      })
    },

    //畅玩卡介绍
    cardInfo() {
      wx.navigateTo({
        url: '/pages/my/cardIntroduction/cardIntroduction',
      })
    },


    // 长按复制
    copy: function(e) {
      var that = this;
      wx.setClipboardData({
        data: that.data.chargenum,
        success: function(res) {
          wx.showToast({
            title: '复制成功',
          });
        }
      });
    },

    //参与的组团
    takeGroup() {
      if(!this.data.group_has_more) return;
      this.setData({group_has_more: false});
      let url = '/portal/Group/group_list'
      let params = {
        page: this.data.group_page_number
      }
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, params, header).then((res) => {
        console.log(res)
        if(res.this_page < res.total_page) {
            this.setData({group_has_more: true});
        }
        // console.log('list', list);
        this.setData({groupList: this.data.groupList.concat(res.list)});
      })
    },

    //成交记录
    record() {
      if(!this.data.record_has_more) return;
      this.setData({record_has_more: false});
      let url = '/portal/Group/group_complete'
      let params = {
        page: this.data.record_page_number
      }
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, params, header).then((res) => {
        console.log(res)
        if(res.this_page < res.total_page) {
            this.setData({record_has_more: true});
        }
          var recordList= res.list
          for(var i=0;i<recordList.length;i++){
              var chargeTime = recordList[i].create_time
              chargeTime = time.formatTimeTwo(chargeTime, 'Y-M-D')
              console.log(chargeTime)
              recordList[i].create_time = chargeTime
          }
        this.setData({recordList: this.data.recordList.concat(recordList)});
      })
    },

    //团购配置人数价格
    PayDetail() {
      let url = '/portal/Group/group_option'
      let params = {}
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, params, header).then((res) => {
        // console.log(res)
        this.setData({
          pay_info: res.info
        })
      })
    },
    //团购规则
    getRule() {
      let url = '/wxapp/public/arc_group'
      let params = {}
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, {}, {}).then((res) => {
        // console.log('团购规则', res)
        this.setData({
          rule: res
        })
      })
    },

    //发起团购
    goPay(e) {
      // var id=e.currentTarget.dataset.id
      let url = '/portal/Group/group_create'
      let params = {}
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, params, header).then((res) => {
        // wx.setStorageSync('group_id', res.group_id)
        this.setData({
          group_info: res.pay,
          group_id: res.group_id
        })
        this.wxPay(res.pay)
      })
    },

    //发起团购支付
    wxPay(data) {
      const self = this;
      if (self.data.is_agree) {
        wx.requestPayment({
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': function(res) {
            console.log(res)
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              success: function() {
                setTimeout(function() {
                  const params = {
                    rule: self.data.rule,
                    pay_info: self.data.pay_info,
                  }
                  wx.navigateTo({
                    url: '/pages/my/shareGroupBuy/shareGroupBuy?params=' + JSON.stringify(params) + '&group_id=' + self.data.group_id,
                  })
                }, 500)
              }
            });
          },
          'fail': function(res) {}
        })
      } else {
        wx.showToast({
          title: '请同意服务协议',
          icon: 'none'
        })
      }


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      this.takeGroup()
      this.record()
      this.PayDetail()
    },

      /**
       * 页面上拉触底事件的处理函数
       */
    onReachBottom: function () {
          const self = this;
          if(+self.data.current === 0) {
              if(self.data.group_has_more) {
                  self.data.group_page_number ++;
                  self.takeGroup();
              }else {
                  wx.showToast({
                      title: '没有更多数据了~',
                      icon: 'none'
                  });
              }
              console.log('上拉加载');
          }else if(+self.data.current === 1) {
              if(self.data.record_has_more) {
                  self.data.record_page_number ++;
                  self.record();
              }else {
                  wx.showToast({
                      title: '没有更多数据了~',
                      icon: 'none'
                  });
              }
              console.log('上拉加载');
          }

      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
      let that = this;
      // 设置菜单中的转发按钮触发转发事件时的转发内容
      var shareObj = {
        title: '科学队长', // 默认是小程序的名称(可以写slogan等)
        path: '/pages/eyeTest/articleDetail/articleDetail?doctorid=' + app.globalData.doctorid + '&id=' + this.data.id, // 默认是当前页面,必须是以‘/’开头的完整路径
        imageUrl: '', //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
        success: function(res) {
          if (res.errMsg == 'shareAppMessage:ok') {}
        },
        fail: function() {
          // 转发失败之后的回调
          if (res.errMsg == 'shareAppMessage:fail cancel') {
            // 用户取消转发
          } else if (res.errMsg == 'shareAppMessage:fail') {
            // 转发失败,其中 detail message 为详细失败信息
          }
        },
        complete: function() {
          // 转发结束之后的回调(转发成不成功都会执行)
        }
      }  

      // 来自页面内的按钮的转发
      // if (option.from == 'button') {
      //   // 此处可以修改 shareObj 中的内容
      //   // shareObj.imageUrl = '/pages/shareimg/share.png'
      // }   // 返回shareObj

      return shareObj;
    }
  })