feature-selector.js
4.9 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
module.exports = {
init() {
},
props: {
isVisible: Boolean,
entity: Object,
onAddToCart: Function
},
watch: {
isVisible() {
const self = this;
if (this.isVisible) {
this.overlay = $.overlay({
onClose: function() {
self.isVisible = false;
}
});
this.overlay.show();
} else {
this.overlay.hide();
this.$parent.$emit('featureselector.close');
}
},
entity() {
const thumbnails = {};
const selection = {};
const colorSizes = {};
const stocks = {};
// 更新颜色
this.colors = this.entity.goodsList.filter((goods)=> {
// 确保商品启用
return goods.status !== 0;
}).map((goods)=> {
// 缩略图
thumbnails[goods.colorId] = goods.colorImage;
// 更新颜色对应尺码
colorSizes[goods.colorId] = goods.goodsSizeBoList.map((size)=> {
if (!stocks[goods.colorId]) {
stocks[goods.colorId] = 0;
}
// 默认选中有库存的第一个颜色尺码
if (size.goodsSizeStorageNum > 0) {
if (!selection.color) {
selection.color = goods.colorId;
}
if (!selection.size && size.goodsSizeStorageNum > 0) {
selection.size = size.goodsSizeSkuId;
}
// 计算所有尺码的库存
stocks[goods.colorId] += size.goodsSizeStorageNum;
}
return {
text: size.sizeName,
value: size.goodsSizeSkuId,
disabled: size.goodsSizeStorageNum === 0
};
});
return {
text: goods.colorName,
value: goods.colorId,
disabled: stocks[goods.colorId] === 0 // 是否售完
};
});
this.sizes = colorSizes[selection.color];
this.colorSizes = colorSizes;
this.thumbnails = thumbnails;
// 选择默认值
this.$emit('feature:color.select', selection.color);
this.$emit('feature:size.select', selection.size);
}
},
data() {
return {
colors: [],
sizes: [],
colorSizes: {},
thumbnails: {},
selection: {
color: null,
size: null,
thumbnail: ''
}
};
},
components: {
featureOptions: require('../feature-options.vue')
},
created() {
// 选择颜色
this.$on('feature:color.select', (opt)=> {
const selection = {
color: opt,
size: ((color, size)=> {
// 切换颜色后选择匹配的尺码
const sizes = this.colorSizes[color];
if (sizes && sizes.length > 0) {
const oldSizes = sizes.filter((item) => {
return item.value === size;
});
if (oldSizes && oldSizes.length > 0) {
const newSizes = this.colorSizes[opt];
const matchedSize = newSizes.filter((item)=> {
return !item.disabled && item.text === oldSizes[0].text;
});
if (matchedSize && matchedSize.length > 0) {
return matchedSize[0].value;
}
}
}
return null;
})(this.selection.color, this.selection.size),
thumbnail: this.thumbnails[opt]
};
this.sizes = this.colorSizes[opt];
Object.assign(this.selection, selection);
});
// 选择尺码
this.$on('feature:size.select', (opt)=> {
const selection = {
size: opt
};
Object.assign(this.selection, selection);
});
},
methods: {
/**
* 将当前选择添加到购物车
*/
addToCart() {
// console.log(`${this.selection.color}:${this.selection.size}`);
const sku = this.selection.size;
$.post('/product/cart.json', {
productSku: sku,
buyNumber: 1
}).then((result)=> {
this.onAddToCart(result);
});
}
}
};