groupBuy.js 11.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  // 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,
      countDownList: [],
      actEndTimeList: [],
      goodsList: [{
        actEndTime: ''
      }]

    },
    //获取banner
    getBanner() {
          let url = '/portal/Group/banner'
          app.post(url, {}, {}).then((res) => {
              // console.log('获取banner',res)
              this.setData({banner: res.info});
          })
      },
    //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: '复制成功',
          });
        }
      });
    },


    //成交记录
    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'
        })
      }
    },


    //参与的组团
    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) => {
        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 = '2018-12-27 13:59:44'
          goodsList[i].actEndTime = groupList[i].over_time
        }
        goodsList.pop()
        let endTimeList = [];
        goodsList.forEach(o => {
          endTimeList.push(o.actEndTime)
        })
        this.setData({
          actEndTimeList: endTimeList
        });
        // console.log(endTimeList)
        this.countDown();
        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),
            goodsList: goodsList
        });
      })
    },

    timeFormat(param) { //小于10的格式化函数
      return param < 10 ? '0' + param : param;
    },

    countDown() { //倒计时函数
      // 获取当前时间,同时得到活动结束时间数组
      let newTime = new Date().getTime();
      let endTimeList = this.data.actEndTimeList;
      // console.log(endTimeList)
      let countDownArr = [];
      // 对结束时间进行处理渲染到页面
      endTimeList.forEach(o => {
        let endTime = new Date(o).getTime();
        // console.log(endTime)
        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);
      })

      var groupList = this.data.groupList
      for (var i = 0; i < groupList.length; i++) {
        for (var j = 0; j < countDownArr.length; j++) {
          groupList[i].hou = countDownArr[i].hou
          groupList[i].min = countDownArr[i].min
          groupList[i].sec = countDownArr[i].sec
        }
      }
      // 渲染,然后每隔一秒执行一次倒计时函数
      this.setData({
        countDownList: countDownArr,
        groupList: groupList
      })
      // console.log(this.data.countDownList)
      setTimeout(this.countDown, 1000);
    },

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

    },

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