cart.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const sheep_api_trade_cart = require("../api/trade/cart.js");
  4. const cart = common_vendor.defineStore({
  5. id: "cart",
  6. state: () => ({
  7. shopNameMap: [],
  8. shopSkuMap: [],
  9. list: [],
  10. // 购物车列表
  11. selectedIds: [],
  12. // 已选列表
  13. isAllSelected: false,
  14. // 是否全选
  15. totalPriceSelected: 0
  16. // 选中项总金额
  17. }),
  18. actions: {
  19. // 获取购物车列表
  20. async getList() {
  21. const {
  22. data,
  23. code
  24. } = await sheep_api_trade_cart.CartApi.getCartList();
  25. if (code === 0) {
  26. this.list = data.validList;
  27. this.shopSkuMap = data.shopSkuMap;
  28. this.shopNameMap = data.shopNameMap;
  29. this.selectedIds = [];
  30. this.isAllSelected = true;
  31. this.totalPriceSelected = 0;
  32. this.list.forEach((item) => {
  33. if (item.selected) {
  34. this.selectedIds.push(item.id);
  35. this.totalPriceSelected += item.count * item.sku.price;
  36. } else {
  37. this.isAllSelected = false;
  38. }
  39. });
  40. }
  41. },
  42. // 添加购物车
  43. async add(goodsInfo) {
  44. const {
  45. code
  46. } = await sheep_api_trade_cart.CartApi.addCart({
  47. skuId: goodsInfo.id,
  48. count: goodsInfo.goods_num
  49. });
  50. if (code === 0) {
  51. await this.getList();
  52. }
  53. },
  54. // 更新购物车
  55. async update(goodsInfo) {
  56. const {
  57. code
  58. } = await sheep_api_trade_cart.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 sheep_api_trade_cart.CartApi.deleteCart(id);
  77. if (code === 0) {
  78. await this.getList();
  79. }
  80. },
  81. // 单选购物车商品
  82. async selectSingle(goodsId) {
  83. const {
  84. code
  85. } = await sheep_api_trade_cart.CartApi.updateCartSelected({
  86. ids: [goodsId],
  87. selected: !this.selectedIds.includes(goodsId)
  88. // 取反
  89. });
  90. if (code === 0) {
  91. await this.getList();
  92. }
  93. },
  94. // 全选购物车商品
  95. async selectAll(flag) {
  96. const {
  97. code
  98. } = await sheep_api_trade_cart.CartApi.updateCartSelected({
  99. ids: this.list.map((item) => item.id),
  100. selected: flag
  101. });
  102. if (code === 0) {
  103. await this.getList();
  104. }
  105. },
  106. // 清空购物车。注意,仅用于用户退出时,重置数据
  107. emptyList() {
  108. this.list = [];
  109. this.selectedIds = [];
  110. this.isAllSelected = true;
  111. this.totalPriceSelected = 0;
  112. }
  113. },
  114. persist: {
  115. enabled: true,
  116. strategies: [{
  117. key: "cart-store"
  118. }]
  119. }
  120. });
  121. const __vite_glob_0_1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  122. __proto__: null,
  123. default: cart
  124. }, Symbol.toStringTag, { value: "Module" }));
  125. exports.__vite_glob_0_1 = __vite_glob_0_1;
  126. exports.cart = cart;