seckill-detail.js 6.23 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: false,
    commonTipsCompVisible: false,
    signTipsCompVisible: false,
    innerTitle: "提示",
    innerText: "",
    userInfo: {},
    productInfo: {},
    navBackTimeout: 0,
    options: {},
    wxTimerList: {},
    lastTime: "",
    isSellOut: false,
    orderStatus: 0, // 0.未下单  1.下单成功
    isTimeEnd: false, //倒计时结束
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onShow() {
    if (wxTimer) {
      wxTimer.calibration()
    }
  },
  onLoad(options) {
    this.setData({
      options
    })
    this.initData();
  },
  onUnload() {
    this.removeTimer();
  },

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

  // 开始倒计时
  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.setData({
          isTimeEnd: true
        })
        _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({
        orderStatus: 0,
        isSellOut: false,
        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);
        this.seckillOrderComp.setRemark("");
      }
    }
  },

  /**
   * 秒杀详情
   */
  querySeckillDetail() {
    let code = this.data.options.code;
    let _this = this;
    return new Promise((resolve, reject) => {
      app.post({
        toast: false,
        url: app.api.seckillDetail,
        data: {
          seckillCode: code
        }
      }).then((result) => {
        this.setData({
          productInfo: result
        })
        console.log("result:", result);
        resolve();
      }).catch((err) => {
        if (err.code == 1002) {
          _this.setData({
            signTipsCompVisible: true,
          })
        } else {
          _this.setData({
            commonTipsCompVisible: true,
            innerText: err.errMsg || "您未达到进入条件"
          })
        }

      });
    })
  },

  /**
   * 拍卖出价提交
   * @param {*} price
   */
  querySeckillSubmit(data) {
    let code = this.data.options.code;
    data.seckillCode = code;
    app.post({
      toast: false,
      loading: true,
      url: app.api.seckillSubmit,
      data: data
    }).then((result) => {
      console.log("result:", result);
      this.setData({
        orderStatus: 1,
      })
      wx.showModal({
        title: "恭喜您",
        content: '秒杀成功',
        showCancel: false,
        success(res) {}
      })
    }).catch((err) => {
      console.log("querySeckillSubmit err:", err);

      switch (err.code) {
        // 库存不足
        case 1013:
          this.setData({
            isSellOut: true,
          })
          break;

          // 已经参与该描述活动
        case 1014:
          wx.showModal({
            content: '您已经参加过该秒杀活动',
            showCancel: false,
            success(res) {}
          })
          break;

        default:
          wx.showToast({
            title: err.errMsg || "系统开小差",
            icon: "none"
          })
          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,
      commonTipsCompVisible: false,
      signTipsCompVisible: false,
    })
  },


  /**
   * 去验证
   */
  toVipLoginHandler() {
    app.router.push({
      openType: "redirect",
      path: "vipLogin"
    })
  },

  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    console.log("@seckill-detail || evt:", name)
    switch (name) {

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

        // 返回个人中心
      case "_evt_return_user_center":
        this.hideMask();
        app.router.push({
          openType: "switchTab",
          path: "userCenter"
        })
        break;

        // 确认下单
      case "_evt_submit_order":
        this.querySeckillSubmit(data);
        break;

        // 通用按钮
      case "_evt_common_comp_button":
        this.hideMask();
        wx.navigateBack({
          delta: 1
        });
        break;

        // 去验证
      case "_evt_to_verify":
        this.hideMask();
        this.toVipLoginHandler();
        break;

        // 暂不验证
      case "_evt_not_verify":
        this.hideMask();
        wx.navigateBack({
          delta: 1
        });
        break;

      default:
        break;
    }
  },
})