detail.vue 14.0 KB
Newer Older
sin's avatar
sin committed
1 2 3 4
<template>
  <div class="goods">
     <headerNav title="商品详情"/>
    <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 7 8 9 10 11
        <img :src="thumb" >
      </van-swipe-item>
    </van-swipe>

    <van-cell-group>
      <van-cell>
12 13 14 15
        <span class="goods-price">{{ formatPrice(initialSku.price) }}</span>
        <!--<span class="goods-market-price">{{ formatPrice(goods.market_price) }}</span>-->
        <div class="goods-title">{{ spu.name }}</div>
        <div class="goods-subtit">{{spu.sellPoint}}</div>
sin's avatar
sin committed
16 17
      </van-cell>
      
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
      <!--<van-cell   @click="onClickShowTag" class="goods-tag" >-->
        <!--<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>-->
      <!--</van-cell>   -->
sin's avatar
sin committed
36 37
    </van-cell-group>

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    <!--<van-cell-group class="goods-cell-group">-->
      <!--<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>-->
      <!---->
      <!--<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>-->
    <!--</van-cell-group>-->
sin's avatar
sin committed
57 58 59 60 61
    
    <van-cell-group class="goods-cell-group">
      <van-cell  is-link @click="showSku" >
        <template slot="title">
            <span style="margin-right: 10px;">已选</span>
62
            <span>{{ formatSkuText(initialSku) }}</span>
sin's avatar
sin committed
63 64 65 66 67 68 69
        </template>
      </van-cell>
      
    </van-cell-group>
    
    <div class="goods-info">
        <p class="goods-info-title" >图文详情</p>
70
        <div v-html="spu.description"></div>
sin's avatar
sin committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    </div>
    <van-goods-action>
        
      <van-goods-action-mini-btn icon="like-o" @click="sorry">
        收藏
      </van-goods-action-mini-btn>
      <van-goods-action-mini-btn icon="cart" @click="onClickCart">
        购物车
      </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>
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    <!--<van-actionsheet v-model="show" title="促销" style="font-size:14px;">-->
        <!---->
            <!--<van-cell  is-link @click="sorry" >-->
                <!--<template slot="title">-->
                    <!--<van-tag type="danger">多买优惠</van-tag>-->
                    <!--<span> 满2件,总价打9折</span>-->
                <!--</template>-->
            <!--</van-cell>-->
            <!--<van-cell  is-link @click="sorry" >-->
                <!--<template slot="title">-->
                    <!--<van-tag type="danger">满减</van-tag>-->
                    <!--<span> 满100元减50元</span>-->
                <!--</template>-->
            <!--</van-cell>-->
            <!--<van-cell  is-link @click="sorry" >-->
                <!--<template slot="title">-->
                    <!--<van-tag type="danger">限购</van-tag>-->
                    <!--<span> 购买不超过5件时享受单件价¥8.00,超出数量以结算价为准</span>-->
                <!--</template>-->
            <!--</van-cell>-->
    <!--</van-actionsheet>-->
sin's avatar
sin committed
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
    <!--<van-actionsheet v-model="showTag" title="服务说明" style="font-size:14px;">-->
        <!---->
            <!--<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>-->
    <!--</van-actionsheet>-->

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

sin's avatar
sin committed
164 165 166 167 168
  </div>
</template>

<script>
import skuData from '../../data/sku';
169
import { getProductSpuInfo } from '../../api/product';
sin's avatar
sin committed
170 171 172 173 174 175 176

