GoodSkuSelect.vue
21 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
530
531
532
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
<!--
* @FileDescription: sku选择器(花呗、拼团下单)
* @Author: kahu
* @Date: 2022/11/7
* @LastEditors: kahu
* @LastEditTime: 2022/11/7
-->
<template>
<div class="content">
<u-popup
v-model="goodsDetailShowFlag"
mode="bottom"
border-radius="14"
>
<view class="goosDetailshow-box">
<view class="detailImg-box flex-row-plus">
<image
class="detailImg"
:src="$baseURL+selectedSku.image"
></image>
<view class="flex-column-plus mar-left-40">
<view class="" style="color: #f4281e;">
<label class="fs24">¥</label>
<label
class="fs36 mar-left-10"
v-text="selectedSku.activityType === 1 && btnType === 4 ? selectedSku['salePrice'] : selectedSku.price"
></label>
</view>
<label class="fs24 font-color-999 mar-top-20">库存 {{ selectedSku.stockNumber }} 件</label>
<label class="fs24 mar-top-20">已选</label>
</view>
</view>
<view class="color-box flex-column-plus">
<view
v-for="(skuRowItem,skuRowIndex) in productData['names']"
:key="skuRowIndex"
>
<label
class="fs26 font-color-333"
v-if="skuRowItem['nameCode']"
>{{ skuRowItem.skuName }}</label>
<view class="colorName-box">
<view
class="pad-bot-30"
v-for="(skuColItem, skuColIndex) in skuRowItem.values"
:key="skuColIndex"
>
<view
class="colorName"
:class="{'colorName-on' : selectedAttr[skuRowItem['nameCode']] === skuColItem.valueCode}"
@click="handleClickSkuItem(skuRowItem['nameCode'], skuColItem.valueCode)"
>
{{ skuColItem.skuValue }}
</view>
</view>
</view>
</view>
</view>
<view class="goodsNumCent">
<view
class="goodsNum-box flex-row-plus flex-sp-between"
:class="{'bottom-line' :aliAgingObj.supportHanaUta}"
>
<label class="font-color-333 fs26">数量</label>
<view class="goodsNum">
<view
class="item subtract"
@click="handleNumSub"
>-
</view>
<view
class="item goodsNumber"
v-model="buyNum"
>{{ buyNum }}
</view>
<view
class="item add"
@click="handleNumAdd"
>+
</view>
</view>
</view>
</view>
<!-- 花呗分期 -->
<view
class="huabei-box flex-column-plus"
v-if="aliAgingObj.supportHanaUta"
>
<label class="font-color-999 fs24">花呗分期</label>
<scroll-view
class="fenqi-box"
scroll-x="true"
>
<view
class="huabei-item"
:class="[{'fenqi-on' :aliAgingObj.selectIndex === 0},{'disabled' :aliAgingObj.disableSelectList[0]}]"
@click="handleSelectAliAging(0)"
>
<label class="huabei-period">分3期(含手续费)</label>
<label class="huabei-money">¥{{ aliAgingObj.agingMoneyList[0] }}/期</label>
</view>
<view
class="huabei-item"
:class="[{'fenqi-on' :aliAgingObj.selectIndex === 1},{'disabled' :aliAgingObj.disableSelectList[1]}]"
@click="handleSelectAliAging(1)"
>
<label class="huabei-period">分6期(含手续费)</label>
<label class="huabei-money">¥{{ aliAgingObj.agingMoneyList[1] }}/期</label>
</view>
<view
class="huabei-item"
:class="[{'fenqi-on' :aliAgingObj.selectIndex === 2},{'disabled' :aliAgingObj.disableSelectList[2]}]"
@click="handleSelectAliAging(2)"
>
<label class="huabei-period">分12期(含手续费)</label>
<label class="huabei-money">¥{{ aliAgingObj.agingMoneyList[2] }}/期</label>
</view>
</scroll-view>
</view>
<view
v-if="btnType===6"
class="skuSelectBtn"
>
<view
class="flex-row-plus offShelf"
v-if="productData.shelveState === 0"
>
商品已下架
</view>
<view
class="flex-row-plus flex-items flex-sp-around"
v-else-if="selectedSku.activityType === 1"
>
<view
class="selectJoinShop selectBtn font-color-333"
@click="handleBuyNow"
>单独购买
</view>
<view
class="selectBuyNow selectBtn font-color-FFEBC4"
@click="handleBuyWithGroup(1)"
>我要开团
</view>
</view>
<view
class="flex-row-plus flex-items flex-sp-around"
v-else
>
<view
class="selectJoinShop selectBtn"
@click="handleAddCart"
>加入购物车
</view>
<view
class="selectBuyNow selectBtn"
@click="handleBuyNow"
>立即购买
</view>
</view>
</view>
<view v-else>
<view
v-if="selectedSku.activityType === 1 && collageId !== 0"
class="goosDetailbut-box flex-items-plus"
:style="{'padding-bottom':(isIphone === true? 60:20)+'rpx'}"
>
<view
class="joinbuyBut"
@click="handleBuyWithGroup(2)"
>确定
</view>
</view>
<view
v-else-if="selectedSku.activityType === 1 && btnType === 3"
class="goosDetailbut-box flex-row-plus"
:style="{'padding-bottom':(isIphone === true? 60:20)+'rpx'}"
>
<view
class="buyNowBut"
@click="handleBuyWithGroup(1)"
>去拼团
</view>
</view>
<view
v-else
class="goosDetailbut-box flex-row-plus"
:style="{'padding-bottom':(isIphone === true? 60:20)+'rpx'}"
>
<view
v-if="btnType === 1"
class="buyNowBut"
@click="handleAddCart"
>确认
</view>
<view
v-else
class="buyNowBut"
@click="handleBuyNow(2)"
>确认
</view>
</view>
</view>
</view>
</u-popup>
</div>
</template>
<script>
import NET from "../../../utils/request";
import API from "../../../config/api";
export default {
name: "GoodSkuSelect",
data() {
return {
// 是否展示SKU弹窗
goodsDetailShowFlag: false,
// 已经选中的valueCode key => value names.nameCode=>values.valueCode 处理选中渲染
selectedAttr: {},
// 当前选中的skuMap对象(服务端数据)
selectedSku: {},
// 1加入购物车 2立即购买 3开团 4单独购买 6SKU选择
btnType: 0,
buyNum: 1,
// 花呗对象
aliAgingObj: {
supportHanaUta: false,
selectIndex: -1,
disableSelectList: [true, true, true],
agingMoneyList: ["0.00", "0.00", "0.00"]
}
}
},
props: {
productData: {
type: Object,
default: () => ({})
},
isIphone: {
type: Boolean,
default: () => false
},
collageId: {
type: Number,
default: () => 0
}
},
methods: {
/**
* 当前SKU数量减少
*/
handleNumSub() {
if (this.buyNum > 1) {
this.buyNum = this.buyNum - 1
} else {
uni.showToast({
title: '亲!至少一件哦!',
icon: "none"
})
}
},
/**
* 当前SKU数量加
*/
handleNumAdd() {
if (this.buyNum < this.selectedSku.stockNumber) {
this.buyNum = this.buyNum + 1
} else {
uni.showToast({
title: '库存不足!',
icon: "none"
})
}
},
/**
* 根据skuId选择SKU
* @param skuId SkuId
*/
handleSelectBySkuId(skuId) {
if (!skuId) return
// 当前商品后端返回的所有sku的排列组合
const allSkuValueGroupMap = this.productData.map
for (const allSkuValueGroupMapKey in allSkuValueGroupMap) {
if (parseInt(allSkuValueGroupMap[allSkuValueGroupMapKey].skuId) !== parseInt(skuId)) continue;
this.selectedSku = allSkuValueGroupMap[allSkuValueGroupMapKey]
this.echoFatherRowText(this.productData, this.selectedSku, this.buyNum)
// 控制组件选中渲染
const selectValueCodes = this.selectedSku['valueCodes'].split(',')
for (const skuRow of this.productData.names) {
for (const skuCol of skuRow.values) {
if (!selectValueCodes.includes(skuCol['valueCode'])) continue;
this.$set(this.selectedAttr, skuRow['nameCode'], skuCol['valueCode'])
break; // 一行的sku只会有一个value
}
}
}
},
/**
* 点击sku的一项
* @param nameCode SKU行的nameCode
* @param valueCode SKU列的valueCode
* nameCodeValueCodeClick
*/
handleClickSkuItem(nameCode, valueCode) {
// 当前选中
this.$set(this.selectedAttr, nameCode, valueCode)
// 获取到所有的sku的values.valueCode
let values = []
for (const key in this.selectedAttr) {
values.push(this.selectedAttr[key])
}
// 当前选中的sku的key组合
// 后端返回的productData.map中,排列组合了所有values[].valueCode的情况,使用逗号分隔
const nowSelectSkuValueGroupKey = values.join(',') // 相较于allSkuValueGroupMap的key
// 后端返回的所有sku组合(values.valueCode)
const allSkuValueGroupMap = this.productData.map
// 遍历后端数据
for (const allSkuValueGroupMapKey in allSkuValueGroupMap) {
// 当和当前选中的sku一致
if (nowSelectSkuValueGroupKey === allSkuValueGroupMapKey) {
this.selectedSku = allSkuValueGroupMap[allSkuValueGroupMapKey]
this.echoFatherRowText(this.productData, this.selectedSku, this.buyNum)
}
}
},
/**
* 回显父组件(通讯)
* @param productData 当前商品对象
* @param skuItem 当前选中的sku的后端数据
* @param buyNum 当前sku购买数量
*/
echoFatherRowText(productData, skuItem, buyNum) {
// 获取到当前选中的sku的valueCode
const currentSku = []
// 取出所有的valueCode
const nowSelectValueCodeList = skuItem['valueCodes'].split(',')
const skuRows = productData.names
for (const skuRow of skuRows) {
const skuValues = skuRow.values
for (const skuValue of skuValues) {
if (!nowSelectValueCodeList.includes(skuValue.valueCode)) continue;
const currentSkuItem = {skuText: ''}
if (skuValue.valueCode === '单款项') {
currentSkuItem['skuText'] = skuValue['skuValue']
} else {
currentSkuItem['skuText'] = `${ skuValue.skuName }:${ skuValue.skuValue }`
}
currentSku.push(currentSkuItem)
break; // 只会对应一个value数据,找到就break减少循环
}
}
this.$emit('getCurrentSku', {
skuItem,
currentSku,
buyNum
})
// 选中sku之后,做一些相应的操作
// postSelectSku依赖于getCurrentSku的数据
this.$emit('postSelectSku')
},
//加入购物车
async handleAddCart() {
this.handleCheckIsLogin()
if (this.selectedSku.stockNumber < 1) {
return uni.showToast({
title: '库存不足',
icon: "none"
})
}
// uni.showLoading({
// mask: true,
// title: '添加中...',
// })
try {
const postData = {
skuId: this.selectedSku.skuId,
number: this.buyNum,
}
await NET.request(API.ShoppingaddCart, postData, 'POST')
// 埋点
this.$store.dispatch('doPointer', {
eventType: 2,
productIds: this.productId
})
// 给购物车小图标赋值数量
let newAllCartNum = uni.getStorageSync('allCartNum') + this.buyNum
uni.setStorageSync('allCartNum', newAllCartNum)
this.$emit('changeCartNum',newAllCartNum)
uni.showToast({
title: '添加成功',
icon: 'none'
})
setTimeout(()=>{
this.buyNum = 1
this.goodsDetailShowFlag = false
},2000)
}finally {
uni.hideLoading()
}
},
/**
* 立即购买(下单)
*/
handleBuyNow() {
this.handleCheckIsLogin()
if (this.selectedSku.stockNumber < 1) {
return uni.showToast({
title: '库存不足',
icon: "none"
})
}
if (this.buyNum > this.selectedSku.stockNumber && this.selectedSku.stockNumber !== 0) {
return uni.showToast({
title: '已超出最大数量限制',
icon: "none"
})
}
//组装后端数据
let list = [{
ifWork: 0,
shopId: this.productData.shopId,
shopName: this.productData.shopName,
shopDiscountId: this.shopDiscountId > 0 ? this.shopDiscountId : null,
shopSeckillId: this.shopSeckillId > 0 ? this.shopSeckillId : null,
skus: [{
productId: this.productData.productId,
skuId: this.selectedSku.skuId,
productName: this.productData.productName,
image: this.selectedSku.image,
price: this.selectedSku.price,
weight: 0,
number: this.buyNum,
SKU: "",
total: this.selectedSku.price * this.buyNum,
ifLogistics: 1
}]
}]
uni.setStorageSync('skuItemDTOList', list)
this.buyNum = 1
this.goodsDetailShowFlag = false
uni.navigateTo({
url: '../orderModule/orderConfirm?type=1',
})
},
/**
* 拼团下单
* @param type 1单独开团2拼团
*/
handleBuyWithGroup(type) {
this.handleCheckIsLogin()
if (this.selectedSku.stockNumber < 1) {
return uni.showToast({
title: '库存不足',
icon: "none"
})
}
const data = {
number: this.buyNum,
productId: this.productId,
shopId: this.shopId,
skuId: this.selectedSku.skuId,
shopGroupWorkId: this.selectedSku.shopGroupWorkId,
type: type
}
if (type !== 1) {
data.collageId = this.collageId
}
uni.removeStorageSync("skuItemDTOList")
uni.setStorageSync('skuItemList', data)
this.goodsDetailShowFlag = false
this.buyNum = 1
uni.navigateTo({
url: '/pages_category_page1/orderModule/orderConfirm?type=1',
})
},
/**
* 是否登录
* @return {boolean|void}
*/
handleCheckIsLogin() {
const userInfo = uni.getStorageSync('storage_key')
if (!userInfo || (userInfo && JSON.stringify(userInfo) === '{}')) {
return uni.navigateTo({
url: '../../pages_category_page2/userModule/login'
})
}
return true
},
/**
* 选择分期
* @param index 0:3期 1:6期 2:12期
*/
handleSelectAliAging(index) {
const aliAgingObj = this.aliAgingObj
if (aliAgingObj.selectIndex !== index && !aliAgingObj.disableSelectList[index]) {
aliAgingObj.selectIndex = index;
} else {
aliAgingObj.selectIndex = -1;
}
},
/**
* 渲染是否支持花呗
* @param productData 当前商品
* @param skuPrice:number 选中SKU的价格
*/
handleRenderAliAging(productData, skuPrice) {
if (productData.ifHuabei !== 1) return
const aliAgingObj = this.aliAgingObj
if (skuPrice && skuPrice >= 0.03) {
aliAgingObj.supportHanaUta = true
aliAgingObj.disableSelectList[0] = false;
aliAgingObj.agingMoneyList[0] = (parseInt((skuPrice / 3 * 100) / 100 + '')).toFixed(2) + ""
}
if (skuPrice && skuPrice >= 0.06) {
aliAgingObj.supportHanaUta = false;
aliAgingObj.disableSelectList[1] = false;
aliAgingObj.agingMoneyList[1] = (parseInt((skuPrice / 6 * 100) / 100 + '')).toFixed(2) + ""
}
if (skuPrice && skuPrice >= 0.12) {
aliAgingObj.supportHanaUta = true;
aliAgingObj.disableSelectList[2] = false;
aliAgingObj.agingMoneyList[2] = (parseInt((skuPrice / 12 * 100) / 100 + '')).toFixed(2) + ""
}
},
}
}
</script>
<style
lang="scss"
scoped
>
.goosDetailshow-box {
margin-bottom: -5upx;
.detailImg-box {
margin-top: 30upx;
margin-left: 30upx;
border-bottom: 1upx solid #EDEDED;
padding-bottom: 20upx;
width: 690upx;
.detailImg {
width: 180upx;
height: 180upx;
}
}
.color-box {
padding: 30upx 30upx;
width: 690upx;
.colorName-box {
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: flex-start;
align-items: center;
margin-top: 30upx;
margin-left: -30upx;
.colorName {
background-color: #FFFFFF;
margin-left: 30upx;
padding: 10upx 32upx;
font-size: 26upx;
border: 2rpx solid #E4E5E6;
z-index: 2;
color: #333333;
}
.colorName-on {
border-radius: 6rpx;
background-color: #e8f2ea;
color: #39a03e;
margin-left: 30upx;
padding: 10upx 32upx;
font-size: 26upx;
text-align: center;
z-index: 1;
border: none;
}
}
}
.modelNum-box {
padding: 30upx 30upx;
border-bottom: 1upx solid #EDEDED;
width: 690upx;
.modelNumName-box {
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: flex-start;
align-items: center;
margin-top: 30upx;
margin-left: -30upx;
.modelNumName-on {
background-color: #FFE4D0;
color: #FF7800;
margin-left: 30upx;
padding: 10upx 32upx;
border-radius: 28upx;
border: 1upx solid #FF7800;
font-size: 26upx;
text-align: center;
}
.modelNumName {
background-color: #F5F5F5;
margin-left: 30upx;
padding: 10upx 32upx;
border-radius: 28upx;
font-size: 26upx;
}
}
}
.goodsNumCent {
padding: 0 30upx;
.goodsNum-box {
width: 100%;
padding: 30rpx 0 180rpx 0;
border-top: 2rpx solid #EDEDED;
.goodsNum {
height: 50upx;
display: flex;
align-items: center;
.item {
width: 50upx;
height: 50upx;
line-height: 48rpx;
border: 1upx solid #999999;
text-align: center;
}
.subtract {
border-right: 0upx;
}
.goodsNumber {
line-height: 50rpx;
}
.add {
border-left: 0upx;
}
}
}
}
.bottom-line {
border-bottom: 1upx solid #EDEDED;
}
.huabei-box {
padding: 30upx 30upx;
width: 690upx;
.fenqi-box {
margin-top: 15upx;
width: 120%;
.huabei-item {
display: inline-block;
background: #f3f3f3;
padding: 16upx 24upx;
margin: 5upx 10upx;
border-radius: 15upx;
text-align: center;
font-size: 7upx;
.huabei-period {
display: block;
}
}
.fenqi-on {
border: 1px solid #EF7F93;
color: #EF7F93;
}
.disabled {
color: #cacaca;
}
}
}
.goosDetailbut-box {
justify-content: center;
.joinShopCartBut {
width: 343upx;
height: 80upx;
border-radius: 40upx 0 0 40upx;
background-color: #FFC300;
color: #FFFEFE;
font-size: 28upx;
line-height: 80upx;
text-align: center;
margin-left: 30upx;
}
.buyNowBut {
width: 90%;
height: 90upx;
background-color: #333333;
font-size: 28upx;
line-height: 90upx;
text-align: center;
color: #FFEBC4;
}
}
}
.skuSelectBtn {
padding-bottom: 30rpx;
.selectBtn {
width: 342rpx;
height: 100rpx;
line-height: 100rpx;
text-align: center;
border: 2rpx solid #39be7a;
font-size: 28rpx;
border-radius: 8rpx;
color: #39be7a;
}
.selectBuyNow {
background: #39be7a;
color: #fff;
}
}
.couponItemimg {
width: 150upx;
height: 60upx;
}
.joinbuyBut {
width: 190upx;
height: 80upx;
background: #333333;
color: #FFEBC4;
font-size: 28upx;
line-height: 80upx;
text-align: center;
margin-left: 30upx;
}
</style>