cart.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const common_assets = require("../common/assets.js");
  4. const likeProduct = () => "./components/likeProduct.js";
  5. const _sfc_main = {
  6. components: {
  7. likeProduct
  8. },
  9. data() {
  10. return {
  11. totalMoney: 0,
  12. carts: [],
  13. checkAll: false
  14. };
  15. },
  16. onLoad() {
  17. },
  18. onShow() {
  19. this.getCarts();
  20. },
  21. onReachBottom() {
  22. },
  23. methods: {
  24. delCart() {
  25. var selectCarts = this.carts.flatMap((item) => item.list.filter((listItem) => listItem.checked === true)).map((el) => el.id);
  26. if (selectCarts.length == 0) {
  27. common_vendor.index.showToast({
  28. icon: "none",
  29. title: "请选择商品删除"
  30. });
  31. return;
  32. }
  33. let data = { ids: selectCarts };
  34. delCart(data).then(
  35. (res) => {
  36. if (res.code == 200) {
  37. common_vendor.index.showToast({
  38. icon: "success",
  39. title: "操作成功"
  40. });
  41. this.getCarts();
  42. } else {
  43. common_vendor.index.showToast({
  44. icon: "none",
  45. title: res.msg
  46. });
  47. }
  48. },
  49. (rej) => {
  50. }
  51. );
  52. console.log(selectCarts);
  53. },
  54. computedMoney() {
  55. var money = 0;
  56. this.carts.forEach((item, index, arr) => {
  57. item.list.forEach((it) => {
  58. if (it.checked) {
  59. money += it.price * it.cartNum;
  60. }
  61. });
  62. });
  63. console.log(money);
  64. this.totalMoney = money;
  65. },
  66. handleCheckAll() {
  67. this.checkAll = !this.checkAll;
  68. var that = this;
  69. this.carts.forEach((item, index, arr) => {
  70. item.checked = that.checkAll;
  71. item.list.forEach((it) => {
  72. it.checked = that.checkAll;
  73. });
  74. });
  75. this.computedMoney();
  76. },
  77. checkShopChange(item) {
  78. item.checked = !item.checked;
  79. item.list.forEach((it) => {
  80. it.checked = item.checked;
  81. });
  82. this.computedMoney();
  83. },
  84. checkChange(item, shop) {
  85. item.checked = !item.checked;
  86. shop.checked = shop.list.every((it) => it.checked == true);
  87. this.computedMoney();
  88. },
  89. changeNum(e, item) {
  90. item.cartNum = e.detail.value.replace(/\D/g, "");
  91. if (item.cartNum <= 1) {
  92. common_vendor.index.showToast({
  93. title: "已经是底线啦!",
  94. icon: "none",
  95. duration: 2e3
  96. });
  97. return;
  98. }
  99. if (item.cartNum < 1) {
  100. item.cartNum = 1;
  101. }
  102. if (item.cartNum >= item.stock) {
  103. item.cartNum = item.stock;
  104. }
  105. this.changeCartNum(item);
  106. },
  107. changeCartNum(item) {
  108. let data = { number: item.cartNum, id: item.id };
  109. cartNum(data).then(
  110. (res) => {
  111. if (res.code == 200) {
  112. common_vendor.index.showToast({
  113. icon: "none",
  114. title: "操作成功"
  115. });
  116. this.computedMoney();
  117. } else {
  118. common_vendor.index.showToast({
  119. icon: "none",
  120. title: res.msg
  121. });
  122. }
  123. },
  124. (rej) => {
  125. }
  126. );
  127. },
  128. getCarts() {
  129. getCarts().then(
  130. (res) => {
  131. if (res.code == 200) {
  132. this.carts = res.carts;
  133. this.carts.forEach((item) => {
  134. item.checked = false;
  135. item.list.forEach((it) => {
  136. it.checked = false;
  137. });
  138. });
  139. this.computedMoney();
  140. } else {
  141. common_vendor.index.showToast({
  142. icon: "none",
  143. title: "请求失败"
  144. });
  145. }
  146. },
  147. (rej) => {
  148. }
  149. );
  150. },
  151. // 购物车减法
  152. delNum(item) {
  153. if (item.cartNum <= 1) {
  154. common_vendor.index.showToast({
  155. title: "已经是底线啦!",
  156. icon: "none",
  157. duration: 2e3
  158. });
  159. return;
  160. }
  161. item.cartNum--;
  162. if (item.cartNum < 1) {
  163. item.cartNum = 1;
  164. }
  165. this.changeCartNum(item);
  166. },
  167. // 购物车加法
  168. addNum(item) {
  169. console.log(item);
  170. item.cartNum++;
  171. if (item.cartNum >= item.stock) {
  172. item.cartNum = item.stock;
  173. }
  174. this.changeCartNum(item);
  175. },
  176. // 结算
  177. submit() {
  178. let selectCarts = this.carts.filter((item) => item.list.some((listItem) => listItem.checked === true)).map((item) => ({
  179. storeId: item.list[0].storeId || "",
  180. data: {
  181. type: "cart",
  182. cartIds: item.list.filter((it) => it.checked == true).map((it) => it.id).join(",")
  183. }
  184. }));
  185. if (selectCarts.length == 0) {
  186. common_vendor.index.showToast({
  187. icon: "none",
  188. title: "请选择商品"
  189. });
  190. return;
  191. }
  192. common_vendor.index.navigateTo({
  193. url: "./confirmOrder?type=cart&confirmParam=" + encodeURIComponent(JSON.stringify(selectCarts))
  194. });
  195. },
  196. showProduct(item) {
  197. common_vendor.index.navigateTo({
  198. url: "../shopping/productDetails?productId=" + item.productId
  199. });
  200. }
  201. }
  202. };
  203. if (!Array) {
  204. const _component_likeProduct = common_vendor.resolveComponent("likeProduct");
  205. _component_likeProduct();
  206. }
  207. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  208. return common_vendor.e({
  209. a: common_vendor.f($data.carts, (shop, idx, i0) => {
  210. return common_vendor.e({
  211. a: shop.storeName && shop.storeName != "null"
  212. }, shop.storeName && shop.storeName != "null" ? {
  213. b: shop.checked,
  214. c: shop.checked,
  215. d: common_vendor.o(($event) => $options.checkShopChange(shop), idx),
  216. e: common_vendor.t(shop.storeName)
  217. } : {}, {
  218. f: common_vendor.f(shop.list, (item, index, i1) => {
  219. return common_vendor.e({
  220. a: item.checked,
  221. b: item.checked,
  222. c: common_vendor.o(($event) => $options.checkChange(item, shop), index),
  223. d: item.productAttrImage == null || item.productAttrImage == "" ? item.productImage : item.productAttrImage,
  224. e: common_vendor.t(_ctx.utils.getDictLabelName("storeProductType", item.productType)),
  225. f: common_vendor.t(item.productName),
  226. g: common_vendor.t(item.productAttrName),
  227. h: common_vendor.t(item.price),
  228. i: item.cartNum <= 1
  229. }, item.cartNum <= 1 ? {
  230. j: common_assets._imports_0$8
  231. } : {
  232. k: common_assets._imports_1$5
  233. }, {
  234. l: common_vendor.o(($event) => $options.delNum(item), index),
  235. m: common_vendor.o(($event) => $options.changeNum($event, item), index),
  236. n: item.cartNum,
  237. o: common_vendor.o(($event) => $options.addNum(item), index),
  238. p: index
  239. });
  240. }),
  241. g: idx
  242. });
  243. }),
  244. b: common_assets._imports_2$4,
  245. c: $data.carts.length == 0
  246. }, $data.carts.length == 0 ? {
  247. d: common_assets._imports_2$3
  248. } : {}, {
  249. e: common_vendor.sr("product", "50e5f29e-0"),
  250. f: $data.checkAll,
  251. g: common_vendor.o(($event) => $options.handleCheckAll()),
  252. h: common_vendor.o(($event) => $options.delCart()),
  253. i: common_vendor.t($data.totalMoney.toFixed(2)),
  254. j: common_vendor.o((...args) => $options.submit && $options.submit(...args))
  255. });
  256. }
  257. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  258. wx.createPage(MiniProgramPage);