178 lines
7.4 KiB
JavaScript
178 lines
7.4 KiB
JavaScript
"use strict";
|
||
const common_vendor = require("../../common/vendor.js");
|
||
const stores_price = require("../../stores/price.js");
|
||
const stores_bluetooth = require("../../stores/bluetooth.js");
|
||
const stores_order = require("../../stores/order.js");
|
||
const stores_region = require("../../stores/region.js");
|
||
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
|
||
__name: "weigh",
|
||
setup(__props) {
|
||
const priceStore = stores_price.usePriceStore();
|
||
const bluetoothStore = stores_bluetooth.useBluetoothStore();
|
||
const orderStore = stores_order.useOrderStore();
|
||
const regionStore = stores_region.useRegionStore();
|
||
const category = common_vendor.ref("");
|
||
const currentWeight = common_vendor.ref(0);
|
||
const count = common_vendor.ref(0);
|
||
const countInput = common_vendor.ref("");
|
||
const inputMode = common_vendor.ref("bluetooth");
|
||
const manualWeight = common_vendor.ref("");
|
||
const currentPrice = common_vendor.computed(() => {
|
||
return priceStore.getPriceByCategory(category.value);
|
||
});
|
||
const estimatedAmount = common_vendor.computed(() => {
|
||
if (!currentPrice.value) return 0;
|
||
const value = currentPrice.value.unit === "kg" ? inputMode.value === "bluetooth" ? currentWeight.value : parseFloat(manualWeight.value) || 0 : count.value;
|
||
return Number((value * currentPrice.value.price).toFixed(2));
|
||
});
|
||
common_vendor.onLoad(async (options) => {
|
||
if (options == null ? void 0 : options.category) {
|
||
category.value = options.category;
|
||
}
|
||
await priceStore.loadPrices();
|
||
bluetoothStore.init();
|
||
});
|
||
const goToBluetooth = () => {
|
||
common_vendor.index.navigateTo({ url: "/pages/bluetooth/bluetooth" });
|
||
};
|
||
const resetWeight = () => {
|
||
bluetoothStore.resetWeight();
|
||
currentWeight.value = 0;
|
||
};
|
||
const decreaseCount = () => {
|
||
if (count.value > 0) {
|
||
count.value--;
|
||
countInput.value = count.value.toString();
|
||
}
|
||
};
|
||
const increaseCount = () => {
|
||
count.value++;
|
||
countInput.value = count.value.toString();
|
||
};
|
||
const onCountInput = () => {
|
||
const value = parseInt(countInput.value) || 0;
|
||
count.value = Math.max(0, value);
|
||
};
|
||
const quickCountInput = (num) => {
|
||
count.value += num;
|
||
countInput.value = count.value.toString();
|
||
};
|
||
const switchMode = (mode) => {
|
||
inputMode.value = mode;
|
||
if (mode === "manual") {
|
||
manualWeight.value = currentWeight.value > 0 ? currentWeight.value.toString() : "";
|
||
}
|
||
};
|
||
const onManualWeightInput = () => {
|
||
const value = parseFloat(manualWeight.value);
|
||
if (!isNaN(value) && value >= 0) {
|
||
currentWeight.value = value;
|
||
}
|
||
};
|
||
const quickInput = (weight) => {
|
||
const current = parseFloat(manualWeight.value) || 0;
|
||
manualWeight.value = (current + weight).toFixed(2);
|
||
onManualWeightInput();
|
||
};
|
||
const confirmRecycle = async () => {
|
||
if (!currentPrice.value) {
|
||
common_vendor.index.showToast({ title: "请选择回收种类", icon: "none" });
|
||
return;
|
||
}
|
||
const value = currentPrice.value.unit === "kg" ? inputMode.value === "bluetooth" ? currentWeight.value : parseFloat(manualWeight.value) || 0 : count.value;
|
||
if (value <= 0) {
|
||
common_vendor.index.showToast({ title: currentPrice.value.unit === "kg" ? "请输入重量" : "请输入数量", icon: "none" });
|
||
return;
|
||
}
|
||
const orderItem = {
|
||
category: currentPrice.value.category,
|
||
name: currentPrice.value.name,
|
||
weight: currentPrice.value.unit === "kg" ? value : void 0,
|
||
count: currentPrice.value.unit === "piece" ? count.value : void 0,
|
||
unit: currentPrice.value.unit,
|
||
price: currentPrice.value.price,
|
||
amount: estimatedAmount.value
|
||
};
|
||
common_vendor.index.showModal({
|
||
title: "确认回收",
|
||
content: `确定回收${currentPrice.value.name} ${value}${currentPrice.value.unit === "kg" ? "kg" : "个"},金额¥${estimatedAmount.value.toFixed(2)}?`,
|
||
success: async (res) => {
|
||
if (res.confirm) {
|
||
common_vendor.index.showLoading({ title: "保存中..." });
|
||
const region = regionStore.getRegion();
|
||
const order = await orderStore.createOrder([orderItem], region);
|
||
common_vendor.index.hideLoading();
|
||
if (order) {
|
||
common_vendor.index.showToast({ title: "回收成功", icon: "success" });
|
||
setTimeout(() => {
|
||
common_vendor.index.navigateBack();
|
||
}, 1500);
|
||
} else {
|
||
common_vendor.index.showToast({ title: "保存失败", icon: "none" });
|
||
}
|
||
}
|
||
}
|
||
});
|
||
};
|
||
common_vendor.onMounted(() => {
|
||
const updateWeight = () => {
|
||
currentWeight.value = bluetoothStore.currentWeight;
|
||
};
|
||
bluetoothStore.$subscribe(() => {
|
||
updateWeight();
|
||
});
|
||
updateWeight();
|
||
});
|
||
common_vendor.onShow(() => {
|
||
priceStore.loadPrices();
|
||
});
|
||
return (_ctx, _cache) => {
|
||
var _a, _b, _c, _d, _e;
|
||
return common_vendor.e({
|
||
a: common_vendor.t(common_vendor.unref(bluetoothStore).isConnected ? "🔵" : "⚪"),
|
||
b: common_vendor.t(common_vendor.unref(bluetoothStore).isConnected ? "已连接" : "未连接"),
|
||
c: common_vendor.unref(bluetoothStore).isConnected ? 1 : "",
|
||
d: common_vendor.t(((_a = currentPrice.value) == null ? void 0 : _a.name) || "-"),
|
||
e: common_vendor.t(((_b = currentPrice.value) == null ? void 0 : _b.price) || 0),
|
||
f: common_vendor.t(((_c = currentPrice.value) == null ? void 0 : _c.unit) === "kg" ? "kg" : "个"),
|
||
g: ((_d = currentPrice.value) == null ? void 0 : _d.unit) === "kg"
|
||
}, ((_e = currentPrice.value) == null ? void 0 : _e.unit) === "kg" ? common_vendor.e({
|
||
h: inputMode.value === "bluetooth" ? 1 : "",
|
||
i: common_vendor.o(($event) => switchMode("bluetooth")),
|
||
j: inputMode.value === "manual" ? 1 : "",
|
||
k: common_vendor.o(($event) => switchMode("manual")),
|
||
l: inputMode.value === "bluetooth"
|
||
}, inputMode.value === "bluetooth" ? {
|
||
m: common_vendor.t(currentWeight.value.toFixed(2))
|
||
} : {
|
||
n: common_vendor.o([($event) => manualWeight.value = $event.detail.value, onManualWeightInput]),
|
||
o: manualWeight.value
|
||
}, {
|
||
p: inputMode.value === "bluetooth"
|
||
}, inputMode.value === "bluetooth" ? {
|
||
q: common_vendor.o(goToBluetooth),
|
||
r: common_vendor.o(resetWeight)
|
||
} : {
|
||
s: common_vendor.o(($event) => quickInput(0.5)),
|
||
t: common_vendor.o(($event) => quickInput(1)),
|
||
v: common_vendor.o(($event) => quickInput(2)),
|
||
w: common_vendor.o(($event) => quickInput(5))
|
||
}) : {
|
||
x: common_vendor.o([($event) => countInput.value = $event.detail.value, onCountInput]),
|
||
y: countInput.value,
|
||
z: common_vendor.o(decreaseCount),
|
||
A: common_vendor.o(increaseCount),
|
||
B: common_vendor.o(($event) => quickCountInput(1)),
|
||
C: common_vendor.o(($event) => quickCountInput(5)),
|
||
D: common_vendor.o(($event) => quickCountInput(10)),
|
||
E: common_vendor.o(($event) => quickCountInput(50))
|
||
}, {
|
||
F: common_vendor.t(estimatedAmount.value.toFixed(2)),
|
||
G: common_vendor.o(confirmRecycle)
|
||
});
|
||
};
|
||
}
|
||
});
|
||
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-6f825acf"]]);
|
||
wx.createPage(MiniProgramPage);
|