From d859e982302e7ba0f4d50988a450b6b0efd60f1d Mon Sep 17 00:00:00 2001 From: WindowBird <13870814+windows-bird@user.noreply.gitee.com> Date: Tue, 14 Oct 2025 16:10:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=9B=E4=B8=AA=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E6=8E=A7=E5=88=B6=E7=89=8C=E4=BD=8D-=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/memorial/adminMemorial.vue | 104 ++++++++++--------------------- 1 file changed, 33 insertions(+), 71 deletions(-) diff --git a/pages/memorial/adminMemorial.vue b/pages/memorial/adminMemorial.vue index efe648d..79242f6 100644 --- a/pages/memorial/adminMemorial.vue +++ b/pages/memorial/adminMemorial.vue @@ -94,100 +94,62 @@ export default { } }, methods: { - // 底部按钮:强制开启 - async handleForceOpen() { + // 校验是否已选择单元 + ensureUnitSelected() { + if (!this.selectedUnitId) { + uni.showToast({ title: "请先选择单元", icon: "none" }); + return false; + } + return true; + }, + + // 通用 PUT 请求封装:包含 Loading 与结果提示 + async performPut(url, data, logTag = "performPut") { uni.showLoading({ title: "处理中...", mask: true }); try { - const res = await this.$request.put( - `/bst/memorial/open/${this.selectedUnitId}`, - ); + const res = await this.$request.put(url, data); 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) { uni.showToast({ title: "请求失败", icon: "none" }); - console.error("handleForceOpen error", error); + console.error(`${logTag} error`, error); + throw error; } finally { uni.hideLoading(); } }, + // 底部按钮:强制开启 + async handleForceOpen() { + if (!this.ensureUnitSelected()) return; + await this.performPut(`/bst/memorial/open/${this.selectedUnitId}`, null, "handleForceOpen"); + }, + // 底部按钮:强制关闭 async handleForceClose() { - uni.showLoading({ title: "处理中...", mask: true }); - try { - const res = await this.$request.put( - `/bst/memorial/close/${this.selectedUnitId}`, - ); - if (res && (res.code === 200 || res.status === 200)) { - uni.showToast({ title: res.msg || "操作成功", icon: "success" }); - } else { - uni.showToast({ - title: (res && res.msg) || "操作失败", - icon: "none", - }); - } - } catch (error) { - uni.showToast({ title: "请求失败", icon: "none" }); - console.error("handleForceClose error", error); - } finally { - uni.hideLoading(); - } + if (!this.ensureUnitSelected()) return; + await this.performPut(`/bst/memorial/close/${this.selectedUnitId}`, null, "handleForceClose"); }, // 底部按钮:时长归零 async handleResetDuration() { - uni.showLoading({ title: "处理中...", mask: true }); - try { - const res = await this.$request.put( - `/bst/memorial/reset/${this.selectedUnitId}`, - ); - if (res && (res.code === 200 || res.status === 200)) { - uni.showToast({ title: res.msg || "操作成功", icon: "success" }); - } else { - uni.showToast({ - title: (res && res.msg) || "操作失败", - icon: "none", - }); - } - } catch (error) { - uni.showToast({ title: "请求失败", icon: "none" }); - console.error("handleResetDuration error", error); - } finally { - uni.hideLoading(); - } + if (!this.ensureUnitSelected()) return; + await this.performPut(`/bst/memorial/reset/${this.selectedUnitId}`, null, "handleResetDuration"); }, // 底部按钮:增加时长 async handleIncreaseDuration() { - uni.showLoading({ title: "处理中...", mask: true }); - try { - // 默认增加 20 秒;unit: 4 表示“秒” - const payload = { - memorialId: String(this.selectedUnitId), - seconds: "20", - unit: "1", - }; - const res = await this.$request.put(`/bst/memorial/addTime`, payload); - if (res && (res.code === 200 || res.status === 200)) { - uni.showToast({ title: res.msg || "操作成功", icon: "success" }); - } else { - uni.showToast({ - title: (res && res.msg) || "操作失败", - icon: "none", - }); - } - } catch (error) { - uni.showToast({ title: "请求失败", icon: "none" }); - console.error("handleIncreaseDuration error", error); - } finally { - uni.hideLoading(); - } + if (!this.ensureUnitSelected()) return; + const payload = { + memorialId: String(this.selectedUnitId), + seconds: "20", + unit: "1", + }; + await this.performPut(`/bst/memorial/addTime`, payload, "handleIncreaseDuration"); }, // 初始化页面