cart.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. list: [],
  8. // 购物车列表
  9. selectedIds: [],
  10. // 已选列表
  11. isAllSelected: false,
  12. // 是否全选
  13. totalPriceSelected: 0
  14. // 选中项总金额
  15. }),
  16. actions: {
  17. // 获取购物车列表
  18. async getList() {
  19. const { data, code } = await sheep_api_trade_cart.CartApi.getCartList();
  20. if (code === 0) {
  21. this.list = data.validList;
  22. this.selectedIds = [];
  23. this.isAllSelected = true;
  24. this.totalPriceSelected = 0;
  25. this.list.forEach((item) => {
  26. if (item.selected) {
  27. this.selectedIds.push(item.id);
  28. this.totalPriceSelected += item.count * item.sku.price;
  29. } else {
  30. this.isAllSelected = false;
  31. }
  32. });
  33. }
  34. },
  35. // 添加购物车
  36. async add(goodsInfo) {
  37. const { code } = await sheep_api_trade_cart.CartApi.addCart({
  38. skuId: goodsInfo.id,
  39. count: goodsInfo.goods_num
  40. });
  41. if (code === 0) {
  42. await this.getList();
  43. }
  44. },
  45. // 更新购物车
  46. async update(goodsInfo) {
  47. const { code } = await sheep_api_trade_cart.CartApi.updateCartCount({
  48. id: goodsInfo.goods_id,
  49. count: goodsInfo.goods_num
  50. });
  51. if (code === 0) {
  52. await this.getList();
  53. }
  54. },
  55. // 移除购物车
  56. async delete(ids) {
  57. const { code } = await sheep_api_trade_cart.CartApi.deleteCart(ids.join(","));
  58. if (code === 0) {
  59. await this.getList();
  60. }
  61. },
  62. // 单选购物车商品
  63. async selectSingle(goodsId) {
  64. const { code } = await sheep_api_trade_cart.CartApi.updateCartSelected({
  65. ids: [goodsId],
  66. selected: !this.selectedIds.includes(goodsId)
  67. // 取反
  68. });
  69. if (code === 0) {
  70. await this.getList();
  71. }
  72. },
  73. // 全选购物车商品
  74. async selectAll(flag) {
  75. const { code } = await sheep_api_trade_cart.CartApi.updateCartSelected({
  76. ids: this.list.map((item) => item.id),
  77. selected: flag
  78. });
  79. if (code === 0) {
  80. await this.getList();
  81. }
  82. },
  83. // 清空购物车。注意,仅用于用户退出时,重置数据
  84. emptyList() {
  85. this.list = [];
  86. this.selectedIds = [];
  87. this.isAllSelected = true;
  88. this.totalPriceSelected = 0;
  89. }
  90. },
  91. persist: {
  92. enabled: true,
  93. strategies: [
  94. {
  95. key: "cart-store"
  96. }
  97. ]
  98. }
  99. });
  100. const __vite_glob_0_1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  101. __proto__: null,
  102. default: cart
  103. }, Symbol.toStringTag, { value: "Module" }));
  104. exports.__vite_glob_0_1 = __vite_glob_0_1;
  105. exports.cart = cart;