cart.js 6.9 KB

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