detail.vue 18.0 KB
Newer Older
sin's avatar
sin committed
1 2
<template>
  <div class="goods">
sin's avatar
sin committed
3
    <headerNav title="商品详情"/>
sin's avatar
sin committed
4
    <van-swipe class="goods-swipe" :autoplay="3000">
5
      <van-swipe-item v-for="thumb in spu.picUrls" :key="thumb">
sin's avatar
sin committed
6
        <img :src="thumb">
sin's avatar
sin committed
7 8
      </van-swipe-item>
    </van-swipe>
9 10
      <!-- TODO 这里需要优化下,芋艿 -->
      <van-cell-group>
11
          <van-cell>
12 13
            <div v-if="calSkuPriceResult.originalPrice && calSkuPriceResult.originalPrice !== calSkuPriceResult.buyPrice">
                <span class="goods-price">{{ formatPrice(calSkuPriceResult.buyPrice) }}</span>
14 15 16 17 18 19
                <span class="goods-market-price">{{ formatPrice(calSkuPriceResult.originalPrice) }}</span>
            </div>
            <div v-else>
                <span class="goods-price">{{ formatPrice(initialSku.price) }}</span>
            </div>

20 21 22
            <div class="goods-title">{{ spu.name }}</div>
            <div class="goods-subtit">{{spu.sellPoint}}</div>
          </van-cell>
sin's avatar
sin committed
23

24
      <!--<van-cell   @click="onClickShowTag" class="goods-tag" >-->
sin's avatar
sin committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      <!--<template slot="title" style="font-size:10px;">-->
      <!--<img src="https://haitao.nos.netease.com/ba8a4c2fdaa54f82a45261293c116af61419663676663i46n3jlh10028.png"/>-->
      <!--<span >挪威品牌</span>-->
      <!--<img src="https://haitao.nosdn2.127.net/13bd59e6e29a4f06b278c586629e690d.png" />-->
      <!--<span >跨境商品</span>-->
      <!--<van-icon name="passed" color="red" />-->
      <!--<span >次日达</span>-->
      <!--<van-icon name="passed" color="red" />-->
      <!--<span >自提</span>-->
      <!--<van-icon name="passed" color="red" />-->
      <!--<span >闪电退款</span>-->
      <!--<van-icon name="passed" color="red" />-->
      <!--<span >前海保税仓</span>-->
      <!--<van-icon name="passed" color="red" />-->
      <!--<span >七天无理由退货(拆封后不支持)</span>-->
      <!--</template>-->
41
      <!--</van-cell>   -->
sin's avatar
sin committed
42 43
    </van-cell-group>

44 45

    <!--<van-cell-group class="goods-cell-group">-->
sin's avatar
sin committed
46 47 48 49 50 51 52 53
    <!--<van-cell is-link  @click="showPromotion" >-->
    <!--<template slot="title">-->
    <!--<span style="margin-right: 10px;">领券</span>-->
    <!--<van-tag type="danger" mark  style="margin-right: 5px;">满180减30</van-tag>-->
    <!--<van-tag type="danger" mark  style="margin-right: 5px;">满300减100</van-tag>-->
    <!--</template>-->
    <!--</van-cell>-->
    <!---->
54

sin's avatar
sin committed
55 56 57 58 59 60 61 62
    <!--<van-cell  is-link @click="showPromotion" >-->
    <!--<template slot="title">-->
    <!--<span style="margin-right: 10px;">促销</span>-->
    <!--<van-tag type="danger" style="margin-right: 5px;">多买优惠</van-tag>-->
    <!--<van-tag type="danger" style="margin-right: 5px;">满减</van-tag>-->
    <!--<van-tag type="danger" style="margin-right: 5px;">限购</van-tag>-->
    <!--</template>-->
    <!--</van-cell>-->
63
    <!--</van-cell-group>-->
sin's avatar
sin committed
64

sin's avatar
sin committed
65
    <van-cell-group class="goods-cell-group">
sin's avatar
sin committed
66
      <van-cell is-link @click="showSku">
sin's avatar
sin committed
67
        <template slot="title">
sin's avatar
sin committed
68 69
          <span style="margin-right: 10px;">已选</span>
          <span>{{ formatSkuText(initialSku) }}</span>
sin's avatar
sin committed
70 71
        </template>
      </van-cell>
sin's avatar
sin committed
72

sin's avatar
sin committed
73
    </van-cell-group>
sin's avatar
sin committed
74

