mine.ts 6.3 KB

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