index.js
6.98 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
const navStyle = 'background-color: transparent;'
let navBarHeight = 38
import homeApi from '~/api/module/home.js'
import cartApi from '~/api/module/cart.js'
import mineApi from '~/api/module/mine.js'
import commonApi from '~/api/module/common.js'
Page({
data: {
navStyle,
tabs: ['商品详情', '商品参数'],
curTabIndex: 0,
service: {
1001: '不支持无理由退货',
1002: '支持7天无理由退货',
1003: '支持7天无理由退货(拆封后不支持)',
1004: '支持7天无理由退货(激活后不支持)',
1005: '支持7天无理由退货(安装后不支持)',
1006: '支持7天无理由退货(使用后不支持)',
},
skuInfo: '',
payWayActions: [{ name: '金额支付', type: 1, disabled: false }, { name: '积分兑换', type: 2, disabled: false }],
detailInfo: {},
eventFlag: false,
addressList: [],
defaultAddress: {}
},
appHide() {
wx.offAppHide()
wx.onAppHide(() => {
})
},
previewImage(e) {
let {
current,
item
} = e.currentTarget.dataset
let urls = []
item.map(i => {
urls.push(i.imgUrl)
})
wx.p.wxApi.previewImage({
current,
urls
})
},
navbarReady(e) {
navBarHeight = e.detail.height
},
onPageScroll(e) {
const { curTabIndex } = this.data
if (curTabIndex == 0) {
this.scrollY = e.scrollTop
}
this.setData({
navStyle: e.scrollTop >= navBarHeight || curTabIndex ? '' : navStyle,
opacity: curTabIndex == 1 ? 1 : e.scrollTop / 200,
})
},
goodsActionReady(e) {
this.setData({
padding_bottom: e.detail
})
},
goodsActionClick(e) {
let { detailInfo } = this.data
// if (e.detail == 'join' && detailInfo.goodsSource == 1) return this.toAddShopCar()
this.showSkuModal(e.detail)
},
onLoad(options) {
let userInfo = wx.p.storage.get('userInfo')
this.setData({userInfo})
wx.p.router.getRouterParams(options).then(params => {
this.setData({pageParams: params}, () => {this.heartbeatFn({type: 'add'})})
this.getDetail(params)
}).catch(err => {
console.error('参数获取失败:', err)
})
this.getPlatFormAddress()
},
onShow() {
this.heartbeatFn({type: 'add'})
},
heartbeatFn(e) {
let eventApi = e.type == 'add' ? 'dynamicRecordAdd' : 'dynamicRecordLeave'
let { pageParams, userInfo, reviewId } = this.data
if (!pageParams) return
let params = e.type == 'add' ? {
goodsId: pageParams?.id,
goodsType: 1,
customerMobile: userInfo?.phone,
customerName: userInfo?.nickName,
merchantId: userInfo?.merchantId,
merchantName: userInfo?.merchantName,
} : {id: reviewId}
commonApi[eventApi](params).then(res => {
this.setData({reviewId: res?.data})
})
},
showSkuModal(type) {
this.setData({
showSku: true,
clickType: type
})
wx.setPageStyle({
style: {
overflow: 'hidden'
}
})
},
// 获取默认的地址
getPlatFormAddress() {
wx.p.wxApi.showLoading()
mineApi.getCustomerAddressList().then(res => {
wx.hideLoading()
let _isSetDefault = false, contactAddress = '', defaultAddress = {}
if (res && res.length) {
res.forEach(el => {
if (el.isDefault == 1) {
contactAddress = el.provinceName + el.cityName + el.countyName + el.address,
defaultAddress = el
_isSetDefault = true
}
})
if (!_isSetDefault) {
let _first = res[0] || {}
contactAddress = _first.provinceName + _first.cityName + _first.countyName + _first.address,
defaultAddress = _first
}
}
this.setData({
addressList: res || [],
contactAddress,
defaultAddress
})
})
},
// 显示地址选择弹窗
toShowAddressModal() {
this.setData({
showAddress: true
})
},
// 地址选择确定
getAddress(e) {
this.setData({
contactAddress: e.detail.contactAddress,
defaultAddress: e.detail.defaultAddress
})
},
back() {
wx.p.router.back()
},
changeTab(e) {
if (this.data.opacity < 0.8) return
this.setData({
curTabIndex: e.currentTarget.dataset.index,
opacity: e.currentTarget.dataset.index
}, () => {
setTimeout(() => {
wx.pageScrollTo({
scrollTop: this.scrollY
})
}, 200)
})
},
// 获取详情
getDetail(options) {
homeApi.getGoodsDetailInfo({id: options.id, businessType: 1}).then(res => {
if (res && res.specificationResponses?.length) {
this.toGetSpecString(res, res.startQty)
}
if (res && res.platformGlGoodsSpecifications?.length) {
let item = res.platformGlGoodsSpecifications[0] || {}
res.goodsStock = item.stock
res.specificationData = item.specificationData
}
res?.platformGoodsResources?.forEach(item => {
if (item.resType == 1) item.imgUrl = item.content
})
if (res.goodsSource == 2) {
res.imageResponses = res?.platformGoodsResources?.filter(val => val.bizType == 1)
res.goodsDescList = res?.platformGoodsResources?.filter(val => val.bizType == 2)
}
this.setData({
detailInfo: res || {},
eventFlag: true,
activeSku: res.specificationData,
goodsStock: res.goodsStock || 0
})
})
},
// 处理云中鹤已选商品
toGetSpecString(info, count) {
let skuInfo = this.data, num = '', weight = ''
num = `${count}${info.goodsUnit}`
weight = `${info.grossWeight}${info.grossWeightUnit}`
skuInfo = `${info.specificationResponses[0].specValue}` + `,${weight}` + `,${num}`
this.setData({
skuInfo
})
},
getCount(e) {
let { detailInfo } = this.data
if (detailInfo?.goodsSource == 1) this.toGetSpecString(this.data.detailInfo, e.detail)
this.count = e.detail
},
// 添加购物车
toAddShopCar() {
const { detailInfo, userInfo } = this.data
const PARAMS = {
goodsId: detailInfo.id,
sellQty: this.count || detailInfo.startQty,
goodsSource: detailInfo.goodsSource,
scoreSource: userInfo?.channelSource,
specificationData: detailInfo?.specificationData,
merchantId: userInfo?.merchantId
}
if (!detailInfo.goodsStock) return wx.p.wxApi.showToast('暂无库存')
cartApi.addToCart(PARAMS).then(res => {
wx.p.wxApi.showToast('加入购物车成功')
this.joinSuccess()
})
},
joinSuccess() {
// 获取最新购物车数据更新显示的数量
const goodsAction = this.selectComponent('#goods-action')
if (goodsAction) {
goodsAction.getCartNum()
}
},
// 同步选择的地址
resetAddress(e) {
this.setData(e.detail)
},
closeSku() {
this.setData({
showSku: false
})
wx.setPageStyle({
style: {
overflow: 'unset'
}
})
},
onHide() {
this.heartbeatFn({type: 'level'})
},
onUnload: function () {
this.heartbeatFn({type: 'level'})
},
onShareAppMessage: function () {
}
})