allowance.js 5.4 KB
// page/subPackage/pages/allowance/allowance.js
import {
  API_HOST,
  SERVICE_HOST
} from '../../../../libs/config';
import {
  GET,
  POST
} from '../../../../libs/request';
import {
  parseBrandListData
} from '../../../../utils/productList';
import {
  jumpByUrl
} from '../../../../libs/urlRoute';

import { listen } from '../../../../utils/login';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    explain: [],
    redPacSum: 0,
    bannerData: [],
    bannerRatio: 0.5,
    recommdData: {
      isLoading:false,
      hasMore:true,
      currentPage:1,
      productList:[],
    },
    current_page_name: 'allowance',
    redExplainUrl:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.fetchData();
    this.fetchSourceData();
    this.fetchProductList();
    listen(function(){
      // console.log("Listen successful")
      this.fetchData();
    })
  },

  /**
   * 获取网络数据
   */
  fetchData: function() {
    if (!app.isLogin() || !app.getUid()) {
      return;
    }
    wx.showLoading({
      title: '',
    })
    let that = this;
    let params = {
      method: 'app.redpac.redpacNum',
      uid: app.getUid()
    }
    GET(API_HOST, params)
      .then(data => {
        if (data && data.code == 200 && data.data) {
          let explain = that.data.explain;
          explain = data.data.redpacContext;
          let sum = data.data.sum;
          that.setData({
            redPacSum: sum,
            explain
          })
        }
        wx.hideLoading()
      })
      .catch(error => {
        wx.hideLoading()
      })
  },

  /**
   * 获取banner数据
   */
  fetchSourceData: function() {
    let that = this;
    let param = {
      content_code: '94d5b1976f0e8bbc93c5a2b7e51ed470',
    }
    GET(API_HOST + '/operations/api/v5/resource/get', param)
      .then(data => {
        if (data && data.code == 200 && data.data && data.data.length > 0) {
          let templateData = data.data[0].data;
          let imageWidth = templateData.imageWidth;
          let imageHeight = templateData.imageHeight;
          let bannerRatio = imageHeight / imageWidth;

          templateData.list.map((item, index) => {
            item.src = item.src.replace(/{width}/g, imageWidth).replace(/{height}/g, imageHeight).replace('{mode}', 2);
          })
          that.setData({
            bannerData: templateData.list,
            bannerRatio
          })
        }
      })
  },

  
  /**
   * 获取猜你喜欢数据
   */
  fetchProductList: function() {
    let that = this;
    let recommdData = that.data.recommdData;
    if(recommdData.isLoading || !recommdData.hasMore){
      return;
    }
    recommdData.isLoading = true;
    that.setData({
      recommdData
    })
    let params = {
      method: 'app.search.redpackage.productList',
      limit: 20,
      page: recommdData.currentPage
    }
    GET(API_HOST, params)
      .then(json => {
        if (json && json.code == 200 && json.data && json.data.product_list) {
          let data = json.data.product_list;
          data = parseBrandListData(data);
          // console.log(data)
          recommdData.isLoading = false;
          recommdData.productList = recommdData.productList.concat(data);
          let hasMore = recommdData.currentPage < json.data.page_total;
          recommdData.hasMore = hasMore;
          if(hasMore){
            recommdData.currentPage = recommdData.currentPage + 1;
          }
          that.setData({
            recommdData,
          })
        }
      })
      .catch(error=>{
        recommdData.isLoading = false;
        that.setData({
          recommdData
        })
      })
  },
  /**
   * 两张banner的点击
   */
  bannerTab: function(event) {
    // console.log(event)
    let url = event.currentTarget.dataset.url;
    jumpByUrl(url)
  },

  /**
   * 查看明细
   */
  goDetail: function(event) {
    // console.log("==Go detail==")
    wx.navigateTo({
      url: './allowanceList',
    })
  },
  /**
   * 
   */
  goExplain: function(event) {
    let that = this;
    // console.log("==Go explain==")
    if (this.data.redExplainUrl){
      that.jumpToExlpain();
      return;
    }
    let param={
      code:"20181002-161516",
      method:'app.help.detail',
      return_type:'plain'

    }
    GET(API_HOST,param)
    .then(json=>{
      // console.log(json)
      let redExplainUrl = json;
      this.setData({
        redExplainUrl
      })
      that.jumpToExlpain();
    })
    .catch(error=>{

    })
  },
  jumpToExlpain:function(){
    let redExplainUrl = this.data.redExplainUrl;
    let timeStamp = Date.parse(new Date()) + '';
    try {
      wx.setStorageSync(timeStamp, redExplainUrl);
    } catch (e) {
    }
    wx.navigateTo({
      url: './h5page?timeStamp=' + timeStamp,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.fetchProductList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})