cart.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. productIds: [],
  50. kindCount: 0,
  51. }
  52. const mutations = {
  53. // 设置购物车商品数量
  54. setKindCount(state, count) {
  55. state.kindCount = count
  56. },
  57. // 设置购物车有效商品
  58. setGoodsList(state, list) {
  59. state.goodsList = list
  60. },
  61. // 设置购物车失效商品
  62. setFailureList(state, list) {
  63. state.failureList = list
  64. },
  65. // 设置购物车是否为空
  66. setEmptyType(state, flag) {
  67. state.isEmpty = flag
  68. },
  69. saveCartIds(state, ids) {
  70. state.cartIds = ids
  71. },
  72. saveProductIds(state, ids){
  73. state.productIds = ids
  74. },
  75. // 勾选/不勾选 失效商品
  76. selectFailure(state, { productId, checked }) {
  77. state.failureList.forEach(prod => {
  78. if (prod.productId === productId) prod.productsChecked = checked
  79. })
  80. },
  81. // 勾选/取消勾选所有失效商品
  82. selectAllFailure(state, checked) {
  83. state.failureList.forEach(prod => {
  84. prod.productsChecked = checked
  85. })
  86. },
  87. // 重选商品
  88. resetSelectProducts(state, cartIds = []) {
  89. state.goodsList.forEach((shop) => {
  90. shop.productList.forEach(prod => {
  91. if (cartIds.includes(prod.cartId)) prod.productsChecked = true
  92. })
  93. shop.checked = shop.productList.every((prod) => prod.productsChecked === true)
  94. })
  95. },
  96. // 勾选商品
  97. selectProduct(state, { shopId, productId, checked }) {
  98. state.goodsList.forEach((shop) => {
  99. if (shop.shopId !== shopId) return
  100. shop.productList.forEach(prod => {
  101. if (prod.productId !== productId) return
  102. prod.productsChecked = checked
  103. })
  104. shop.checked = shop.productList.every((prod) => prod.productsChecked === true)
  105. })
  106. },
  107. // 全选/全不选 供应商下所有商品
  108. selectAllShopProduct(state, { shopId, checked }) {
  109. state.goodsList.forEach((shop) => {
  110. if (shop.shopId !== shopId) return
  111. shop.checked = checked
  112. shop.productList.forEach(prod => {
  113. prod.productsChecked = checked
  114. })
  115. })
  116. },
  117. // 全选/全不选 部商品
  118. selectAllProduct(state, checked) {
  119. state.goodsList.forEach((shop) => {
  120. shop.checked = checked
  121. shop.productList.forEach(prod => {
  122. prod.productsChecked = checked
  123. })
  124. })
  125. },
  126. // 设置购物车商品数量角标
  127. setTabBarBadge(state, num) {
  128. if (num >= 100) {
  129. uni.setTabBarBadge({
  130. index: 1,
  131. text: '99+'
  132. })
  133. } else if (num > 0) {
  134. uni.setTabBarBadge({
  135. index: 1,
  136. text: String(num)
  137. })
  138. } else {
  139. uni.removeTabBarBadge({
  140. index: 1,
  141. })
  142. }
  143. },
  144. }
  145. const actions = {
  146. // 初始化购物车
  147. initCart({ rootGetters, commit, state }) {
  148. return productService.QueryShoppingCartList({ userId: rootGetters.userId })
  149. .then(response => {
  150. let data = response.data
  151. commit('setTabBarBadge', data.cartQuantity)
  152. commit('setKindCount', data.cartQuantity)
  153. if (data.shopList.length > 0 || data.products.length > 0) {
  154. commit('setEmptyType', false)
  155. } else {
  156. commit('setEmptyType', true)
  157. }
  158. if (data.shopList && data.shopList.length > 0) {
  159. commit('setGoodsList', totalShopPeice(data.shopList))
  160. commit('resetSelectProducts', state.cartIds) // 勾选已选商品
  161. } else {
  162. commit('setGoodsList', [])
  163. }
  164. if (data.products && data.products.length > 0) {
  165. commit('setFailureList', data.products)
  166. } else {
  167. commit('setFailureList', [])
  168. }
  169. })
  170. .catch(error => {
  171. showMsg(error.msg, 2000)
  172. })
  173. },
  174. // 获取购物车商品数量
  175. getCartNumber({ commit, rootGetters }) {
  176. productService.QueryShoppingQuantity({ userId: rootGetters.userId })
  177. .then(response => {
  178. commit('setTabBarBadge', response.data)
  179. commit('setKindCount', response.data)
  180. })
  181. .catch(error => {
  182. console.log('查询购物车数量错误信息', error)
  183. })
  184. },
  185. // 添加购物车
  186. addToCart({ dispatch, rootGetters }, { productId, productCount = 1, heUserId = 0 }) {
  187. if (!rootGetters.hasLogin) return navigateTo('/pages/authorize/login')
  188. productService.shoppingAddCart({
  189. productId,
  190. userId: rootGetters.userId,
  191. productCount,
  192. heUserId
  193. }).then(response => {
  194. showMsg('加入购物车成功', 1500, true, 'success')
  195. dispatch('getCartNumber')
  196. }).catch(error => {
  197. showMsg(error.msg, 2000)
  198. })
  199. },
  200. // 从购物车移除
  201. removeFromCart({ dispatch, state }, cartIds = []) {
  202. // 从哪里获取cartIds
  203. const removeCartIds = cartIds.length > 0 ? cartIds : state.cartIds
  204. return productService.ShoppingCartDelete({
  205. cartIds: removeCartIds.join(',')
  206. }).then(response => {
  207. dispatch('initCart')
  208. showMsg('删除成功', 2000)
  209. }).catch(error => {
  210. showMsg(error.msg, 2000)
  211. })
  212. },
  213. // 删除失效商品
  214. removeFailureFromCart({ dispatch, state }) {
  215. const cartIds = state.failureList.reduce((cartIds, prod) => {
  216. cartIds.push(prod.cartId)
  217. return cartIds
  218. }, [])
  219. return dispatch('removeFromCart', cartIds)
  220. },
  221. // 加减购物车商品更新到后台
  222. updateShoppogCount({ dispatch, state }, { cartId, productCount }) {
  223. return productService.ShoppingCartUpdate({
  224. cartId,
  225. productCount
  226. }).finally(() => {
  227. dispatch('initCart')
  228. })
  229. }
  230. }
  231. export default {
  232. namespaced: true,
  233. state,
  234. mutations,
  235. actions
  236. }