mine.ts 7.4 KB

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