cart.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { defineStore } from 'pinia'
  2. import { getToken } from '@/utils/common'
  3. import { addCart, editCart, deleteCart, clearCart, getCartList, getCartSum } from '@/addon/mall/api/cart';
  4. interface Cart {
  5. cartList : AnyObject
  6. totalNum : number
  7. totalMoney : any
  8. isRepeat : boolean
  9. }
  10. const useCartStore = defineStore('cart', {
  11. state: () : Cart => {
  12. return {
  13. cartList: {}, // 购物车列表
  14. totalNum: 0, // 购物车商品总数量
  15. totalMoney: 0, // 购物车商品总价格
  16. isRepeat: false
  17. }
  18. },
  19. actions: {
  20. // 查询购物车列表
  21. getList() {
  22. if (!getToken()) return;
  23. getCartList({}).then(res => {
  24. let data = res.data;
  25. // 每次查询清空
  26. for (let k in this.cartList) {
  27. delete this.cartList[k]
  28. }
  29. if (Object.values(data).length) {
  30. Object.values(data).forEach((item:any) => {
  31. item.goods_list.forEach((subItem:any)=>{
  32. if (subItem.goods.status == 1 && subItem.goods.delete_time == 0) {
  33. let cart = {
  34. id: subItem.id,
  35. goods_id: subItem.goods_id,
  36. sku_id: subItem.sku_id,
  37. stock: subItem.goodsSku.stock,
  38. num: subItem.num,
  39. sale_price: subItem.goodsSku.sale_price,
  40. site_id: subItem.site_id
  41. }
  42. if (subItem.goods.is_discount && subItem.goodsSku.sale_price != subItem.goodsSku.price) {
  43. cart.sale_price = subItem.goodsSku.sale_price ? subItem.goodsSku.sale_price : subItem.goodsSku.price // 折扣价
  44. } else if (subItem.goods.member_discount && getToken() && subItem.goodsSku.member_price != subItem.goodsSku.price) {
  45. cart.sale_price = subItem.goodsSku.member_price ? subItem.goodsSku.member_price : subItem.goodsSku.price // 会员价
  46. }
  47. if (!this.cartList['goods_' + cart.goods_id]) {
  48. this.cartList['goods_' + cart.goods_id] = {};
  49. }
  50. this.cartList['goods_' + cart.goods_id]['sku_' + cart.sku_id] = cart;
  51. }
  52. })
  53. })
  54. }
  55. this.calculateNum();
  56. })
  57. },
  58. /**
  59. * 购物车数量增加
  60. * data:数据源
  61. * step:记步数量,默认为:1,每次加一个,设置为:0时,按照 num 修改
  62. */
  63. increase(data, step = 1, callback:any = null) {
  64. if (!data || (data && Object.keys(data).length == 0) || !data.goods_id || !data.sku_id) return;
  65. if (!getToken()) return;
  66. let num = data.num || 0; // 当前数量
  67. let updateNum = num + step; // 变更数量
  68. let api = data.id ? editCart : addCart;
  69. if (updateNum > parseInt(data.stock)) {
  70. ElMessage.error('商品库存不足')
  71. return;
  72. }
  73. if (this.isRepeat) return;
  74. this.isRepeat = true;
  75. // 更新存储数据
  76. if (data.id) {
  77. this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id].num = updateNum;
  78. } else {
  79. // 如果商品第一次添加,则初始化数据
  80. if (!this.cartList['goods_' + data.goods_id]) {
  81. this.cartList['goods_' + data.goods_id] = {};
  82. }
  83. this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id] = {
  84. id: data.id,
  85. goods_id: data.goods_id,
  86. sku_id: data.sku_id,
  87. stock: data.stock,
  88. num: updateNum,
  89. sale_price: data.sale_price,
  90. site_id: data.site_id
  91. }
  92. }
  93. this.calculateNum();
  94. api({
  95. id: data.id,
  96. goods_id: data.goods_id,
  97. sku_id: data.sku_id,
  98. num: updateNum,
  99. site_id: data.site_id
  100. }).then(res => {
  101. if(typeof callback == 'function') callback();
  102. this.getList()
  103. this.isRepeat = false;
  104. }).catch(res => {
  105. this.isRepeat = false;
  106. })
  107. },
  108. /**
  109. * 购物车数量减少
  110. * data:数据源
  111. * step:记步数量,默认为:1,每次减一个,设置为:0时,按照 num 修改
  112. */
  113. reduce(data, step = 1) {
  114. if (!data || (data && Object.keys(data).length == 0) || !data.goods_id || !data.sku_id) return;
  115. if (!getToken()) return;
  116. let num = data.num || 0; // 当前数量
  117. let updateNum = num - 1; // 变更数量
  118. let api = updateNum > 0 ? editCart : deleteCart;
  119. if (this.isRepeat) return;
  120. this.isRepeat = true;
  121. // 更新存储数据
  122. if (updateNum > 0) {
  123. this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id].num = updateNum;
  124. } else {
  125. delete this.cartList['goods_' + data.goods_id]['sku_' + data.sku_id];
  126. if (Object.keys(this.cartList['goods_' + data.goods_id]).length == 0) delete this.cartList['goods_' + data.goods_id];
  127. }
  128. this.calculateNum();
  129. api({
  130. ids: data.id, // 删除接口用
  131. id: data.id,
  132. goods_id: data.goods_id,
  133. sku_id: data.sku_id,
  134. num: updateNum,
  135. site_id: data.site_id
  136. }).then(res => {
  137. this.getList();
  138. this.isRepeat = false;
  139. }).catch(res => {
  140. this.isRepeat = false;
  141. })
  142. },
  143. // 购物车删除
  144. delete(ids, callback:any = null) {
  145. if (!ids) return;
  146. deleteCart({
  147. ids
  148. }).then(res => {
  149. this.getList();
  150. this.isRepeat = false;
  151. if (callback) callback();
  152. }).catch(res => {
  153. this.isRepeat = false;
  154. })
  155. },
  156. // 计算购物车商品的总数量、总价格
  157. calculateNum() {
  158. this.totalNum = 0;
  159. this.totalMoney = 0;
  160. if (Object.keys(this.cartList).length) {
  161. for (let goods in this.cartList) {
  162. let totalNum = 0;
  163. let totalMoney = 0;
  164. for (let sku in this.cartList[goods]) {
  165. if (typeof this.cartList[goods][sku] == 'object') {
  166. totalNum += this.cartList[goods][sku].num;
  167. totalMoney += this.cartList[goods][sku].num * this.cartList[goods][sku].sale_price;
  168. }
  169. }
  170. this.cartList[goods].totalNum = totalNum;
  171. this.cartList[goods].totalMoney = totalMoney.toFixed(2);
  172. this.totalNum += totalNum;
  173. this.totalMoney += totalMoney;
  174. }
  175. }
  176. this.totalMoney = this.totalMoney.toFixed(2);
  177. }
  178. }
  179. })
  180. export default useCartStore