mine.ts 7.4 KB

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