index.js
9.4 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
let App = getApp();
const util = require('../../utils/util');
let wxParse = require("../../res/wxParse/wxParse.js");
Page({
data: {
// banner轮播组件属性
indicatorDots: true, // 是否显示面板指示点
autoplay: true, // 是否自动切换
interval: 3000, // 自动切换时间间隔
duration: 800, // 滑动动画时长
wxapp: [],
detail:[],
api_url: App.Domain,
goods_spec_arr: [], // 记录规格的数组
specData:[],
goods_num:1,//购买数量
goods_sku_id: 0, // 规格id
goods_price:0,
stock_num:0,
line_price:0,
cartnum:0,
addcart_loading:false,
current_img_index:0,
sku_hidden_arr:[]
},
onLoad: function (options) {
let that = this;
//页面启动后 调取首页的数据
that.setData({
wxapp: wx.getStorageSync('wxapp')
});
App.wx_setcolor(that.data.wxapp);
// 商品id
that.data.goods_id = options.goods_id;
// 获取商品信息
App._get('goods/detail', {
goods_id: that.data.goods_id
}, function (result) {
// 初始化商品多规格
if (result.data.detail.spec_type === '20') {
that.initManySpecData(result.data);
}else{
that.setData(
{
goods_sku_id: result.data.detail.spec[0].spec_sku_id,
goods_price: result.data.detail.spec[0].goods_price,
line_price: result.data.detail.spec[0].line_price,
stock_num: result.data.detail.spec[0].stock_num
}
);
}
//根据选择后的情况 分配sku的可选情况
if (result.data.detail.spec_type === '20') {
that.make_sku_showData(result.data.specData,0);
}
//这里处理富文本
if (result.data.detail.content.length > 0) {
//首先把图片格式化
result.data.detail.content = result.data.detail.content.replace(/<img src="\/uploads\//ig, "<img src=\"" + that.data.api_url +"\/uploads\/");
wxParse.wxParse('content', 'html', result.data.detail.content, that, 0);
}
that.setData(
{
detail: result.data.detail,
specData: result.data.specData,
goods_spec_arr: that.data.goods_spec_arr,
}
);
console.log(result);
});
//这里获得购物车数量
App._get('cart/getTotalNum', {}, function (result) {
that.setData( {
cartnum: result.data.cart_total_num,
} );
});
},
onShow: function () {
},
make_sku_showData: function (data,break_num) {
var that = this;
//显示的sku数据为:
var Showskuiteam = data.spec_attr;
//初始化显示数据hidden为false
Showskuiteam.forEach(function (value, index, array) {
value.spec_items.forEach(function (value1, index1, array1) { value1.hidden = false; });
});
//循环 行规格 可选格式化,根据后面所有不变的sku规格
Showskuiteam.forEach(function (value, index, array) {
//这里 那一个选项
//if (index != break_num) {
that.for_eachsku_showData(Showskuiteam, index);
//}
});
},
for_eachsku_showData: function (Showskuiteam, ForNum) {
//影藏sku组合情况:
var Sku_hidden = this.data.sku_hidden_arr;
//现在选择的情况是:
var Nowselect = this.data.goods_spec_arr;
//循环 每行规格 可选格式化,根据后面所有不变的sku规格
Sku_hidden.forEach(function (Sku_hiddenvalue, Sku_hiddenindex, Sku_hiddenarray) {
//针对每个影藏sku 匹配
var peiduiNum = 0;
Sku_hiddenvalue.forEach(function (value, index, array) {
if (index != ForNum) {
if (value == Nowselect[index]) {
peiduiNum++;
}
}
});
if (peiduiNum == (Nowselect.length - 1)) {
//此时 此sku为影藏项目
Showskuiteam.forEach(function (Showskuiteamvalue, Showskuiteamindex, Showskuiteamarray) {
Showskuiteamvalue.spec_items.forEach(function (value1, index1, array1) {
if (value1.item_id == Sku_hiddenvalue[ForNum]) {
value1.hidden = true;
}
});
});
}
});
},
/**
* 初始化商品多规格
*/
initManySpecData: function (data) {
var that = this;
for (let i in data.specData.spec_list) {
if (data.specData.spec_list[i].form.stock_num >= 0){
var sku_id = data.specData.spec_list[i].spec_sku_id.split('_');
//初始化 sku 显示
// 商品价格/划线价/库存
that.setData({
goods_sku_id: data.detail.spec[i].spec_sku_id,
goods_price: data.detail.spec[i].goods_price,
line_price: data.detail.spec[i].line_price,
stock_num: data.detail.spec[i].stock_num
});
for (let j in sku_id) {
that.data.goods_spec_arr[j] = parseInt(sku_id[j]);
}
break;
}
}
//初始化 影藏sku数组
that.data.sku_hidden_arr = [];
for (let i in data.specData.spec_list) {
if (data.specData.spec_list[i].form.stock_num < 0) {
that.data.sku_hidden_arr.push(data.specData.spec_list[i].spec_sku_id.split('_'));
}
}
},
check_good_sel_sku: function (goods_spec_arr) {
var re_r = true;
//影藏sku组合情况:
var Sku_hidden = this.data.sku_hidden_arr;
Sku_hidden.forEach(function (Sku_hiddenvalue, Sku_hiddenindex, Sku_hiddenarray) {
//针对每个影藏sku 匹配
var peiduiNum = 0;
Sku_hiddenvalue.forEach(function (value, index, array) {
if (value == goods_spec_arr[index]) {
peiduiNum++;
}
});
if (peiduiNum == Sku_hiddenvalue.length) {
//发现了不合法
re_r = false;
}
});
return re_r;
},
make_good_sel_sku: function (goods_spec_arr, attr_idx) {
var that = this;
//首先判断此选项是否合法
if (that.check_good_sel_sku(goods_spec_arr)){
}else{
//循环sku列表 找到当前选择的第一匹配sku项目
var spec_list = this.data.specData.spec_list;
spec_list.forEach(function (value, index, array) {
if (value.form.stock_num >= 0) {
var sku_id_arr = value.spec_sku_id.split('_');
sku_id_arr.forEach(function (sku_id_arrvalue, sku_id_arrindex, sku_id_arrarray) {
if (sku_id_arrindex == attr_idx && goods_spec_arr[sku_id_arrindex] == sku_id_arrvalue){
//找到目前的匹配项 可使用的sku
goods_spec_arr = sku_id_arr;
}
});
}
});
}
//格式化
goods_spec_arr.forEach(function (value, index, array) {
goods_spec_arr[index] = parseInt(value);
});
that.setData({
goods_spec_arr: goods_spec_arr,
});
},
RonChange: function (e) {
console.log(e);
let goods_spec_arr = this.data.goods_spec_arr;
goods_spec_arr[e.currentTarget.dataset.attr_idx] = parseInt(e.detail);
//这里如果发现目前选项是不可选的,那么通过分配其余可选的选项
this.make_good_sel_sku(goods_spec_arr, e.currentTarget.dataset.attr_idx);
this.updateSpecGoods();
this.make_sku_showData(this.data.specData, e.currentTarget.dataset.attr_idx);
this.setData({
specData: this.data.specData,
});
// console.log(e);
},
/** 更新商品规格信息 */
updateSpecGoods: function () {
let spec_sku_id = this.data.goods_spec_arr.join('_');
// 查找skuItem
let spec_list = this.data.specData.spec_list,
skuItem = spec_list.find((val) => {
return val.spec_sku_id == spec_sku_id;
});
// 记录goods_sku_id
// 更新商品价格、划线价、库存
if (typeof skuItem === 'object') {
this.setData({
goods_sku_id: skuItem.spec_sku_id,
goods_price: skuItem.form.goods_price,
line_price: skuItem.form.line_price,
stock_num: skuItem.form.stock_num,
});
}
//这里对 规格的封面图进行格式化
//首张图片为
var frist_img = skuItem.form.imgshow;
if (frist_img === null || frist_img === ''){
return;
}
var imgs_arr = this.data.detail.imgs_url;
//查找有重复的 立即删掉
for (let i in imgs_arr) {
if (imgs_arr[i] == frist_img){
imgs_arr.splice(i, 1);
}
}
imgs_arr.unshift(frist_img);
this.setData({
'detail.imgs_url': imgs_arr,
current_img_index:0,
});
},
onstepChange: function (e) {
//console.log(e);
this.setData({
goods_num: e.detail,
});
},
Tap_topimg: function (e) {
console.log(e);
let that = this;
let id = e.target.dataset.id;
wx.previewImage({
current: that.data.detail.imgs_url[id],
urls: that.data.detail.imgs_url
})
},
addcart: function (e) {
let that = this;
console.log(e);
that.setData({
addcart_loading: true
});
// 加入购物车
App._post('cart/add', {
goods_id: that.data.goods_id,
goods_num: that.data.goods_num,
goods_sku_id: that.data.goods_sku_id,
}, function (result) {
App.showSuccess(result.msg);
that.setData({
cartnum: result.data.cart_total_num,
});
}, null, function (result) {
that.setData({
addcart_loading: false
});
});
},
ByNow: function (e) {
let that = this;
wx.navigateTo({
url: '../cart/checout?' + util.urlencode({
type: 'buyNow',
goods_id: that.data.goods_id,
goods_num: that.data.goods_num,
goods_sku_id: that.data.goods_sku_id,
})
});
}
})