mine.ts 6.6 KB

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