增加时长功能完善

This commit is contained in:
WindowBird 2025-10-14 16:43:15 +08:00
parent d859e98230
commit 07459ccd39

View File

@ -29,6 +29,8 @@
<view class="bottom">
<view class="btn-grid">
<view class="grid-btn" @click="handleAllOpen">全部开启</view>
<view class="grid-btn" @click="handleAllClose">全部关闭</view>
<view class="grid-btn" @click="handleForceOpen">强制开启</view>
<view class="grid-btn" @click="handleForceClose">强制关闭</view>
<view class="grid-btn" @click="handleResetDuration">时长归零</view>
@ -111,7 +113,10 @@ export default {
if (res && (res.code === 200 || res.status === 200)) {
uni.showToast({ title: res.msg || "操作成功", icon: "success" });
} else {
uni.showToast({ title: (res && res.msg) || "操作失败", icon: "none" });
uni.showToast({
title: (res && res.msg) || "操作失败",
icon: "none",
});
}
return res;
} catch (error) {
@ -123,33 +128,114 @@ export default {
}
},
//
async promptDurationAndUnit() {
// 1: 2: 3: 4:
const unitOptions = [
{ name: "天", value: "1" },
{ name: "小时", value: "2" },
{ name: "分钟", value: "3" },
{ name: "秒", value: "4" },
];
//
const unitRes = await new Promise((resolve, reject) => {
uni.showActionSheet({
itemList: unitOptions.map((o) => o.name),
success: (res) => resolve(res),
fail: (err) => reject(err),
});
});
const chosen = unitOptions[unitRes.tapIndex];
//
const inputRes = await new Promise((resolve) => {
uni.showModal({
title: `输入时长(单位:${chosen.name}`,
editable: true,
placeholderText: "请输入时长数值:",
success: (res) => resolve(res),
});
});
if (!inputRes || !inputRes.confirm) {
throw new Error("UserCancelled");
}
const value = String((inputRes.content || "").trim());
//
if (!/^\d+(\.\d+)?$/.test(value)) {
uni.showToast({ title: "请输入有效的数字", icon: "none" });
throw new Error("InvalidNumber");
}
return { amount: value, unit: chosen.value };
},
async handleAllOpen() {
if (!this.ensureUnitSelected()) return;
await this.performPut(
`/app/memorial/sendCommandGateway`,
null,
"handleForceOpen",
);
},
//
async handleForceOpen() {
if (!this.ensureUnitSelected()) return;
await this.performPut(`/bst/memorial/open/${this.selectedUnitId}`, null, "handleForceOpen");
await this.performPut(
`/bst/memorial/open/${this.selectedUnitId}`,
null,
"handleForceOpen",
);
},
//
async handleForceClose() {
if (!this.ensureUnitSelected()) return;
await this.performPut(`/bst/memorial/close/${this.selectedUnitId}`, null, "handleForceClose");
await this.performPut(
`/bst/memorial/close/${this.selectedUnitId}`,
null,
"handleForceClose",
);
},
//
async handleResetDuration() {
if (!this.ensureUnitSelected()) return;
await this.performPut(`/bst/memorial/reset/${this.selectedUnitId}`, null, "handleResetDuration");
await this.performPut(
`/bst/memorial/reset/${this.selectedUnitId}`,
null,
"handleResetDuration",
);
},
//
async handleIncreaseDuration() {
if (!this.ensureUnitSelected()) return;
try {
const { amount, unit } = await this.promptDurationAndUnit();
const payload = {
memorialId: String(this.selectedUnitId),
seconds: "20",
unit: "1",
seconds: amount,
unit,
};
await this.performPut(`/bst/memorial/addTime`, payload, "handleIncreaseDuration");
await this.performPut(
`/bst/memorial/addTime`,
payload,
"handleIncreaseDuration",
);
} catch (e) {
//
if (
e &&
e.message !== "UserCancelled" &&
e.message !== "InvalidNumber"
) {
console.error("handleIncreaseDuration prompt error", e);
}
}
},
//
@ -285,15 +371,15 @@ export default {
.bottom {
position: fixed;
left: 0;
bottom: 0;
bottom: 10rpx;
width: 100%;
background: #fffbf5;
height: 180rpx;
height: 200rpx;
}
.btn-grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 20rpx;
padding: 20rpx 32rpx;