mine.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { getPatients, getPatientPhone } from "../home/request";
  2. import { toCertificationPage } from "../home/router";
  3. import { getOrderList, getRecordCountMethod } from "../home/request";
  4. Page({
  5. data: {
  6. patients: [] as (App.Patient.Model & { isDefault: "Y" | "N" })[],
  7. patient: null as App.Patient.Model | null,
  8. patientDescription: "",
  9. userInfo: null,
  10. tabbarValue: "/pages/mine/mine",
  11. phone: "",
  12. loaded: false,
  13. mineOrderList: [
  14. {
  15. url: "../../assets/icon/obligation@3x.png",
  16. name: "待付款",
  17. count: 0,
  18. type: 1,
  19. tab: "pending",
  20. },
  21. {
  22. url: "../../assets/icon/delivery@3x.png",
  23. name: "待收货",
  24. count: 0,
  25. type: 2,
  26. tab: "received",
  27. },
  28. {
  29. url: "../../assets/icon/deal@3x.png",
  30. name: "交易成功",
  31. count: 0,
  32. type: 3,
  33. tab: "completed",
  34. },
  35. {
  36. url: "../../assets/icon/all@3x.png",
  37. name: "全部",
  38. count: 0,
  39. type: 4,
  40. tab: "all",
  41. },
  42. ],
  43. },
  44. // 读取缓存,若存在则直接渲染并返回已读到的数据
  45. hydrateFromCache() {
  46. const cachedPatient = (wx.getStorageSync("patient") || null) as App.Patient.Model | null;
  47. const cachedPhone = (wx.getStorageSync("patientPhone") || "") as string;
  48. if (cachedPatient || cachedPhone) {
  49. this.setData({
  50. patient: cachedPatient || null,
  51. phone: cachedPhone || "",
  52. loaded: true,
  53. });
  54. }
  55. return { cachedPatient, cachedPhone } as {
  56. cachedPatient: App.Patient.Model | null;
  57. cachedPhone: string;
  58. };
  59. },
  60. // 写入缓存
  61. syncCache(patient: App.Patient.Model | null, phone: string) {
  62. try {
  63. wx.setStorageSync("patient", patient || null);
  64. wx.setStorageSync("patientPhone", phone || "");
  65. wx.setStorageSync("patientName", patient?.name);
  66. } catch {}
  67. },
  68. // 轻量对比(避免 JSON 深比较):患者用 patientId 或 name,对比手机号字符串
  69. isDifferent(cachedPatient: App.Patient.Model | null, newPatient: App.Patient.Model | null, cachedPhone: string, newPhone: string) {
  70. const cachedId = cachedPatient?.patientId || "";
  71. const newId = newPatient?.patientId || "";
  72. const cachedName = cachedPatient?.name || "";
  73. const newName = newPatient?.name || "";
  74. return cachedId !== newId || cachedName !== newName || (cachedPhone || "") !== (newPhone || "");
  75. },
  76. // 获取各状态调理记录数量
  77. async getRecordCount() {
  78. console.log("获取各状态调理记录数量");
  79. try {
  80. const res = await getRecordCountMethod();
  81. console.log(res.data, "获取各状态调理记录数量");
  82. if (res && res.data) {
  83. this.setData({
  84. "mineOrderList[0].count": res.data.pendingPayment,
  85. "mineOrderList[1].count": res.data.pendingReceiptGoods,
  86. // "mineOrderList[2].count": res.data.successTrade,
  87. // "mineOrderList[3].count": res.data.total,
  88. });
  89. }
  90. console.log(this.data.mineOrderList, "mineOrderList");
  91. } catch (error: any) {
  92. console.error("获取各状态调理记录数量失败:", error);
  93. wx.showToast({
  94. title: error.message,
  95. icon: "none",
  96. duration: 2000,
  97. });
  98. }
  99. },
  100. async load(retry = false) {
  101. if (retry) {
  102. await wx
  103. .showModal({
  104. title: "提示",
  105. content: "数据加载失败,是否重试?",
  106. showCancel: true,
  107. })
  108. .then((res) => {
  109. if (res.confirm) {
  110. this.load();
  111. }
  112. });
  113. return;
  114. }
  115. this.getRecordCount();
  116. try {
  117. // 1) 先读缓存渲染,避免初始闪烁
  118. const { cachedPatient, cachedPhone } = this.hydrateFromCache();
  119. // 2) 并行请求接口,完成后轻量对比,有变更再更新与回写缓存
  120. wx.showLoading({ title: "加载中" });
  121. const [{ patient }, phoneRes] = await Promise.all([getPatients(), getPatientPhone()]);
  122. const latestPhone = phoneRes?.phone || "";
  123. if (this.isDifferent(cachedPatient, patient || null, cachedPhone || "", latestPhone) || !this.data.loaded) {
  124. this.setData({ patient: patient || null, phone: latestPhone, loaded: true });
  125. this.syncCache(patient || null, latestPhone);
  126. }
  127. } catch (error) {
  128. console.error("加载失败:", error);
  129. wx.showToast({
  130. title: "加载失败",
  131. icon: "none",
  132. duration: 2000,
  133. });
  134. } finally {
  135. wx.hideLoading();
  136. if (!this.data.loaded) {
  137. this.setData({ loaded: true });
  138. }
  139. }
  140. },
  141. // 健康档案
  142. toHealthPage() {
  143. if (!this.data.patient?.patientId) {
  144. wx.showToast({
  145. title: "请先登录",
  146. icon: "none",
  147. });
  148. toCertificationPage();
  149. } else {
  150. wx.navigateTo({ url: `/module/health/pages/home/home` });
  151. }
  152. },
  153. // 药膳
  154. toDietTonicPage() {
  155. wx.navigateTo({
  156. url: `/module/article/pages/diet-list/diet-list?classify=tonic`,
  157. });
  158. },
  159. // 茶饮
  160. toDietTeaPage() {
  161. wx.navigateTo({
  162. url: `/module/article/pages/diet-list/diet-list?classify=tea`,
  163. });
  164. },
  165. // 订单列表
  166. toOrderListPage(e: any) {
  167. console.log("tab", e);
  168. const tab = e.currentTarget.dataset.tab;
  169. wx.navigateTo({
  170. url: `/module/article/pages/order-list/order-list?tab=${tab}`,
  171. });
  172. },
  173. // 获取全部订单列表
  174. async getOrderList() {
  175. const patientId = wx.getStorageSync("patientId");
  176. console.log(patientId, "patientId");
  177. const res = await getOrderList(patientId, "");
  178. console.log(res, "获取订单列表");
  179. },
  180. onShow() {
  181. // 如果需要每次显示页面时刷新数据,可以在这里调用 load
  182. this.load();
  183. },
  184. // 足迹
  185. toFootPrintPage() {
  186. wx.navigateTo({
  187. url: "/module/article/pages/foot-print/foot-print",
  188. });
  189. },
  190. // 健康宣教
  191. toHealthEducationPage() {
  192. wx.showToast({
  193. title: "敬请期待",
  194. icon: "none",
  195. });
  196. // wx.navigateTo({
  197. // url: '/module/article/pages/evangelism-notice/evangelism-notice'
  198. // });
  199. },
  200. onAddressTap() {
  201. console.log("onAddressTap");
  202. // 跳转到收货地址页面
  203. wx.navigateTo({
  204. url: "/module/article/pages/manage-address/manage-address",
  205. });
  206. },
  207. onConsultationTap() {
  208. // 跳转到咨询记录页面
  209. wx.showToast({
  210. title: "敬请期待",
  211. icon: "none",
  212. });
  213. // wx.navigateTo({
  214. // url: '/module/article/pages/consultation-record/consultation-record'
  215. // });
  216. },
  217. onTreatmentTap() {
  218. // 跳转到线下非药物治疗页面
  219. wx.navigateTo({
  220. url: "/module/care/pages/offlineTreatment/offlineTreatment",
  221. });
  222. },
  223. });