offlineTreatment.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. },
  59. onTabChange(e: any) {
  60. const progress = e.detail.value;
  61. this.setData({
  62. currentTab: progress,
  63. });
  64. // 不立即清空数据,避免空状态闪烁
  65. this.getOfflineTreatmentList(progress);
  66. },
  67. // 获取线下非药物治疗记录列表
  68. async getOfflineTreatmentList(progress: string) {
  69. // 启用加载态,保留当前列表,避免空态闪烁
  70. this.setData({ isLoading: true });
  71. try {
  72. const res = await getOfflineTreatmentListMethod(progress);
  73. console.log(res.data, "res.data")
  74. // 确保 res.data 是数组,避免 undefined 或 null 导致的错误
  75. const list = Array.isArray(res?.data) ? res.data : [];
  76. // 如果传入了treatmentId,标记匹配的项目为高亮并置顶
  77. const treatmentId = this.data.treatmentId;
  78. console.log(treatmentId, "treatmentId")
  79. console.log(list, "list")
  80. if (treatmentId && list.length > 0) {
  81. const highlightedList = list.map((item: any) => ({
  82. ...item,
  83. isHighlighted: item.id === treatmentId || String(item.id) === String(treatmentId),
  84. }));
  85. // 将高亮的项目移到最前面
  86. const highlightedItem = highlightedList.find((item: any) => item.isHighlighted);
  87. const otherItems = highlightedList.filter((item: any) => !item.isHighlighted);
  88. const sortedList = highlightedItem ? [highlightedItem, ...otherItems] : highlightedList;
  89. console.log(sortedList, "sortedList")
  90. this.setData({ treatmentList: sortedList, isLoading: false });
  91. } else {
  92. // 只有在数据加载完成后才更新列表
  93. this.setData({ treatmentList: list, isLoading: false });
  94. }
  95. } catch (error: any) {
  96. console.error('获取数据失败:', error);
  97. // 只有在真正出错时才显示错误提示
  98. wx.showToast({
  99. title: error.errMsg || "获取数据失败",
  100. icon: "none",
  101. });
  102. this.setData({ treatmentList: [], isLoading: false });
  103. }
  104. },
  105. });