action-bar.js 4.52 KB
// page/subPackage/pages/zeroSell/components/action-bar.js

import ZeroSellService from '../../service/zero-sell';
import CommonService from '../../service/common';
import { fomartCountdownTime } from '../../../../utils/util';

const event = global.event;

var app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // UNKNOWN: 0,
    // READY: 1, // 未开始
    // START: 2, // 参加
    // ALEADY: 3, // 已参加活动
    // WAIT: 4, // 等待抽奖中
    // LUCK: 5, // 抽奖结束
    // END: 6, // 活动结束
    // PEOPLE_LIMIT_WAIT: 7 // 人数达到活动限制、已满员、人数不足统一为待开奖状态
    status: {
      type: Number,
      value: 1,
    },
    num: {
      type: Number,
      value: 0,
    },
    shareUid: {
      type: Number,
      value: 0,
    },
    actId: {
      type: Number,
      value: 0,
    },
    copyText: {
      type: String,
      value: '',
    },
    text: {
      type: String,
      value: '',
    },
    type: {
      type: Number,
      value: 0,
    },
    // time in seconds
    startTime: {
      type: Number,
      value: 0,
      observer: function(startTime) {
        this.setData({
          remainingTime: fomartCountdownTime({ startTime }).join(':'),
        });
      },
    },
    isLogin: {
      type: Boolean,
      value: false,
    },
    activityStatus: {
      type: Number,
      status: 1,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    code: '',
    error: false,
    fellow: false,
    isStart: false,
    remainingTime: '',
  },

  ready() {
    this.service = new ZeroSellService();
    this.commonService = new CommonService();
    const timeId = setInterval(() => {
      if (this.data.isStart) {
        const changeStatusTimeId = setTimeout(()=> {
          this.triggerEvent('changeStatus');
          clearTimeout(changeStatusTimeId)
        }, 1000);
        clearInterval(timeId);
      }
      const fomartTimes = fomartCountdownTime({
        startTime: this.properties.startTime,
      });
      this.setData({
        remainingTime: fomartTimes.join(':') || '00:00:00',
        isStart: fomartTimes.length === 0 && this.properties.status === 1,
      });
    }, 1000);
  },

  /**
   * 组件的方法列表
   */
  methods: {
    fetchCode() {
      wx.showLoading({
        mask: true,
      });
      event.emit('user-is-login', {
        logged: this.getCode.bind(this),
        loginSuccess: function() {},
      });
    },

    getCode(userInfo) {
      this.service
        .fetchCode({
          shareUid: this.properties.shareUid,
          uid: app.getUid(),
          actPrizeId: this.properties.actId,
          userThumb: userInfo.avatarUrl,
          userName: userInfo.nickName,
        })
        .then(result => {
          wx.hideLoading();
          if (result.code === 200) {
            this.setData({
              show: true,
              code: result.data.prizeCode,
            });
            this.triggerEvent('gaincodesuccess', this.properties.actId);
          } else {
            this.setData({
              error: true,
              errorText: result.message,
            });
          }
        });
    },
    goShare() {
      this.triggerEvent('share');
      this.setData({
        show: false,
        error: false,
        fellow: false,
      });
    },

    hadJoinToShare() {
      this.goShare();
      this.triggerEvent('changeStatus');
    },

    goMyList() {
      wx.reLaunch({
        url: '/pages/zeroSell/index?tabIndex=3&reload=0',
      });
    },

    goList() {
      wx.navigateTo({
        url: '/pages/zeroSell/index?reload=0',
      });
    },

    cancelFellow() {
      this.setData({
        fellow: false,
      });
    },

    cancelAlert() {
      this.setData({
        show: false,
        error: false,
      });

      this.triggerEvent('changeStatus');
    },

    openFellowAlert() {
      this.setData({
        fellow: true,
      });
    },

    hiddenAlert(e) {
      this.setData({
        show: false,
        error: false,
        fellow: false,
      });
      this.triggerEvent('changeStatus');
    },

    submitFormId(e) {
      return this.commonService.addWechatFormId({
        formId: e.detail.formId,
        uid: app.getUid() || 0,
        openId: app.getOpenID() || '',
        miniapp_type: app.getMiniappType(),
      });
    },

    goLogin() {
      const event = global.event;
      event.emit('user-is-login', {
        logged: this.logged,
        loginSuccess: this.loginSuccess,
      });
    },

    logged() {},

    loginSuccess() {},
  },
});