cart.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {
  2. defineStore
  3. } from 'pinia';
  4. import CartApi from '@/sheep/api/trade/cart';
  5. const cart = defineStore({
  6. id: 'cart',
  7. state: () => ({
  8. shopNameMap: [],
  9. shopSkuMap: [],
  10. list: [], // 购物车列表
  11. selectedIds: [], // 已选列表
  12. isAllSelected: false, // 是否全选
  13. totalPriceSelected: 0, // 选中项总金额
  14. }),
  15. actions: {
  16. // 获取购物车列表
  17. async getList() {
  18. const {
  19. data,
  20. code
  21. } = await CartApi.getCartList();
  22. if (code === 0) {
  23. this.list = data.validList
  24. this.shopSkuMap = data.shopSkuMap
  25. this.shopNameMap = data.shopNameMap
  26. // 计算各种关联属性
  27. this.selectedIds = [];
  28. this.isAllSelected = true;
  29. this.totalPriceSelected = 0;
  30. this.list.forEach((item) => {
  31. if (item.selected) {
  32. this.selectedIds.push(item.id);
  33. this.totalPriceSelected += item.count * item.sku.price;
  34. } else {
  35. this.isAllSelected = false;
  36. }
  37. });
  38. }
  39. },
  40. // 添加购物车
  41. async add(goodsInfo) {
  42. // 添加购物项
  43. const {
  44. code
  45. } = await CartApi.addCart({
  46. skuId: goodsInfo.id,
  47. count: goodsInfo.goods_num,
  48. });
  49. // 刷新购物车列表
  50. if (code === 0) {
  51. await this.getList();
  52. }
  53. },
  54. // 更新购物车
  55. async update(goodsInfo) {
  56. const {
  57. code
  58. } = await CartApi.updateCartCount({
  59. id: goodsInfo.goods_id,
  60. count: goodsInfo.goods_num,
  61. });
  62. if (code === 0) {
  63. await this.getList();
  64. }
  65. },
  66. // 移除购物车
  67. async delete(ids) {
  68. let id;
  69. if (typeof ids === 'number') {
  70. id = ids
  71. }else {
  72. id = ids.join(',')
  73. }
  74. const {
  75. code
  76. } = await CartApi.deleteCart(id);
  77. if (code === 0) {
  78. await this.getList();
  79. }
  80. },
  81. // 单选购物车商品
  82. async selectSingle(goodsId) {
  83. const {
  84. code
  85. } = await CartApi.updateCartSelected({
  86. ids: [goodsId],
  87. selected: !this.selectedIds.includes(goodsId), // 取反
  88. });
  89. if (code === 0) {
  90. await this.getList();
  91. }
  92. },
  93. // 全选购物车商品
  94. async selectAll(flag) {
  95. const {
  96. code
  97. } = await CartApi.updateCartSelected({
  98. ids: this.list.map((item) => item.id),
  99. selected: flag
  100. });
  101. if (code === 0) {
  102. await this.getList();
  103. }
  104. },
  105. // 清空购物车。注意,仅用于用户退出时,重置数据
  106. emptyList() {
  107. this.list = [];
  108. this.selectedIds = [];
  109. this.isAllSelected = true;
  110. this.totalPriceSelected = 0;
  111. },
  112. },
  113. persist: {
  114. enabled: true,
  115. strategies: [{
  116. key: 'cart-store',
  117. }, ],
  118. },
  119. });
  120. export default cart;