@{{ variable.name }}
@{{ value.name }}
@@ -66,20 +66,31 @@
| 型号 |
- |
+ @{{ product.model }} |
| Sku |
- |
+ @{{ product.sku }} |
- @include('shared.quantity', ['quantity' => '1'])
-
-
+
+
+
@@ -132,10 +143,10 @@
quantity: 0,
sku: "",
},
+ quantity: 1,
source: {
skus: @json($product['skus']),
- variables: @json($product['variables']),
- // variables: JSON.parse(@json($product['variables'] ?? [])),
+ variables: @json($product['variables'] ?? []),
}
},
@@ -143,30 +154,64 @@
},
beforeMount () {
- const skuDefault = this.source.skus.find(e => e.is_default)
+ const skus = JSON.parse(JSON.stringify(this.source.skus));
+ const skuDefault = skus.find(e => e.is_default)
this.selectedVariantsIndex = skuDefault.variants
+ // 如果没有默认的sku,则取第一个sku的第一个变量的第一个值
+ if (skuDefault.variants == null) {
+ this.product = skus[0]
+ }
// 为 variables 里面每一个 values 的值添加一个 selected 字段
- this.source.variables.forEach(variable => {
- variable.values.forEach(value => {
- this.$set(value, 'selected', false)
+ if (this.source.variables.length) {
+ this.source.variables.forEach(variable => {
+ variable.values.forEach(value => {
+ this.$set(value, 'selected', false)
+ })
})
- })
- // console.log(this.selectedVariantsIndex)
- this.checkedVariants()
- this.getSku();
+ this.checkedVariants()
+ this.getSelectedSku();
+ }
},
methods: {
checkedVariableValue(variable_idnex, value_index, value) {
this.source.variables[variable_idnex].values.forEach((v, i) => {
- v.selected = false
- if (i == value_index) {
- v.selected = true
- }
+ v.selected = i == value_index
})
+ this.updateSelectedVariantsIndex();
+ this.getSelectedSku();
+ },
+
+ // 把对应 selectedVariantsIndex 下标选中 variables -> values 的 selected 字段为 true
+ checkedVariants() {
+ this.source.variables.forEach((variable, index) => {
+ variable.values[this.selectedVariantsIndex[index]].selected = true
+ })
+ },
+
+ getSelectedSku() {
+ // 通过 selectedVariantsIndex 的值比对 skus 的 variables
+ const sku = this.source.skus.find(sku => sku.variants.toString() == this.selectedVariantsIndex.toString())
+ this.product = sku;
+ },
+
+ addCart(isBuyNow) {
+ const data = {
+ sku_id: this.product.id,
+ quantity: this.quantity
+ };
+ console.log(data);
+ return;
+
+ $http.post('/carts', data).then((res) => {
+ layer.msg(res.message)
+ })
+ },
+
+ updateSelectedVariantsIndex() {
// 获取选中的 variables 内 value的 下标 index 填充到 selectedVariantsIndex 中
this.source.variables.forEach((variable, index) => {
variable.values.forEach((value, value_index) => {
@@ -175,26 +220,7 @@
}
})
})
-
- this.getSku();
},
-
- // 把对应 selectedVariantsIndex 下标选中 variables -> values 的 selected 字段为 true
- checkedVariants() {
- this.source.variables.forEach((variable, index) => {
- // variable.values.forEach(value => {
- // value.selected = false
- // })
- variable.values[this.selectedVariantsIndex[index]].selected = true
- })
- },
-
- // 根据 selectedVariantsIndex 下标获取对应的 sku
- getSku() {
- const sku = this.source.skus.find(sku => sku.variants.toString() === this.selectedVariantsIndex.toString())
- console.log(sku);
- this.product = sku
- }
}
})