coupon.js 10.3 KB

// coupon.js
import { API_HOST, SERVICE_HOST } from '../../common/config';
import { GET, POST } from '../../common/request';
import objectAssign from '../../vendors/object-assign';
import { shouldDiscardTap } from '../../utils/util';

//获取应用实例
let app = getApp();

const screenHeight = app.globalData.systemInfo.screenHeight;
const screenWidth = app.globalData.systemInfo.screenWidth;
const DEVICE_WIDTH_RATIO = screenWidth / 320;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    over_time_tag:'./images/ygq-chuo-tab@2x.png',
    has_used_tag:'./images/ysy@2x.png',
    coupon_normal_bg:'./images/yhq-bg-2@2x.png',
    coupon_yunfei_bg:'./images/yhq-bg-1@2x.png',
    coupon_used_bg:'./images/has-used-ic@2x.png',
    up_arrow:'./images/up-ic@2x.png',
    down_arrow:'./images/down-ic@2x.png',
    showEmptyContainer:false,
    screen_height: screenHeight,
    screen_width: screenWidth,
    coupon_list:[],
    
    exchangeSuccess:false,//兑换成功 刷新列表表示
    exchangeBtn:false,//兑换按钮是否可用
    couponNum:'',//输入的优惠券码

    currentStatus:1,//1:未使用 2 已使用

    loadMore:false,
    couponData:{
      notuse:{
        hasmore:false,
        data:[],
        currentPage:1,
        isLoadMore:false,
        isLoading:false,
      },
      hasused: {
        hasmore: false,
        data: [],
        currentPage: 1,
        isLoadMore: false,
        isLoading: false,
      },
    }
    // coupon:{
    //   notuse: unusable_coupons,
    //   use: usable_coupons,
    // }
  },

