index.js 11.7 KB
Newer Older
李嘉林 committed
1
const app = getApp();
李嘉林 committed
2
const { goodsApi, DFSImg, $themeToLink } = app;
李嘉林 committed
3 4 5 6 7 8
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true
  },
  behaviors: [],
李嘉林 committed
9 10 11 12 13
  properties: {
    datas: {
      type: Object
    }
  },
李嘉林 committed
14 15 16
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
李嘉林 committed
17 18
    titleIcon: "",
    classificationLoadPage: 1,
李嘉林 committed
19 20 21
    classificationLoadNum: 10,
    queryProductInfoLoading: false,
    loading: false,
李嘉林 committed
22 23
    finished: false,
    goodsList:[],
李嘉林 committed
24 25 26 27 28 29
  },
  // 数据监听器
  observers: {},
  // 组件生命周期
  lifetimes: {
    created() {},
李嘉林 committed
30 31 32
    ready() {
      this.init();
    },
李嘉林 committed
33 34 35
    moved() {},
    detached() {}
  },
李嘉林 committed
36
  attached() {},
李嘉林 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data;

      // show事件发生前,页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true
      });

      // 清除定时器等操作
    },
    // 页面尺寸变化时
    resize() {}
李嘉林 committed
61 62 63 64 65 66 67 68 69 70
  },
  // 组件方法
  methods: {
    init() {
      let {
        goodsType,
        goodsSource,
        angleSignImg,
        titleIcon
      } = this.data.datas.componentData;
71
      angleSignImg = DFSImg(angleSignImg);
李嘉林 committed
72 73 74
      if (titleIcon) {
        titleIcon = DFSImg(titleIcon);
      }
李嘉林 committed
75 76 77 78
      this.setData({
        "data.datas.componentData.angleSignImg": angleSignImg,
        titleIcon
      });
李嘉林 committed
79
      console.log(this.data.titleIcon, "----------------------------68");
李嘉林 committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      if (goodsType == 0) {
        if (goodsSource == "classify") {
          //商品分类
          this.getProductList(1, 1);
        } else {
          //指定商品
          this.getProductList(2);
        }
      } else if (goodsType == 1) {
        if (goodsSource == "classify") {
          //商品分类
          this.getInterList(1);
        } else {
          //指定商品
          this.getInterList(2);
        }
      }
    },
    getProductList(code, type) {
      let { goodsList, goodsSort } = this.data.datas.componentData;
李嘉林 committed
100 101 102 103 104 105
      let {
        classificationLoadPage,
        loading,
        finished,
        queryProductInfoLoading
      } = this.data;
李嘉林 committed
106 107 108 109
      if (queryProductInfoLoading == true) {
        return;
      }
      this.setData({ queryProductInfoLoading: true });
李嘉林 committed
110 111 112
      // type 1初始化 2滚动加载
      //分类查
      if (code == 1) {
李嘉林 committed
113 114 115 116 117 118 119
        if (type == 1) {
          classificationLoadPage = 1;
        } else {
          classificationLoadPage++;
          loading = true;
        }
        this.setData({ classificationLoadPage, loading });
李嘉林 committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        let query = {
          categoryId: this.data.datas.componentData.categoryId,
          page: this.showRowNum() ? classificationLoadPage : 1,
          rows: this.showRowNum()
            ? this.data.classificationLoadNum
            : this.getItemNum(),
          sortColumn: goodsSort,
          sortType: goodsSort != 2 ? 1 : 0,
          whetherShowSoldOutGoods: this.data.datas.componentData
            .whetherShowSoldOutGoods,
          whetherQueryCollectCount: true,
          whetherUseVirtualSalesQty: true,
          whetherUseVirtualCollectCount: true,
          whetherFindDistributionCommission: this
            .whetherFindDistributionCommission,
          whetherFindRecommendedCardFlag: this.data.datas.componentData.vipPrice
            ? 1
            : 0,
          whetherShowGoodsDefaultTagFlag: this.data.datas.componentData
            .marketingTag
            ? 1
            : 0,
          whetherFindMultiShopFlag: 1
        }; // 当前页 // 一页多少条 //查询销售数量 //查询收藏数 //是否使用虚拟销售数量 //是否使用虚拟收藏人数 //是否查询商品佣金(0:不查询,1:展示,为空不查佣金) //是否查询会员价  1是  0否 //是否展示营销标签  1是  0否 //是否查询多商户   //商城使用
李嘉林 committed
144
        goodsApi.queryProductInfo(query).then(res => {
李嘉林 committed
145
          this.setData({ loading: false, queryProductInfoLoading: false });
李嘉林 committed
146
          if (res.data.code == 200) {
李嘉林 committed
147 148 149 150 151 152 153
            let goodsLists = res.data.data.list;
            if (type == 1) {
              goodsList = goodsLists;
            } else {
              goodsList = [...goodsList, ...goodsLists];
            }
            if (goodsLists && goodsLists.length < 10) {
李嘉林 committed
154
              this.setData({ finished: true });
李嘉林 committed
155
            }
李嘉林 committed
156
            this.goodsListVal(goodsList, type);
李嘉林 committed
157 158
          }
        });
李嘉林 committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      } else {
        // 商品产品详情
        let terminalProductIds = goodsList.map(element => {
          return element.terminalProductId;
        });
        if (terminalProductIds.length == 0) {
          return;
        }
        let briefQuery = {
          sortColumn: goodsSort,
          sortType: goodsSort != 2 ? 1 : 0,
          page: 1,
          rows: terminalProductIds.length,
          terminalProductIds: terminalProductIds,
          whetherShowSoldOutGoods: this.whetherShowSoldOutGoods
        }; // 当前页 // 一页多少条
        this.briefProduct(briefQuery);
      }
    },
    async briefProduct(query) {
      await goodsApi.queryBriefProductInfo(query).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data.list;
182 183
          let ids = data.map(element => {
            let goodsList = this.render ? res.data.list : res.data.data.list;
李嘉林 committed
184 185 186 187 188 189 190 191 192 193 194
            return element.terminalProductId;
          });
          this.moreProduct(ids);
        }
      });
    },
    async moreProduct(terminalProductIds) {
      let {
        vipPrice,
        marketingTag,
        whetherFindDistributionCommission,
195
        goodsList
李嘉林 committed
196 197 198 199 200 201 202 203 204
      } = this.data.datas.componentData;
      let moreQuery = {
        terminalProductIds,
        whetherQueryCollectCount: true, //查询收藏数
        whetherUseVirtualSalesQty: true, //是否使用虚拟销售数量
        whetherUseVirtualCollectCount: true, //是否使用虚拟收藏人数
        whetherFindDistributionCommission: whetherFindDistributionCommission, //是否查询商品佣金(0:不查询,1:展示,为空不查佣金)
        whetherFindRecommendedCardFlag: vipPrice ? 1 : 0, //是否查询会员价  1是  0否
        whetherShowGoodsDefaultTagFlag: marketingTag ? 1 : 0, //是否展示营销标签  1是  0否
205
        whetherFindMultiShopFlag: 1 //是否查询多商户   //商城使用
李嘉林 committed
206 207 208 209
      };
      goodsApi.queryMoreProductInfo(moreQuery).then(res => {
        if (res.data.code == 200) {
          goodsList = res.data.data;
李嘉林 committed
210
          this.goodsListVal(goodsList, 1);
李嘉林 committed
211 212 213
        }
      });
    },
