offlineTreatment.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import I18nBehavior from "../../../../i18n/behavior";
  2. import { getOfflineTreatmentListMethod } from "../../request";
  3. Page({
  4. behaviors: [I18nBehavior],
  5. data: {
  6. i18n: {
  7. offlineTreatment: { title: '线下', ing: '进行中', finish: '完成' },
  8. },
  9. currentTab: "",
  10. treatmentList: [],
  11. isLoading: false,
  12. treatmentId: "",
  13. },
  14. async onLoad(options: any) {
  15. console.log(options, '传过来的参数')
  16. if (options.id) {
  17. this.setData({
  18. treatmentId: options.id,
  19. });
  20. }
  21. // await this.getOfflineTreatmentList("");
  22. },
  23. async onShow() {
  24. await this.getOfflineTreatmentList("");
  25. },
  26. // 跳转到核销记录
  27. onRecord(e: any) {
  28. const id = e.currentTarget.dataset.id;
  29. if (id) {
  30. wx.navigateTo({
  31. url: `/module/care/pages/care/verifyRecord?id=${id}`,
  32. });
  33. }
  34. },
  35. // 去预约
  36. goAppointment(e: any) {
  37. const { item } = e.currentTarget.dataset;
  38. const { offlineId, conditioningProgramName, offlineDuration, itemImgSecond, estimatedStartDate } = item;
  39. const goodsInfo = {
  40. name: conditioningProgramName || '',
  41. duration: offlineDuration || 0,
  42. image: itemImgSecond || '',
  43. offlineId: offlineId || '',
  44. serviceTime: estimatedStartDate || '',
  45. }
  46. console.log(goodsInfo, "goodsInfo======去预约页面")
  47. if (offlineId) {
  48. // 去预约页面
  49. wx.navigateTo({
  50. url: `/module/order/pages/appointment/appointment?goodsInfo=${JSON.stringify(goodsInfo)}`,
  51. });
  52. } else {
  53. wx.showToast({
  54. title: "线下服务ID不能为空",
  55. icon: "none",
  56. });
  57. }
  58. // const id = e.currentTarget.dataset.id;
  59. // if (id) {
  60. // // 跳转到预约页面,传递项目id
  61. // wx.navigateTo({
  62. // url: `/module/care/pages/careDetail/careDetail?id=${id}`,
  63. // });
  64. // }
  65. },
  66. onTabChange(e: any) {
  67. const progress = e.detail.value;
  68. this.setData({
  69. currentTab: progress,
  70. });
  71. // 不立即清空数据,避免空状态闪烁
  72. this.getOfflineTreatmentList(progress);
  73. },
  74. // 获取线下非药物治疗记录列表
  75. async getOfflineTreatmentList(progress: string) {
  76. // 启用加载态,保留当前列表,避免空态闪烁
  77. this.setData({ isLoading: true });
  78. try {
  79. const res = await getOfflineTreatmentListMethod(progress);
  80. console.log(res.data, "res.data")
  81. // 确保 res.data 是数组,避免 undefined 或 null 导致的错误
  82. const list = Array.isArray(res?.data) ? res.data : [];
  83. // 如果传入了treatmentId,标记匹配的项目为高亮并置顶
  84. const treatmentId = this.data.treatmentId;
  85. console.log(treatmentId, "treatmentId")
  86. console.log(list, "list")
  87. if (treatmentId && list.length > 0) {
  88. const highlightedList = list.map((item: any) => ({
  89. ...item,
  90. isHighlighted: item.id === treatmentId || String(item.id) === String(treatmentId),
  91. }));
  92. // 将高亮的项目移到最前面
  93. const highlightedItem = highlightedList.find((item: any) => item.isHighlighted);
  94. const otherItems = highlightedList.filter((item: any) => !item.isHighlighted);
  95. const sortedList = highlightedItem ? [highlightedItem, ...otherItems] : highlightedList;
  96. console.log(sortedList, "sortedList")
  97. this.setData({ treatmentList: sortedList, isLoading: false });
  98. } else {
  99. // 只有在数据加载完成后才更新列表
  100. this.setData({ treatmentList: list, isLoading: false });
  101. }
  102. } catch (error: any) {
  103. console.error('获取数据失败:', error);
  104. // 只有在真正出错时才显示错误提示
  105. wx.showToast({
  106. title: error.errMsg || "获取数据失败",
  107. icon: "none",
  108. });
  109. this.setData({ treatmentList: [], isLoading: false });
  110. }
  111. },
  112. });