/**
 * 使用未使用切换函数
 */
  useTapped:function(e){
    this.setData({
      currentStatus: e.target.dataset.type
    });
    let curCoupon = this.data.couponData[this.getcurrentChannelStr()];
    if (curCoupon.data && curCoupon.data.length > 0) {
      this.setCouponList();
      return;
    }
    this.fetchData();
  },

  useNowTapped:function(e){
    // console.log(e);
    let couponCode = e.target.dataset.coupon_code;
    let couponId = e.target.dataset.coupon_id;
    let couponTitle = e.target.dataset.coupon_title;
    wx.navigateTo({
      url: '../goodsList/PromotionList?' + 'page_name=coupon' + '&coupon_id=' + couponId + '&coupon_code=' + couponCode + '&promotionTitle=' + '以下商品可使用' + couponTitle + '的优惠券' +'&title=优惠活动商品'+'&page_param'+'',
    })
  },

  couponCodeInput:function(e){
    this.setData({
      couponCode:e.detail.value,
      exchangeBtn: e.detail.value?true:false,
    });
  },

  /**
  * 重新刷新未使用列表
  */
  refreshCoupon:function(){
    let _couponData = this.data.couponData;
    let _channelData =  _couponData['notuse'];
    _channelData.currentPage = 1;
    _channelData.isLoading = false;
    _couponData['notuse'] = _channelData;
    this.setData({
      couponData: _couponData,
      exchangeSuccess: true
    })
    this.fetchData();
  },  
  /**
   * 兑换优惠券
   */
  exchangeCoupon:function(e){
    let couponCode = this.data.couponCode;
    if(!couponCode){
      return;
    }
    let param={
      method:'app.coupons.bindPCoupon',
      coupon_code: couponCode,
      fromPage:"aFP_CouponExchange"
    }
    GET(API_HOST,param)
    .then(json=>{
      if(json && json.code && json.code==200){
        if(json.data){
          //兑换成功
          wx.showToast({
            title: json.message,
            icon:'success',
            duration:2000
          })
          //此处应该刷新未使用列表
          this.refreshCoupon();
        }else{
          this.wetoast.toast({
            title: json.message,
            titleClassName: 'wetoast-title',
            duration: 1500
          });
        }
      }else{
        this.wetoast.toast({
          title: json.message,
          titleClassName: 'wetoast-title',
          duration: 1500
        });
      }
    })
    .then(error=>{
      this.wetoast.toast({
        title: error.message,
        titleClassName: 'wetoast-title',
        duration: 1500
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    });
    new app.WeToast();
    this.fetchData();
  },

  fetchData:function(){
    if (app.globalData.userInfo.uid == 0 || app.globalData.userInfo.uid ==undefined){
      wx.hideLoading();
      return
    }
    var curCoupon = this.data.couponData[this.getcurrentChannelStr()];
    if (curCoupon.isLoading){
      return;
    }
    var currentStatus = this.data.currentStatus;
    if (this.data.exchangeSuccess) {
      curCoupon = this.data.couponData['notuse'];
      currentStatus = 1;
    }
    let _type = currentStatus == 1 ? "notuse" :"use";
    // let _method = currentStatus == 1 ? 'app.coupons.lists' :'app.coupons.list'
    let param={
      method: "app.coupons.lists",
      'type': _type,
      page: curCoupon.currentPage,//待调整
      limit:10,
      fromPage:"aFP_MineCoupon",
    }
    this.setLoading(true);
    GET(API_HOST,param)
    .then(json=>{
      wx.hideLoading();
      if(json && json.code && json.code == 200){
        this.setLoading(false);
        json.data.couponList.map((item, index) => {
          item.status = currentStatus;
          item.bottomHeight = item.notes.length * 20 + 40;
          if (currentStatus == 1) {
            //未使用情况下
            if (item.couponType == '5') {
              //运费券
              item.containerBg = this.data.coupon_yunfei_bg;
              item.usebtn = './images/ljsy-2@2x.png'
            } else {
              item.containerBg = this.data.coupon_normal_bg;
              item.usebtn = './images/ljsy-red@2x.png'
            }
            item.overState = item.overState ? '(' + item.overState + ')' : "";
            item._tag = '';
            item.bottom_hidden = true;
            // if(item.){
            //   //过期情况下
            //   }
          } else {
            //已使用模式
            item.containerBg = this.data.coupon_used_bg;
            item.usebtn = '';
            item._tag = this.data.has_used_tag;
            item.bottom_hidden = true;
            item.overState = '';
          }
        });
        let _couponData = this.data.couponData;
        let currentChannelData = _couponData[this.getcurrentChannelStr()];
        if(this.data.exchangeSuccess){
          currentChannelData.data = json.data.couponList;
        }else{
          currentChannelData.data = currentChannelData.data.concat(json.data.couponList);
        }
        currentChannelData.hasmore = currentChannelData.data.length < json.data.total ? true:false;
        currentChannelData.currentPage = currentChannelData.data.length < json.data.total ? currentChannelData.currentPage + 1 :            currentChannelData.currentPage;

        _couponData[this.getcurrentChannelStr()] = currentChannelData;
        this.setData({
          couponData: _couponData,
        })
  
       this.setCouponList();
      }else{
        this.setLoading(false);
        this.setCouponList();
      }
    })
    .catch(error=>{
      wx.hideLoading();
      this.setLoading(false);
      this.setCouponList();
    })

  },

  /**
   * 设置是否在加载
   */
  setLoading:function(e){
    let _couponData = this.data.couponData;
    let currentCoupon = _couponData[this.getcurrentChannelStr()];
    currentCoupon.isLoading = e;
    _couponData[this.getcurrentChannelStr()] = currentCoupon;
    this.setData({
      couponData: _couponData
    })
  },
  /**
   * 设置各个频道的list到公共list中
   * 及时刷新列表
   */
  setCouponList:function(){
    let _exchangeSuccess = this.data.exchangeSuccess;
    this.setData({
        exchangeSuccess: false
      })
    let currentCouponData = this.data.couponData[this.getcurrentChannelStr()];
    /**
     * 刷新列表
     * 兑换成功:1、当前在未使用状态下:则可以直接刷新列表
     *          2、如果当前在已使用状态下则不能设置couponlist
     */
    this.setData({
      loadMore: currentCouponData.hasmore,
      showEmptyContainer: currentCouponData.data && currentCouponData.data.length > 0 ? false : true,
      exchangeSuccess: false
    })
    if (this.data.currentStatus == 1){//未使用状态下
      this.setData({
        coupon_list: currentCouponData.data,
      })
    }else{
      if (!_exchangeSuccess){
        this.setData({
          coupon_list: currentCouponData.data,
        })
      }
    }
    // if(!this.data.exchangeSuccess){
    //   this.setData({
    //     coupon_list: currentCouponData.data,
    //     loadMore: currentCouponData.hasmore,
    //     showEmptyContainer: currentCouponData.data && currentCouponData.data.length > 0 ? false : true,
    //     exchangeSuccess: false
    //   })
    // }else{
    //   this.setData({
    //     exchangeSuccess: false
    //   })
    // }
  },

  showDetailMsg:function(e){
    let currentStatu = this.data.currentStatus;
    let index = e.target.dataset.code
    let _couponData = this.data.couponData;
    var curCoupon = _couponData[this.getcurrentChannelStr()];
    curCoupon.data[index].bottom_hidden = !curCoupon.data[index].bottom_hidden;
    _couponData[this.getcurrentChannelStr()] = curCoupon;

    this.setData({
      couponData: _couponData
    })
    
    this.setCouponList();
  },

  //去逛逛
  goBackToTab:function(e){
    wx.navigateTo({
      url: '../goodsList/newArrival?&page_name=coupon&page_param=' + ''
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let currentData = this.data.couponData[this.getcurrentChannelStr()];
    if(currentData.hasmore){
      this.fetchData();
    }
  },

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

  getcurrentChannelStr:function(){
    let currentStatus = this.data.currentStatus;
    if (currentStatus == 1 || this.data.exchangeSuccess){
      return 'notuse';
    }
    return 'hasused';
  },
})