index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import Vue from 'vue'
  2. export const deepClone = (obj) => { // 深克隆方法
  3. const targetObj = obj.constructor === Array ? [] : {}
  4. Object.keys(obj).forEach(keys => {
  5. if (obj[keys] && typeof obj[keys] === 'object') {
  6. targetObj[keys] = deepClone(obj[keys])
  7. } else {
  8. targetObj[keys] = obj[keys]
  9. }
  10. })
  11. return targetObj
  12. }
  13. // 清除某件商品 typeList 中 typeListItem 的 size 和 choosed 中 value 为 0 的 key,并在 size 长度为 0 时,清除 typeListItem
  14. export const clearTypeListZeros = typeList => {
  15. typeList.forEach(e => {
  16. Object.keys(e.size).forEach(ee => {
  17. if(e.size[ee] === 0) {
  18. delete e.size[ee]
  19. delete e.choosed[ee]
  20. }
  21. })
  22. })
  23. for(let i = typeList.length - 1; i > -1; i --) {
  24. if(!Object.keys(typeList[i].size).length) {
  25. typeList.splice(i, 1)
  26. }
  27. }
  28. }
  29. export const clearGoodTypeListZero = shopcarList => { // 当某件商品的 typeList 为 0 时,从购物车清除这件商品
  30. for(let i = shopcarList.length - 1; i > -1; i --) {
  31. if(!shopcarList[i].typeList.length) {
  32. shopcarList.splice(i, 1)
  33. }
  34. shopcarList[i] ? shopcarList[i].choosed = shopcarList[i].typeList.some(e => Object.keys(e.choosed).some(ee => e.choosed[ee] === true)) : ''
  35. }
  36. }
  37. export const mergeGoodToShopcar = (origin, target) => { // 当新添加的商品购车已经存在时合并
  38. origin.typeList.forEach(typeItem => {
  39. for(let targetTypeListIndex = target.typeList.length - 1; targetTypeListIndex > -1; targetTypeListIndex --) {
  40. if (typeItem.type_id === target.typeList[targetTypeListIndex].type_id) {
  41. Object.keys(target.typeList[targetTypeListIndex].size).forEach(size => {
  42. if(typeItem.size[size]) {
  43. typeItem.size[size] += target.typeList[targetTypeListIndex].size[size]
  44. } else {
  45. Vue.set(typeItem.size, size, target.typeList[targetTypeListIndex].size[size])
  46. }
  47. Vue.set(typeItem.choosed, size, true)
  48. })
  49. target.typeList.splice(targetTypeListIndex, 1)
  50. }
  51. }
  52. })
  53. origin.typeList.splice(origin.typeList.length - 1, 0, ...target.typeList)
  54. origin.typeList.sort((a, b) => +a.type_id - +b.type_id)
  55. }
  56. export const confirmOrder = goodList => { // 点击确认订单结算将数据转换为后端需要的数据格式
  57. const list = []
  58. goodList.forEach(goodItem => {
  59. clearTypeListZeros(goodItem.typeList)
  60. goodItem.typeList.forEach(typeItem => {
  61. Object.keys(typeItem.size).forEach(size => {
  62. const item = {}
  63. item.id = goodItem.id // 商品id
  64. item.type_id = typeItem.type_id // type_id
  65. item.num = typeItem.size[size] // 数量
  66. item.size_id = typeItem.size_id[size] // size_id
  67. list.push(item)
  68. })
  69. })
  70. })
  71. return list
  72. }
  73. export const clearShoplistConfirmed = (goodList, payload) => { // 提交订单后清除购物车中已经提交的类型或者商品
  74. payload.forEach((e, i) => {
  75. e.typeList.forEach((ee, ei) => {
  76. Object.keys(ee.size).forEach(eee => {
  77. goodList[i].typeList[ei].size[eee] = 0
  78. })
  79. })
  80. clearTypeListZeros(goodList[i].typeList) // 清除这件商品的 typeList
  81. })
  82. clearGoodTypeListZero(goodList) // 清除购物车中 typeList 长度为 0 的商品
  83. }
  84. export const divideOrder = goodList => { // 点击确认订单结算将数据转换为后端需要的数据格式
  85. const list = []
  86. goodList.forEach(goodItem => {
  87. goodItem.typeList.forEach(typeItem => {
  88. Object.keys(typeItem.size).forEach((sizeItem, sizeIndex) => {
  89. const item = {}
  90. item.id = goodItem.id
  91. item.type_id = typeItem.type_id
  92. item.num = typeItem.num[sizeIndex]
  93. item.size_id = typeItem.size_id[sizeIndex]
  94. if (+item.num) {
  95. list.push(item) // 清除 num 为 0 的 size
  96. }
  97. })
  98. })
  99. })
  100. return list
  101. }
  102. export const getQueryString = name => {
  103. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  104. var r = window.location.search.substr(1).match(reg);
  105. if (r != null) {
  106. return unescape(r[2]);
  107. }
  108. return null;
  109. }