cart.js 7.1 KB

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