export default {
  components: {
  },
  data() {
    this.skuData = skuData;
    return {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      spu: {}, // 商品信息
      vanSku: {
        tree: [], // 规格数组
        list: [], // sku 数组
      },
      // TODO 后面,要加 sku 的 title 和 picture
      vanSpu: {
        title: '',
        picture: '',
      },
      initialSku: { // 选中的 sku
        // 具体规格
        // price 价格
        // quantity 选中的数量
      },
      attrValueMap: new Map(), // 规格值的映射

sin's avatar
sin committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      show:false,
      showTag:false,
      goods: {
        title: '【每日一粒益智又长高】 Lifeline Care 儿童果冻鱼油DHA维生素D3聪明长高 软糖 30粒 2件装',
        subtitle:'【品牌直采】Q弹美味,无腥味果冻鱼油,每粒含足量鱼油DHA,帮助视网膜和大脑健康发育,让你的宝宝明眼又聪明,同时补充400国际单位维生素D3,强壮骨骼和牙齿。特含DPA,让宝宝免疫力更强,没病来扰。',
        price: 2680,
        market_price:9999,
        express: '免运费',
        remain: 19,
        thumb: [
          'https://img.yzcdn.cn/public_files/2017/10/24/e5a5a02309a41f9f5def56684808d9ae.jpeg',
          'https://img.yzcdn.cn/public_files/2017/10/24/1791ba14088f9c2be8c610d0a6cc0f93.jpeg'
        ],
        info:'<p style="text-align:center;"><img src="https://haitao.nosdn2.127.net/ac19460151ee4d95a6657202bcfc653c1531470912089jjjq8ml410763.jpg" ></p><p style="text-align:center;"><img src="https://haitao.nos.netease.com/2a91cfad22404e5498d347672b1440301531470912182jjjq8mnq10764.jpg" ></p><p style="text-align:center;"><img src="https://haitao.nos.netease.com/caddd5a213de4c1cb1347c267e8275731531470912412jjjq8mu410765.jpg" ></p>',
      },
209 210 211 212
      showBase: false, // 是否显示 sku 坦诚
      closeOnClickOverlay: true, // 是否在点击蒙层后关闭
      hideStock: true, // 是否显示商品剩余库存

sin's avatar
sin committed
213 214 215 216 217 218
    };
  },
  methods: {
    formatPrice(data) {
      return '¥' + (data / 100).toFixed(2);
    },
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    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;
    },

    stepperChange(value) { // 选择 sku 数量时
      this.initialSku.quantity = value;
    },
    skuSelected({skuValue, selectedSku, selectedSkuComb}) { // 选择 sku
      console.log(skuValue);
      console.log(selectedSku);
      console.log(selectedSkuComb);
      this.initialSku = {
        ...selectedSkuComb,
        quantity: 1,
      };
    },

sin's avatar
sin committed
243 244 245 246 247 248 249 250 251
    onClickCart() {
      this.$router.push('/cart');
    },
    sorry() {
      Toast('暂无后续逻辑~');
    },
    showPromotion() {
        this.show=true;
    },
252
    showSku() { // 展示 sku 选择
sin's avatar
sin committed
253 254 255 256 257 258 259 260 261 262 263
        this.showBase=true;
    },
    onClickShowTag(){
        this.showTag=true;
    },
    onBuyClicked(data) {
      this.$toast(JSON.stringify(data));
    },
    onAddCartClicked(data) {
      this.$toast(JSON.stringify(data));
    },
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
  },
  mounted() {
    let id = this.$route.params.id; // 商品编号
    let response = getProductSpuInfo(id);
    response.then(data => {
      // 设置 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;
        }
        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;
            }
          }
          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);
        }
      }
      // debugger;
      this.vanSku = vanSku;
      // 初始化
      // TODO 芋艿,需要处理下第一个有效的 sku
      this.initialSku = vanSku.list[0];
      this.initialSku.quantity = 1;
    });
sin's avatar
sin committed
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
  }
};
</script>

<style lang="less">
.goods {
  padding-bottom: 50px;
  &-swipe {
    img {
      width: 7.5rem;
      height: 7.5rem;
      display: block;
    }
  }
  &-tag{
      font-size: 12px;
        border-top: 1px solid #e5e5e5;
        span{
      margin-right: 10px;
        }
        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;
    font-size: 13px;
    color: #999;
  }
  &-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{
      width: 100%;
  }
}
</style>