couponList.js 8.37 KB
// pages/couponList/couponList.js
import { API_HOST, SERVICE_HOST } from '../../../../libs/config';
import { GET, POST } from '../../../../libs/request';

let app = getApp();
const screenHeight = app.globalData.systemInfo.screenHeight;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentFixedFilterIndex: 0,// 0已使用,1:未使用,2:过期
    showFliter: false,
    selectedSearchFliterIndex: 1,//筛选项Index
    screenHeight,
    filters: [],
    useCoupons: {
      page: 0,
      limit: 20,
      list: [],
      isLast: false,
      filter: 0, //优惠券过滤:0/null:无; 1:店铺券; 2:活动券; 3:免邮券	
      useType:  'use',
      totleNum: 0,
      totleText: '0',
    },//已使用

    notuseCoupons: {
      page: 0,
      limit: 20,
      list: [],
      isLast: false,
      filter: 0,//优惠券过滤:0/null:无; 1:店铺券; 2:活动券; 3:免邮券	
      useType: 'notuse',
      totleNum: 0,
      totleText: '0',
    },//未使用

    overtimeCoupons: {
      page: 0,
      limit: 20,
      list: [],
      isLast: false,
      filter: 0,//优惠券过滤:0/null:无; 1:店铺券; 2:活动券; 3:免邮券	
      useType: 'overtime',
      totleNum: 0,
      totleText: '0',
    },//过期

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCouponList(this.data.notuseCoupons,true);
    this.getCouponNums();
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.info(res.windowHeight);
        that.setData({
          scHeight: res.windowHeight
        });
      }
    }); 
  },

  getCouponNums: function (params, reload) {
    let that = this;
    let param = {
      method: 'app.coupons.getCouponNums',
      uid: app.getUid(),
    };

    GET(API_HOST, param)
      .then(json => {
        if (json && json.code == 200 && json.data) {
          let notuse = json.data.notuse > 99 ? '99+' : json.data.notuse;
          let use = json.data.use > 99 ? '99+' : json.data.use;
          let overtime = json.data.overtime > 99 ? '99+' : json.data.overtime;
          let useCoupons = that.data.useCoupons;
          useCoupons.totleText = use;
          useCoupons.totleNum = json.data.use ? json.data.use : 0;
          let notuseCoupons = that.data.notuseCoupons;
          notuseCoupons.totleText = notuse;
          notuseCoupons.totleNum = json.data.notuse ? json.data.notuse : 0;
          let overtimeCoupons = that.data.overtimeCoupons;
          overtimeCoupons.totleText = overtime;
          overtimeCoupons.totleNum = json.data.overtime ? json.data.overtime : 0;
          that.setData({ useCoupons, notuseCoupons, overtimeCoupons });
        }
      })
      .catch(error => {
        console.log(error)
      })
  },

  getCouponList: function (params,reload) {
    
    if (reload) {
      params.page = 0;
      params.list = [];
      params.isLast = false;
    }

    let that = this;
    let param = {
      method: 'app.coupons.get',
      uid: app.getUid(),	
      type: params.useType,
      filter: params.filter,
      limit: params.limit,
      page: params.page + 1,	
    };

    GET(API_HOST, param)
      .then(json => {
        if (json && json.code == 200 && json.data) {
          if (that.data.currentFixedFilterIndex == 0) {
            let notuseCoupons = that.data.notuseCoupons;
            let list = notuseCoupons.list;
            list = list.concat(json.data.couponList ? json.data.couponList : []);
            notuseCoupons.list = list;
            notuseCoupons.page = param.page;
            notuseCoupons.filter = params.filter;
            notuseCoupons.isLast = list.length == json.data.total ? true : false;
            let filters = json.data.filters ? json.data.filters : [];
            that.setData({ notuseCoupons, filters});

          } else if (that.data.currentFixedFilterIndex == 1) {
            let useCoupons = that.data.useCoupons;
            let list = useCoupons.list;
            list = list.concat(json.data.couponList ? json.data.couponList:[]);
            useCoupons.page = param.page;
            useCoupons.list = list;
            useCoupons.filter = params.filter;
            useCoupons.isLast = list.length == json.data.total ? true : false; 
            that.setData({ useCoupons });

          } else if (that.data.currentFixedFilterIndex == 2) {
            let overtimeCoupons = that.data.overtimeCoupons;
            let list = overtimeCoupons.list;
            list = list.concat(json.data.couponList ? json.data.couponList : []);
            overtimeCoupons.page = param.page;
            overtimeCoupons.list = list;
            overtimeCoupons.filter = params.filter;
            overtimeCoupons.isLast = list.length == json.data.total ? true : false;      
            that.setData({ overtimeCoupons });
          }
        }
      })
      .catch(error => {
        console.log(error)
      })
  },

  selectFliterContent: function (event) {
    if (this.data.currentFixedFilterIndex == 0) {
      let showFliter = !this.data.showFliter;
      this.setData({ showFliter });
    }
  },
  
  didSelectFliter: function (event) {
    let item = event.detail;
    if (item) {
      let selectedSearchFliterIndex = item.selectIndex;
      let showFliter = !this.data.showFliter;
      if (selectedSearchFliterIndex == -1) {
        selectedSearchFliterIndex = this.data.selectedSearchFliterIndex;
      }
      this.setData({
        showFliter,
        selectedSearchFliterIndex
      });
      if (this.data.currentFixedFilterIndex == 0) {
        let notuseCoupons = this.data.notuseCoupons
        notuseCoupons.filter = event.detail.filter_id;
        this.getCouponList(notuseCoupons, true);
      } 
    } else{
      let showFliter = !this.data.showFliter;
      this.setData({
        showFliter,
      });
    }
  },

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

  fixedFilterTapped: function (event) {
    let currentFixedFilterIndex = event.currentTarget.dataset.type;
    let showFliter = this.data.showFliter;
    showFliter = currentFixedFilterIndex != this.data.currentFixedFilterIndex ? false : showFliter;
    this.setData({ currentFixedFilterIndex, showFliter});

    let item = this.data.notuseCoupons;
    if (currentFixedFilterIndex == 0) {
      item = this.data.notuseCoupons
    } else if (currentFixedFilterIndex == 1) {
      item = this.data.useCoupons
    } else if (currentFixedFilterIndex == 2) {
      item = this.data.overtimeCoupons
    }
    if (item.list.length == 0){
      this.getCouponList(item, true);
    }
  },

  duihuanTapped: function (event) {
    let inputValue = event.detail;
    this.bindPCoupon(inputValue);
  },
  
  loadMore: function(event) {
    let item = this.data.notuseCoupons;
    if (this.data.currentFixedFilterIndex == 0) {
      item = this.data.notuseCoupons
    } else if (this.data.currentFixedFilterIndex == 1) {
      item = this.data.useCoupons
    } else if (this.data.currentFixedFilterIndex == 2) {
      item = this.data.overtimeCoupons
    }
    console.log(item)
    if (!item.isLast) {
      this.getCouponList(item, false);
    }
  },

  bindPCoupon: function (inputValue) {
    let that = this;
    if (app.getUid()) {
      let param = {
        method: 'app.coupons.bindPCoupon',
        uid: app.getUid(),
        fromPage: 'iFP_Coupon',
        coupon_code: inputValue ? inputValue : '',
      };

      GET(API_HOST, param)
        .then(function (data) {
          if (data && data.code == 200) {
            wx.showToast({
              title: '优惠券兑换成功!',
              icon:'success',
              duration:2000
             })
            that.getCouponList(that.data.notuseCoupons, true);
            that.getCouponNums();
          } else {
            wx.showModal({
              content: data.message,
              showCancel: false,
              confirmText: "确定",
            });
          }
        })
        .catch(function (error) {
        });
    } else {
      wx.showModal({
        content: "请先登录!",
        showCancel: false,
        confirmText: "确定",
      });
    }
  },
})