consultation-record.ts 3.8 KB

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