"use strict"; const common_vendor = require("../common/vendor.js"); const api_order = require("../api/order.js"); const common_assets = require("../common/assets.js"); const _sfc_main = { // components: { // likeProduct // }, data() { return { shopList: [], totalMoney: 0, // carts: [], checkAll: false }; }, onLoad() { }, onReachBottom() { }, mounted() { this.queryLiveCart(); }, methods: { // 获得key getKey() { api_order.liveOrderKey().then( (res) => { if (res.code == 200) { console.log("下订单的key>>>>", res); this.orderKey = res.orderKey; console.log("key>>>>", this.orderKey); common_vendor.index.navigateTo({ url: "/pages_shop/confirmCreateOrder?type=cart&orderKey=" + this.orderKey }); } else { common_vendor.index.showToast({ title: res.msg, icon: "none" }); } }, (rej) => { } ); }, // 修改购物车 modifyLiveCart(item) { let data = { checked: item.checked, cartId: item.cartId, cartNum: item.cartNum }; api_order.modifyLiveCart(data).then( (res) => { if (res.code == 200) { this.computedMoney(); } else { common_vendor.index.showToast({ icon: "none", title: res.msg }); } }, (rej) => { } ); }, // modifyLiveCartList() { // modifyLiveCart().then( // res => { // if (res.code == 200) { // this.shopList = res.rows; // console.log(this.shopList) // console.log(res.rows) // this.shopList.forEach(item => { // item.checked == 1 ? true : false // }); // // this.computedMoney(); // } else { // uni.showToast({ // icon: 'none', // title: "请求失败", // }); // } // }, // rej => {} // ); // }, // 查询购物车列表 queryLiveCart() { api_order.queryLiveCartList().then( (res) => { if (res.code == 200) { this.shopList = res.rows; this.shopList.forEach((item) => { item.checked == 1 ? true : false; }); } else { common_vendor.index.showToast({ icon: "none", title: "请求失败" }); } }, (rej) => { } ); }, // 删除购物车 delCart() { const data = this.shopList.filter((item) => item.checked).map((item) => item.cartId); if (data.length === 0) { common_vendor.index.showToast({ icon: "none", title: "请选择商品删除" }); return; } api_order.delLiveCart(data).then((res) => { if (res.code === 200) { common_vendor.index.showToast({ icon: "success", title: "操作成功" }); this.queryLiveCart(); } else { common_vendor.index.showToast({ icon: "none", title: res.msg }); } }); }, // 计算价格 computedMoney() { this.totalMoney = this.shopList.reduce((total, item) => { return total + (item.checked ? item.price * item.cartNum : 0); }, 0); }, // 全选 handleCheckAll() { this.checkAll = !this.checkAll; this.shopList.forEach((item) => { item.checked = this.checkAll; }); this.computedMoney(); }, //选择 checkChange(item, shop) { item.checked = !item.checked; this.computedMoney(item.checked); }, // 改数量 changeNum(e, item) { item.cartNum = e.detail.value.replace(/\D/g, ""); if (item.cartNum <= 1) { common_vendor.index.showToast({ title: "已经是底线啦!", icon: "none", duration: 2e3 }); return; } if (item.cartNum < 1) { item.cartNum = 1; } if (item.cartNum >= item.stock) { item.cartNum = item.stock; } this.modifyLiveCart(item); }, // 购物车减法 delNum(item) { if (item.cartNum <= 1) { common_vendor.index.showToast({ title: "已经是底线啦!", icon: "none", duration: 2e3 }); return; } item.cartNum--; if (item.cartNum < 1) { item.cartNum = 1; } this.modifyLiveCart(item); }, // 购物车加法 addNum(item) { item.cartNum++; if (item.cartNum >= item.stock) { item.cartNum = item.stock; } this.modifyLiveCart(item); }, // 结算 submit() { const selectedItems = this.shopList.filter((item) => item.checked); if (selectedItems.length === 0) { common_vendor.index.showToast({ icon: "none", title: "请选择商品" }); return; } this.getKey(); }, showProduct(item) { common_vendor.index.navigateTo({ url: "./goods?productId=" + item.productId }); } } }; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return common_vendor.e({ a: common_vendor.f($data.shopList, (item, index, i0) => { return common_vendor.e({ a: item.checked, b: item.checked, c: common_vendor.o(($event) => $options.checkChange(item, _ctx.shop), index), d: common_vendor.o(($event) => $options.showProduct(item), index), e: item.imgUrl, f: common_vendor.t(item.productName), g: common_vendor.t(item.price), h: item.cartNum <= 1 }, item.cartNum <= 1 ? { i: common_assets._imports_0$7 } : { j: common_assets._imports_1$6 }, { k: common_vendor.o(($event) => $options.delNum(item), index), l: common_vendor.o(($event) => $options.changeNum($event, item), index), m: item.cartNum, n: common_vendor.o(($event) => $options.addNum(item), index), o: index }); }), b: common_assets._imports_2$4, c: $data.shopList.length == 0 }, $data.shopList.length == 0 ? { d: common_assets._imports_2$3 } : {}, { e: $data.checkAll, f: common_vendor.o(($event) => $options.handleCheckAll()), g: common_vendor.o(($event) => $options.delCart()), h: common_vendor.t($data.totalMoney.toFixed(2)), i: common_vendor.o((...args) => $options.submit && $options.submit(...args)) }); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]); wx.createPage(MiniProgramPage);