goodsDetail.js
19.8 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
import wx from '../../utils/wx';
import event from '../../common/event';
import moment from '../../vendors/moment.min';
import {getUserInfoLogin} from '../../common/login';
import formatImage from '../../utils/formatImage';
import detailModel from '../../models/product/detail';
import config from '../../config';
import Yas from '../../common/yas';
import {getGoodInfo, getGoodSize} from '../../utils/productDetail';
import Picker from './picker/picker';
import {Actionsheet} from '../../vendors/zanui/index';
let app = getApp();
let router = global.router;
let {windowHeight} = app.getSystemInfo();
let yas;
Page(Object.assign({
data: {
login: false,
shopId: 0,
shopInfo: {},
productSkn: 0,
countInCart: 0,
productName: '',
marketPrice: '',
salesPrice: '',
productImages: [],
storage_sum: 0,
buyImmediate: false,
loadingForPayment: false,
selectedSKU: 0,
colorSelected: false,
productSupportServiceList: [],
productDesc: [],
productPhrare: [],
productSizeTitle: [],
productSizeContent: [],
productMeasurementImage: [],
productWashTipsBoList: [],
productMaterialList: [],
productIntroImageList: [],
pickerData: {
view: {
isShow: false,
isSoldOutSoon: false,
goodsList: [],
sizeList: [],
image: '',
goodPrice: '',
price: '',
buyNumber: 1,
minusButtonEnable: false,
plusButtonEnable: true,
buyButtonEnable: true,
buy_limit_number: 0,
bundle_count: 0
},
sourceType: 'goodsDetail'
},
showSnapshootShare: false,
snapshootShareData: {},
actionSheet: {
componentId: 'shareActionSheet',
show: false,
closeOnClickOverlay: true,
cancelText: '取消',
actions: [
{
name: '分享给朋友',
className: 'action-class',
loading: false,
openType: 'share'
}
]
},
preferList: [],
// 促销
proList: [],
proTypes: [],
expandPro: false,
showMenu: false,
showBackTop: false,
showBottomTipView: false,
productOnlyProvideByYohoAPP: false,
productOnlyProvideByYohoStore: false,
productNotForSale: false,
},
getUserInfoLogin,
onLoad: function(options) {
event.on('user-login-success', () => {
this.setData({
login: !!app.getUid()
});
});
let productSkn = options.productSkn ? options.productSkn : '';
if (!productSkn) {
productSkn = options.product_skn ? options.product_skn : '';
}
this.setData({productSkn});
this.productInfo();
setTimeout(() => {
this.supportService();
this.productIntro();
this.preferList();
this.getPromotion();
this._getShopInfo();
this._getCountInCart();
}, 500);
yas = new Yas(app);
yas.pageOpenReport();
},
onShow: function() {
this.setData({
login: !!app.getUid()
});
},
onPageScroll: function({scrollTop}) {
const showMenu = scrollTop > 50;
const show = scrollTop > windowHeight * 2;
if (show !== this.data.showBackTop) {
this.setData({
showBackTop: show
});
}
if (showMenu !== this.data.showMenu) {
this.setData({
showMenu: showMenu
});
}
},
backTop: function() {
wx.pageScrollTo({
scrollTop: 0
});
},
toShop: function() {
router.go('home');
},
onShareAppMessage: function(res) {
let params = {
FROM: res.from,
SHARE_RESUIL: 0,
TITLE: this.data.productName,
DESC: `我在${app.getMiniappName()}发现一个不错的商品赶快来看看吧!`,
PATH: `/pages/goodsDetail/goodsDetail?productSkn=${this.data.productSkn}`,
};
// 用户点击右上角分享
return {
title: params.TITLE, // 分享标题
desc: params.DESC, // 分享描述
path: params.PATH, // 分享路径
success: function() {
params.SHARE_RESUIL = 1;
yas.report('YB_SHARE_RESULT_L', params);
},
fail: function() {
params.SHARE_RESUIL = 2;
yas.report('YB_SHARE_RESULT_L', params);
}
};
},
share: function() {
this.setData({
'actionSheet.show': true
});
},
handleZanActionsheetCancel() {
this.setData({
'actionSheet.show': false
});
},
// 当行动按钮中有一个被点击时触发
// index 代表被点击按钮在传入参数 actions 中的位置
handleZanActionsheetClick({index}) {
this.setData({
'actionSheet.show': false
});
if (index === 1) {
this.setData({
showSnapshootShare: true
});
}
},
hideSnap: function() {
this.setData({
showSnapshootShare: false
});
},
preferList: function() {
detailModel.preference({
page: 1,
limit: 20,
shopId: app.getShopId(),
product_skn: this.data.productSkn
}).then(res => {
if (res.code === 200) {
const keyAdapter = {
skn: 'product_skn',
salesPrice: 'sales_price',
marketPrice: 'market_price',
productName: 'product_name',
defaultImages: 'default_images'
};
let list = [];
(res.data.product_list || []).forEach(product => {
let item = {};
Object.keys(keyAdapter).forEach(key => {
item[key] = product[keyAdapter[key]];
});
list.push(item);
});
this.setData({
preferList: list
});
}
});
},
productInfo: function() {
detailModel.productInfo(this.data.productSkn)
.then(res => {
if (res.code === 200) {
let ImgUrls = [];
let color_length = 0;
if (res.data.goods_list) {
color_length = res.data.goods_list.length;
}
res.data.goods_list.map((item, index) => {
let images = item.images_list;
ImgUrls = [...ImgUrls, ...images];
if (index === 0 && color_length === 1) {
item.selected = true;
this.data.colorSelected = true;
this.data.selectedProductId = item.goods_id;
}
});
let isSoldOutSoon = false;
let tags = res.data.tags;
if (tags) {
isSoldOutSoon = tags.includes('is_soon_sold_out');
}
// pickerData
let pickData = this.data.pickerData;
pickData.view.goodsList = res.data.goods_list;
pickData.view.isSoldOutSoon = isSoldOutSoon;
// 默认显示第一种颜色的尺码
pickData.view.sizeList = res.data.goods_list.length ? res.data.goods_list[0].size_list : [];
let plusButtonEnable = false;
let minusButtonEnable = false;
// 根据库存设置该尺码是否可以选择
let size_length = 0;
if (pickData.view.sizeList) {
size_length = pickData.view.sizeList.length;
}
pickData.view.sizeList && pickData.view.sizeList.map((item, index) => {
item.enable = item.storage_number > 0;
if (index === 0 && size_length === 1 && this.data.colorSelected) {
// 当颜色被选中 并且只有一个size 时 才会自动 做选中尺码操作
pickData.view.buyButtonEnable = item.enable;
if (item.enable) {
item.selected = true;
plusButtonEnable = item.storage_number > 1;
this.setData({
selectedSKU: item.product_sku,
storageNumber: item.storage_number
});
}
}
});
pickData.view.plusButtonEnable = plusButtonEnable;
pickData.view.minusButtonEnable = minusButtonEnable;
pickData.view.image = formatImage.image(ImgUrls.length > 0 ? ImgUrls[0].image_url : '', 168, 232);
pickData.view.price = res.data.format_market_price;
pickData.view.goodPrice = res.data.format_sales_price;
let default_image = formatImage.image(ImgUrls.length > 0 ? ImgUrls[0].image_url : '', 750, 1000);
let product_qrCode = config.domains.api + '/wechat/miniapp/img-check.jpg?param=' + res.data.product_skn; // eslint-disable-line
let snapData = {
product_name: res.data.product_name,
product_price: '¥' + res.data.sales_price,
default_image,
product_qrCode,
product_skn: this.data.productSkn
};
this.setData({
productName: res.data.product_name,
marketPrice: res.data.format_market_price,
salesPrice: res.data.format_sales_price,
productImages: ImgUrls,
pickerData: pickData,
storage_sum: res.data.storage_sum,
snapshootShareData: snapData,
shopId: res.data.shop_id,
brandId: res.data.brand_id,
productOnlyProvideByYohoAPP: res.data.is_secKill === 'Y' || res.data.is_advance === 'Y' ||
res.data.attribute === 3 || res.data.attribute === 4 || res.data.isLimitBuy || res.data.is_deposit_advance === 'Y', // eslint-disable-line
productOnlyProvideByYohoStore: res.data.store_show_status === 3 || res.data.store_show_status === 4, // eslint-disable-line
productNotForSale: res.data.attribute === 2,
showBottomTipView: true
});
}
});
},
supportService: function() {
let params = {
product_skn: this.data.productSkn,
coupon_limit_status: 0
};
detailModel.supportService(params)
.then(res => {
this.setData({
productSupportServiceList: res.data
});
});
},
productIntro: function() {
let params = {
productskn: this.data.productSkn
};
detailModel.productIntro(params)
.then(data => {
let infoList = data.productDescBo;
let productPhrare = data.productDescBo.phrase;
let size = data.sizeInfoBo;
size = getGoodSize(size);
infoList = getGoodInfo(infoList);
this.setData({
productDesc: infoList,
productPhrare,
productSizeTitle: size.titleList,
productSizeContent: size.sizeList,
productMeasurementImage: data.sizeImage,
productWashTipsBoList: data.washTipsBoList,
productMaterialList: data.productMaterialList,
productIntroImageList: data.productIntroBo.imageList
});
});
},
buyImmediate: function() {
if (this.data.productOnlyProvideByYohoAPP || this.data.productOnlyProvideByYohoStore || this.data.productNotForSale) { // eslint-disable-line
return wx.showToast({
title: '该商品暂时不支持当前小程序购买,请到Yoho!Buy有货查看',
icon: 'none',
duration: 2000
});
}
if (this.data.storage_sum === 0) {
return;
}
this.setData({buyImmediate: true});
if (app.getUid()) {
Picker.pickerShow(this);
}
},
hidePicker: function(e) {
if (e.target.id === 'picker-bg') {
Picker.pickerHide(this);
}
},
chooseColor: function(e) {
let pickData = this.data.pickerData;
// 先遍历将所有颜色、尺码置为未选中状态
pickData.view.goodsList && pickData.view.goodsList.map(item => {
item.selected = false;
item.size_list.map(subItem => {
subItem.selected = false;
subItem.enable = subItem.storage_number > 0;
});
});
// 将当前颜色置为选中状态
let tempItem = e.target.dataset.key;
// 遍历颜色尺码,根据库存切换显示状态
tempItem && tempItem.size_list && tempItem.size_list.map(item => {
item.enable = item.storage_number > 0;
});
tempItem.selected = true;
this.data.selectedProductId = tempItem.goods_id;
// 设置标志位,表示已经选择颜色
let colorSelected = true;
// 重置库存、当前选中sku的值
pickData.storageNumber = 0;
pickData.selectedSKU = 0;
this.data.selectedSKU = 0;
pickData.view.goodsList[Number(e.target.id)] = tempItem;
pickData.view.sizeList = tempItem.size_list;
pickData.view.minusButtonEnable = false;
// 将购买数量设为初始状态
pickData.view.buyNumber = 1;
this.setData({
colorSelected,
pickerData: pickData
});
},
chooseSize: function(e) {
let pickData = this.data.pickerData;
// 现遍历将所有尺码置为未选中状态
pickData.view.sizeList && pickData.view.sizeList.map(item => {
item.selected = false;
});
// 将当前尺码置为选中状态
let tempItem = e.currentTarget.dataset.key;
tempItem.selected = true;
pickData.view.sizeList[parseInt(e.currentTarget.id)] = tempItem;
// 设置数量加减按钮启用状态
pickData.view.plusButtonEnable = (!pickData.view.buy_limit_number || pickData.view.buy_limit_number > 1) && tempItem.storage_number > 1; // eslint-disable-line
pickData.view.minusButtonEnable = false;
pickData.view.buyButtonEnable = true;
if (pickData.view.bundle_count === 0) {
pickData.view.buyNumber = 1;
}
if (tempItem.storage_number <= 0) {
wx.showToast({
title: '该尺码已经售罄',
icon: 'none',
duration: 2000
});
pickData.view.buyButtonEnable = false;
}
this.setData({
pickerData: pickData,
selectedSKU: tempItem.storage_number > 0 ? tempItem.product_sku : 0,
storageNumber: tempItem.storage_number
});
},
bindMinus: function() {
let pickerData = this.data.pickerData;
let buyNumber = parseInt(pickerData.view.buyNumber);
if (pickerData.view.bundle_count > 0) {
if (buyNumber > pickerData.view.bundle_count) {
pickerData.view.minusButtonEnable = true;
} else {
this.minReachedLimitAction(pickerData.view.bundle_count);
pickerData.view.minusButtonEnable = false;
return;
}
}
if (buyNumber > 1) {
buyNumber = buyNumber - 1;
}
pickerData.view.buyNumber = buyNumber;
if (buyNumber === 1) {
pickerData.view.minusButtonEnable = false;
} else {
pickerData.view.minusButtonEnable = true;
}
if (pickerData.view.buy_limit_number !== 0 && buyNumber < pickerData.view.buy_limit_number) {
pickerData.view.plusButtonEnable = true;
}
if (buyNumber < this.data.storageNumber) {
pickerData.view.plusButtonEnable = true;
}
this.setData({pickerData});
},
// 增加购买数量
bindPlus: function() {
if (!this.data.colorSelected) {
return wx.showToast({
title: '请选择颜色',
icon: 'none',
duration: 1000
});
}
if (!this.data.selectedSKU) {
return wx.showToast({
title: '请选择尺码',
icon: 'none',
duration: 1000
});
}
let pickerData = this.data.pickerData;
let buyNumber = pickerData.view.buyNumber;
if (pickerData.view.buy_limit_number !== 0 && buyNumber >= pickerData.view.buy_limit_number) {
return this.plusReachedLimitAction(pickerData.view.buy_limit_number);
}
if (buyNumber >= this.data.storageNumber) {
return this.plusReachedMaxAction();
}
if (pickerData.view.buy_limit_number !== 0 && buyNumber < pickerData.view.buy_limit_number) {
pickerData.view.plusButtonEnable = true;
}
if (buyNumber < this.data.storageNumber) {
pickerData.view.plusButtonEnable = true;
buyNumber = parseInt(buyNumber) + 1;
}
if (buyNumber >= this.data.storageNumber) {
pickerData.view.plusButtonEnable = false;
}
if (buyNumber > 1) {
pickerData.view.minusButtonEnable = true;
}
pickerData.view.buyNumber = buyNumber;
this.setData({pickerData});
},
plusReachedMaxAction: function() {
return wx.showToast({
title: '对不起,没有更多库存了',
icon: 'none',
duration: 1000
});
},
plusReachedLimitAction: function(n) {
return wx.showToast({
title: '限购' + n + '件',
icon: 'none',
duration: 1000
});
},
minReachedLimitAction: function(n) {
return wx.showToast({
title: n + '件起售',
icon: 'none',
duration: 1000
});
},
goCart: function() {
router.go('shoppingCart');
},
addToCart: function() {
if (this.data.productOnlyProvideByYohoAPP ||
this.data.productOnlyProvideByYohoStore ||
this.data.productNotForSale) {
return wx.showToast({
title: '该商品暂时不支持当前小程序购买,请到Yoho!Buy有货查看',
icon: 'none',
duration: 2000
});
}
if (this.data.storage_sum === 0) {
return;
}
this.setData({buyImmediate: false});
if (app.getUid()) {
Picker.pickerShow(this);
}
},
// 确定选择
confirmChoose: function(e) {
if (!this.data.colorSelected) {
return wx.showToast({
title: '请选择颜色',
icon: 'none',
duration: 1000
});
}
if (!this.data.selectedSKU) {
return wx.showToast({
title: '请选择尺码',
icon: 'none',
duration: 1000
});
}
this._dataReport();
if (this.data.buyImmediate) {
return this._payNow(e);
}
this._addToCart();
},
// 促销
getPromotion: function() {
detailModel.promotion({product_skn: this.data.productSkn})
.then(res => {
res.data.forEach(item => {
item.time = `${
moment(item.startTime * 1000).format('YYYY.MM.DD')}-${moment(item.endTime * 1000).format('YYYY.MM.DD')
}`;
});
let proList = res.data;
let proTypes = [];
proList.forEach(item => {
let proType = item.promotionType;
if (proTypes.indexOf(proType) === -1) {
proTypes.push(proType);
}
});
this.setData({
proList,
proTypes,
});
})
.catch(() => {});
},
toggleExpandPro: function() {
this.setData({
expandPro: !this.data.expandPro
});
},
_addToCart() {
let params = {
product_sku: this.data.selectedSKU > 0 ? this.data.selectedSKU : 0,
buy_number: this.data.pickerData.view.buyNumber,
edit_product_sku: 0,
promotion_id: '',
goods_type: 0,
selected: 'Y'
};
detailModel.addToShoppingCart(params)
.then(res => {
if (res.code === 200) {
this._getCountInCart();
Picker.pickerHide(this);
wx.showToast({
title: res.message,
icon: 'none',
duration: 2000
});
} else {
return Promise.reject({
message: res.message
});
}
})
.catch(err => {
wx.showToast({
title: err && err.message || '加入购物车失败,请稍后重试',
icon: 'none',
duration: 2000
});
});
},
_getCountInCart: function() {
detailModel.getCountInCart({product_skn: this.data.productSkn})
.then(res => {
if (res.code === 200) {
this.setData({
countInCart: res.data.cart_goods_count,
});
} else {
return Promise.reject();
}
}).catch(() => {});
},
_getShopInfo: function() {
const params = {
brand_id: this.data.brandId,
shop_id: this.data.shopId
};
detailModel.getShopInfo(params)
.then(res => {
const shopList = res.data;
if (shopList && shopList.length > 0) {
const shopInfo = shopList[0];
this.setData({
shopInfo,
shopList
});
}
});
},
_payNow: function() {
router.go('cartEnsure', {
productSku: this.data.selectedSKU,
buyNumber: this.data.pickerData.view.buyNumber
});
},
_dataReport: function() {
yas.report('YB_GDS_DT_BILL', {
PRD_SKN: this.data.productSkn,
RPD_SKU: this.data.selectedSKU,
PRD_ID: this.data.selectedProductId
});
}
}, Actionsheet));