cart.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * 用户购物车仓库
  3. */
  4. import { productService } from '@/services/index.js'
  5. import { msg as showMsg } from '@/common/util.js'
  6. import { navigateTo } from '@/common/utilsTools.js'
  7. // 计算价格
  8. function totalShopPeice(goodsList) {
  9. return goodsList.map((item, index) => {
  10. const priceObj = item.productList.reduce((price, pros, i) => {
  11. // 处理阶梯价
  12. if (pros.activeStatus == 1) {
  13. pros.ladderList.forEach((item, index) => {
  14. if (pros.productCount >= item.buyNum) pros.price = item.buyPrice
  15. })
  16. } else {
  17. pros.price = pros.price
  18. }
  19. // 计算每个商品下商品的价格
  20. let _price = pros.price * pros.productCount
  21. price.totalOriginalPrice += _price
  22. if (pros.promotion && pros.promotion.type != 2 && pros.promotion.mode == 2) {
  23. if (_price >= pros.promotion.touchPrice) {
  24. _price = _price - pros.promotion.reducedPrice
  25. price.reducedPrice += pros.promotion.reducedPrice
  26. }
  27. price.totalPrice += _price
  28. } else {
  29. price.reducedPrice = 0
  30. price.totalPrice += pros.price * pros.productCount
  31. }
  32. return price
  33. }, {
  34. totalPrice: 0,
  35. reducedPrice: 0,
  36. totalOriginalPrice: 0
  37. })
  38. item.totalPrice = priceObj.totalPrice
  39. item.reducedPrice = priceObj.reducedPrice
  40. item.totalOriginalPrice = priceObj.totalOriginalPrice
  41. return item
  42. })
  43. }
  44. const state = {
  45. isEmpty: true,
  46. goodsList: [], //购物车的商品
  47. failureList: [], //失效商品列表
  48. cartIds: [], // 已勾选商品cartId列表
  49. kindCount: 0,
  50. }
  51. const mutations = {
  52. // 设置购物车商品数量
  53. setKindCount(state, count) {
  54. state.kindCount = count
  55. },
  56. // 设置购物车有效商品
  57. setGoodsList(state, list) {
  58. state.goodsList = list
  59. },
  60. // 设置购物车失效商品
  61. setFailureList(state, list) {
  62. state.failureList = list
  63. },
  64. // 设置购物车是否为空
  65. setEmptyType(state, flag) {
  66. state.isEmpty = flag
  67. },
  68. saveCartIds(state, ids) {
  69. state.cartIds = ids
  70. },
  71. // 勾选/不勾选 失效商品
  72. selectFailure(state, { productId, checked }) {
  73. state.failureList.forEach(prod => {
  74. if (prod.productId === productId) prod.productsChecked = checked
  75. })
  76. },
  77. // 勾选/取消勾选所有失效商品
  78. selectAllFailure(state, checked) {
  79. state.failureList.forEach(prod => {
  80. prod.productsChecked = checked
  81. })
  82. },
  83. // 重选商品
  84. resetSelectProducts(state, cartIds = []) {
  85. state.goodsList.forEach((shop) => {
  86. shop.productList.forEach(prod => {
  87. if (cartIds.includes(prod.cartId)) prod.productsChecked = true
  88. })
  89. shop.checked = shop.productList.every((prod) => prod.productsChecked === true)
  90. })
  91. },
  92. // 勾选商品
  93. selectProduct(state, { shopId, productId, checked }) {
  94. state.goodsList.forEach((shop) => {
  95. if (shop.shopId !== shopId) return
  96. shop.productList.forEach(prod => {
  97. if (prod.productId !== productId) return
  98. prod.productsChecked = checked
  99. })
  100. shop.checked = shop.productList.every((prod) => prod.productsChecked === true)
  101. })
  102. },
  103. // 全选/全不选 供应商下所有商品
  104. selectAllShopProduct(state, { shopId, checked }) {
  105. state.goodsList.forEach((shop) => {
  106. if (shop.shopId !== shopId) return
  107. shop.checked = checked
  108. shop.productList.forEach(prod => {
  109. prod.productsChecked = checked
  110. })
  111. })
  112. },
  113. // 全选/全不选 部商品
  114. selectAllProduct(state, checked) {
  115. state.goodsList.forEach((shop) => {
  116. shop.checked = checked
  117. shop.productList.forEach(prod => {
  118. prod.productsChecked = checked
  119. })
  120. })
  121. },
  122. // 设置购物车商品数量角标
  123. setTabBarBadge(state, num) {
  124. if (num >= 100) {
  125. uni.setTabBarBadge({
  126. index: 1,
  127. text: '99+'
  128. })
  129. } else if (num > 0) {
  130. uni.setTabBarBadge({
  131. index: 1,
  132. text: String(num)
  133. })
  134. } else {
  135. uni.removeTabBarBadge({
  136. index: 1,
  137. })
  138. }
  139. },
  140. }
  141. const actions = {
  142. // 初始化购物车
  143. initCart({ rootGetters, commit, state }) {
  144. return productService.QueryShoppingCartList({ userId: rootGetters.userId })
  145. .then(response => {
  146. let data = response.data
  147. commit('setTabBarBadge', data.cartQuantity)
  148. commit('setKindCount', data.cartQuantity)
  149. if (data.shopList.length > 0 || data.products.length > 0) {
  150. commit('setEmptyType', false)
  151. } else {
  152. commit('setEmptyType', true)
  153. }
  154. if (data.shopList && data.shopList.length > 0) {
  155. commit('setGoodsList', totalShopPeice(data.shopList))
  156. commit('resetSelectProducts', state.cartIds) // 勾选已选商品
  157. } else {
  158. commit('setGoodsList', [])
  159. }
  160. if (data.products && data.products.length > 0) {
  161. commit('setFailureList', data.products)
  162. } else {
  163. commit('setFailureList', [])
  164. }
  165. })
  166. .catch(error => {
  167. showMsg(error.msg, 2000)
  168. })
  169. },
  170. // 获取购物车商品数量
  171. getCartNumber({ commit, rootGetters }) {
  172. productService.QueryShoppingQuantity({ userId: rootGetters.userId })
  173. .then(response => {
  174. commit('setTabBarBadge', response.data)
  175. commit('setKindCount', response.data)
  176. })
  177. .catch(error => {
  178. console.log('查询购物车数量错误信息', error)
  179. })
  180. },
  181. // 添加购物车
  182. addToCart({ dispatch, rootGetters }, { productId, productCount = 1, heUserId = 0 }) {
  183. if (!rootGetters.hasLogin) return navigateTo('/pages/login/login')
  184. productService.shoppingAddCart({
  185. productId,
  186. userId: rootGetters.userId,
  187. productCount,
  188. heUserId
  189. }).then(response => {
  190. showMsg('加入购物车成功', 1500, true, 'success')
  191. dispatch('getCartNumber')
  192. }).catch(error => {
  193. showMsg(error.msg, 2000)
  194. })
  195. },
  196. // 从购物车移除
  197. removeFromCart({ dispatch, state }, cartIds = []) {
  198. // 从哪里获取cartIds
  199. const removeCartIds = cartIds.length > 0 ? cartIds : state.cartIds
  200. return productService.ShoppingCartDelete({
  201. cartIds: removeCartIds.join(',')
  202. }).then(response => {
  203. dispatch('initCart')
  204. showMsg('删除成功', 2000)
  205. }).catch(error => {
  206. showMsg(error.msg, 2000)
  207. })
  208. },
  209. // 删除失效商品
  210. removeFailureFromCart({ dispatch, state }) {
  211. const cartIds = state.failureList.reduce((cartIds, prod) => {
  212. cartIds.push(prod.cartId)
  213. return cartIds
  214. }, [])
  215. return dispatch('removeFromCart', cartIds)
  216. },
  217. // 加减购物车商品更新到后台
  218. updateShoppogCount({ dispatch, state }, { cartId, productCount }) {
  219. productService.ShoppingCartUpdate({
  220. cartId,
  221. productCount
  222. }).finally(() => {
  223. dispatch('initCart')
  224. })
  225. }
  226. }
  227. export default {
  228. namespaced: true,
  229. state,
  230. mutations,
  231. actions
  232. }