myList.js 5.72 KB
// page/subPackage/pages/zeroSell/myList.js

import ZeroSellService from './service/zero-sell'
import { Actionsheet} from '../../vendors/zanui/index';
import router from './router/router';
import {wrapperName} from './helper'

let app = null;

Page(Object.assign({

  /**
   * 页面的初始数据
   */
  data: {
    shareProduct: {},
    recommends: [],
    list: [{
      page: 1,
      data: []
    }, {
      page: 1,
      data: []
    }],
    tabIndex: 0,
    footText: '',

    actionsheet: {
      componentId: 'shareActionSheet',
      show: false,
      closeOnClickOverlay: true,
      cancelText: '取消',
      isNewShareStyle:true,
      unionUserImageUrl:'http://img12.static.yhbimg.com/sns/2018/08/02/15/029b6acc4f8bc0620ecd7ec2133fcf900c.png',
      actions: [{
        name: '分享给好友',
        className: 'action-class',
        loading: false,
        openType: 'share',
        image_src: '../../images/share_wechat@2x.png',
      }, 
      {
        name: '生成海报分享',
        className: 'action-class',
        loading: false,
        image_src: '../../images/share_wxpeng@2x.png'
      }]
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.service = new ZeroSellService()
    app = getApp();
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      [`list[${this.data.tabIndex}].page`]: 1
    });

    this._init();
    this._getRecommend();
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let key = `list[${this.data.tabIndex}].page`;
    
    this.setData({
      [key]: 1
    });

    this._init().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let type = this.data.tabIndex;
    let list = this.data.list;
    let page = list[type].page;
    let oldData = list[type].data;
    let key = `list[${type}].data`;
    let keyPage = `list[${type}].page`;

    return this._getPage(type, page).then(data => {
      this.setData({
        [key]: oldData.concat(data),
        [keyPage]: ++page
      });
    });
  },

  onTabChange({detail}) {
    this.setData({
      tabIndex: detail,
      [`list[${detail}].page`]: 1
    });

    this._init();
  },

  _init(){
    let type = this.data.tabIndex;
    let list = this.data.list;
    let page = list[type].page
    let key = `list[${type}].data`
    let keyPage = `list[${type}].page`

    return this._getPage(type, page).then(data => {
      this.setData({
        [key]: data,
        [keyPage]: ++page
      });
    });
  },

  _getPage(type, page) {
    this.setData({
      footText: '内容加载中...'
    });
    return this.service.getMyList({type, page}).then(result => {

      if(result.code !== 200 || result.data.length === 0) {
        if(type == 0) {
          this.setData({
            footText: '您还没有参与的抽奖,赶紧去参加吧'
          });
        } else {
          this.setData({
            footText: '暂无已公布活动,请继续参加活动'
          });
        }
      } else {
        this.setData({
          footText: ''
        });
      }
      

      if (result.code !== 200) {
        return [];
      }

      return result.data;
    });
  },

  _getRecommend() {
    this.service.getRecommend({
      actPrizeId: 0
    })
    .then(response => {
      if (response && response.code === 200 && response.data){
        this.setData({
          recommends: response.data,
        })
      }
    })
    .catch(error => {
      console.log(error)
    })
  },

  share({detail}){
    this.setData({
      'actionsheet.show': true,
      'shareProduct': detail
    })
  },

  handleZanActionsheetCancel({ componentId }) {
    this.setData({
      'actionsheet.show': false
    })
  },

  handleZanActionsheetClick({ componentId, index }) {
    this.setData({
      'actionsheet.show': false
    });

    let qrcode = this.service.getQrCode({
      shareUid: app.getUid(),
      actPrizeId: this.data.shareProduct.act_prize_id
    });

    if (index === 1) {
      if (this.data.shareProduct.name) {
        router.go('snapShare', {
            product_name: this.data.shareProduct.name,
            default_image: this.data.shareProduct.cover_img,
            product_price: this.data.shareProduct.price,
            product_qrCode: qrcode
        });
      }
    };
  },
  onShareAppMessage(res) {
    let params = {
      TITLE: `【0元抽奖】点一下,免费拿走${this.data.shareProduct.name}`,
      DESC: '我在YO!LUCK发现一个不错的商品赶快来看看吧!'
    };

    if (res.from === 'menu') {
      // 用户点击右上角分享
      return {
        title: params.TITLE, // 分享标题
        desc: params.DESC, // 分享描述
        path: `pages/zeroSell/detail?actPrizeId=${this.data.shareProduct.act_prize_id}`,
        imageUrl: this.data.shareProduct.cover_img,
        success: function() {
        },
        fail: function() {
        }
      };
    } else if (res.from === 'button') {
      // 用户点击分享按钮

      return {
        title: params.TITLE, // 分享标题
        desc: params.DESC, // 分享描述
        path: `/pages/zeroSell/detail?actPrizeId=${this.data.shareProduct.act_prize_id}&shareUid=${app.getUid()}`,
        imageUrl: this.data.shareProduct.cover_img,
        success: function() {
        },
        fail: function() {
        }
      };
    }
  }
}, Actionsheet))