consultation-record.ts 3.8 KB

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