|
@@ -24,7 +24,7 @@
|
|
|
</scroll-view>
|
|
|
<view class="purchase-num padding-lr-sm">
|
|
|
<uni-section title="购买数量"></uni-section>
|
|
|
- <uni-number-box v-model="vModelValue" @change="changeValue(value)" />
|
|
|
+ <uni-number-box v-model="vModelValue" :min="1" @change="changeValue()" />
|
|
|
</view>
|
|
|
<view class="btn-box">
|
|
|
<view class="btn reset" @click="toShop">加入购物车</view>
|
|
@@ -50,23 +50,28 @@
|
|
|
intSkuId: [], // 选择唯一的id
|
|
|
caMsg: [], // 根据id查出来的数据
|
|
|
isSelected: false,
|
|
|
+ isData: null,
|
|
|
+ preSele: [],
|
|
|
+ itemsL: [],
|
|
|
+ toList: [],
|
|
|
+ total: 0
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
- changeValue(e) {},
|
|
|
+ changeValue(value) {},
|
|
|
change(e) {
|
|
|
if (!e.show) { // 当弹出层关闭时
|
|
|
// 重置规格选择
|
|
|
- if (this.specs.length > 0) {
|
|
|
+ if (this.specs.length > 0 && this.preSele.length < 0) {
|
|
|
this.specs.forEach(e => {
|
|
|
e.values.forEach(s => {
|
|
|
s.selected = false
|
|
|
})
|
|
|
})
|
|
|
+ //this.vModelValue = 1
|
|
|
}
|
|
|
this.speL = []
|
|
|
- // 重置购买数量
|
|
|
- this.vModelValue = 1
|
|
|
+ this.seleBack()
|
|
|
}
|
|
|
},
|
|
|
async selectSpec1(item, spec) {
|
|
@@ -74,19 +79,25 @@
|
|
|
item.values.forEach(value => {
|
|
|
value.selected = false
|
|
|
})
|
|
|
- // 然后,选择当前点击的选项
|
|
|
- spec.selected = true
|
|
|
- if (spec.selected) {
|
|
|
- this.speL.push(spec)
|
|
|
+
|
|
|
+ spec.selected = !spec.selected
|
|
|
+ const newIds = spec.ids
|
|
|
+ if (this.preSele.length > 0) {
|
|
|
+ this.speL = this.preSele
|
|
|
}
|
|
|
+ const filSpeL = this.speL.filter(value => value.ids !== newIds);
|
|
|
+
|
|
|
+ filSpeL.push(spec)
|
|
|
+
|
|
|
+ this.speL = filSpeL
|
|
|
|
|
|
const seleList = this.speL.filter(item => item.selected == true)
|
|
|
- const items = seleList.map(item => item.label)
|
|
|
+ this.itemsL = seleList.map(item => item.label)
|
|
|
+
|
|
|
// 计算选中规格项的skuIds数组的交集
|
|
|
if (seleList.length > 0) {
|
|
|
let intersection = seleList[0].skuIds
|
|
|
|
|
|
- // 遍历数组中的每个对象
|
|
|
for (let i = 1; i < seleList.length; i++) {
|
|
|
// 使用filter和includes方法找到交集
|
|
|
intersection = intersection.filter(skuId => {
|
|
@@ -102,40 +113,68 @@
|
|
|
// 如果没有选中的规格项,返回空字符串
|
|
|
this.intSkuId = '';
|
|
|
}
|
|
|
- // console.log('speL', this.speL)
|
|
|
- // 确定每一个都有选择后发起请求
|
|
|
+
|
|
|
this.isSelected = this.specs.every(spec => spec.values.some(value => value.selected))
|
|
|
- // console.log('isSelected', this.isSelected)
|
|
|
+
|
|
|
if (this.isSelected) {
|
|
|
// 如果所有规格都至少有一个选项被选中
|
|
|
const res = await this.$request('get', `/item/sku/${this.intSkuId}`)
|
|
|
- this.caMsg = res.data
|
|
|
- // 返回选中规格项的label值
|
|
|
- //console.log('intSkuId', this.intSkuId)
|
|
|
- //console.log('items', items)
|
|
|
+ if (res) {
|
|
|
+ this.caMsg = res.data
|
|
|
+ this.vModelValue = 1
|
|
|
+ console.log('this.caMsg', this.caMsg)
|
|
|
+ }
|
|
|
+
|
|
|
//console.log('所有规格都已选择')
|
|
|
- const itemDetal = {
|
|
|
- id: this.intSkuId,
|
|
|
- item: Array.from(items).join(','),
|
|
|
- count: this.vModelValue
|
|
|
+ if (this.isData == true) {
|
|
|
+ this.preSele = seleList
|
|
|
}
|
|
|
- this.$emit('seChanged', itemDetal);
|
|
|
+
|
|
|
} else {}
|
|
|
},
|
|
|
- async open() {
|
|
|
+ // 返回选中的数据
|
|
|
+ seleBack() {
|
|
|
+ const itemDetal = {
|
|
|
+ id: this.intSkuId,
|
|
|
+ item: Array.from(this.itemsL).join(','),
|
|
|
+ count: this.vModelValue
|
|
|
+ }
|
|
|
+ this.$emit('seChanged', itemDetal);
|
|
|
+ },
|
|
|
+ async open(e) {
|
|
|
+ if (e || e != null) {
|
|
|
+ this.isData = e
|
|
|
+ }
|
|
|
this.$refs.popup.open('bottom')
|
|
|
- // console.log("this.cardMsg")
|
|
|
+
|
|
|
const spuId = this.cardMsg.spuId
|
|
|
const res = await this.$request('get', `/item/sku/queryPropMapping?spuId=${spuId}`)
|
|
|
- this.specs = res.data.filter(item => item.values && item.values.length > 0);
|
|
|
+ this.specs = res.data.filter(item => item.values && item.values.length > 0)
|
|
|
this.specs.forEach(item => {
|
|
|
Object.assign(item, {
|
|
|
values: item.values.map(value => ({
|
|
|
...value,
|
|
|
- ids: item.id,
|
|
|
- selected: false
|
|
|
+ ids: item.id
|
|
|
+ // selected: false
|
|
|
}))
|
|
|
})
|
|
|
+ if (this.preSele.length <= 0) {
|
|
|
+ Object.assign(item, {
|
|
|
+ values: item.values.map(value => ({
|
|
|
+ ...value,
|
|
|
+ selected: false
|
|
|
+ }))
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ Object.assign(item, {
|
|
|
+ values: item.values.map(value => ({
|
|
|
+ ...value,
|
|
|
+ selected: this.preSele.some(prevSpe =>
|
|
|
+ prevSpe.label === value.label && prevSpe.ids ===
|
|
|
+ value.ids)
|
|
|
+ }))
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
},
|
|
|
toShop() {
|
|
@@ -158,7 +197,6 @@
|
|
|
quantity: count,
|
|
|
extendProps: userId
|
|
|
}
|
|
|
- // console.log('params', params)
|
|
|
this.$request('post', `/front/shoppingCart`, params, true).then(response => {
|
|
|
// 请求成功
|
|
|
if (response.code == 200) {
|
|
@@ -180,15 +218,65 @@
|
|
|
// 请求失败
|
|
|
console.error('请求失败:', error);
|
|
|
})
|
|
|
- // this.$emit('update-shopmsg', this.shopMsg);
|
|
|
// 关闭弹出层
|
|
|
this.$refs.popup.close()
|
|
|
}
|
|
|
},
|
|
|
+ totalPrice() {
|
|
|
+ const e = uni.getStorageSync('carl')
|
|
|
+ const userId = uni.getStorageSync('appUserId')
|
|
|
+ const toList = {
|
|
|
+ storeId: e.id,
|
|
|
+ storeName: e.name,
|
|
|
+ skuId: this.caMsg.skuId,
|
|
|
+ basePrice: this.caMsg.retailPrice,
|
|
|
+ price: this.caMsg.retailPrice,
|
|
|
+ quantity: this.vModelValue,
|
|
|
+ extendProps: userId,
|
|
|
+ imgUrl: this.caMsg.img,
|
|
|
+ spuId: this.caMsg.spuId,
|
|
|
+ skuName: this.caMsg.skuName,
|
|
|
+ skuProperties: this.caMsg.skuProperties,
|
|
|
+ skuTitle: this.caMsg.title,
|
|
|
+ }
|
|
|
+ let total = 0;
|
|
|
+ // 如果 selected 属性不存在,直接计算所有商品
|
|
|
+ let priceInCents = Math.round(toList.price * 100);
|
|
|
+ let quant = toList.quantity;
|
|
|
+ total += priceInCents * quant;
|
|
|
+
|
|
|
+ this.toList.push(toList)
|
|
|
+ // 将总价格转换回浮点数(以元为单位)
|
|
|
+ this.total = (total / 100).toFixed(2);
|
|
|
+ },
|
|
|
toBuy() {
|
|
|
+ this.totalPrice()
|
|
|
+ const carlist = {
|
|
|
+ carlist: this.toList,
|
|
|
+ total: this.total
|
|
|
+ }
|
|
|
uni.navigateTo({
|
|
|
- url: `/pages/order/submitOrder/submitOrder`
|
|
|
+ url: `/pages/order/submitOrder/submitOrder?data=${encodeURIComponent(JSON.stringify(carlist))}`
|
|
|
})
|
|
|
+ this.toList = []
|
|
|
+ console.log('carlist', carlist)
|
|
|
+ this.$refs.popup.close()
|
|
|
+ // if (carlist.carlist.length <= 0) {
|
|
|
+ // // this.$refs.uNotify.show({'请选择'})
|
|
|
+ // uni.showToast({
|
|
|
+ // title: '请选择',
|
|
|
+ // icon: 'error',
|
|
|
+ // duration: 2000
|
|
|
+ // });
|
|
|
+ // } else {
|
|
|
+ // uni.navigateTo({
|
|
|
+ // url: `/pages/order/submitOrder/submitOrder?data=${encodeURIComponent(JSON.stringify(carlist))}`
|
|
|
+ // })
|
|
|
+ // this.$refs.popup.close()
|
|
|
+ // }
|
|
|
+ // uni.navigateTo({
|
|
|
+ // url: `/pages/order/submitOrder/submitOrder`
|
|
|
+ // })
|
|
|
}
|
|
|
}
|
|
|
}
|