75 76 77 78 79 80
<!--      <van-cell is-link @click="sorry">-->
<!--          <template slot="title">-->
<!--              <van-tag type="danger">多买优惠</van-tag>-->
<!--              <span> 满2件,总价打9折</span>-->
<!--          </template>-->
<!--      </van-cell>-->
81 82
      <!-- TODO 芋艿,后续【限时折扣】需要改下样式 -->
      <van-cell v-if="calSkuPriceResult.timeLimitedDiscount" is-link @click="sorry">
83
          <template slot="title">
84 85
              <van-tag type="danger">限时折扣</van-tag>
              <span> {{ formatTimeLimitedDiscountText(calSkuPriceResult.timeLimitedDiscount) }} </span>
86 87
          </template>
      </van-cell>
88
      <van-cell v-if="calSkuPriceResult.fullPrivilege" is-link @click="sorry">
89
          <template slot="title">
90 91
              <van-tag type="danger">满减送</van-tag>
              <span>  {{ formatFullPrivilegeText(calSkuPriceResult.fullPrivilege) }} </span>
92 93 94
          </template>
      </van-cell>

sin's avatar
sin committed
95
    <div class="goods-info">
sin's avatar
sin committed
96 97
      <p class="goods-info-title">图文详情</p>
      <div v-html="spu.description"></div>
sin's avatar
sin committed
98 99
    </div>
    <van-goods-action>
sin's avatar
sin committed
100

101
      <van-goods-action-mini-btn icon="like-o"  :class="{active:hasCollectionType === 1 }" @click="onFavoriteClicked">
sin's avatar
sin committed
102 103
        收藏
      </van-goods-action-mini-btn>
104
      <van-goods-action-mini-btn icon="cart" :info="cartCount > 0 ? cartCount : undefined" @click="onClickCart">
sin's avatar
sin committed
105 106 107 108 109 110 111 112 113
        购物车
      </van-goods-action-mini-btn>
      <van-goods-action-big-btn @click="showSku">
        加入购物车
      </van-goods-action-big-btn>
      <van-goods-action-big-btn primary @click="showSku">
        立即购买
      </van-goods-action-big-btn>
    </van-goods-action>
114 115

    <!--<van-actionsheet v-model="showTag" title="服务说明" style="font-size:14px;">-->
sin's avatar
sin committed
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
    <!---->
    <!--<van-cell>-->
    <!--<template slot="title">-->
    <!--<van-icon name="passed" color="red" style="margin-right: 10px;" />-->
    <!--<span >次日达</span>-->
    <!--<div style="margin-left: 24px;font-size:10px;color:#7d7d7d;">指定时间前下单,次日送达</div>-->
    <!--</template>-->
    <!--</van-cell>-->
    <!--<van-cell>-->
    <!--<template slot="title">-->
    <!--<van-icon name="passed" color="red" style="margin-right: 10px;" />-->
    <!--<span >自提</span>-->
    <!--<div style="margin-left: 24px;font-size:10px;color:#7d7d7d;">我们提供多种自提服务,包括自提点、自助提货柜、移动自提车等服务</div>-->
    <!--</template>-->
    <!--</van-cell>-->
    <!--<van-cell>-->
    <!--<template slot="title">-->
    <!--<van-icon name="passed" color="red" style="margin-right: 10px;" />-->
    <!--<span >闪电退款</span>-->
    <!--<div style="margin-left: 24px;font-size:10px;color:#7d7d7d;">签收7天内退货的,提供先退款后退货服务。</div>-->
    <!--</template>-->
    <!--</van-cell>-->
    <!--<van-cell>-->
    <!--<template slot="title">-->
    <!--<van-icon name="passed" color="red" style="margin-right: 10px;" />-->
    <!--<span >七天无理由退货(拆封后不支持)</span>-->
    <!--<div style="margin-left: 24px;font-size:10px;color:#7d7d7d;">七天无理由退货(拆封后不支持)</div>-->
    <!--</template>-->
    <!--</van-cell>-->
    <!--<van-cell>-->
    <!--<template slot="title">-->
    <!--<van-icon name="passed" color="red" style="margin-right: 10px;" />-->
    <!--<span >前海保税仓</span>-->
    <!--<div style="margin-left: 24px;font-size:10px;color:#7d7d7d;">本商品由前海保税仓发货</div>-->
    <!--</template>-->
    <!--</van-cell>-->
152 153
    <!--</van-actionsheet>-->

sin's avatar
sin committed
154
    <van-sku
