collect_list.js 6.6 KB
var t = function(t) {
    return t && t.__esModule ? t : {
      default: t
    };
  }(require("../../../utils/LoadMore.js")),
  e = getApp(),
  a = e.request,
  o = new t.default();
var oo = e.globalData.setting,
  app_d = e.globalData;

Page({
  data: {
    url: e.globalData.setting.imghost,
    resourceUrl: e.globalData.setting.resourceUrl,
    iurl: e.globalData.setting.imghost,
    collects: null,
    currentPage: 1,
    curpage: 1,
    collects_Recommend: [],
    ismore: 0,
    iscollects: 0,
    loadName: null,
    editEd: 0, //商品编辑按钮是否点击
    isall: false, //是否全选
    sum: 0,
    total_collects:0,
    total_goods:0,
    pageSize:8,//分页数量
  },
  onLoad: function() {
    o.init(this, "", "collects"),
     this.requestCollectList();
     // this.requestList();
  },
  requestCollectList: function() {
    isShowLoading: 0;
    var th = this;
    e = "/api/weshop/goodscollect/list?" + "pageSize=" + th.data.pageSize +"&user_id=" +
      app_d.user_id + "&store_id=" + oo.stoid + "&page=" + th.data.currentPage;
      o.request(e, function(e) {
         th.data.currentPage++;

         if(th.data.total_collects)
            th.setData({ iscollects: 1, total_collects:e.data.data.total})

        if (th.data.total_collects<th.data.pageSize){
          th.requestList();
        }      
    });
  },

  //--到底部刷新---
  onReachBottom: function() {
    if (this.data.currentPage*this.data.pageSize<this.data.total_collects){
      this.requestCollectList();
    }else{
      this.requestList();
    }
       
  },
  cancelCollect: function(t) {
    var e = t.currentTarget.dataset.id,
      o = this;
    a.delete("/api/weshop/goodscollect/del/" + oo.stoid + "/" + e, {
      success: function(t) {
        o.deleteItemData(e);
      }
    });
  },
  deleteItemData: function(t) {
    for (var e = 0; e < this.data.collects.length; e++)
      if (this.data.collects[e].collect_id == t) {
        this.data.collects.splice(e, 1), this.setData({
          collects: this.data.collects
        });
        break;
      }
  },
  //图片失败,默认图片
  bind_bnerr: function (e) {
    var _errImg = e.target.dataset.errorimg;
    var _errObj = {};
    _errObj[_errImg] = this.data.url + "/miniapp/images/del/logo.jpg";
    this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;

  },

  //图片失败,默认图片
  bind_bnerr2: function(e) {
    var _errImg = e.target.dataset.errorimg;
    var th = this;
    this.setData({
      [_errImg]: th.data.iurl + "/miniapp/images/default_g_img.gif",
    })
  },
 

  requestList: function() {
    this.setData({
      loadName: '加载中...'
    })
    var e = this;
    a.get("/api/weshop/goods/page", {
      isShowLoading: 0,
      data: {
        store_id: 1,
        user_id: oo.user_id,
        page: e.data.curpage,
        pageSize: 10
      },
      success: function(t) {
        e.data.curpage++;
        var arr1 = e.data.collects_Recommend;
        var arr2 = t.data.data.pageData;
        var arr3 = [...arr1, ...arr2];

        var ismore = 0;
        if (arr3.length == t.data.data.total) ismore = 1

        e.setData({
          collects_Recommend: arr3,
          total_goods: t.data.data.total,
          ismore_goods: ismore,
          loadName: null
        }), wx.stopPullDownRefresh();
      }
    });
  },


  goto: function(ee) {
    // e.goto("/pages/index/index/index");
    wx.switchTab({
      url: '/pages/index/index/index',
    })
  },
  //编辑商品显示
  editcollects: function() {
    var th = this.data;
    var t = this;
    if (th.editEd == 0) {
      t.select_checked(false, null)
      t.setData({
        editEd: 1,
      })
    } else {
      t.select_checked(false, null)
      t.setData({
        editEd: 0
      })
    }
  },
  iconClick: function(e) {
    var th = this.data;
    var index = e.currentTarget.dataset.index;
    var collect = "collects[" + index + "].checked";
    if (this.data.collects[index].checked == true) {
      this.setData({
        [collect]: false,
        isall: 0,
        sum: --th.sum
      })
    } else {
      var id = th.collects[index].collect_id;
      this.setData({
        [collect]: true,
        sum: ++th.sum
      })
      if (th.sum == th.collects.length) {
        this.setData({
          isall: 1
        })
      }
    }
  },
  //全选为收藏商品checked为true
  allClick: function() {
    var th = this;
    //判断是否全选
    if (th.data.isall) {
      for (var i = 0; i <= th.data.collects.length; i++) {
        var collect = "collects[" + i + "].checked";
        this.setData({
          [collect]: false,
          isall: 0
        })
      }
      this.setData({
        sum: 0
      })
    } else {
      for (var i = 0; i <= th.data.collects.length; i++) {
        var collect = "collects[" + i + "].checked";
        this.setData({
          [collect]: true,
          isall: 1
        })
      }
      this.setData({
        sum: th.data.collects.length
      })
    }
  },
  //分页删除
  alldelete: function(e) {
    var th = this;
    // 全选的话直接删除
    if (th.data.isall) {
      a.delete("/api/weshop/goodscollect/removeUser/" + oo.stoid + "/" + app_d.user_id, {
        isShowLoading: 0,
        success: function(e) {
          th.setData({
            collects: []
          })
          getApp().my_warnning("取消成功", 1, th);

        }
      })
    } else { //按照多个id删除收藏商品
  
      var new_colls= [];
      var collectId = th.data.collects;

      var str="";
      for (var i = 0; i < th.data.collects.length; i++) {
        if (collectId[i].checked) {
          str+= collectId[i].collect_id+",";     
        }else{
          new_colls.push(collectId[i]);
        }
      }
       str = str.substr(0, str.length - 1);  
       var url = "/api/weshop/goodscollect/removeByIds/" + oo.stoid + "/" + app_d.user_id + "/" + str;

      a.delete(url, {
          isShowLoading: 0,
          success: function(e) {
          
            th.setData({
              collects: new_colls,
              sum: 0
            })
            // th.select_checked(false,null);
              getApp().my_warnning("取消收藏成功", 1, th);
            }
          
        })
      
    }
  },
  select_checked:function(check,sum){
        var th = this;
        if(sum==null){
          for (var i = 0; i < this.data.collects.length; i++) {
            var collect = "collects[" + i + "].checked";
            th.setData({
              [collect]: check
            })
        }
        }else{
          var collect = "collects[" + sum + "].checked";
          th.setData({
            [collect]: check
          })
        }
  }
});