consultation-record.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import I18nBehavior from "../../../../i18n/behavior";
  3. import { Post } from "../../../../lib/request/method";
  4. import { getFullImageUrl } from "../../../../utils/util";
  5. // module/chats/pages/consultation-record/consultation-record.ts
  6. Page({
  7. behaviors: [I18nBehavior, PageContainerBehavior],
  8. properties: {},
  9. data: {
  10. i18n: {
  11. consultChat: { _: '聊天', title: '记录' },
  12. },
  13. //有item传过来不分页
  14. //是否分页
  15. isPage: false,
  16. page: 1,
  17. pageSize: 2,
  18. hasMore: true,
  19. isLoading: false,
  20. consultationList: [] as Array<{
  21. id: number;
  22. startTime: string;
  23. endTime: string;
  24. manualTime: string; //转人工的时间
  25. items: Array<{
  26. id: number; //咨询记录id
  27. consultRecordId: number; //咨询记录id
  28. messageContent: string;
  29. sendTime: string; //发送时间
  30. sendType: string; // 发送类型 1-患者 2-医生 3-系统 4-AI
  31. messageType: string; // 消息类型 1-文本 2-图片
  32. isRead: string;
  33. }>;
  34. }>,
  35. },
  36. observers: {},
  37. onLoad(options: any) {
  38. if (options.item) {
  39. const item = JSON.parse(decodeURIComponent(options.item));
  40. this.setData({
  41. consultationList: item,
  42. isPage: false,
  43. });
  44. } else {
  45. this.setData({
  46. isPage: true,
  47. });
  48. this.loadData();
  49. }
  50. },
  51. // 获取聊天记录的数据
  52. async loadData() {
  53. if (this.data.isLoading || !this.data.hasMore) return;
  54. this.setData({ isLoading: true });
  55. try {
  56. const res = await Post(
  57. `/consultManage/pageConsult?pageNum=${this.data.page}&pageSize=${this.data.pageSize}`,
  58. {}
  59. );
  60. if (res.data && res.data.data && res.data.data.length > 0) {
  61. // 处理图片消息的URL
  62. res.data.data.forEach((consultation: any) => {
  63. consultation.items?.forEach((item: any) => {
  64. if (item.messageType === "2") {
  65. item.messageContent = getFullImageUrl(item.messageContent);
  66. }
  67. });
  68. });
  69. // 追加新数据到现有列表
  70. const newList =
  71. this.data.page === 1
  72. ? res.data.data
  73. : [...this.data.consultationList, ...res.data.data];
  74. // 判断是否还有更多数据
  75. // 方式1: 如果有 total 字段,通过比较已加载数量和总数判断
  76. // 方式2: 如果返回的数据长度小于 pageSize,说明没有更多数据了
  77. let hasMoreData = false;
  78. if (res.data.total !== undefined && res.data.total !== null) {
  79. hasMoreData = newList.length < res.data.total;
  80. } else {
  81. // 如果返回的数据长度等于 pageSize,可能还有更多数据
  82. hasMoreData = res.data.data.length >= this.data.pageSize;
  83. }
  84. this.setData({
  85. consultationList: newList,
  86. page: this.data.page + 1,
  87. hasMore: hasMoreData,
  88. isLoading: false,
  89. });
  90. } else {
  91. // 没有数据时,第一页清空列表,后续页保留已有数据
  92. this.setData({
  93. ...(this.data.page === 1 && { consultationList: [] }),
  94. hasMore: false,
  95. isLoading: false,
  96. });
  97. }
  98. } catch (error) {
  99. console.error("加载咨询记录失败", error);
  100. this.setData({
  101. isLoading: false,
  102. });
  103. }
  104. },
  105. onScrollToLower() {
  106. if (this.data.isPage) {
  107. this.loadData();
  108. }
  109. },
  110. //预览图片
  111. previewImage(e: any) {
  112. const currentUrl = e.currentTarget.dataset.url;
  113. // 获取所有咨询记录中所有图片消息的 URL 列表
  114. const urls: string[] = [];
  115. this.data?.consultationList?.forEach((consultation) => {
  116. consultation?.items?.forEach((item) => {
  117. if (item?.messageType === "2" && item?.messageContent) {
  118. urls.push(item?.messageContent);
  119. }
  120. });
  121. });
  122. wx.previewImage({
  123. current: currentUrl, // 当前显示图片的链接
  124. urls: urls.length > 0 ? urls : [currentUrl], // 需要预览的图片链接列表
  125. fail: (err) => {
  126. console.error("预览图片失败", err);
  127. },
  128. });
  129. },
  130. });