cart.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import Vue from 'vue'
  2. import { deepClone } from '@/common/util/index.js'
  3. // 解压
  4. const unzip = b64Data => {
  5. let strData = atob(b64Data);
  6. const charData = strData.split('').map(function (x) {
  7. return x.charCodeAt(0);
  8. });
  9. const binData = new Uint8Array(charData);
  10. const data = pako.inflate(binData);
  11. strData = String.fromCharCode.apply(null, new Uint16Array(data));
  12. return decodeURIComponent(strData);
  13. }
  14. // 压缩
  15. const zip = str => btoa(pako.gzip(encodeURIComponent(str), {to: 'string'}))
  16. const storage = list => {
  17. const num = list.reduce((t, e) => {
  18. return e.size.reduce((tt, ee, ii) => {
  19. return tt + e.cart[ii]
  20. }, t)
  21. }, 0)
  22. // if (num) {
  23. // uni.setTabBarBadge({ index: 1, text: num + '' })
  24. // } else {
  25. // uni.removeTabBarBadge({ index: 1 })
  26. // }
  27. document.cookie = `CART=${zip(JSON.stringify(list))}; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/;`
  28. }
  29. let cartinfo = []
  30. function sum(arr) {
  31. var len = arr.length;
  32. if(len == 0){
  33. return 0;
  34. } else if (len == 1){
  35. return arr[0];
  36. } else {
  37. return arr[0] + sum(arr.slice(1));
  38. }
  39. }
  40. try{
  41. cartinfo = document.cookie.split(';').findIndex((e) => e.match('CART')) == -1 ? [] :
  42. JSON.parse(unzip(document.cookie.split(';')[document.cookie.split(';').findIndex((e) => e.match('CART'))].split('=')[1]))
  43. }catch(e){
  44. cartinfo = []
  45. }
  46. if (cartinfo.length) {
  47. for(var i=0;i<cartinfo.length;i++) {
  48. cartinfo[i].goods_num= sum(cartinfo[i].cart)
  49. cartinfo[i].goods_money = sum(cartinfo[i].cart) * cartinfo[i].money
  50. // cartinfo[i].open = false
  51. }
  52. // for(var i=0;i<cartinfo.length;i++) {
  53. // cartinfo[i].goods_money= 0
  54. // }
  55. // console.log(cartinfo)
  56. // setTimeout(() => {
  57. // uni.setTabBarBadge({
  58. // index: 1,
  59. // text: cartinfo.reduce((t, e) => {
  60. // return e.size.reduce((tt, ee, ii) => {
  61. // return tt + e.cart[ii]
  62. // }, t)
  63. // }, 0) + ''
  64. // })
  65. // })
  66. }
  67. export default {
  68. namespaced: true,
  69. state: {
  70. list: cartinfo
  71. },
  72. getters: {
  73. shopcarNum: state => { // 计算购车商品数量
  74. return state.list.reduce((t, e) => {
  75. return e.size.reduce((tt, ee, ii) => {
  76. return tt + e.cart[ii]
  77. }, t)
  78. }, 0)
  79. },
  80. choosedNum: state => { // 计算购车已选商品数量
  81. return state.list.reduce((t, e) => {
  82. return e.size.reduce((tt, ee, ii) => {
  83. return tt + (e.sizeChoosed[ii] ? e.cart[ii] : 0)
  84. }, t)
  85. }, 0)
  86. },
  87. choosedPrice: state => { // 计算购车已选商品价值
  88. return state.list.reduce((t, e) => {
  89. return e.size.reduce((tt, ee, ii) => {
  90. return tt + (e.sizeChoosed[ii] ? e.cart[ii] * e.money : 0)
  91. }, t)
  92. }, 0)
  93. },
  94. // choosedList: state => { // 获取购物车已选中的商品列表
  95. // const list = deepClone(state.list)
  96. // for(let i = list.length - 1; i >= 0; i --) {
  97. // if (!list[i].choosed) {
  98. // list.splice(i, 1)
  99. // } else {
  100. // list[i].typeList.forEach(ee => {
  101. // Object.keys(ee.choosed).forEach(eee => {
  102. // if (!ee.choosed[eee]) {
  103. // delete ee.size[eee]
  104. // delete ee.choosed[eee]
  105. // }
  106. // })
  107. // })
  108. // }
  109. // }
  110. // return list
  111. // },
  112. choosedAll: state => { // 计算购车是否为全选状态
  113. const list = deepClone(state.list)
  114. list.forEach(goodItem => {
  115. for(let i = goodItem.cart.length - 1; i >= 0; i --) {
  116. if (goodItem.cart[i] === 0) {
  117. goodItem.cart.splice(i, 1)
  118. goodItem.size.splice(i, 1)
  119. goodItem.size_id.splice(i, 1)
  120. goodItem.sizeChoosed.splice(i, 1)
  121. }
  122. }
  123. })
  124. return list.every(e => {
  125. return e.sizeChoosed.every(ee => ee)
  126. })
  127. },
  128. giftNum: state => { // 根据商品数量计算赠品数量
  129. return state.list.reduce((t, e) => {
  130. // return tt + (e.sizeChoosed[ii] ?
  131. // ((e.unit == '件') ? e.cart[ii] * 2 : e.cart[ii]) : 0)
  132. return e.size.reduce((tt, ee, ii) => {
  133. if (e.sizeChoosed[ii]) {
  134. if (e.unit == '件') {
  135. if(e.name=='爱丫丫抗菌袜'){
  136. tt += e.cart[ii] * 0;
  137. }else{
  138. tt += e.cart[ii] * 2;
  139. }
  140. } else {
  141. tt += e.cart[ii];
  142. }
  143. }
  144. return tt
  145. // ((e.name.indexOf('简约') !== -1 || e.name.indexOf('高腰') !== -1 || e.name.indexOf('青春') !== -1) ? e.cart[ii] * 2 : e.cart[ii]) : 0)
  146. }, t)
  147. }, 0)
  148. },
  149. },
  150. mutations: {
  151. ADD(state, payload, ) { // 添加商品到购物车 (已存在合并,不存在添加)
  152. const index = state.list.findIndex(e => e.attr_id === payload.attr_id) // 检查添加商品是否存在于购物车中
  153. if(index === -1) {
  154. state.list.push(payload)
  155. } else {
  156. Object.assign(state.list[index], payload)
  157. }
  158. setTimeout(() => storage(state.list), 123)
  159. // const old = console.log(state.list[0].cart[0])
  160. // this.commit('cart/COUNTCHANGE', { value: old + 1, index: 0, sizeIndex: 0, del: true })
  161. // this.commit('cart/COUNTCHANGE', { value: old, index: 0, sizeIndex: 0, del: true })
  162. },
  163. GETNEWGOODSNUM(state, payload) {
  164. state.list.forEach(v => {
  165. if (v.name == '红色贺岁款(精装版)') {
  166. v.residue = payload
  167. }
  168. })
  169. },
  170. DELETE(state, payload) {
  171. state.list.splice(payload, 1)
  172. storage(state.list)
  173. },
  174. CLEAR(state) { // 清空购物车
  175. state.list = []
  176. storage(state.list)
  177. },
  178. CLEAREMPTYITEM(state) {
  179. for(let i = state.list.length - 1; i >= 0; i --) {
  180. if (state.list[i].cart.reduce((t, e) => t + e, 0) === 0) { // 当某个尺码数量变为 0 时,进行一波操作,删除这件商品的这个类型或者商品
  181. state.list.splice(i, 1)
  182. }
  183. }
  184. storage(state.list)
  185. },
  186. COUNTCHANGE(state, payload) { // 修改购物商品数量相关操作
  187. Vue.set(state.list[payload.index].cart, payload.sizeIndex, payload.value)
  188. if (payload.del && payload.value === 0 && state.list[payload.index].cart.reduce((t, e) => t + e, 0) === 0) { // 当某个尺码数量变为 0 时,进行一波操作,删除这件商品的这个类型或者商品
  189. state.list.splice(payload.index, 1)
  190. }
  191. storage(state.list)
  192. },
  193. CHOOSEDCHANGE(state, payload) { // 修改选择状态相关操作
  194. if (payload.length === 3) { // 点击 选择/取消 商品类型, 选择/取消 该商品商品类型,同时和 选择/取消 商品联动
  195. state.list[payload[1]].sizeChoosed[payload[2]] = payload[0]
  196. state.list[payload[1]].choosed = state.list[payload[1]].sizeChoosed.some((e, i) => e && state.list[payload[1]].cart[i])
  197. } else if (payload.length === 2) { // 点击 选择/取消 商品, 选择/取消 该商品所有的类型
  198. state.list[payload[1]].choosed = payload[0]
  199. state.list[payload[1]].sizeChoosed = Array(state.list[payload[1]].sizeChoosed.length).fill(payload[0])
  200. } else { // 点击 取消/全选
  201. state.list.forEach((e, i) => {
  202. e.choosed = payload[0]
  203. e.sizeChoosed = Array(e.size.length).fill(payload[0])
  204. })
  205. }
  206. state.list = JSON.parse(JSON.stringify(state.list))
  207. storage(state.list)
  208. }
  209. }
  210. }