sin's avatar
sin committed
155 156 157 158 159 160 161 162 163 164 165 166 167
      v-model="showBase"
      :sku="vanSku"
      :initial-sku="initialSku"
      :goods="vanSpu"
      :goods-id="spu.id"
      :hide-stock="hideStock"
      :close-on-click-overlay="closeOnClickOverlay"
      @stepper-change="stepperChange"
      @sku-selected="skuSelected"
      @buy-clicked="onBuyClicked"
      @add-cart="onAddCartClicked"
    />
    <!--:quota="skuData.quota"-->
168

sin's avatar
sin committed
169 170 171 172
  </div>
</template>

<script>
sin's avatar
sin committed
173
  // import skuData from '../../data/sku';
174
  import {getProductSpuInfo,collectionSpu} from '../../api/product';
175
  import {addCart, countCart, getCartCalcSkuPrice} from '../../api/order';
176
  import {hasUserSpuFavorite} from  '../../api/user';
177
  import {Dialog} from 'vant';
YunaiV's avatar
YunaiV committed
178
  import {checkLogin} from "../../utils/cache";
sin's avatar
sin committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

  export default {
    components: {},
    data() {
      // this.skuData = skuData;
      return {
        spu: {}, // 商品信息
        vanSku: {
          tree: [], // 规格数组
          list: [], // sku 数组
        },
        // TODO 后面,要加 sku 的 title 和 picture
        vanSpu: {
          title: '',
          picture: '',
        },
        initialSku: { // 选中的 sku
          // 具体规格
          // price 价格
          // quantity 选中的数量
        },
        attrValueMap: new Map(), // 规格值的映射

202
        showBase: false, // 是否显示 sku 弹层
sin's avatar
sin committed
203 204
        closeOnClickOverlay: true, // 是否在点击蒙层后关闭
        hideStock: true, // 是否显示商品剩余库存
205

206 207
        cartCount: 0,

208 209 210
        calSkuPriceResult: {

        },
211
          hasCollectionType:0
212

213 214
      };
    },
sin's avatar
sin committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228
    methods: {
      formatPrice(data) {
        return '¥' + (data / 100).toFixed(2);
      },
      formatSkuText(data) { // 渲染已选择的 sku 的文本
        let text = '';
        for (let prop in data) {
          if (prop.indexOf('attr_') === 0) {
            text = text + this.attrValueMap.get(data[prop]) + ' ';
          }
        }
        text = text + 'x ' + data.quantity + ' 件';
        return text;
      },
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
      formatTimeLimitedDiscountText(activity) {
        let text = '';
        let timeLimitedDiscount = activity.timeLimitedDiscount.items[0];
        if (timeLimitedDiscount.preferentialType === 1) {
          text += '减 ' + timeLimitedDiscount.preferentialValue / 100.0 + ' 元';
        } else if (timeLimitedDiscount.preferentialType === 2) {
          text += '打 ' + timeLimitedDiscount.preferentialValue / 10.0 + ' 折';
        }
        if (activity.timeLimitedDiscount.quota > 0) {
          text += '【限购 ' + activity.timeLimitedDiscount.quota + ' 件】';
        }
        return text;
      },
      formatFullPrivilegeText(activity) {
        let text = '';
        let fullPrivilege = activity.fullPrivilege;
        for (let i in fullPrivilege.privileges) {
          let privilege = fullPrivilege.privileges[i];
          if (i > 0) {
            text += ';';
          }
          if (fullPrivilege.cycled) {
            text += '每';
          }
          if (privilege.meetType === 1) {
254
            text += '满 ' + privilege.meetValue / 100.0 + ' 元,';
255 256 257 258 259 260 261 262 263 264 265
          } else if (privilege.meetType === 2) {
            text += '满 ' + privilege.meetValue + ' 件,';
          }
          if (privilege.preferentialType === 1) {
            text += '减 ' + privilege.preferentialValue / 100.0 + ' 元';
          } else if (privilege.preferentialType === 2) {
            text += '打 ' + privilege.preferentialValue / 10.0 + ' 折';
          }
        }
        return text;
      },
266

sin's avatar
sin committed
267 268 269 270
      stepperChange(value) { // 选择 sku 数量时
        this.initialSku.quantity = value;
      },
      skuSelected({skuValue, selectedSku, selectedSkuComb}) { // 选择 sku
271
        // TODO 芋艿,需要改下,禁用用户取消选中。
272 273 274
        // console.log(skuValue);
        // console.log(selectedSku);
        // console.log(selectedSkuComb);
sin's avatar
sin committed
275 276 277
        this.initialSku = {
          ...selectedSkuComb,
          quantity: 1,
278
        };
279 280 281 282 283 284
        // 执行 sku 价格计算
        this.doCalcSkuPrice(this.initialSku.id)
      },
      doCalcSkuPrice(skuId) {
        getCartCalcSkuPrice(skuId).then(data => {
          this.calSkuPriceResult = data;
285 286 287 288
          // 修改 vanSku.list 里匹配的 sku 的价格(目的,将优惠价赋值到其上)
          for (let i in this.vanSku.list) {
            let sku = this.vanSku.list[i];
            if (sku.id === skuId) {
289
              sku.price = data.buyPrice;
290 291 292
              break;
            }
          }
293
        });
sin's avatar
sin committed
294
      },
295 296 297 298 299 300 301 302 303 304 305 306 307 308
        initHasUserSpuFavorite(spuId){
            if (!checkLogin()) {
                this.hasCollectionType = 0;
                return;
            }
            //初始化验证商品收藏
            hasUserSpuFavorite(spuId).then(data => {
                let hasCollection = data;
                // alert("是否收藏==" + hasCollection);
                if (hasCollection) {
                    this.hasCollectionType = 1;
                }
            });
        },
309

sin's avatar
sin committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
      onClickCart() {
        this.$router.push('/cart');
      },
      sorry() {
        Toast('暂无后续逻辑~');
      },
      showPromotion() {
        this.show = true;
      },
      showSku() { // 展示 sku 选择
        this.showBase = true;
      },
      onClickShowTag() {
        this.showTag = true;
      },
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
      onFavoriteClicked(){
        if (!checkLogin()) {
          Dialog.alert({
            title: '系统提示',
            message: '未登陆用户,暂时不支持使用购物车',
          });
          return;
        }
        let id = this.$route.params.id; // 商品编号
          let hasCollectionType = 1; // 收藏类型  默认收藏
          hasUserSpuFavorite(id).then(data => {
              let hasCollection = data;
              // alert("是否收藏==" + hasCollection);
              if (hasCollection){
                  hasCollectionType = 2;
              }

              // alert("hasCollectionType==" + hasCollectionType);
              collectionSpu(id,hasCollectionType).then(data =>{
                  let v = data;
345 346 347 348 349 350 351 352
                  this.hasCollectionType = hasCollectionType;
                  // if (hasCollectionType == 1 && v){
                  //     // alert("商品已收藏");
                  //     this.hasCollectionType = hasCollectionType;
                  // }else if (hasCollectionType == 2 && v){
                  //     // alert("商品已取消");
                  //     this.hasCollectionType = hasCollectionType;
                  // }
353 354 355 356
              })
          });

      },
sin's avatar
sin committed
357
      onBuyClicked(data) {
358
        const { selectedNum } = data;
sin's avatar
sin committed
359 360 361
        this.$router.push({
          path:'/order',
          query:{
362
            goodsId: data.id,
363
            skuId: data.selectedSkuComb.id,
sin's avatar
sin committed
364
            quantity: selectedNum,
365
          }
sin's avatar
sin committed
366 367 368
        });
      },
      onAddCartClicked(data) {
YunaiV's avatar
YunaiV committed
369 370 371 372 373 374 375
        if (!checkLogin()) {
          Dialog.alert({
            title: '系统提示',
            message: '未登陆用户,暂时不支持使用购物车',
          });
          return;
        }
376 377 378 379 380 381 382 383 384 385 386
        const { selectedNum } = data;
        // debugger;
        addCart(data.selectedSkuComb.id,selectedNum).then(data => {
          // 修改购物车数量
          this.cartCount = data;
          // 提示
          Dialog.alert({
            title: '系统提示',
            message: '添加购物车成功',
          });
        });
sin's avatar
sin committed
387 388 389
      },
    },
    mounted() {
390
      // 获得商品数据
sin's avatar
sin committed
391
      let id = this.$route.params.id; // 商品编号
392
      getProductSpuInfo(id).then(data => {
sin's avatar
sin committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
        // 设置 spu
        this.spu = data;
        // 初始化 vanSku
        let vanSku = {
          tree: [],
          list: [],
        };
        for (let i = 0; i < data.skus.length; i++) {
          let sku = data.skus[i];
          // list 商品 sku 信息
          let skuVO = {
            id: sku.id, // skuId,下单时后端需要
            price: sku.price, // 价格(单位分)
            stock_num: sku.quantity // 当前 sku 组合对应的库存
          };
          for (let j = 0; j < sku.attrs.length; j++) {
            let attr = sku.attrs[j];
            skuVO['attr_' + attr.attrId] = attr.attrValueId;
411
          }
sin's avatar
sin committed
412 413 414 415 416 417 418 419 420 421
          vanSku.list.push(skuVO);
          // tree 规格
          for (let j = 0; j < sku.attrs.length; j++) {
            let attr = sku.attrs[j];
            let attrVO;
            for (let k = 0; k < vanSku.tree.length; k++) {
              if (attr.attrName === vanSku.tree[k].k) {
                attrVO = vanSku.tree[k];
                break;
              }
422
            }
sin's avatar
sin committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
            if (!attrVO) { // 未找到,则初始化该规格
              attrVO = {
                k: attr.attrName, // skuKeyName:规格类目名称
                v: [],
                k_s: 'attr_' + attr.attrId,
              };
              vanSku.tree.push(attrVO);
            }
            let attrValueFound = false; // 如果规格值已经存在,则不再添加
            for (let k = 0; k < attrVO.v.length; k++) {
              if (attr.attrValueId === attrVO.v[k].id) {
                attrValueFound = true;
                break;
              }
            }
            if (!attrValueFound) {
              attrVO.v.push({
                id: attr.attrValueId, // skuValueId:规格值 id
                name: attr.attrValueName, // skuValueName:规格值名称
              });
            }
            // 初始化 attrValueMap
            this.attrValueMap.set(attr.attrValueId, attr.attrValueName);
446
          }
447

448
        }
sin's avatar
sin committed
449 450 451 452 453 454
        // debugger;
        this.vanSku = vanSku;
        // 初始化
        // TODO 芋艿,需要处理下第一个有效的 sku
        this.initialSku = vanSku.list[0];
        this.initialSku.quantity = 1;
455 456
        // 执行 sku 价格计算
        this.doCalcSkuPrice(this.initialSku.id);
457 458 459

        this.initHasUserSpuFavorite(id);

sin's avatar
sin committed
460
      });
461
      // 获得购物车数量
YunaiV's avatar
YunaiV committed
462 463 464 465 466
      if (checkLogin()) {
        countCart().then(data => {
          this.cartCount = data;
        })
      }
sin's avatar
sin committed
467 468
    }
  };
