cart.js 7.8 KB

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