|
|
@@ -9,6 +9,7 @@ Page({
|
|
|
userInfo: null,
|
|
|
tabbarValue: "/pages/mine/mine",
|
|
|
phone: "",
|
|
|
+ loaded: false,
|
|
|
mineOrderList: [
|
|
|
{
|
|
|
url: "../../assets/icon/obligation@3x.png",
|
|
|
@@ -40,6 +41,38 @@ Page({
|
|
|
},
|
|
|
],
|
|
|
},
|
|
|
+ // 读取缓存,若存在则直接渲染并返回已读到的数据
|
|
|
+ hydrateFromCache() {
|
|
|
+ const cachedPatient = (wx.getStorageSync("patient") || null) as App.Patient.Model | null;
|
|
|
+ const cachedPhone = (wx.getStorageSync("patientPhone") || "") as string;
|
|
|
+ if (cachedPatient || cachedPhone) {
|
|
|
+ this.setData({
|
|
|
+ patient: cachedPatient || null,
|
|
|
+ phone: cachedPhone || "",
|
|
|
+ loaded: true,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return { cachedPatient, cachedPhone } as {
|
|
|
+ cachedPatient: App.Patient.Model | null;
|
|
|
+ cachedPhone: string;
|
|
|
+ };
|
|
|
+ },
|
|
|
+ // 写入缓存
|
|
|
+ syncCache(patient: App.Patient.Model | null, phone: string) {
|
|
|
+ try {
|
|
|
+ wx.setStorageSync("patient", patient || null);
|
|
|
+ wx.setStorageSync("patientPhone", phone || "");
|
|
|
+ wx.setStorageSync("patientName", patient?.name);
|
|
|
+ } catch {}
|
|
|
+ },
|
|
|
+ // 轻量对比(避免 JSON 深比较):患者用 patientId 或 name,对比手机号字符串
|
|
|
+ isDifferent(cachedPatient: App.Patient.Model | null, newPatient: App.Patient.Model | null, cachedPhone: string, newPhone: string) {
|
|
|
+ const cachedId = cachedPatient?.patientId || "";
|
|
|
+ const newId = newPatient?.patientId || "";
|
|
|
+ const cachedName = cachedPatient?.name || "";
|
|
|
+ const newName = newPatient?.name || "";
|
|
|
+ return cachedId !== newId || cachedName !== newName || (cachedPhone || "") !== (newPhone || "");
|
|
|
+ },
|
|
|
// 获取各状态调理记录数量
|
|
|
async getRecordCount() {
|
|
|
console.log("获取各状态调理记录数量");
|
|
|
@@ -81,19 +114,17 @@ Page({
|
|
|
}
|
|
|
this.getRecordCount();
|
|
|
try {
|
|
|
+ // 1) 先读缓存渲染,避免初始闪烁
|
|
|
+ const { cachedPatient, cachedPhone } = this.hydrateFromCache();
|
|
|
+
|
|
|
+ // 2) 并行请求接口,完成后轻量对比,有变更再更新与回写缓存
|
|
|
wx.showLoading({ title: "加载中" });
|
|
|
- const { patient } = await getPatients();
|
|
|
- const res = await getPatientPhone();
|
|
|
- console.log(res, "获取手机号");
|
|
|
- if (res && res.phone) {
|
|
|
- this.setData({
|
|
|
- phone: res.phone,
|
|
|
- });
|
|
|
+ const [{ patient }, phoneRes] = await Promise.all([getPatients(), getPatientPhone()]);
|
|
|
+ const latestPhone = phoneRes?.phone || "";
|
|
|
+ if (this.isDifferent(cachedPatient, patient || null, cachedPhone || "", latestPhone) || !this.data.loaded) {
|
|
|
+ this.setData({ patient: patient || null, phone: latestPhone, loaded: true });
|
|
|
+ this.syncCache(patient || null, latestPhone);
|
|
|
}
|
|
|
- wx.setStorageSync("patientName", patient?.name);
|
|
|
- this.setData({
|
|
|
- patient: patient || null,
|
|
|
- });
|
|
|
} catch (error) {
|
|
|
console.error("加载失败:", error);
|
|
|
wx.showToast({
|
|
|
@@ -103,6 +134,9 @@ Page({
|
|
|
});
|
|
|
} finally {
|
|
|
wx.hideLoading();
|
|
|
+ if (!this.data.loaded) {
|
|
|
+ this.setData({ loaded: true });
|
|
|
+ }
|
|
|
}
|
|
|
},
|
|
|
|