sin's avatar
sin committed
469 470 471
</script>

<style lang="less">
sin's avatar
sin committed
472
  .goods {
473
      .active {
474
          color: #f44;
475
      }
sin's avatar
sin committed
476 477 478 479 480 481 482 483
    padding-bottom: 50px;

    &-swipe {
      img {
        width: 7.5rem;
        height: 7.5rem;
        display: block;
      }
sin's avatar
sin committed
484
    }
sin's avatar
sin committed
485 486

    &-tag {
sin's avatar
sin committed
487
      font-size: 12px;
sin's avatar
sin committed
488 489 490 491
      border-top: 1px solid #e5e5e5;

      span {
        margin-right: 10px;
sin's avatar
sin committed
492
      }
sin's avatar
sin committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

      i {
        color: red;
        margin-right: 3px;
      }

      img {
        width: 12px;
        margin-right: 3px;
        margin-top: 6px;
      }
    }

    &-title {
      line-height: 18px;
      padding-top: 10px;
      margin-bottom: 6px;
      font-size: 14px;
      color: #333;
      font-weight: 700;
      border-top: 1px solid #f0f0f0;
    }

    &-subtit {
      font-size: 13px;
      color: #333;
      line-height: 21px;
    }

    &-price {
      color: #f44;
      font-size: 20px;
    }

    &-market-price {
      text-decoration: line-through;
      margin-left: 8px;
sin's avatar
sin committed
530 531 532
      font-size: 13px;
      color: #999;
    }
sin's avatar
sin committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

    &-cell-group {
      margin: 15px 0;

      .van-cell__value {
        color: #999;
      }
    }

    &-info-title {
      height: 44px;
      line-height: 44px;
      text-align: center;
      font-size: 14px;
      font-weight: 700;
      margin: 10px;
      border-top: 1px solid #e5e5e5;
    }

    &-info p {
      margin: 0;
      padding: 0;
      margin-block-end: 0;
      margin-block-start: 0;
      display: grid;
    }

    &-info img {
sin's avatar
sin committed
561
      width: 100%;
sin's avatar
sin committed
562
    }
sin's avatar
sin committed
563
  }
sin's avatar
sin committed
564
</style>