From 35442cbb865d875a0ca03b6ea76be494a265960d Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Tue, 16 Dec 2025 16:45:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.vue | 128 +++++++++++------- unpackage/dist/build/web/index.html | 2 +- .../{index.8e5a8a07.js => index.5f04b5d5.js} | 2 +- .../static/js/pages-index-index.3e11c865.js | 1 + .../static/js/pages-index-index.bd0c5c51.js | 1 - 5 files changed, 83 insertions(+), 51 deletions(-) rename unpackage/dist/build/web/static/js/{index.8e5a8a07.js => index.5f04b5d5.js} (99%) create mode 100644 unpackage/dist/build/web/static/js/pages-index-index.3e11c865.js delete mode 100644 unpackage/dist/build/web/static/js/pages-index-index.bd0c5c51.js diff --git a/pages/index/index.vue b/pages/index/index.vue index 2fbbeb4..86c1cb7 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -231,18 +231,6 @@ import { API } from "@/config/index.js"; export default { data() { return { - batches: [ - { - id: 1, - direction: "in", - name: "2025010101", - }, - { - id: 2, - direction: "out", - name: "2025010102", - }, - ], isWeixinBrowser: false, userInfo: null, showEditPopup: false, @@ -260,18 +248,7 @@ export default { paid_count: 0, billed_count: 0, // 测试数据 - batches - batches: [ - { - id: 1, - direction: "in", - name: "2025010101", - }, - { - id: 2, - direction: "out", - name: "2025010102", - }, - ], + batches: [], }, }; }, @@ -296,6 +273,30 @@ export default { uni.$off("loginSuccess", this.onLoginSuccess); }, methods: { + // token 失效统一处理 + handleTokenInvalid() { + // 清理本地 token + uni.removeStorageSync("token"); + // 简单清空当前页关键数据 + this.userInfo = null; + // #ifdef H5 + // H5 微信环境下,尝试重新发起授权登录 + try { + const app = getApp(); + if (app && typeof app.wxH5AuthLogin === "function") { + app.wxH5AuthLogin(); + return; + } + } catch (e) { + console.warn("handleTokenInvalid 调用 wxH5AuthLogin 失败:", e); + } + // #endif + // 其他环境给出提示 + uni.showToast({ + title: "登录已失效,请重新进入", + icon: "none", + }); + }, // 等待 token 获取成功后调用接口 waitForTokenAndFetch() { const token = uni.getStorageSync("token"); @@ -347,15 +348,22 @@ export default { fail: reject, }); }); - if (res.data && res.data.errcode === 0) { - const data = res.data.data; - // 将水位数据存储到 statistics 中 - if (data) { - console.log("data", data); - this.statistics.taihu_to_xujiang = data.taihu_to_xujiang || null; - this.statistics.xujiang_to_taihu = data.xujiang_to_taihu || null; + if (res.data) { + if (res.data.errcode === 4000) { + // token 鉴权失败,重新登录 + this.handleTokenInvalid(); + return; + } + if (res.data.errcode === 0) { + const data = res.data.data; + // 将水位数据存储到 statistics 中 + if (data) { + console.log("data", data); + this.statistics.taihu_to_xujiang = data.taihu_to_xujiang || null; + this.statistics.xujiang_to_taihu = data.xujiang_to_taihu || null; + } + console.log(this.statistics); } - console.log(this.statistics); } }, async fetchStatistics() { @@ -371,16 +379,23 @@ export default { fail: reject, }); }); - if (res.data && res.data.errcode === 0) { - // 保留已有的水位数据,合并新的统计数据 - const existingWaterData = { - taihu_to_xujiang: this.statistics.taihu_to_xujiang, - xujiang_to_taihu: this.statistics.xujiang_to_taihu, - }; - this.statistics = { - ...res.data.data, - ...existingWaterData, - }; + if (res.data) { + if (res.data.errcode === 4000) { + // token 鉴权失败,重新登录 + this.handleTokenInvalid(); + return; + } + if (res.data.errcode === 0) { + // 保留已有的水位数据,合并新的统计数据 + const existingWaterData = { + taihu_to_xujiang: this.statistics.taihu_to_xujiang, + xujiang_to_taihu: this.statistics.xujiang_to_taihu, + }; + this.statistics = { + ...res.data.data, + ...existingWaterData, + }; + } } }, async fetchUserInfo() { @@ -399,14 +414,31 @@ export default { }); }); if (res.data) { - const data = res.data || {}; - const payerType = Number(data.payer_type ?? data.payerType ?? 1); - this.userInfo = { ...data, payer_type: payerType }; + // 兼容两种返回结构:直接返回用户数据或带 errcode 包裹 + if (typeof res.data.errcode !== "undefined") { + if (res.data.errcode === 4000) { + // token 鉴权失败 + this.handleTokenInvalid(); + return; + } + if (res.data.errcode !== 0) { + // 其他错误直接提示 + uni.showToast({ + title: res.data.errmsg || "获取用户信息失败", + icon: "none", + }); + this.userInfo = null; + return; + } + } + const raw = res.data.data || res.data; + const payerType = Number(raw.payer_type ?? raw.payerType ?? 1); + this.userInfo = { ...raw, payer_type: payerType }; this.editForm = { - name: data.name || "", + name: raw.name || "", payer_type: payerType, - phone: data.phone || "", - id_card: data.id_card || "", + phone: raw.phone || "", + id_card: raw.id_card || "", }; } } catch (e) { diff --git a/unpackage/dist/build/web/index.html b/unpackage/dist/build/web/index.html index b4cd95d..36d7170 100644 --- a/unpackage/dist/build/web/index.html +++ b/unpackage/dist/build/web/index.html @@ -1,2 +1,2 @@