|
@@ -39,65 +39,63 @@ export default defineComponent({
|
|
|
data() {
|
|
|
return {
|
|
|
radio: '',
|
|
|
- selectItem: {
|
|
|
- id: '',
|
|
|
- pic: '',
|
|
|
- stock: 0,
|
|
|
- price: 0,
|
|
|
- spDataJson: '',
|
|
|
- skuCode: ''
|
|
|
- },
|
|
|
total: 1,
|
|
|
- totalData:{}
|
|
|
+ totalData: {},
|
|
|
+ skuStockList: []
|
|
|
}
|
|
|
},
|
|
|
+ mounted() {
|
|
|
+ this.setList()
|
|
|
+ },
|
|
|
computed: {
|
|
|
- skuStockList() {
|
|
|
- // 处理规格
|
|
|
- const skuStockList = this.item.skuStockList || [
|
|
|
- {
|
|
|
- id: -1,
|
|
|
- price: this.item.price,
|
|
|
- pic: this.item.pic,
|
|
|
- stock: this.item.stock,
|
|
|
- spData: null
|
|
|
- }
|
|
|
- ]
|
|
|
- skuStockList.forEach((item: any) => {
|
|
|
- item.stock = item.stock - item.lockStock
|
|
|
- if (item.spData) {
|
|
|
- const spData = JSON.parse(item.spData)
|
|
|
- let str = ''
|
|
|
- spData.forEach((sp: any) => {
|
|
|
- str += `${sp.value}`
|
|
|
- })
|
|
|
- item.spDataJson = str
|
|
|
- } else {
|
|
|
- item.spDataJson = '默认'
|
|
|
+ selectItem() {
|
|
|
+ const select = this.skuStockList.find((n: any) => n.id == this.radio) as any
|
|
|
+ if (select) {
|
|
|
+ return {
|
|
|
+ ...select,
|
|
|
+ stock: select.stock - (select.lockStock < 0 ? 0 : select.lockStock)
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ stock: 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ setList() {
|
|
|
+ // 处理规格
|
|
|
+ let skuStockList = [] as any
|
|
|
+ const item = JSON.parse(JSON.stringify(this.item))
|
|
|
+ if (Array.isArray(item.skuStockList)) {
|
|
|
+ skuStockList = item.skuStockList.map((n: any) => {
|
|
|
+ n.pic = n.pic || item.pic
|
|
|
+ if (n.spData) {
|
|
|
+ const spData = JSON.parse(n.spData)
|
|
|
+ let str = ''
|
|
|
+ spData.forEach((sp: any) => {
|
|
|
+ str += `${sp.value}`
|
|
|
+ })
|
|
|
+ n.spDataJson = str
|
|
|
+ } else {
|
|
|
+ n.spDataJson = '默认'
|
|
|
+ }
|
|
|
+ n.lock = n.stock - n.lockStock
|
|
|
+ return {
|
|
|
+ ...n
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if (!skuStockList.length) return skuStockList
|
|
|
// 处理默认显示
|
|
|
let index = 0
|
|
|
if (this.defaultRadio) {
|
|
|
let i = skuStockList.findIndex(n => n.id == this.defaultRadio)
|
|
|
index = i > -1 ? i : 0
|
|
|
}
|
|
|
- const { id, price, pic, stock, spDataJson, skuCode } = skuStockList[index]
|
|
|
- this.getProductAddCartCount(id)
|
|
|
- this.radio = id
|
|
|
- this.selectItem = {
|
|
|
- id,
|
|
|
- price,
|
|
|
- pic: pic || this.item.pic,
|
|
|
- stock,
|
|
|
- spDataJson,
|
|
|
- skuCode
|
|
|
- }
|
|
|
-
|
|
|
- return skuStockList
|
|
|
- }
|
|
|
- },
|
|
|
- methods: {
|
|
|
+ this.getProductAddCartCount(skuStockList[index].id)
|
|
|
+ this.radio = skuStockList[index].id
|
|
|
+ this.skuStockList = skuStockList
|
|
|
+ },
|
|
|
async onAddCart() {
|
|
|
const selectItem = this.selectItem
|
|
|
const item = this.item
|
|
@@ -138,8 +136,8 @@ export default defineComponent({
|
|
|
},
|
|
|
|
|
|
// 获取购物车当前产品的数量
|
|
|
- async getProductAddCartCount(id?:any) {
|
|
|
- id = id ? id : this.skuStockList.find((n: any) => n.id == this.radio)?.id
|
|
|
+ async getProductAddCartCount(id?: any) {
|
|
|
+ id = id ? id : this.selectItem.id
|
|
|
if (this.totalData[id]) {
|
|
|
this.total = this.totalData[id]
|
|
|
return
|
|
@@ -190,10 +188,7 @@ export default defineComponent({
|
|
|
v-slots={{
|
|
|
title: () => <div class={styles.title}>规格</div>,
|
|
|
label: () => (
|
|
|
- <RadioGroup
|
|
|
- class={styles['radio-group']}
|
|
|
- modelValue={this.radio}
|
|
|
- >
|
|
|
+ <RadioGroup class={styles['radio-group']} modelValue={this.radio}>
|
|
|
{this.skuStockList.map((item: any) => {
|
|
|
const isActive = item.id === this.radio
|
|
|
const type = isActive ? 'primary' : 'default'
|
|
@@ -203,18 +198,8 @@ export default defineComponent({
|
|
|
name={item.id}
|
|
|
onClick={() => {
|
|
|
if (this.radio == item.id) return
|
|
|
- // 处理默认显示
|
|
|
- const { id, price, pic, stock, spDataJson, skuCode } = item
|
|
|
this.radio = item.id
|
|
|
- this.selectItem = {
|
|
|
- id,
|
|
|
- price,
|
|
|
- pic: pic || this.item.pic,
|
|
|
- stock,
|
|
|
- spDataJson,
|
|
|
- skuCode
|
|
|
- }
|
|
|
- this.getProductAddCartCount(id)
|
|
|
+ this.getProductAddCartCount(item.id)
|
|
|
}}
|
|
|
>
|
|
|
<Tag size="large" plain={isActive} type={type}>
|