123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- import { defineStore } from 'pinia'
- import { getToken } from '@/utils/common'
- import { addCart, editCart, deleteCart, clearCart, getCartList, getCartSum } from '@/addon/mall/api/cart';
- interface Cart {
- cartList : AnyObject
- totalNum : number
- totalMoney : any
- isRepeat : boolean
- }
- const useCartStore = defineStore('cart', {
- state: () : Cart => {
- return {
- cartList: {},
- totalNum: 0,
- totalMoney: 0,
- isRepeat: false
- }
- },
- actions: {
-
- getList() {
- if (!getToken()) return;
- getCartList({}).then(res => {
- let data = res.data;
-
- for (let k in this.cartList) {
- delete this.cartList[k]
- }
- if (Object.values(data).length) {
- Object.values(data).forEach((item:any) => {
- item.goods_list.forEach((subItem:any)=>{
- if (subItem.goods.status == 1 && subItem.goods.delete_time == 0) {
- let cart = {
- id: subItem.id,
- goods_id: subItem.goods_id,
- sku_id: subItem.sku_id,
- stock: subItem.goodsSku.stock,
- num: subItem.num,
- sale_price: subItem.goodsSku.sale_price,
- site_id: subItem.site_id
- }
- if (subItem.goods.is_discount && subItem.goodsSku.sale_price != subItem.goodsSku.price) {
- cart.sale_price = subItem.goodsSku.sale_price ? subItem.goodsSku.sale_price : subItem.goodsSku.price
- } else if (subItem.goods.member_discount && getToken() && subItem.goodsSku.member_price != subItem.goodsSku.price) {
- cart.sale_price = subItem.goodsSku.member_price ? subItem.goodsSku.member_price : subItem.goodsSku.price
- }
-
- if (!this.cartList['goods_' + cart.goods_id]) {
- this.cartList['goods_' + cart.goods_id] = {};
- }
- this.cartList['goods_' + cart.goods_id]['sku_' + cart.sku_id] = cart;
- }
- })
- })
- }
- this.calculateNum();
- })
- },
-
- increase(data, step = 1, callback:any = null) {
- if (!data || (data && Object.keys(data).length == 0) || !data.goods_id || !data.sku_id) return;
- if (!getToken()) return;
- let num = data.num || 0;
- let updateNum = num + step;
- let api = data.id ? editCart : addCart;
- if (updateNum > parseInt(data.stock)) {
- ElMessage.error('商品库存不足')
- return;
- }
- if (this.isRepeat) return;
- this.isRepeat = true;
-
- if (data.id) {
- this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id].num = updateNum;
- } else {
-
- if (!this.cartList['goods_' + data.goods_id]) {
- this.cartList['goods_' + data.goods_id] = {};
- }
- this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id] = {
- id: data.id,
- goods_id: data.goods_id,
- sku_id: data.sku_id,
- stock: data.stock,
- num: updateNum,
- sale_price: data.sale_price,
- site_id: data.site_id
- }
- }
- this.calculateNum();
- api({
- id: data.id,
- goods_id: data.goods_id,
- sku_id: data.sku_id,
- num: updateNum,
- site_id: data.site_id
- }).then(res => {
- if(typeof callback == 'function') callback();
- this.getList()
- this.isRepeat = false;
- }).catch(res => {
- this.isRepeat = false;
- })
- },
-
- reduce(data, step = 1) {
- if (!data || (data && Object.keys(data).length == 0) || !data.goods_id || !data.sku_id) return;
- if (!getToken()) return;
- let num = data.num || 0;
- let updateNum = num - step;
- let api = updateNum > 0 ? editCart : deleteCart;
- if (this.isRepeat) return;
- this.isRepeat = true;
-
- if (updateNum > 0) {
- this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id].num = updateNum;
- } else {
- delete this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id];
- if (Object.keys(this.cartList['goods_' + data.goods_id]).length == 0) delete this.cartList['goods_' + data.goods_id];
- }
- this.calculateNum();
- api({
- ids: data.id,
- id: data.id,
- goods_id: data.goods_id,
- sku_id: data.sku_id,
- num: updateNum,
- site_id: data.site_id
- }).then(res => {
- this.getList();
- this.isRepeat = false;
- }).catch(res => {
- this.isRepeat = false;
- })
- },
-
- delete(ids, callback:any = null) {
- if (!ids) return;
- deleteCart({
- ids
- }).then(res => {
- this.getList();
- this.isRepeat = false;
- if (callback) callback();
- }).catch(res => {
- this.isRepeat = false;
- })
- },
-
- calculateNum() {
- this.totalNum = 0;
- this.totalMoney = 0;
- if (Object.keys(this.cartList).length) {
- for (let goods in this.cartList) {
- let totalNum = 0;
- let totalMoney = 0;
- for (let sku in this.cartList[goods]) {
- if (typeof this.cartList[goods][sku] == 'object') {
- totalNum += this.cartList[goods][sku].num;
- totalMoney += this.cartList[goods][sku].num * this.cartList[goods][sku].sale_price;
- }
- }
- this.cartList[goods].totalNum = totalNum;
- this.cartList[goods].totalMoney = totalMoney.toFixed(2);
- this.totalNum += totalNum;
- this.totalMoney += totalMoney;
- }
- }
- this.totalMoney = this.totalMoney.toFixed(2);
- }
- }
- })
- export default useCartStore
|