seckill-detail.js 4.54 KB
import {
  getBindtapData,
  formatDateTime,
} from '../../utils/util';
let Date = require('../../utils/date.js');
var timer = require('../../utils/wxTimer.js');

var wxTimer;

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    seckillOrderVisible: true,
    userInfo: {},
    productInfo: {},
    navBackTimeout: 0,
    options: {},
    wxTimerList: {},
    lastTime: "",
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onShow() {
    if (wxTimer) {
      wxTimer.calibration()
    }
  },
  onLoad(options) {
    this.setData({
      options
    })
    this.querySeckillDetail().then((result) => {
      this.startTimer();
    })
    this.initData();
  },
  onUnload() {
    this.removeTimer();
  },

  initData() {
    this.queryMember().then((result) => {});
  },

  // 开始倒计时
  startTimer() {
    let _this = this;
    if (wxTimer) {
      wxTimer.stop();
    }
    let beginTime = _this.getTimeStr();
    console.log("beginTime:", beginTime);
    if (!beginTime) return;
    wxTimer = new timer({
      beginTime: beginTime,
      complete() {
        _this.removeTimer();
        _this.querySeckillDetail();
      },
      interval: 1,
      intervalFn() {
        let lastTime = _this.getTimeStr({
          day: true
        });
        _this.setData({
          lastTime
        })
        // console.log("lastTime:", lastTime);
      }
    })
    wxTimer.start(_this);
  },

  /**
   * 移除倒计时
   */
  removeTimer() {
    if (wxTimer) {
      wxTimer.stop()
    }
  },

  /**
   * 获取时间字符串
   * @param {*} initObj
   */
  getTimeStr(initObj) {
    let productInfo = this.data.productInfo;
    let endTime = productInfo && productInfo.endTime || 0;
    return formatDateTime(endTime, initObj)
  },


  /**
   * 我要秒杀
   */
  onSubmitHandler() {
    let productInfo = this.data.productInfo;
    if (productInfo && productInfo.status == 1) {
      // 显示弹窗
      this.setData({
        seckillOrderVisible: true
      })
      let minNum = productInfo.minNum;
      this.seckillOrderComp = this.selectComponent("#seckillOrderComp");
      console.log("this.seckillOrderComp:",this.seckillOrderComp);
      console.log("minNum:",minNum);
      if (this.seckillOrderComp) {
        this.seckillOrderComp.setNum(minNum);
      }
    }
  },

  /**
   * 秒杀详情
   */
  querySeckillDetail() {
    let code = this.data.options.code;
    let _this = this;
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.seckillDetail,
        data: {
          seckillCode: code
        }
      }).then((result) => {
        this.setData({
          productInfo: result
        })
        console.log("result:", result);
        resolve();
      }).catch((err) => {
        clearTimeout(_this.data.navBackTimeout);
        _this.data.navBackTimeout = setTimeout(() => {
          wx.navigateBack({
            delta: 1
          });
        }, 1000);
      });
    })
  },

  /**
   * 拍卖出价提交
   * @param {*} price
   */
  querySeckillSubmit(price) {
    let code = this.data.options.code;
    app.post({
      toast: false,
      url: app.api.seckillSubmit,
      data: {
        seckillCode: code,
        price: price * 100 //转成分
      }
    }).then((result) => {
      console.log("result:", result);
      // this.setData({
      //   auctionBidSuccessVisible: true
      // })
    }).catch((err) => {
      console.log("querySeckillSubmit err:", err);

      // switch (err.code) {
      //   // 来晚一步,该价格已经被其他用户提交 显示组件
      //   case 1010:
      //     this.setData({
      //       auctionBidFailVisible: true
      //     })
      //     break;

      //   default:
      //     wx.showToast({
      //       title: err.errMsg || "系统开小差"
      //     })
      //     break;
      // }
    });
  },

  /**
   * 获取会员信息
   */
  queryMember() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.member,
        data: {}
      }).then((result) => {
        this.setData({
          userInfo: result
        })
        resolve(result);
      })
    });
  },

  // 隐藏蒙层
  hideMask() {
    this.setData({
      authorizeVisible: false,
      seckillOrderVisible: false,
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    console.log("@seckill-detail || evt:", name)
    switch (name) {

      // 隐藏弹窗
      case "_evt_hide_mask":
        this.hideMask();
        break;



      default:
        break;
    }
  },
})