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

  Page({

    /**
     * 页面的初始数据
     */
    data: {
      current: 0,
      chargenum: "",
      page: 0,
      groupList: [],
      recordList: [],
      pay_info: [],
      is_agree: false,
      countDownList: [],
      actEndTimeList: [],
      goodsList: [{
        actEndTime: ''
      }]

    },

    //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) {
      console.log(e)
      var index=e.currentTarget.dataset.index
      var recordList = this.data.recordList
      var chargenum = recordList[index].redeem_code
      var that = this;
      wx.setClipboardData({
        data: chargenum,
        success: function(res) {
          console.log(res)
          wx.showToast({
            title: '复制成功',
          });
        }
      });
    },


    //参与的组团
    takeGroup() {
      let url = '/portal/Group/group_list'
      let params = {
        page: this.data.page
      }
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, params, header).then((res) => {
        var groupList = res.list
        var goodsList = this.data.goodsList
        console.log(groupList.length)
        for (var i = 0; i < groupList.length; i++) {
          goodsList.push({
            actEndTime:''
          })
          goodsList[i].actEndTime = groupList[i].over_time
        }
        this.setData({
          groupList: res.list,
          goodsList: goodsList
        })
      })
    },

    //成交记录
    record() {
      let url = '/portal/Group/group_complete'
      let params = {
        page: this.data.page
      }
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, params, header).then((res) => {
        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')
          recordList[i].create_time = chargeTime
        }
        this.setData({
          recordList: 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'
        })
      }


    },

    timeFormat(param) { //小于10的格式化函数
      return param < 10 ? '0' + param : param;
    },
    countDown() { //倒计时函数
      // 获取当前时间,同时得到活动结束时间数组
      let newTime = new Date().getTime();
      let endTimeList = this.data.actEndTimeList;
      let countDownArr = [];

      // 对结束时间进行处理渲染到页面
      endTimeList.forEach(o => {
        let endTime = new Date(o).getTime();
        let obj = null;
        // 如果活动未结束,对时间进行处理
        if (endTime - newTime > 0) {
          let time = (endTime - newTime) / 1000;
          // 获取天、时、分、秒
          let day = parseInt(time / (60 * 60 * 24));
          let hou = parseInt(time % (60 * 60 * 24) / 3600);
          let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
          let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
          obj = {
            day: this.timeFormat(day),
            hou: this.timeFormat(hou),
            min: this.timeFormat(min),
            sec: this.timeFormat(sec)
          }
        } else { //活动已结束,全部设置为'00'
          obj = {
            day: '00',
            hou: '00',
            min: '00',
            sec: '00'
          }
        }
        countDownArr.push(obj);
      })
      // 渲染,然后每隔一秒执行一次倒计时函数
      this.setData({
        countDownList: countDownArr
      })
      setTimeout(this.countDown, 1000);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      this.takeGroup()
      this.record()
      this.PayDetail()
      let endTimeList = [];
      // 将活动的结束时间参数提成一个单独的数组,方便操作
      this.data.goodsList.forEach(o => {
        endTimeList.push(o.actEndTime)
      })
      this.setData({
        actEndTimeList: endTimeList
      });
      // 执行倒计时函数
      this.countDown();

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: 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;
    }
  })