diff --git a/pages/goods/index.vue b/pages/goods/index.vue index 19601af1..76f5a369 100644 --- a/pages/goods/index.vue +++ b/pages/goods/index.vue @@ -138,7 +138,7 @@ state.selectedSku = e; } - // 添加购物车 TODO 芋艿:待测试 + // 添加购物车 function onAddCart(e) { sheep.$store('cart').add(e); } diff --git a/pages/index/cart.vue b/pages/index/cart.vue index 02c052f1..3ba03389 100644 --- a/pages/index/cart.vue +++ b/pages/index/cart.vue @@ -1,200 +1,220 @@ + \ No newline at end of file + .goods-box { + background-color: #fff; + } + } + } + diff --git a/sheep/api/cart.js b/sheep/api/cart.js index f4aacaa3..c89eadd9 100644 --- a/sheep/api/cart.js +++ b/sheep/api/cart.js @@ -15,6 +15,7 @@ export default { request({ url: 'cart', method: 'POST', + // TODO 芋艿:这里没提示 custom: { showSuccess: true, successMsg: '已添加到购物车~', diff --git a/sheep/api/trade/cart.js b/sheep/api/trade/cart.js new file mode 100644 index 00000000..f6a0104a --- /dev/null +++ b/sheep/api/trade/cart.js @@ -0,0 +1,47 @@ +import request2 from '@/sheep/request2'; + +const CartApi = { + addCart: (data) => { + return request2({ + url: '/app-api/trade/cart/add', + method: 'POST', + data: data, + // TODO 芋艿:这里没提示 + custom: { + showSuccess: true, + successMsg: '已添加到购物车~', + } + }); + }, + updateCartCount: (data) => { + return request2({ + url: '/app-api/trade/cart/update-count', + method: 'PUT', + data: data + }); + }, + updateCartSelected: (data) => { + return request2({ + url: '/app-api/trade/cart/update-selected', + method: 'PUT', + data: data + }); + }, + deleteCart: (ids) => { + return request2({ + url: '/app-api/trade/cart/delete', + method: 'DELETE', + params: { + ids + } + }); + }, + getCartList: () => { + return request2({ + url: '/app-api/trade/cart/list', + method: 'GET', + }); + }, +}; + +export default CartApi; \ No newline at end of file diff --git a/sheep/store/cart.js b/sheep/store/cart.js index 8e076343..6e70d4a4 100644 --- a/sheep/store/cart.js +++ b/sheep/store/cart.js @@ -1,98 +1,93 @@ import { defineStore } from 'pinia'; -import cartApi from '@/sheep/api/cart'; +import CartApi from '@/sheep/api/trade/cart'; const cart = defineStore({ id: 'cart', state: () => ({ list: [], // 购物车列表 selectedIds: [], // 已选列表 - isAllSelected: false, //是否全选 - cartSelectedTotalPrice: '0.00', // 选中项总金额 + isAllSelected: false, // 是否全选 + totalPriceSelected: 0, // 选中项总金额 }), - getters: { - totalPriceSelected: (state) => { - let price = 0; - if (!state.selectedIds.length) return price.toFixed(2); - state.list.forEach((item) => { - price += state.selectedIds.includes(item.id) - ? Number(item.sku.price/100) * item.count - : 0; - }); - return price.toFixed(2); - }, - }, actions: { // 获取购物车列表 async getList() { - const { data, code } = await cartApi.list(); + const { data, code } = await CartApi.getCartList(); if (code === 0) { this.list = data.validList; + + // 计算各种关联属性 + this.selectedIds = []; + this.isAllSelected = true; + this.totalPriceSelected = 0; + this.list.forEach((item) => { + if (item.selected) { + this.selectedIds.push(item.id); + this.totalPriceSelected += item.count * item.sku.price; + } else { + this.isAllSelected = false; + } + }); } }, + // 添加购物车 async add(goodsInfo) { - const { error } = await cartApi.append({ - goods_id: goodsInfo.goods_id, - goods_num: goodsInfo.goods_num, - goods_sku_price_id: goodsInfo.id, + // 添加购物项 + const { code } = await CartApi.addCart({ + skuId: goodsInfo.id, + count: goodsInfo.goods_num, }); - if (error === 0) { - this.getList(); + // 刷新购物车列表 + if (code === 0) { + await this.getList(); } }, // 更新购物车 async update(goodsInfo) { - const { error } = await cartApi.update({ + const { code } = await CartApi.updateCartCount({ id: goodsInfo.goods_id, count: goodsInfo.goods_num, - goods_sku_price_id: goodsInfo.goods_sku_price_id, }); - if (error === 0) { - // this.getList(); + if (code === 0) { + await this.getList(); } }, // 移除购物车 async delete(ids) { - if (typeof ids === 'array') { - ids = ids.join(','); - } - const { code } = await cartApi.delete(ids); + const { code } = await CartApi.deleteCart(ids.join(',')); if (code === 0) { - this.selectAll(false); - this.getList(); + await this.getList(); } }, - // 选择购物车商品 - selectSingle(goodsId) { - if (!this.selectedIds.includes(goodsId)) { - this.selectedIds.push(goodsId); - } else { - this.selectedIds.splice(this.selectedIds.indexOf(goodsId), 1); + // 单选购物车商品 + async selectSingle(goodsId) { + const { code } = await CartApi.updateCartSelected({ + ids: [goodsId], + selected: !this.selectedIds.includes(goodsId), // 取反 + }); + if (code === 0) { + await this.getList(); } - this.isAllSelected = this.selectedIds.length === this.list.length; }, - // 全选 - selectAll(flag) { - this.isAllSelected = flag; - if (!flag) { - this.selectedIds = []; - } else { - this.list.forEach((item) => { - this.selectedIds.push(item.id); - }); + // 全选购物车商品 + async selectAll(flag) { + const { code } = await CartApi.updateCartSelected({ + ids: this.list.map((item) => item.id), + selected: flag + }); + if (code === 0) { + await this.getList(); } }, // 清空购物车 - emptyList() { - this.list = []; - this.selectedIds = []; - this.isAllSelected = false; - this.cartSelectedTotalPrice = '0.00'; + async emptyList() { + await this.delete(this.list.map((item) => item.id)); }, }, persist: {