shopcar.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { deepClone, clearTypeListZeros, clearGoodTypeListZero, mergeGoodToShopcar, clearShoplistConfirmed } from '@/common/util/index.js'
  2. export default {
  3. namespaced: true,
  4. state: {
  5. list: []
  6. },
  7. getters: {
  8. shopcarNum: state => { // 计算购车商品数量
  9. return state.list.reduce((t, e) => {
  10. return e.typeList.reduce((tt, ee) => {
  11. return Object.keys(ee.size).reduce((ttt, eee) => {
  12. return ttt + ee.size[eee]
  13. }, tt)
  14. }, t)
  15. }, 0)
  16. },
  17. choosedNum: state => { // 计算购车已选商品数量
  18. return state.list.reduce((t, e) => {
  19. return e.typeList.reduce((tt, ee) => {
  20. return Object.keys(ee.size).reduce((ttt, eee) => {
  21. return ttt + (ee.choosed[eee] ? ee.size[eee] : 0)
  22. }, tt)
  23. }, t)
  24. }, 0)
  25. },
  26. choosedPrice: state => { // 计算购车已选商品价值
  27. return state.list.reduce((t, e) => {
  28. return e.typeList.reduce((tt, ee) => {
  29. return Object.keys(ee.size).reduce((ttt, eee) => {
  30. return ttt + (ee.choosed[eee] ? ee.size[eee] : 0) * e.price
  31. }, tt)
  32. }, t)
  33. }, 0)
  34. },
  35. choosedList: state => { // 获取购物车已选中的商品列表
  36. const list = deepClone(state.list)
  37. for(let i = list.length - 1; i >= 0; i --) {
  38. if (!list[i].choosed) {
  39. list.splice(i, 1)
  40. } else {
  41. list[i].typeList.forEach(ee => {
  42. Object.keys(ee.choosed).forEach(eee => {
  43. if (!ee.choosed[eee]) {
  44. delete ee.size[eee]
  45. delete ee.choosed[eee]
  46. }
  47. })
  48. })
  49. }
  50. }
  51. return list
  52. },
  53. choosedAll: state => { // 计算购车是否为全选状态
  54. return state.list.every(e => {
  55. return e.typeList.every(ee => {
  56. return Object.keys(ee.choosed).every(eee => {
  57. return ee.choosed[eee]
  58. })
  59. })
  60. })
  61. }
  62. },
  63. mutations: {
  64. ADD(state, payload) { // 添加商品到购物车 (已存在合并,不存在添加)
  65. const index = state.list.findIndex(e => e.id === payload.id) // 检查添加商品是否存在于购物车中
  66. setTimeout(() => {
  67. if(state.list.length === 0 || index === -1) {
  68. state.list.push(payload)
  69. } else {
  70. mergeGoodToShopcar(state.list[index], payload)
  71. }
  72. }, 567)
  73. },
  74. DEL(state, index) { // 删除这件商品
  75. state.list.splice(index, 1)
  76. },
  77. CLEARCONFIRM(state, payload) { // 提交订单后删除购物车已经提交的商品
  78. clearShoplistConfirmed(state.list, payload)
  79. },
  80. COUNTCHANGE(state, payload) { // 修改购物商品数量相关操作
  81. const index = payload[0], typeIndex = payload[1], key = payload[2], value = payload[3]
  82. state.list[index].typeList[typeIndex].size[key] = value
  83. if (value === 0) { // 当某个尺码数量变为 0 时,进行一波操作,删除这件商品的这个类型或者商品
  84. clearTypeListZeros(state.list[index].typeList) // 清除这件商品 typeList 中 对象的 size 对象的 key 数量为 0 的对象
  85. clearGoodTypeListZero(state.list) // 清除购物车中 typeList 长度为 0 的商品
  86. }
  87. },
  88. CHOOSEDCHANGE(state, payload) { // 修改选择状态相关操作
  89. const index = payload[0], typeIndex = payload[1], key = payload[2]
  90. if (payload.length === 3) { // 点击 选择/取消 商品类型, 选择/取消 该商品商品类型,同时和 选择/取消 商品联动
  91. state.list[index].typeList[typeIndex].choosed[key] = !state.list[index].typeList[typeIndex].choosed[key]
  92. state.list[index].choosed = state.list[index].typeList.some(e => Object.keys(e.choosed).some(ee => e.choosed[ee] === true))
  93. } else if (payload.length === 1) { // 点击 选择/取消 商品, 选择/取消 该商品所有的类型
  94. state.list[index].choosed = !state.list[index].choosed
  95. state.list[index].typeList.forEach(e => Object.keys(e.choosed).forEach(ee => e.choosed[ee] = state.list[index].choosed))
  96. } else { // 点击 取消/全选
  97. const status = this.getters['shopcar/choosedAll']
  98. state.list.forEach(e => {
  99. e.choosed = !status
  100. e.typeList.forEach(ee => {
  101. Object.keys(ee.choosed).forEach(eee => {
  102. ee.choosed[eee] = !status
  103. })
  104. })
  105. })
  106. }
  107. }
  108. }
  109. }