cart.js 3.0 KB

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