cart.js 6.7 KB

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