cartStore.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. class CartStore {
  2. constructor() {
  3. this.storageKey = 'shopping_cart';
  4. this.currentShopId = ''; // 当前店铺ID
  5. }
  6. // 设置当前店铺ID
  7. setShopId() {
  8. this.currentShopId = uni.getStorageSync('shop_id');
  9. }
  10. // 获取购物车数据
  11. getCart() {
  12. try {
  13. const cartStr = uni.getStorageSync(this.storageKey);
  14. if (!cartStr) {
  15. return {};
  16. }
  17. return JSON.parse(cartStr);
  18. } catch (e) {
  19. console.error('获取购物车失败:', e);
  20. return {};
  21. }
  22. }
  23. // 保存购物车数据
  24. saveCart(cartData) {
  25. try {
  26. uni.setStorageSync(this.storageKey, JSON.stringify(cartData));
  27. } catch (e) {
  28. console.error('保存购物车失败:', e);
  29. }
  30. }
  31. // 获取指定店铺的购物车
  32. getShopCart(shopId = null) {
  33. const cart = this.getCart();
  34. const targetShopId = shopId || this.currentShopId;
  35. if (!targetShopId) return null;
  36. if (!cart[targetShopId]) {
  37. cart[targetShopId] = {
  38. total_price: 0, //所有价格总和
  39. total_num: 0,
  40. goods_list: [],
  41. pack_price: 0, //配送费总额
  42. total_goods_money: 0, //商品价格总额
  43. shop_info: null
  44. };
  45. this.saveCart(cart);
  46. }
  47. return cart[targetShopId];
  48. }
  49. // 添加商品到购物车
  50. addToCart(goods, goodsSpec = [], quantity = 1) {
  51. if (!this.currentShopId) {
  52. console.error('未设置店铺ID');
  53. return false;
  54. }
  55. const cart = this.getCart();
  56. if (!cart[this.currentShopId]) {
  57. cart[this.currentShopId] = {
  58. total_price: 0,
  59. total_num: 0,
  60. goods_list: [],
  61. pack_price: 0, //配送费总额
  62. total_goods_money: 0, //商品价格总额
  63. shop_info: null
  64. };
  65. }
  66. const shopCart = cart[this.currentShopId];
  67. // 生成商品唯一标识(结合商品ID和规格ID)
  68. const specIdStr = goodsSpec.length > 0 ?
  69. goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') :
  70. 'no_spec';
  71. const goodsKey = `${goods.goods_id}_${specIdStr}`;
  72. // 计算商品单价
  73. const unitPrice = this.calculatePrice(goods, goodsSpec);
  74. // 获取商品配送费
  75. const packPrice = parseFloat(goods.pack_price || 0);
  76. // 查找是否已存在相同商品
  77. const existingIndex = shopCart.goods_list.findIndex(
  78. item => this.getGoodsKey(item) === goodsKey
  79. );
  80. if (existingIndex > -1) {
  81. // 已存在,更新数量
  82. shopCart.goods_list[existingIndex].goods_number += quantity;
  83. } else {
  84. // 新商品,添加到列表
  85. shopCart.goods_list.push({
  86. goods_id: goods.goods_id,
  87. goods_name: goods.goods_name || '',
  88. goods_image: goods.goods_image || '',
  89. // price: this.calculatePrice(goods, goodsSpec),
  90. price: unitPrice,
  91. pack_price: packPrice, // 单个商品配送费
  92. goods_number: quantity,
  93. goods_attr: this.getSpecAttr(goodsSpec),
  94. goods_spec: goodsSpec,
  95. category_id: goods.category_id,
  96. goods_desc: goods.goods_desc || '',
  97. spec_type: goods.spec_type || 1
  98. });
  99. }
  100. // 重新计算总计
  101. this.calculateTotals(shopCart);
  102. // 保存到本地存储
  103. this.saveCart(cart);
  104. return true;
  105. }
  106. // 更新商品数量
  107. updateQuantity(goodsId, goodsSpec = [], quantity) {
  108. if (!this.currentShopId) return false;
  109. const cart = this.getCart();
  110. const shopCart = cart[this.currentShopId];
  111. if (!shopCart) return false;
  112. const specIdStr = goodsSpec.length > 0 ?
  113. goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') :
  114. 'no_spec';
  115. const goodsKey = `${goodsId}_${specIdStr}`;
  116. const goodsIndex = shopCart.goods_list.findIndex(
  117. item => this.getGoodsKey(item) === goodsKey
  118. );
  119. if (goodsIndex > -1) {
  120. if (quantity <= 0) {
  121. // 数量为0,移除商品
  122. shopCart.goods_list.splice(goodsIndex, 1);
  123. } else {
  124. // 更新数量
  125. shopCart.goods_list[goodsIndex].goods_number = quantity;
  126. }
  127. // 重新计算总计
  128. this.calculateTotals(shopCart);
  129. this.saveCart(cart);
  130. return true;
  131. }
  132. return false;
  133. }
  134. // 从购物车移除商品
  135. removeFromCart(goodsId, goodsSpec = []) {
  136. return this.updateQuantity(goodsId, goodsSpec, 0);
  137. }
  138. // 清空当前店铺购物车
  139. clearCurrentShopCart() {
  140. if (!this.currentShopId) return false;
  141. const cart = this.getCart();
  142. cart[this.currentShopId] = {
  143. total_price: 0,
  144. total_num: 0,
  145. goods_list: [],
  146. pack_price: 0,
  147. total_goods_money: 0,
  148. shop_info: null
  149. };
  150. this.saveCart(cart);
  151. return true;
  152. }
  153. // 计算商品价格
  154. calculatePrice(goods, goodsSpec = []) {
  155. let price = goods.seckill_money > 0 ? Number(goods.seckill_money) : Number(goods.price);
  156. // 如果有规格,累加规格价格
  157. if (goodsSpec.length > 0) {
  158. goodsSpec.forEach(spec => {
  159. // 这里需要根据实际情况获取规格价格
  160. // 假设规格信息中包含价格
  161. if (spec.price) {
  162. price += Number(spec.price) * (spec.number || 1);
  163. }
  164. });
  165. }
  166. return Number(price).toFixed(2);
  167. }
  168. // 获取规格描述
  169. getSpecAttr(goodsSpec = []) {
  170. if (goodsSpec.length === 0) return '';
  171. return goodsSpec.map(spec => spec.name || '').join('、');
  172. }
  173. // 生成商品唯一键
  174. getGoodsKey(goodsItem) {
  175. const specIdStr = goodsItem.goods_spec && goodsItem.goods_spec.length > 0 ?
  176. goodsItem.goods_spec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') :
  177. 'no_spec';
  178. return `${goodsItem.goods_id}_${specIdStr}`;
  179. }
  180. // 计算购物车总计
  181. calculateTotals(shopCart) {
  182. // let total_price = 0;
  183. let total_goods_money = 0; // 商品总额(不含配送费)
  184. let total_pack_price = 0; // 配送费总额
  185. let total_num = 0;
  186. // 计算商品总额和总数量
  187. shopCart.goods_list.forEach(item => {
  188. const itemGoodsMoney = item.price * item.goods_number; // 商品金额
  189. const itemPackPrice = parseFloat(item.pack_price || 0) * item.goods_number; // 配送费金额
  190. const itemTotal = item.price * item.goods_number;
  191. // total_goods_money += itemTotal;
  192. // total_num += item.goods_number;
  193. total_goods_money += itemGoodsMoney;
  194. total_pack_price += itemPackPrice;
  195. total_num += item.goods_number;
  196. });
  197. // 更新商品总额
  198. shopCart.total_goods_money = parseFloat(total_goods_money.toFixed(2));
  199. // 更新配送费总额
  200. shopCart.pack_price = parseFloat(total_pack_price.toFixed(2));
  201. // 更新商品总数量
  202. shopCart.total_num = total_num;
  203. // 计算总价:商品总额 + 配送费
  204. const total_price = shopCart.total_goods_money + (shopCart.pack_price || 0);
  205. shopCart.total_price = parseFloat(total_price.toFixed(2));
  206. // shopCart.goods_list.forEach(item => {
  207. // total_price += item.price * item.goods_number;
  208. // total_num += item.goods_number;
  209. // });
  210. // shopCart.total_price = parseFloat(total_price.toFixed(2));
  211. // shopCart.total_num = total_num;
  212. }
  213. // 获取商品在购物车中的数量
  214. getGoodsQuantity(goodsId, goodsSpec = []) {
  215. if (!this.currentShopId) return 0;
  216. const cart = this.getCart();
  217. const shopCart = cart[this.currentShopId];
  218. if (!shopCart) return 0;
  219. const specIdStr = goodsSpec.length > 0 ?
  220. goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') :
  221. 'no_spec';
  222. const goodsKey = `${goodsId}_${specIdStr}`;
  223. const goodsItem = shopCart.goods_list.find(
  224. item => this.getGoodsKey(item) === goodsKey
  225. );
  226. return goodsItem ? goodsItem.goods_number : 0;
  227. }
  228. // 获取指定店铺购物车中某商品的总数量(所有规格合并)
  229. getGoodsTotalQuantity(goodsId) {
  230. if (!this.currentShopId) return 0;
  231. const cart = this.getCart();
  232. const shopCart = cart[this.currentShopId];
  233. if (!shopCart) return 0;
  234. let total = 0;
  235. shopCart.goods_list.forEach(item => {
  236. if (item.goods_id === goodsId) {
  237. total += item.goods_number;
  238. }
  239. });
  240. return total;
  241. }
  242. // 获取购物车统计数据
  243. getCartStats(shopId = null) {
  244. const shopCart = this.getShopCart(shopId);
  245. if (!shopCart) {
  246. return {
  247. total_price: 0,
  248. total_num: 0,
  249. goods_list: [],
  250. pack_price: 0,
  251. total_goods_money: 0
  252. };
  253. }
  254. return {
  255. total_price: shopCart.total_price || 0,
  256. total_num: shopCart.total_num || 0,
  257. goods_list: shopCart.goods_list || [],
  258. pack_price: shopCart.pack_price || 0,
  259. total_goods_money: shopCart.total_goods_money || 0
  260. };
  261. }
  262. // 获取商品总额(不含配送费)
  263. getGoodsTotalMoney(shopId = null) {
  264. const shopCart = this.getShopCart(shopId);
  265. return shopCart ? (shopCart.total_goods_money || 0) : 0;
  266. }
  267. // 获取配送费
  268. getPackPrice(shopId = null) {
  269. const shopCart = this.getShopCart(shopId);
  270. return shopCart ? (shopCart.pack_price || 0) : 0;
  271. }
  272. // 获取总价(商品总额+配送费)
  273. getTotalPrice(shopId = null) {
  274. const shopCart = this.getShopCart(shopId);
  275. return shopCart ? (shopCart.total_price || 0) : 0;
  276. }
  277. // 获取单个商品在购物车中的信息
  278. getGoodsInfo(goodsId, goodsSpec = []) {
  279. if (!this.currentShopId) return null;
  280. const cart = this.getCart();
  281. const shopCart = cart[this.currentShopId];
  282. if (!shopCart) return null;
  283. const specIdStr = goodsSpec.length > 0 ?
  284. goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') :
  285. 'no_spec';
  286. const goodsKey = `${goodsId}_${specIdStr}`;
  287. return shopCart.goods_list.find(
  288. item => this.getGoodsKey(item) === goodsKey
  289. );
  290. }
  291. }
  292. // 创建单例
  293. const cartStore = new CartStore();
  294. export default cartStore;