李嘉林 committed
214
    goodsListVal(goodsList, type) {
李嘉林 committed
215 216
      console.log(goodsList, "------------goodsListVal");
      goodsList.forEach((item, index) => {
李嘉林 committed
217
        item.marketingTag = item.defaultTags
218 219 220 221
          ? this.filtermark(
              item.defaultTags,
              this.data.datas.componentData.marketingTagList
            )
李嘉林 committed
222 223
          : [];
        item.shopLogoUrl = DFSImg(item.shopLogoUrl);
李嘉林 committed
224 225
        item.coverImage = DFSImg(item.coverImage, 400, 400, 1);
        item.productImgUrl = DFSImg(item.productImgUrl, 400, 400, 1);
李嘉林 committed
226 227
        item.minPrice = Number(item.minPrice).toFixed(2);
        item.qty = Number(item.qty);
李嘉林 committed
228
        item.saleQty = item.saleQty ? Number(item.saleQty) : 1;
李嘉林 committed
229
        item.totalCollectionCount = Number(item.totalCollectionCount) || 0;
李嘉林 committed
230 231 232 233 234 235 236 237 238
        item.minPriceAfterRecommendedCardPriceTable = item.minPriceAfterRecommendedCardPriceTable
          ? Number(item.minPriceAfterRecommendedCardPriceTable)
          : "";
        item.minProductGoodsCommission = item.minProductGoodsCommission
          ? Number(item.minProductGoodsCommission).toFixed(2)
          : 0;
        item.minGoodsSuggestedRetailPrice = item.minGoodsSuggestedRetailPrice
          ? Number(item.minGoodsSuggestedRetailPrice).toFixed(2)
          : "";
239 240
        item.showVipPrice = this.showVipPrice(item, index);
        item.showVipTag = this.showVipTag(item, index);
李嘉林 committed
241 242 243 244 245
        if (item.saleTime) {
          item.getSaleTime = 0;
          item.getSaleTime = this.getSaleTime(item.saleTime);
        }
      });
李嘉林 committed
246
      this.setData({
李嘉林 committed
247 248
        "datas.componentData.goodsList": goodsList,
        goodsList
李嘉林 committed
249 250 251 252 253
      });
      if (type == 1) {
        // 初始化
        if (this.selectComponent("#waterfallFlow")) {
          this.selectComponent("#waterfallFlow").initList(goodsList);
254
        }
李嘉林 committed
255 256 257 258 259 260
      } else {
        // 滚动加载
        if (this.selectComponent("#waterfallFlow")) {
          this.selectComponent("#waterfallFlow").reLoadPro(goodsList);
        }
      }
李嘉林 committed
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
    },
    getSaleTime(val) {
      return new Date(val.replace(/-/g, "/")).getTime() - new Date().getTime();
    },
    getItemNum() {
      //数量
      if (
        this.data.datas.componentData.style === "list" &&
        this.data.datas.componentData["goodsSource"] != "assign"
      ) {
        //网格模式
        if (
          this.data.datas.componentData.rowNum *
            this.data.datas.componentData.columnNum >=
          this.data.datas.componentData.goodsList.length
        ) {
          return this.data.datas.componentData.goodsList.length;
        } else {
          return (
            this.data.datas.componentData.rowNum *
            this.data.datas.componentData.columnNum
          );
        }
      } else {
        return this.data.datas.componentData.goodsList.length;
      }
    },
    showRowNum() {
李嘉林 committed
289
      return (
李嘉林 committed
290
        this.data.datas.componentData.moreShow == false &&
李嘉林 committed
291
        this.data.datas.componentData.style == "waterfall" &&
李嘉林 committed
292 293 294
        this.data.datas.componentData.goodsSource == "classify"
      );
    },
