mine.ts 7.2 KB

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