myList.js 7.41 KB
import config from '../../common/config';
import api from '../../common/api';
import event from '../../common/event';
import LimitModel from '../../models/limit/index';
import helper from '../../utils/helper';
import wx from '../../utils/wx';
import {Actionsheet} from '../../vendors/zanui/index';
import commonModel from '../../models/common';
import Yas from '../../common/yas';


let app = getApp();
let router = global.router;
let yas;

/**
 * method=app.limitProduct.queryHelpInfo(助力信息)
 method=app.limitProduct.addhelpUserInfo(添加助力信息)
 method=app.drawline.getMyQueueList 获取我的排队列表
 */
Page(Object.assign({

  /**
     * 页面的初始数据
     */
  data: {
    actId: 0,
    isEnd: 0,
    userInfo: {}, // 用户信息
    shareInfo: {
      default_image: '',
      product_name: '',
      product_qrCode: '',
      product_skn: '',
      limitProductCode: ''
    },
    bgStyle: [ // 排队图片
      '../../static/images/limit/queue_bg_1.png',
      '../../static/images/limit/queue_bg_2.png',
      '../../static/images/limit/queue_bg_3.png',
      '../../static/images/limit/queue_bg_4.png',
      '../../static/images/limit/queue_bg_5.png',
      '../../static/images/limit/queue_bg_6.png',
      '../../static/images/limit/queue_bg_7.png',
      '../../static/images/limit/queue_bg_8.png'
    ],
    vipImage: [ // 3种用户vip等级小图标
      '',
      '../../static/images/limit/level-1.png',
      '../../static/images/limit/level-2.png',
      '../../static/images/limit/level-3.png'
    ],
    defaultAvatar: '../../static/images/icons/default-avatar.png',
    api_status: 0, // 用于防止连续点击参加活动 0:没调用接口 1:正在调用接口
    giftImage: '../../static/images/limit/gift.png',
    hideInviteButton: 1, // 用户显示邀请按钮
    queueList: [], // 排队列表
    actionSheet: {
      componentId: 'shareActionSheet',
      show: false,
      closeOnClickOverlay: true,
      cancelText: '取消',
      actions: [{
        name: '邀请好友助力',
        className: 'action-class',
        loading: false,
        openType: 'share'
      }, {
        name: '生成图片分享到朋友圈',
        className: 'action-class',
        loading: false,
      }]
    },
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function(options) {
    console.log(options);
    let actId = options.actId;
    let isEnd = options.isEnd || 0;
    let uid = app.getUid() || options.uid;


    if (actId) {
      this.setData({
        actId: actId,
        isEnd: parseInt(isEnd)
      });
      this.getQueueList(actId);
      for (let i in options) {
        options[i] = decodeURIComponent(options[i]);
      }
      this.setData({
        shareInfo: Object.assign(this.data.shareInfo, options)
      });
    }

    yas = new Yas();

    yas.pageOpenReport();

  },

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

  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function(options) {
    console.log(options);
    if (app.getUid()) {
      this.setData({
        hideInviteButton: 0
      });
    }
  },

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

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

  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function() {

  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function(res) {
    let params = {
      FROM: res.from,
      SHARE_RESUIL: 0,
      TITLE: decodeURIComponent(this.data.shareInfo.product_name),
      DESC: '我在有货限定频道发现一个不错的商品赶快来看看吧!',
      PATH: `/pages/product/detail/detail?limitProductCode=${this.data.limitProductCode}&originUid=${app.getUid()}&originUnionID=${app.getUnionID()}`, // eslint-disable-line
      IMG: decodeURIComponent(this.data.shareInfo.default_image)
    };

    // 用户点击右上角分享
    return {
      title: params.TITLE, // 分享标题
      desc: params.DESC, // 分享描述
      path: params.PATH, // 分享路径
      imageUrl: params.IMG,
      success: function() {
        params.SHARE_RESUIL = 1;
      },
      fail: function() {
        params.SHARE_RESUIL = 2;
      }
    };
  },
  share: function() {
    this.setData({
      'actionSheet.show': true
    });
  },
  handleZanActionsheetCancel() {
    this.setData({
      'actionSheet.show': false
    });
  },

  // 当行动按钮中有一个被点击时触发
  // index 代表被点击按钮在传入参数 actions 中的位置
  handleZanActionsheetClick({index}) {
    this.setData({
      'actionSheet.show': false
    });

    let uid = this.data.originUid || this.data.uid;

    if (index === 1) {
      router.go('snapShare', this.data.shareInfo);
    }
  },

  /**
     * 邀请好友助力排队按钮
     */
  inviteFriends: function(e) {
    this.share();
  },

  /**
     * 提交formId消息模板
     */
  submitFormId: function(e) {
    console.log('submitFormId:', {formId: e.detail.formId, uid: app.getUid() || 0, openId: app.getOpenID() || ''});
    commonModel.addWechatFormId({
      formId: e.detail.formId,
      uid: app.getUid() || 0,
      openId: app.getOpenID() || '',
      miniapp_type: app.getMiniappType()
    });
  },

  /**
     * 获取排队列表
     */
  getQueueList: function(actId, limit = 20) {
    let that = this;

    return LimitModel.getMyQueueList(actId, limit).then(res => {
      console.log(res);
      if (res.data && res.data.length > 0) {
        let result = [];
        let helpIndex = 0;

        if (res.data[0].helpName && res.data[0].helpheadIco) {
          result.push({
            helpInfo: '好友"' + res.data[0].helpName + '"已为你助力',
            background: helper.imgView(this.data.bgStyle[0], 340, 162, 1),
            helpName: res.data[0].helpName,
            helpheadIco: res.data[0].helpheadIco
          });
          helpIndex = 1;
        }

        res.data.forEach((item, index) => {
          if (!item.headIco) {
            item.headIco = that.data.defaultAvatar;
          } else {
            item.headIco = helper.image(item.headIco, 100, 100, 1);
          }
          item.nickName = item.nickName || '用户' + item.uid;
          item.nickName = item.nickName.length > 12 ? item.nickName.substring(0, 12) + '...' : item.nickName;
          item.queueTime = this.formatTimeText(item.queueTime);
          item.vipLevel = item.vipLevel || 0;
          item.vipImage = this.data.vipImage[item.vipLevel];
          let bgStyle = this.data.bgStyle;
          let bgIndex = (index + helpIndex) % bgStyle.length;

          item.background = helper.imgView(bgStyle[bgIndex], 340, 162, 1);
          result.push(item);
        });
        this.setData({
          queueList: result
        });
        return Promise.resolve({code: 200, data: this.data.queueList});
      } else {
        return Promise.reject({code: 203, message: '获取失败'});
      }
    });
  },

  formatTimeText: function(time) {
    let new_time;

    if (time < 60) {
      new_time = time + 'sec';
    } else if (time >= 60 && time < 60 * 60) {
      new_time = parseInt(time / 60, 10) + 'min';
    } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
      new_time = parseInt(time / (60 * 60), 10) + 'hour';
    } else if (time >= 60 * 60 * 24) {
      new_time = parseInt(time / (60 * 60 * 24), 10) + 'day';
    }
    return new_time;
  }
}, Actionsheet));