goodsCollect.js 8.07 KB
//goodsCollect.js
import { API_HOST, SERVICE_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import { getChannelCode, getGenderCode } from '../../utils/home';


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

const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;
const screenHeight = app.globalData.systemInfo.screenHeight;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFetching: false,
    categoryList: null,
    productList: null,
    currentProductList: null,
    currentCategoryId: '',
    total: 0,
    current_page_name: 'goodsCollect',
    deleteAnimation: '', // 控制是否给相应的单元增加删除动画
    enableScroll: true,
    AnimatingSku: '',
    tempSku: '',
    screenHeight
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.fetchCollectList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease',
    })
  },

  fetchCollectList: function () {
    let gender = getGenderCode(getApp().globalData.selectedChannel);
    let yh_channel = getChannelCode(getApp().globalData.selectedChannel);
    let param = {
      method: 'app.favorite.product',
      gender,
      yh_channel,
      limit: 500,
    }
    this.setData({
      isFetching: true,
    });
    GET(API_HOST, param)
      .then(data => {
        if (!data || !data.code || data.code != 200) {
          this.setData({
            isFetching: false,
          });
          return;
        }
        this.data.total = data.data.total;
        data.data.category_list.splice(0, 0, {
          category_name: '全部',
          category_id: ''
        });

        this.data.categoryList = data.data.category_list;
        this.data.productList = data.data.product_list;

        this.setData({
          isFetching: false,
          total: this.data.total,
          categoryList: this.data.categoryList,
          productList: this.data.productList,
          currentProductList: this.data.productList,
        });
      })
      .catch(error => {
        this.setData({
          isFetching: false,
        });
      });
  },

  onCategoryItemSelected: function (e) {
    console.log(e);
    this.moveStart(e);
    let id = e.currentTarget.dataset.id;
    let productList = [];
    if (id != "") {
      this.data.productList.forEach((item) => {
        if (id == item.category_id) {
          productList.push(item);
        }
      });
    }
    this.setData({
      currentCategoryId: id,
      currentProductList: id != "" ? productList : this.data.productList,
    });
  },

  moveStart: function (e) {
    console.log('moveStart');
    let enableScroll = true;
    this.data.touchX = e.changedTouches[0].clientX;
    this.data.touchY = e.changedTouches[0].clientY;

    var tempAniamtion = this.animation.left('0px').step();
    this.setData({
      deleteAnimation: tempAniamtion.export(),
      enableScroll,
      AnimatingSku: '',
    });

  },

  moveEnd: function (e) {
    console.log('moveEnd');
    var X = e.changedTouches[0].clientX;
    var Y = e.changedTouches[0].clientY;
    var curGoods = e.currentTarget.dataset.type;
    var data = null, tempAniamtion = null;
    var angle = this.angle({ X: X, Y: Y }, { X: this.data.touchX, Y: this.data.touchY });
    if (Math.abs(angle) > 30) return;
    if (this.data.touchX - X > 3) {
      console.log('左滑动');
      if (this.data.AnimatingSku) {
        let enableScroll = false;
        tempAniamtion = this.animation.left('0px').step();
        this.setData({
          AnimatingSku: '',
          deleteAnimation: tempAniamtion.export(),
          enableScroll,
        })
      } else {
        console.log('show');
        let enableScroll = false;
        tempAniamtion = this.animation.left('-70px').step();
        this.setData({
          AnimatingSku: curGoods.product_skn,
          tempSku: curGoods.product_skn,
          deleteAnimation: tempAniamtion.export(),
          enableScroll,
        })
      }
    } else if (this.data.touchX - X < -3) {
      console.log('右滑动');
      let enableScroll = true;
      tempAniamtion = this.animation.left('0px').step();
      this.setData({
        deleteAnimation: tempAniamtion.export(),
        enableScroll,
        AnimatingSku: '',
      });
    }
  },

  navtoProductDetailPage: function (event) {
    let data = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?productSkn=' + data.product_skn + '&page_name=' + this.data.current_page_name,
    });
  },

  navToNewArriaval: function () {
    wx.switchTab({
      url: '../index/index' + '?page_name=' + this.data.current_page_name + '&page_param=' + '',
    })
  },

  //删除喜欢的商品
  deleteGoodsRequest: function (event) {
    var goodsItem = event.currentTarget.dataset.type;
    var category_id = goodsItem.category_id;
    var product_id = goodsItem.product_id;
    let param = {
      method: 'app.favorite.cancel',
      fav_id: product_id,
      type: 'product'
    };
    GET(API_HOST, param)
      .then(data => {
        let total = this.data.total > 0 ? this.data.total - 1 : 0;
        if (total == 0) {
          this.setData({
            isFetching: false,
            total: total,
            categoryList: null,
            productList: null,
            currentProductList: null
          });
          return;
        }
        var productListTemp = [];
        let productLists = this.data.productList;
        let categoryLists = this.data.categoryList;
        var index = 0;
        productLists.forEach((item, i) => {
          if (product_id == item.product_id) {
            index = i;
            return;
          }
        });
        productLists.splice(index, 1); //从数组中移除选中的商品
        if (this.data.currentCategoryId == '') { //全部
          productListTemp = productLists;
        } else {
          productLists.forEach((item) => {
            if (category_id == item.category_id) {
              productListTemp.push(item);
            }
          });
          if (productListTemp.length == 0) {
            productListTemp = productLists;
          }
        }
        var isExist = false;
        productListTemp.forEach((item) => {
          if (category_id == item.category_id) {
            isExist = true; //分类还存在
            return;
          }
        });
        if (!isExist) {
          var index = 0;
          categoryLists.forEach((item, i) => {
            if (item.category_id == category_id) {
              index = i;
              return;
            }
          });
          categoryLists.splice(index, 1); //从数组中移除选中的分类id
          this.setData({
            currentCategoryId: ''
          });
        }
        this.setData({
          isFetching: false,
          total: total,
          categoryList: categoryLists,
          productList: productLists,
          currentProductList: productListTemp,
        });
        that.wetoast.toast({
          title: data.message,
          titleClassName: 'wetoast-title',
          duration: 1000
        });
      })
      .catch(error => {
        that.wetoast.toast({
          title: error.code + error.message,
          titleClassName: 'wetoast-title',
          duration: 1000
        });
      });
  },

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

  },


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

  },

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

  },

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

  },

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

  },

  /**
      * 计算滑动角度
      * @param {Object} start 起点坐标
      * @param {Object} end 终点坐标
      */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
})