cart.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // 购物车模块
  2. import http from '@/shopro/request/index'
  3. const state = {
  4. cartList: [], //购物车列表
  5. checkCart: {}, //检测是否是购物车数据的对象
  6. allSelected: false, // 全选检测
  7. cartNum: 0, //购物车,涉及到刷新数据丢失,所以存了本地,
  8. }
  9. const getters = {
  10. cartList: state => state.cartList,
  11. checkCart: state => state.checkCart,
  12. allSelected: state => state.allSelected,
  13. cartNum: state => state.cartNum,
  14. // 购物车数量和总价
  15. totalCount: state => {
  16. let totalNum = 0;
  17. let totalPrice = 0;
  18. state.cartList.filter(item => {
  19. if (item.checked) {
  20. totalNum += 1;
  21. totalPrice += item.goods_num * (item.sku_price ? item.sku_price.price : 0);
  22. }
  23. })
  24. return {
  25. totalNum,
  26. totalPrice
  27. }
  28. },
  29. // 是否选择了商品
  30. isSel: state => {
  31. let isSel = false;
  32. state.cartList.map(item => {
  33. if (item.checked) {
  34. isSel = true
  35. }
  36. })
  37. return isSel
  38. },
  39. // 活动商品唯一选中可以结算
  40. isActivityPay() {
  41. let num = 0
  42. let activityNum = 0
  43. state.cartList.map(item => {
  44. if (item.checked) {
  45. num += 1
  46. if (item.cart_type === 'activity') {
  47. activityNum += 1
  48. }
  49. }
  50. })
  51. if ((num === 1 && activityNum === 1) || activityNum === 0) {
  52. return true
  53. }
  54. return false
  55. }
  56. }
  57. const mutations = {
  58. // cart数据获取变动。
  59. CART_LIST(state, payload) {
  60. state.cartList = payload;
  61. let cartListNumber = payload.length;
  62. if (Number(cartListNumber)) {
  63. uni.setTabBarBadge({
  64. index: 2,
  65. text: String(cartListNumber)
  66. })
  67. } else {
  68. uni.removeTabBarBadge({
  69. index: 2
  70. })
  71. }
  72. state.cartNum = cartListNumber;
  73. },
  74. // 切换全选。
  75. changeAllSellect(state) {
  76. state.allSelected = !state.allSelected;
  77. },
  78. // 全选设置
  79. getAllSellectCartList(state, flag) {
  80. state.cartList.map(item => {
  81. item.checked = flag
  82. })
  83. },
  84. // 全选检测
  85. checkCartList(state) {
  86. let all = true;
  87. state.cartList.map(item => {
  88. if (!item.checked) {
  89. all = false
  90. }
  91. })
  92. state.allSelected = all;
  93. },
  94. // 是否购物车数据检测
  95. getCheckCart(state) {
  96. let obj = {};
  97. state.cartList.map(item => {
  98. obj[item.goods_id] = {
  99. num: item.goods_num,
  100. cartOrderId: item.id
  101. };
  102. });
  103. state.checkCart = obj;
  104. },
  105. }
  106. const actions = {
  107. // 购物车数据(查)
  108. getCartList({
  109. commit
  110. }) {
  111. return new Promise((resolve, reject) => {
  112. http('cart.index').then(res => {
  113. if (res.code === 1) {
  114. let cartData = res.data;
  115. cartData.length && cartData.map(item => {
  116. item.checked = true;
  117. })
  118. commit('CART_LIST', cartData);
  119. commit('checkCartList');
  120. commit('getCheckCart')
  121. }
  122. }).catch(e => {
  123. reject(e)
  124. })
  125. })
  126. },
  127. // 添加到购物车(增)
  128. addCartGoods({
  129. dispatch
  130. }, data) {
  131. return new Promise((resolve, reject) => {
  132. http('cart.add', {
  133. goods_list: data.list,
  134. from: data.from
  135. }).then(res => {
  136. res.code === 1 && dispatch('getCartList');
  137. resolve(res)
  138. }).catch(e => {
  139. reject(e)
  140. })
  141. })
  142. },
  143. // 修改购物车商品数量(改)|| 删除购物车商品(删)
  144. changeCartList({
  145. commit,
  146. dispatch
  147. }, param) {
  148. return new Promise((resolve, reject) => {
  149. http('cart.edit', {
  150. cart_list: param.ids,
  151. value: param.goodsNum || null,
  152. act: param.art
  153. }).then(res => {
  154. if (param.art === 'delete' && res.code === 1) {
  155. dispatch('getCartList');
  156. }
  157. commit('checkCartList');
  158. resolve(res)
  159. }).catch(e => {
  160. reject(e)
  161. })
  162. })
  163. },
  164. }
  165. export default {
  166. state,
  167. mutations,
  168. actions,
  169. getters
  170. }