317 lines
8.4 KiB
JavaScript
317 lines
8.4 KiB
JavaScript
// 修改状态
|
|
import * as types from '../mutation-types'
|
|
const state = {
|
|
|
|
shopCart: [],
|
|
orderGood: [], // 即将生成订单的商品
|
|
totalPrice: 0,
|
|
ischang: false,
|
|
changeSign: 1,
|
|
goodList: [], // 购物车商品
|
|
cateList: []// 购物车商品类别
|
|
}
|
|
const getters = {
|
|
|
|
shopCart: state => state.shopCart,
|
|
goodList: state => state.goodList,
|
|
cateList: state => state.cateList,
|
|
orderGood: state => state.orderGood,
|
|
orderPrice: state => {
|
|
let total = 0
|
|
|
|
state.orderGood.map(n => {
|
|
if (n.AVERAGE_PRICE) {
|
|
total += Number(n.ORDER_COUNT * n.AVERAGE_PRICE)
|
|
}
|
|
})
|
|
|
|
return Math.round(total * 100) / 100
|
|
},
|
|
goodNum: state => {
|
|
let total = 0
|
|
|
|
state.goodList.map(n => {
|
|
if (n.count && state.changeSign) {
|
|
total += n.count
|
|
}
|
|
})
|
|
|
|
return Math.round(total * 100) / 100
|
|
},
|
|
plusPrice: state => { // 会员价
|
|
let total = 0
|
|
|
|
state.goodList.map(n => {
|
|
if (n.isCheck && n.count) {
|
|
total += Number(n.count * n.COMMODITY_MEMBERPRICE)
|
|
}
|
|
})
|
|
|
|
return Math.round(total * 100) / 100 || 0
|
|
},
|
|
totalPrice: state => { // 普通价格
|
|
let total = 0
|
|
state.goodList.map(n => {
|
|
if (n.isCheck && n.count) {
|
|
total += Number(n.count * n.COMMODITY_RETAILPRICE)
|
|
}
|
|
})
|
|
|
|
return Math.round(total * 100) / 100 || 0
|
|
},
|
|
// 购物车一级类二级商品
|
|
cateGoods: state => {
|
|
let map1 = {}
|
|
// let cateList = []
|
|
state.goodList.map(n => {
|
|
if (!map1[n.USERDEFINEDTYPE_NAME]) {
|
|
map1[n.USERDEFINEDTYPE_NAME] = [n]
|
|
} else {
|
|
map1[n.USERDEFINEDTYPE_NAME].push(n)
|
|
}
|
|
})
|
|
return map1
|
|
},
|
|
cateCountMap: state => { // 拼接类别对应的选中商品数量 key: cateId value: count
|
|
let map1 = {}
|
|
state.cateList.map(n => {
|
|
map1[n.USERDEFINEDTYPE_ID] = n.count || 0
|
|
})
|
|
return map1
|
|
}
|
|
}
|
|
|
|
const mutations = {
|
|
[types.SHOPCART](state, newCate) {
|
|
state.shopCart.push(newCate)
|
|
},
|
|
[types.ORDERGOOD](state, ordergood) {
|
|
state.orderGood = ordergood
|
|
},
|
|
[types.CHANGSHOPCART](state, product) {
|
|
let origin = state.shopCart
|
|
origin.map(n => {
|
|
if (product.USERDEFINEDTYPE_ID === n.USERDEFINEDTYPE_ID) {
|
|
let hasPro = n.goodsList.findIndex(v => { return v.COMMODITY_ID === product.COMMODITY_ID })
|
|
if (hasPro > -1) {
|
|
n.goodsList[hasPro].count = product.count
|
|
} else {
|
|
n.goodsList.push(product)
|
|
}
|
|
}
|
|
})
|
|
state.shopCart = origin
|
|
},
|
|
[types.DELSHOPCART](state, product) {
|
|
let origin = state.shopCart
|
|
let num = ''
|
|
state.shopCart.map((n, i) => {
|
|
if (product.USERDEFINEDTYPE_ID === n.USERDEFINEDTYPE_ID) {
|
|
let hasPro = n.goodsList.findIndex(v => { return v.COMMODITY_ID === product.COMMODITY_ID })
|
|
if (hasPro > -1) {
|
|
n.goodsList.splice(hasPro, 1)
|
|
}
|
|
if (n.goodsList.length === 0) {
|
|
num = i
|
|
}
|
|
}
|
|
})
|
|
if (num !== '') {
|
|
state.shopCart.splice(num, 1)
|
|
}
|
|
state.shopCart = origin
|
|
},
|
|
[types.ADDGOOD](state, product) { // 添加采购数量
|
|
let hasPro = state.goodList.findIndex(v => { return v.COMMODITY_ID === product.COMMODITY_ID })
|
|
if (hasPro > -1) {
|
|
if (state.goodList[hasPro].count > 998) {
|
|
mpvue.showToast({
|
|
title: '购买数量为 1 ~ 999。',
|
|
icon: 'none'
|
|
|
|
})
|
|
return false
|
|
} else {
|
|
state.goodList[hasPro].count += 1
|
|
}
|
|
} else {
|
|
product.isCheck = true
|
|
product.count = 1
|
|
state.goodList.push(product)
|
|
}
|
|
},
|
|
[types.REDGOOD](state, product) { // 减去采购数量
|
|
let hasPro = state.goodList.findIndex(v => { return v.COMMODITY_ID === product.COMMODITY_ID })
|
|
if (hasPro > -1) {
|
|
if (state.goodList[hasPro].count > 1) {
|
|
state.goodList[hasPro].count--
|
|
}
|
|
}
|
|
},
|
|
[types.REMOVEGOOD](state, product) { // 删除单个商品
|
|
let hasPro = state.goodList.findIndex(v => { return v.COMMODITY_ID === product.COMMODITY_ID })
|
|
|
|
if (hasPro > -1) {
|
|
state.goodList.splice(hasPro, 1)
|
|
}
|
|
},
|
|
[types.DELTCHOESE](state, productIds) { // 购物车批量删除
|
|
productIds.map(product => {
|
|
// eslint-disable-next-line eqeqeq
|
|
let hasPro = state.goodList.findIndex(v => { return v.COMMODITY_ID == product })
|
|
if (hasPro > -1) {
|
|
state.goodList.splice(hasPro, 1)
|
|
}
|
|
})
|
|
},
|
|
[types.CHANGCOUNT](state, product) { // 购物车更改数量
|
|
let hasPro = state.goodList.findIndex(v => { return v.COMMODITY_ID === product.COMMODITY_ID })
|
|
if (hasPro > -1) {
|
|
state.goodList[hasPro].count = product.count
|
|
}
|
|
},
|
|
[types.CLEARCART](state) { // 购物车中清理已生成订单的商品
|
|
let newCart = []
|
|
|
|
state.goodList.map(v => {
|
|
let hasPro = state.orderGood.findIndex(pro => { return v.COMMODITY_ID === pro.COMMODITY_ID })
|
|
|
|
if (hasPro === -1) {
|
|
newCart.push(v)
|
|
}
|
|
})
|
|
state.goodList = newCart
|
|
},
|
|
[types.ADDCATE](state, cate) { // 添加采购商品所属类别数量
|
|
let hasCate = state.cateList.findIndex(n => { return n.USERDEFINEDTYPE_NAME === cate.USERDEFINEDTYPE_NAME })
|
|
if (hasCate > -1) {
|
|
state.cateList[hasCate].count++
|
|
} else {
|
|
cate.count = 1
|
|
state.cateList.push(cate)
|
|
}
|
|
},
|
|
[types.RESETCATE](state) { // 重置所选商品类别数量
|
|
let noCateIndex = []
|
|
state.cateList.map((n, i) => {
|
|
let cateCount = 0
|
|
state.goodList.map(v => {
|
|
if (n.USERDEFINEDTYPE_NAME === v.USERDEFINEDTYPE_NAME) {
|
|
cateCount += v.count
|
|
}
|
|
})
|
|
if (cateCount === 0) {
|
|
noCateIndex.push(i)
|
|
}
|
|
n.count = cateCount
|
|
})
|
|
|
|
if (noCateIndex.length) {
|
|
noCateIndex = noCateIndex.reverse()
|
|
noCateIndex.forEach(s => {
|
|
state.cateList.splice(s, 1)
|
|
})
|
|
}
|
|
},
|
|
[types.SETBADGE](state, goodNum) { // 设置购物车红色角标
|
|
if (goodNum > 0) {
|
|
mpvue.setTabBarBadge({
|
|
index: 1,
|
|
text: goodNum + ''
|
|
})
|
|
} else {
|
|
mpvue.removeTabBarBadge({
|
|
index: 1
|
|
|
|
})
|
|
}
|
|
},
|
|
[types.CHANGENUMSIGN](state, num) { // 设置购物车红色角标
|
|
if (num > 0) {
|
|
state.changeSign += num
|
|
} else {
|
|
state.changeSign = 1
|
|
}
|
|
},
|
|
[types.RESETCART](state, num) { // 重置购物车
|
|
state.shopCart = []
|
|
state.orderGood = [] // 即将生成订单的商品
|
|
state.totalPrice = 0
|
|
state.stateischang = false
|
|
state.changeSign = 1
|
|
state.goodList = [] // 购物车商品
|
|
state.cateList = []// 购物车商品类别
|
|
}
|
|
}
|
|
|
|
const actions = {
|
|
[types.CHANGSHOPCART]({ commit, getters }, product) { // 添加购物车
|
|
commit('CHANGSHOPCART', product)
|
|
// setTimeout(() => {
|
|
// commit('SETBADGE', getters.goodNum)
|
|
// }, 200)
|
|
},
|
|
[types.CHANGCOUNT]({ commit, getters }, product) { // 修改购物车商品数量
|
|
commit('CHANGCOUNT', product)
|
|
commit('CHANGENUMSIGN', 1)
|
|
setTimeout(() => {
|
|
commit('RESETCATE')
|
|
// commit('SETBADGE', getters.goodNum)
|
|
}, 200)
|
|
},
|
|
[types.REDGOOD]({ commit, getters }, product) { // 减购物车
|
|
commit('REDGOOD', product)
|
|
commit('CHANGENUMSIGN', 1)
|
|
setTimeout(() => {
|
|
commit('RESETCATE')
|
|
// commit('SETBADGE', getters.goodNum)
|
|
}, 200)
|
|
},
|
|
[types.ADDGOOD]({ commit, getters }, product) { // 添加购物车
|
|
commit('ADDGOOD', product)
|
|
commit('CHANGENUMSIGN', 1)
|
|
// setTimeout(() => {
|
|
// commit('SETBADGE', getters.goodNum)
|
|
// }, 200)
|
|
},
|
|
[types.ADDCATE]({ commit, getters }, cate) { // 添加购物车
|
|
commit('ADDCATE', cate)
|
|
// setTimeout(() => {
|
|
// commit('SETBADGE', getters.goodNum)
|
|
// }, 200)
|
|
},
|
|
[types.REMOVEGOOD]({ commit, getters }, product) { // 删购物车
|
|
commit('REMOVEGOOD', product)
|
|
commit('CHANGENUMSIGN', -1)
|
|
setTimeout(() => {
|
|
commit('RESETCATE')
|
|
// commit('SETBADGE', getters.goodNum)
|
|
}, 200)
|
|
},
|
|
[types.DELTCHOESE]({ commit, getters }, productIds) { // 批量删除购物车
|
|
commit('DELTCHOESE', productIds)
|
|
commit('CHANGENUMSIGN', 0)
|
|
setTimeout(() => {
|
|
commit('RESETCATE')
|
|
// commit('SETBADGE', getters.goodNum)
|
|
}, 200)
|
|
},
|
|
[types.CLEARCART]({ commit, getters }, product) { // 清楚已生成订单的购物车商品
|
|
commit('CLEARCART', product)
|
|
commit('CHANGENUMSIGN', 0)
|
|
setTimeout(() => {
|
|
commit('RESETCATE')
|
|
// commit('SETBADGE', getters.goodNum)
|
|
}, 200)
|
|
}
|
|
|
|
}
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state,
|
|
getters,
|
|
mutations,
|
|
actions
|
|
}
|