cart.ts 8.0 KB

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