myList.js 9.56 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';
import {
  stringify
} from '../../vendors/query-stringify';


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: '',
      unionType: ''
    },
    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: '取消',
      isNewShareStyle: true,
      unionUserImageUrl: '',
      actions: [{
        name: '分享给好友',
        className: 'action-class',
        loading: false,
        openType: 'share',
        image_src: '../../static/images/share_wechat@2x.png',
      }, {
        name: '分享到朋友圈',
        className: 'action-class',
        loading: false,
        image_src: '../../static/images/share_wxpeng@2x.png'
      }]
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  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();

    this.initActionSheet();

  },

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

  },

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

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

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

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

  },

  initActionSheet() {
    let actionSheet = this.data.actionSheet;

    // user_union_type 有值 需要显示 联盟share按钮

    if (app.globalData.userUnionType) { // user_union_type_imageUrl
      let actionsheetParam = [{
        name: '分享给朋友',
        className: 'action-class',
        loading: false,
        openType: 'share',
        image_src: '../../static/images/share_union_wechat@2x.png'
      }, {
        name: '分享到朋友圈',
        className: 'action-class',
        loading: false,
        image_src: '../../static/images/share_union_wxpeng@2x.png'
      }];

      actionSheet.unionUserImageUrl = app.globalData.userUnionTypeImageUrl;
      actionSheet.actions = actionsheetParam;
    } else if (app.globalData.userInfo.uid) {
      // 登录状态
      actionSheet.unionUserImageUrl = app.globalData.userNounionTypeImageUrl;
    }
    this.setData({
      actionSheet
    });
  },


  /**
   * 用户点击右上角分享
   */
  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.shareInfo.limitProductCode}&originUid=${app.getUid()}&originUnionID=${app.getUnionID()}&unionType=${app.getUserUnionType()}`, // 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_RESULT = 1;
      },
      fail: function() {
        params.SHARE_RESULT = 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) {
      yas.report('YB_INVITE_ASSIST_SAVE_PICTURE_C', {
        LIMIT_CODE: this.data.shareInfo.limitProductCode,
        POS_ID: 102
      });
      router.go('snapShare', this.data.shareInfo);
    } else {
      yas.report('YB_INVITE_ASSIST_C', {
        LIMIT_CODE: this.data.shareInfo.limitProductCode,
        POS_ID: 102
      });
    }
  },

  handlerJumpToUnionUrl() {
    this.setData({
      'actionSheet.show': false
    });

    if (app.globalData.userUnionType) {
      router.goUrl('https://activity.yoho.cn/feature/2213.html?title=说明详情');
    } else if (app.globalData.userInfo.uid) {
      router.goUrl('https://activity.yoho.cn/feature/2601.html?title=有货有赚');
    } else {
      // 未登录 状态去登录
      let pages = getCurrentPages();
      let referer = pages[pages.length - 1].route + '?' + decodeURIComponent(stringify(this.data.shareInfo));

      router.go('userCenter', {
        referer
      });
    }
  },

  /**
   * 邀请好友助力排队按钮
   */
  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 = 1000) {
    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));