consultation-record.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. console.log(options, "咨询记录数据");
  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. console.log("res=咨询记录列表", res.data.data);
  61. if (res.data && res.data.data && res.data.data.length > 0) {
  62. // 追加新数据到现有列表
  63. const newList =
  64. this.data.page === 1
  65. ? res.data.data
  66. : [...this.data.consultationList, ...res.data.data];
  67. // 判断是否还有更多数据
  68. // 方式1: 如果有 total 字段,通过比较已加载数量和总数判断
  69. // 方式2: 如果返回的数据长度小于 pageSize,说明没有更多数据了
  70. let hasMoreData = false;
  71. if (res.data.total !== undefined && res.data.total !== null) {
  72. hasMoreData = newList.length < res.data.total;
  73. } else {
  74. // 如果返回的数据长度等于 pageSize,可能还有更多数据
  75. hasMoreData = res.data.data.length >= this.data.pageSize;
  76. }
  77. this.setData({
  78. consultationList: newList,
  79. page: this.data.page + 1,
  80. hasMore: hasMoreData,
  81. isLoading: false,
  82. });
  83. } else {
  84. // 没有数据时,第一页清空列表,后续页保留已有数据
  85. this.setData({
  86. ...(this.data.page === 1 && { consultationList: [] }),
  87. hasMore: false,
  88. isLoading: false,
  89. });
  90. }
  91. } catch (error) {
  92. console.error("加载咨询记录失败", error);
  93. this.setData({
  94. isLoading: false,
  95. });
  96. }
  97. },
  98. onScrollToLower() {
  99. console.log("onScrollToLower");
  100. if (this.data.isPage) {
  101. this.loadData();
  102. }
  103. },
  104. //预览图片
  105. previewImage(e: any) {
  106. const currentUrl = e.currentTarget.dataset.url;
  107. // 获取所有咨询记录中所有图片消息的 URL 列表
  108. const urls: string[] = [];
  109. this.data?.consultationList?.forEach((consultation) => {
  110. consultation?.items?.forEach((item) => {
  111. if (item?.messageType === "2" && item?.messageContent) {
  112. urls.push(item?.messageContent);
  113. }
  114. });
  115. });
  116. wx.previewImage({
  117. current: currentUrl, // 当前显示图片的链接
  118. urls: urls.length > 0 ? urls : [currentUrl], // 需要预览的图片链接列表
  119. fail: (err) => {
  120. console.error("预览图片失败", err);
  121. },
  122. });
  123. },
  124. });