diy_goodsGroup.js 14.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
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,
    url: t.globalData.setting.imghost,
    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 () {

  },


  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.data.classstyle_id = this.data.object.classstyle;
      this.data.goodscount = this.data.object.goodscount;
      this.data.wgroup = this.data.object.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;
      this.init(this.data.classstyle_id, this.data.wgroup);
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  pageLifetimes: {
    show: function () {
      this.data.firist_type_curr=0,
      this.data.currentPage=1,
      this.setData({ goods_array: null})

      // 页面被展示
      // 在组件实例进入页面节点树时执行
      this.data.classstyle_id = this.data.object.classstyle;
      this.data.goodscount = this.data.object.goodscount;
      this.data.wgroup = this.data.object.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;
      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 = [];

      //--初始化卡类--
      th.card_init();

      var os=getApp().globalData.setting;

      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;
              item.cardprice1 = goods.cardprice1;
              item.cardprice2 = goods.cardprice2;
              item.cardprice3 = goods.cardprice3;

              prom_id = res.data.data.prom_id;

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

              var url = "";
              if (prom_type == 6) {
                url = "/api/weshop/teamlist/get/" + os.stoid + "/" + prom_id;
              } else if (prom_type == 1) {
                url = "/api/ms/flash_sale/get/" + os.stoid + "/" + prom_id;
              }
              if(url==""){
                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: [] });
                }
              }else{
                return app.request.promiseGet(url, {})
              }

            }).then(res => {
              index++;
              var prom =null
              if (res && res.data)  res.data.data;
              var now=ut.gettimestamp();
              if (prom != null && prom.is_end==0 && prom.end_time>now && prom.show_time<now) {
                item.prom_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++;
            var now=ut.gettimestamp();

            goods.forEach(async 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;

              item.cardprice1 = val.cardprice1;
              item.cardprice2 = val.cardprice2;
              item.cardprice3 = val.cardprice3;

              prom_id = val.prom_id;

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

              var url = "";
              if (prom_type == 6) {
                url = "/api/weshop/teamlist/get/" + os.stoid + "/" + prom_id;
              } else if (prom_type == 1) {
                url = "/api/ms/flash_sale/get/" + os.stoid + "/" + prom_id;
              }
              if(url!="") {
               await app.request.promiseGet(url, {}).then(res => {
                  var prom = null
                  if (res && res.data) prom=res.data.data;
                  if (prom != null && prom.is_end==0 && prom.end_time>now && prom.show_time<now) {
                    item.prom_price = res.data.data.price;
                  }
                  th.data.requestData.push(item);
                  th.setData({
                    goods_array: th.data.requestData,
                    page: th.data.currentPage - 1,
                  });
                })
              }else{
                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++;
            var now=ut.gettimestamp();
            goods.forEach(async 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;

              item.cardprice1 = val.cardprice1;
              item.cardprice2 = val.cardprice2;
              item.cardprice3 = val.cardprice3;

              prom_id = val.prom_id;

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

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

              if(url!=null){
               await app.request.promiseGet(url, {}).then(res => {
                  var prom =null;
                  if(res && res.data) res.data.data;
                  if (prom != null && prom.is_end==0 && prom.end_time>now && prom.show_time<now) {
                    item.prom_price = res.data.data.price;
                  }
                  th.data.requestData.push(item);
                  th.setData({
                    goods_array: th.data.requestData,
                    page: th.data.currentPage - 1,
                  });
                })
              }else{
                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) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    },
	
	//---卡的初始化----
	card_init: function() {
	  var th = this,os=getApp().globalData.setting;
	  getApp().request.get("/api/weshop/users/get/" + os.stoid + "/" + getApp().globalData.user_id, {
	    success: function(e) {
	      getApp().globalData.userInfo = e.data.data;
	      getApp().getConfig2(function(e) {
	        var swithc_list = e.switch_list;
	        var sw_arr = JSON.parse(swithc_list);
	        //---如果后台又开等级卡的开关---
	        if (sw_arr.rank_switch && sw_arr.rank_switch == "2") {
	          th.setData({
	            rank_switch: true
	          });
	          //---回调卡的列表---
	          th.getPlusCardType(function(ob) {
	            th.setData({
	              card_list: ob.card_list
	            });
	            var ti = setInterval(function() {
	              var user = getApp().globalData.userInfo;
	              if (!user) return false;
	              clearInterval(ti);
	
	              if (user.card_field) {
	                var str = user['card_expiredate'].replace(/-/g, '/');
	                var end = new Date(str);
	                end = Date.parse(end) / 1000;
	                var now = ut.gettimestamp();
	
	                //--- 判断是等级会员,且在有效期范围内 ---
	                if (user.card_field && now < end) {
	                  var card_name = ob.name_map.get(user.card_field);
	                  if (card_name.length > 4) card_name = card_name.substring(0, 4);
	                  th.setData({
	                    card_field: user.card_field,
	                    card_name: card_name,
	                    card_list: ob.card_list
	                  });
	                }

	              }

	            }, 500)
	          })
	
	        }
	      })
	    }
	  })
	
	},
	
	//---  获取卡类列表 ---
	getPlusCardType: function(func) {
	  var storid = o.stoid;
	  var th = this;
	  getApp().request.promiseGet("/api/weshop/plus/vip/mem/bership/list?" + "storeId=" + storid, {}).then(res => {
	    var plusCard = res.data.data;
	    var arr = [1219, 2089, 3031];
	    var new_arr = new Array();
	    var card_name_map = new Map();
	    for (var i = 0; i < plusCard.length; i++) {
	      var name = "card" + plusCard[i].CorrPrice.toLowerCase();
	      card_name_map.set(name, plusCard[i].CardName);
	    }
	
	    var ob = {
	      "card_list": plusCard,
	      "name_map": card_name_map
	    };
	    func(ob);
	  })
	},
	
	
  }
})