cart.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import {
  2. mapGetters
  3. } from "vuex"
  4. import {
  5. cartList
  6. } from "@/api/goods/cart"
  7. export default {
  8. data: () => {
  9. return {
  10. cartList: [], // 购物车
  11. checkAll: false,
  12. totalPrice: "0.00",
  13. totalCount: 0,
  14. invalidGoods: [], // 失效商品集合
  15. loading: true,
  16. modifyNum: 1, // 防止数量跳动
  17. }
  18. },
  19. created() {
  20. this.getCartList()
  21. },
  22. computed: {
  23. ...mapGetters(["defaultGoodsImage"])
  24. },
  25. middleware: 'auth',
  26. methods: {
  27. // 获取购物车数据
  28. getCartList() {
  29. cartList({})
  30. .then(res => {
  31. if (res.code >= 0 && res.data.length) {
  32. this.handleCartList(res.data)
  33. }
  34. this.loading = false
  35. })
  36. .catch(res => {
  37. this.loading = false
  38. })
  39. },
  40. // 处理购物车数据结构
  41. handleCartList(data) {
  42. this.invalidGoods = []
  43. this.cartList = []
  44. var temp = {}
  45. data.forEach((item, index) => {
  46. if (item.goods_state == 1) {
  47. item.checked = true
  48. if (temp["site_" + item.site_id] != undefined) {
  49. temp["site_" + item.site_id].cartList.push(item)
  50. } else {
  51. temp["site_" + item.site_id] = {
  52. siteId: item.site_id,
  53. siteName: item.site_name,
  54. checked: true,
  55. cartList: [item]
  56. }
  57. }
  58. } else {
  59. this.invalidGoods.push(item)
  60. }
  61. })
  62. this.invalidGoods.forEach(v => {
  63. if (v.sku_spec_format) {
  64. v.sku_spec_format = JSON.parse(v.sku_spec_format)
  65. } else {
  66. v.sku_spec_format = []
  67. }
  68. })
  69. Object.keys(temp).forEach(key => {
  70. this.cartList.push(temp[key])
  71. })
  72. this.calculationTotalPrice()
  73. this.cartList.forEach(v => {
  74. v.cartList.forEach(k => {
  75. if (k.sku_spec_format) {
  76. k.sku_spec_format = JSON.parse(k.sku_spec_format)
  77. } else {
  78. k.sku_spec_format = []
  79. }
  80. })
  81. })
  82. },
  83. // 单选
  84. singleElection(siteIndex, index) {
  85. this.calculationTotalPrice()
  86. },
  87. // 店铺全选
  88. siteAllElection(index) {
  89. this.cartList[index].cartList.forEach(item => {
  90. item.checked = this.cartList[index].checked
  91. })
  92. this.calculationTotalPrice()
  93. },
  94. // 全选
  95. allElection() {
  96. if (this.cartList.length) {
  97. this.cartList.forEach(siteItem => {
  98. siteItem.checked = this.checkAll
  99. siteItem.cartList.forEach(item => {
  100. item.checked = this.checkAll
  101. })
  102. })
  103. }
  104. this.calculationTotalPrice()
  105. },
  106. // 计算购物车总价
  107. calculationTotalPrice() {
  108. if (this.cartList.length) {
  109. let totalPrice = 0,
  110. totalCount = 0,
  111. siteAllElectionCount = 0
  112. this.cartList.forEach(siteItem => {
  113. let siteGoodsCount = 0
  114. siteItem.cartList.forEach(item => {
  115. if (item.checked) {
  116. siteGoodsCount += 1
  117. totalCount += 1
  118. totalPrice += item.discount_price * item.num
  119. }
  120. })
  121. if (siteItem.cartList.length == siteGoodsCount) {
  122. siteItem.checked = true
  123. siteAllElectionCount += 1
  124. } else {
  125. siteItem.checked = false
  126. }
  127. })
  128. this.totalPrice = totalPrice.toFixed(2)
  129. this.totalCount = totalCount
  130. this.checkAll = this.cartList.length == siteAllElectionCount
  131. } else {
  132. this.totalPrice = "0.00"
  133. this.totalCount = 0
  134. }
  135. this.modifyNum = 1;
  136. },
  137. // 删除单个
  138. deleteCart(siteIndex, cartIndex) {
  139. this.$confirm("确定要删除该商品吗?", "提示信息", {
  140. confirmButtonText: "确定",
  141. cancelButtonText: "取消",
  142. type: "warning"
  143. }).then(() => {
  144. this.$store
  145. .dispatch("cart/delete_cart", {
  146. cart_id: this.cartList[siteIndex].cartList[cartIndex].cart_id.toString()
  147. })
  148. .then(res => {
  149. if (res.code >= 0) {
  150. this.cartList[siteIndex].cartList.splice(cartIndex, 1)
  151. if (this.cartList[siteIndex].cartList.length == 0) this.cartList.splice(siteIndex, 1)
  152. this.calculationTotalPrice()
  153. this.$message({
  154. type: "success",
  155. message: "删除成功"
  156. })
  157. } else {
  158. this.$message({
  159. message: res.message,
  160. type: "warning"
  161. })
  162. }
  163. })
  164. .catch(err => {
  165. this.$message.error(err.message)
  166. })
  167. })
  168. },
  169. // 删除选择的购物车
  170. deleteCartSelected() {
  171. var cartIds = []
  172. var selectedItem = []
  173. this.cartList.forEach((siteItem, siteIndex) => {
  174. siteItem.cartList.forEach((item, cartIndex) => {
  175. if (item.checked) {
  176. cartIds.push(item.cart_id)
  177. selectedItem.push({
  178. siteIndex: siteIndex,
  179. cartIndex: cartIndex,
  180. siteId: siteItem.siteId,
  181. cartId: item.cart_id
  182. })
  183. }
  184. })
  185. })
  186. if (cartIds.length == 0) {
  187. this.$message({
  188. message: "请选择要删除的商品",
  189. type: "warning"
  190. })
  191. return
  192. }
  193. this.$confirm("确定要删除选择的商品吗?", "提示信息", {
  194. confirmButtonText: "确定",
  195. cancelButtonText: "取消",
  196. type: "warning"
  197. }).then(() => {
  198. this.$store
  199. .dispatch("cart/delete_cart", {
  200. cart_id: cartIds.toString()
  201. })
  202. .then(res => {
  203. if (res.code >= 0) {
  204. selectedItem.forEach(selectedItem => {
  205. this.cartList.forEach((siteItem, siteIndex) => {
  206. siteItem.cartList.forEach((item, cartIndex) => {
  207. if (selectedItem.cartId == item.cart_id) {
  208. siteItem.cartList.splice(cartIndex, 1)
  209. }
  210. if (siteItem.cartList.length == 0) {
  211. this.cartList.splice(siteIndex, 1)
  212. }
  213. })
  214. })
  215. })
  216. this.calculationTotalPrice()
  217. this.$message({
  218. type: "success",
  219. message: "删除成功"
  220. })
  221. } else {
  222. this.$message({
  223. message: res.message,
  224. type: "warning"
  225. })
  226. }
  227. })
  228. .catch(err => {
  229. this.$message.error(err.message)
  230. })
  231. })
  232. },
  233. // 清空失效商品
  234. clearInvalidGoods() {
  235. this.$confirm("确认要清空这些商品吗?", "提示信息", {
  236. confirmButtonText: "确定",
  237. cancelButtonText: "取消",
  238. type: "warning"
  239. }).then(() => {
  240. var cartIds = []
  241. this.invalidGoods.forEach(goodsItem => {
  242. cartIds.push(goodsItem.cart_id)
  243. })
  244. if (cartIds.length) {
  245. this.$store
  246. .dispatch("cart/delete_cart", {
  247. cart_id: cartIds.toString()
  248. })
  249. .then(res => {
  250. if (res.code >= 0) {
  251. this.invalidGoods = []
  252. this.$message({
  253. type: "success",
  254. message: "删除成功"
  255. })
  256. } else {
  257. this.$message({
  258. message: res.message,
  259. type: "warning"
  260. })
  261. }
  262. })
  263. .catch(err => {
  264. this.$message.error(err.message)
  265. })
  266. }
  267. })
  268. },
  269. // 变更购物车数量
  270. cartNumChange(num, params) {
  271. if (num < 1 || !num) num = 1;
  272. // 防止数量跳动
  273. this.modifyNum = 0;
  274. this.$store
  275. .dispatch("cart/edit_cart_num", {
  276. num,
  277. cart_id: this.cartList[params.siteIndex].cartList[params.cartIndex].cart_id
  278. })
  279. .then(res => {
  280. if (res.code >= 0) {
  281. this.cartList[params.siteIndex].cartList[params.cartIndex].num = num;
  282. this.calculationTotalPrice();
  283. } else {
  284. this.$message({
  285. message: res.message,
  286. type: "warning"
  287. });
  288. this.modifyNum = 1;
  289. }
  290. })
  291. .catch(err => {
  292. this.$message.error(err.message);
  293. this.modifyNum = 1;
  294. })
  295. },
  296. // 结算
  297. settlement() {
  298. if (this.totalCount > 0) {
  299. let cart_ids = []
  300. this.cartList.forEach(siteItem => {
  301. siteItem.cartList.forEach(item => {
  302. if (item.checked) {
  303. cart_ids.push(item.cart_id)
  304. }
  305. })
  306. })
  307. if (cart_ids.length > 20) {
  308. this.$message({
  309. message: '购物车最多支持20个商品一起购买',
  310. type: "warning"
  311. });
  312. return;
  313. }
  314. var data = {
  315. cart_ids: cart_ids.toString()
  316. }
  317. this.$store.dispatch("order/setOrderCreateData", data)
  318. this.$router.push({
  319. path: "/order/payment"
  320. })
  321. }
  322. },
  323. imageError(siteIndex, cartIndex) {
  324. this.cartList[siteIndex].cartList[cartIndex].sku_image = this.defaultGoodsImage
  325. },
  326. imageErrorInvalid(index) {
  327. this.invalidGoods[index].sku_image = this.defaultGoodsImage
  328. }
  329. }
  330. }