diy_goodsGroup.js 8.96 KB
var t = getApp(),
  a = t.request,
  o = t.globalData.setting,
  os = o,
  i = require("../../utils/util.js"),
  ut = i,
  s = require("../../utils/common.js");
var regeneratorRuntime = require('../../utils/runtime.js');

Component({
  properties: {
    // 这里定义了innerText属性,属性值可以在组件使用时指定
    object: {
      type: Object,
      value: null,
    },

  },
  data: {
    // 这里是一些组件内部数据
    group: false,
    imghost: o.imghost,
    is_recommend: 0,
    is_hot: 0,
    is_new: 0,
    baseUrl: "/api/weshop/goods/page",
    resourceUrl: t.globalData.setting.resourceUrl,
    currentPage: 1,
    requestData: null,
    requestUrl: "",
    goods_array: null,
    total: null,
    prom_id: 0,
    classstyle_id: null,
    g_id: null,
    wgroup: null,
    goods_btn: null,
    loadText: '加载更多',
    someData: {},
    goodscount: null,
    firist_type_data: null,
    firist_type_curr: 0,
  },

  ready: function () {
    console.log("ready");
    this.data.classstyle_id = this.data.object.classstyle;
    this.data.goodscount = this.data.object.goodscount;
    console.log(this.data.classstyle_id);
    this.data.wgroup = this.data.object.wgroup;
    console.log(this.data.wgroup);
    this.setData({
      classstyle_id: this.data.object.classstyle
    })
    if (this.data.object.goodsclass == 1) {
      this.data.is_recommend = 1
    }
    if (this.data.object.goodsclass == 2) {
      this.data.is_hot = 1
    }
    if (this.data.object.goodsclass == 3) {
      this.data.is_new = 1
    }
    this.data.requestData = new Array();
    this.data.g_id = this.data.object.data;
    var arr = [];

    for (var i = 0; i < this.data.g_id.length; i += this.data.goodscount) {
      arr.push(this.data.g_id.slice(i, i + this.data.goodscount));
    }
    this.data.firist_type_data = arr;
    console.log("firist_type_data");
    console.log(this.data.firist_type_data);
    this.init(this.data.classstyle_id, this.data.wgroup);

  },
  methods: {
    // 这里是一个自定义方法
    init: function (classstyle_id, wgroup) {
      var th = this;
      var g_id = this.data.g_id;
      if (classstyle_id == 1) {
        //手动选择的时候
        g_id = this.data.firist_type_data[th.data.firist_type_curr];
        th.data.firist_type_curr++;
      }

      var th = this,
          app = getApp(),
          index = 0,
          gdata = [];

      classstyle_id = classstyle_id + "";
      switch (classstyle_id) {
        case "1":
          g_id.forEach(function (val, ind) {
            var item = {};
            var prom_id = null;
            console.log(val.goodsid);
            app.request.promiseGet("/api/weshop/goods/get/" + os.stoid + "/" + val.goodsid, {}).then(res => {
              //商品地址
              var goods = res.data.data;
              th.setData({
                goods_btn: res.data.data
              })

              item.goods_name = goods.goods_name;
              item.goods_img = th.data.imghost + goods.original_img;
              item.market_price = goods.market_price;
              item.shop_price = goods.shop_price;
              item.sales_sum = goods.sales_sum;
              item.goods_id = goods.goods_id;
              prom_id = res.data.data.prom_id;

              var prom_type = res.data.data.prom_type; //0普通商品  1秒杀  6拼单

              var url = "";
              if (prom_type == 1) {
                url = "/api/weshop/teamlist/get/" + os.stoid + "/" + prom_id;
              } else if (prom_type == 6) {
                url = "/api/ms/flash_sale/get/" + os.stoid + "/" + prom_id;
              }
              return app.request.promiseGet(url, {})
            }).then(res => {
              index++;
              var prom = res.data.data;
              if (prom != null) {
                item.shop_price = res.data.data.price;
              }
              th.data.requestData.push(item);
              th.setData({
                goods_array: th.data.requestData,
              });

              if (th.data.firist_type_curr == th.data.firist_type_data.length) {
                th.setData({
                  goods_btn: []
                });
              }
            })
          })
          break;
        case "2":

          var item = {};
          app.request.promiseGet("/api/weshop/goods/page", {
            data: {
              store_id: os.stoid ,
              is_recommend: th.data.is_recommend,
              is_hot: th.data.is_hot,
              is_new: th.data.is_new,
              pageSize: th.data.goodscount,
              page: th.data.currentPage,
            }
          }).then(res => {
            //商品地址
            var goods = res.data.data.pageData;
            th.setData({
              goods_btn: res.data.data.pageData,
              total: res.data.data.total
            })
            th.data.currentPage++;

            goods.forEach(function (val, ind) {
              var item = {};
              var prom_id = null;
              console.log(val.goodsid);
              //app.request.promiseGet("/api/weshop/goods/get/" + os.stoid + "/" + val.goods_id, {}).then(res => {
              //商品地址
              //var goods = res.data.data;

              item.goods_name = val.goods_name;
              item.goods_img = th.data.imghost + val.original_img;
              item.market_price = val.market_price;
              item.shop_price = val.shop_price;
              item.sales_sum = val.sales_sum;
              item.goods_id = val.goods_id;
              prom_id = val.prom_id;

              var prom_type = res.data.data.prom_type; //0普通商品  1秒杀  6拼单

              var url = "";
              if (prom_type == 1) {
                url = "/api/weshop/teamlist/get/" + os.stoid + "/" + prom_id;
              } else if (prom_type == 6) {
                url = "/api/ms/flash_sale/get/" + os.stoid + "/" + prom_id;
              }

              app.request.promiseGet(url, {}).then(res => {
                var prom = res.data.data;
                if (prom != null) {
                  item.shop_price = res.data.data.price;
                }
                th.data.requestData.push(item);
                th.setData({
                  goods_array: th.data.requestData,
                  page: th.data.currentPage - 1,
                });
              })
            })

          })
          break;
        case "3":
          var item = {};
          app.request.promiseGet("/api/weshop/goodsGroup/pageGoodsList", {
            data: {
              store_id:os.stoid  ,
              gpid: wgroup,
              pageSize: th.data.goodscount,
              page: th.data.currentPage,
            }
          }).then(res => {
            //商品地址
            var goods = res.data.data.pageData;
            th.setData({
              goods_btn: res.data.data.pageData, total: res.data.data.total
            })

            th.data.currentPage++;
            goods.forEach(function (val, ind) {
              var item = {};
              var prom_id = null;
              console.log(val.goodsid);
              //app.request.promiseGet("/api/weshop/goods/get/" + os.stoid + "/" + val.goods_id, {}).then(res => {
              //商品地址
              var goods = res.data.data;
              console.log("goods");
              console.log(goods);
              // th.setData({
              //   goods_btn: res.data.data
              // })
              item.goods_name = val.goods_name;
              item.goods_img = th.data.imghost + val.original_img;
              item.market_price = val.market_price;
              item.shop_price = val.shop_price;
              item.sales_sum = val.sales_sum;
              item.goods_id = val.goods_id;
              prom_id = val.prom_id;

              var prom_type = res.data.data.prom_type; //0普通商品  1秒杀  6拼单

              var url = "";
              if (prom_type == 1) {
                url = "/api/weshop/teamlist/get/" + os.stoid + "/" + prom_id;
              } else if (prom_type == 6) {
                url = "/api/ms/flash_sale/get/" + os.stoid + "/" + prom_id;
              }

              app.request.promiseGet(url, {}).then(res => {
                var prom = res.data.data;
                if (prom != null) {
                  item.shop_price = res.data.data.price;
                }
                th.data.requestData.push(item);
                th.setData({
                  goods_array: th.data.requestData,
                  page: th.data.currentPage - 1,
                });
              })
            })

          })
          break;
      }
    },

    setLoading: function () {
      var that = this;
      that.init(that.data.classstyle_id, that.data.wgroup);
    },

    //图片失败,默认图片
    bind_bnerr3: function (e) {
      var _errImg = e.target.dataset.errorimg;
      var _errObj = {};
      _errObj[_errImg] = this.data.imghost + "/public/images/default_goods_image_240.gif";
      this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    }
  }
})