李嘉林 committed
295 296 297 298 299 300 301 302 303 304 305 306 307
    filtermark(newlist, marketingTagList) {
      let list = [];
      if (newlist.length <= 0) return;
      if (marketingTagList.length > 0) {
        marketingTagList.forEach((item, index) => {
          newlist.forEach((item1, index1) => {
            if (item1 == item.key) {
              list.push(item);
            }
          });
        });
      }
      return list;
308 309 310 311 312 313 314 315 316 317
    },
    showVipPrice(item, index) {
      // 索引 0 显示价格 1显示名称
      let showFlag = false;
      let vipPrice = item["minPriceAfterRecommendedCardPriceTable"];
      if (vipPrice && Number(vipPrice) < Number(item["minPrice"])) {
        showFlag = true;
      }
      return showFlag;
    },
李嘉林 committed
318
    showVipTag(item, index) {
319 320 321 322
      // 索引 0 显示价格 1显示名称
      let showFlag = false;
      let vipPrice = item["minPriceAfterRecommendedCardPriceTable"];
      let showTag = item["recommendedCardMinPricePriceTableName"];
李嘉林 committed
323
      if (vipPrice && showTag && Number(vipPrice) <= Number(item["minPrice"])) {
324 325 326
        showFlag = true;
      }
      return showFlag;
李嘉林 committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    },
    getScroll(el) {
      // 是指定分类且不显示查看更多的瀑布流组件滚动加载
      let { goodsSource, moreShow, style } = this.data.datas.componentData;
      if (
        goodsSource == "classify" &&
        moreShow == false &&
        style == "waterfall"
      ) {
        this.selectComponent("#waterfallFlow").getScroll(el);
      }
    },
    // 分页加载
    onLoad() {
      console.log("---触发分页加载");
李嘉林 committed
342 343 344
      this.getProductList(1, 2);
    },
    viewMore() {
李嘉林 committed
345 346 347 348 349 350
      let {
        goodsType,
        style,
        categoryName,
        categoryId
      } = this.data.datas.componentData;
李嘉林 committed
351 352 353
      let linkVal = "";
      if (goodsType != 1) {
        if (style == "list") {
李嘉林 committed
354
          linkVal = `/goodsSearch/goodsSearch?categoryName=${categoryName}&categoryId=${categoryId}`;
李嘉林 committed
355
        } else {
李嘉林 committed
356
          linkVal = `/goodsSearch/goodsSearch`;
李嘉林 committed
357 358 359 360 361 362
        }
      } else {
        linkVal = `/pointShop/productList?categoryName=${categoryName}&categoryId=${categoryId}`;
      }
      $themeToLink({
        type: 1,
李嘉林 committed
363
        link: linkVal
李嘉林 committed
364
      });
李嘉林 committed
365
    }
李嘉林 committed
366
  }
李嘉林 committed
367 368
};

李嘉林 committed
369
Component(componentOptions);