index.js
5.07 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
import NP from "number-precision";
import cartApi from '~/api/module/cart.js'
Component({
/**
* 组件的属性列表
*/
properties: {
show: Boolean,
address: Array,
type: String,
goods: {
type: Object,
observer: function(n) {
this.setData({
detailInfo: JSON.parse(JSON.stringify(n))
}, () => {
this.setDefaultSku(n)
})
}
},
defaultAddress: Object,
defaultAds: String,
canPay:Boolean,
clickType: String
},
data: {
currentIndex: 0,
goodsNumber: 1,
showJfModal: false,
detailInfo: {}
},
/**
* 组件的方法列表
*/
methods: {
cancel() {
this.setData({
show: false
})
this.triggerEvent('cancelSku')
},
setDefaultSku(detailInfo) {
let specificationDetailList = detailInfo.platformGlGoodsSpecifications && detailInfo.platformGlGoodsSpecifications[0]
if (detailInfo?.specificationData) {
specificationDetailList = detailInfo?.platformGlGoodsSpecifications?.find(val => val.specificationData == detailInfo.specificationData)
}
let defaultGoods = specificationDetailList?.specificationData.split('&')
detailInfo.platformGlGoodsSpecificationList?.map(e => {
defaultGoods?.map(i => {
let [key, value] = i.split('-')
if (key == e.specificationName) {
e.platformSpecificationValueList.map(c => {
if (c.specificationValue == value) c.active = true
})
}
})
})
this.setData({
detailInfo,
['detailInfo.goodsStock']: specificationDetailList?.stock || detailInfo.goodsStock, // 多规格商品库存在每个规格组中
goodsNumber: detailInfo?.sellQty || 1
})
},
// 选择规格
choose(e) {
let { index, ind } = e.currentTarget.dataset
let { detailInfo } = this.data
let { platformGlGoodsSpecificationList, platformGlGoodsSpecifications } = detailInfo
let activeList = []
if (platformGlGoodsSpecificationList[index].platformSpecificationValueList[ind].active) return
platformGlGoodsSpecificationList[index].platformSpecificationValueList.map(i => (i.active = false))
platformGlGoodsSpecificationList[index].platformSpecificationValueList[ind].active = true
platformGlGoodsSpecificationList.map(i => {
i.platformSpecificationValueList.map(c => {
if (c.active) activeList.push(`${i.specificationName}-${c.specificationValue}`)
})
})
let specificationData = activeList.join('&')
let goodsStock
platformGlGoodsSpecifications?.map(val => {
if (val.specificationData == specificationData) {
goodsStock = val.stock
}
})
this.setData({
detailInfo,
['detailInfo.goodsStock']: goodsStock,
['detailInfo.specificationData']: specificationData
})
const thisPage = wx.p.router.getPageInstance()
thisPage.setData({
activeSku: specificationData,
goodsStock
})
},
changeCount(e) {
this.triggerEvent('getCount', e.detail)
this.setData({
goodsNumber: e.detail
})
},
toShowAddressModal() {
let thisPage = wx.p.router.getPageInstance()
thisPage.setData({
showAddress: true
})
},
intRemain(e) {
this.setData({
remain: e.detail.value
})
},
toCreatedOrder() {
let { clickType, remain, detailInfo, defaultAddress, goodsNumber } = this.data
const userInfo = wx.p.storage.get('userInfo')
if (!['pay', 'join', 'change'].includes(clickType)) {
return this.cancel()
}
if (clickType == 'change') {
this.cancel()
return this.triggerEvent('changeSku', {specificationData: detailInfo.specificationData, sellQty: goodsNumber || detailInfo.startQty})
}
console.log(detailInfo)
if (!detailInfo.goodsStock) return
detailInfo.sellQty = goodsNumber || detailInfo.sellQty
detailInfo?.platformGlGoodsSpecifications?.forEach(val => {
if (val.active) detailInfo.specificationData = val.specificationData
})
if (clickType == 'join') {
// 加入购物车
const PARAMS = {
goodsId: detailInfo.id,
sellQty: goodsNumber || detailInfo.startQty,
goodsSource: detailInfo.goodsSource,
scoreSource: userInfo?.channelSource,
specificationData: detailInfo?.specificationData,
merchantId: userInfo?.merchantId
}
wx.p.wxApi.showLoading()
cartApi.addToCart(PARAMS).then(res => {
wx.hideLoading()
wx.p.wxApi.showToast('加入购物车成功')
this.cancel()
this.triggerEvent('joinSuccess')
})
return
}
if(!defaultAddress.receiveName){
return wx.p.wxApi.showToast('请选择收货地址')
}
// 跳转到确认订单
let query = {
defaultAddress,
buyerMsg: remain,
goodsList: [{...detailInfo}],
}
wx.p.router.go('/packageIndex/pages/confirmOrder/index', query)
}
}
})