index.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import dayjs from "dayjs";
  2. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  3. // module/chats/pages/index/index.ts
  4. interface ScrollIntoViewEvent {
  5. detail: string;
  6. }
  7. interface HandleEvent {
  8. detail: { component: "guide" | "questionnaire"; scroll?: boolean };
  9. }
  10. interface NextTypeEvent {
  11. detail: { messageType: number };
  12. }
  13. interface Message extends Omit<HandleEvent["detail"], "scroll"> {
  14. id: string;
  15. }
  16. function getScrollcontext(this: any) {
  17. return this as {
  18. scroll: WechatMiniprogram.ScrollViewContext;
  19. timer: number;
  20. };
  21. }
  22. Component({
  23. behaviors: [PageContainerBehavior],
  24. lifetimes: {
  25. attached() {
  26. this.setData({
  27. date: dayjs().format("MM-DD HH:mm:ss"),
  28. });
  29. const component = this.data.component as "guide" | "questionnaire";
  30. // 若路由指定进入 guide,确保展示 guide
  31. if (component === "guide") {
  32. this.setData({ isShowGuide: true });
  33. }
  34. this.handle({ detail: { component, scroll: true } });
  35. // 计算底部bottom
  36. const systemInfo = wx.getSystemInfoSync();
  37. const windowHeight = systemInfo.windowHeight;
  38. const safeAreaBottom = systemInfo.safeArea?.bottom ?? windowHeight; // 没有safeArea时,bottom等于windowHeight
  39. const safeBottom = windowHeight - safeAreaBottom; // 安全区底部距离(px)
  40. const safeBottomRpx = (750 / systemInfo.windowWidth) * safeBottom; // rpx
  41. this.setData({
  42. paddingBottom: this.data.paddingBottom + safeBottomRpx,
  43. });
  44. },
  45. ready() {
  46. wx.createSelectorQuery()
  47. .select("#scrollview")
  48. .node()
  49. .exec((res) => {
  50. getScrollcontext.call(this).scroll = res[0].node;
  51. });
  52. },
  53. },
  54. properties: {
  55. component: { type: String, value: "guide" },
  56. messageType: { type: Number, value: 0 },
  57. isShowGuide: { type: Boolean, value: false },
  58. id: { type: Number, value: 0 },
  59. },
  60. data: {
  61. date: "",
  62. messages: {} as Record<number, Message>,
  63. lastId: "",
  64. inputBoxBottom: 0,
  65. tabbarValue: "/module/chats/pages/index/index",
  66. analysisCount: 0,
  67. paddingBottom: 100, // 底部tabbar高度是100
  68. },
  69. observers: {
  70. "messages.**"(messages) {
  71. const message = Object.values(messages).pop() as Message;
  72. this.setData({ lastId: message?.id });
  73. },
  74. },
  75. methods: {
  76. boxBottom(event: boxBottom) {
  77. // 直接使用子组件传递的 inputBoxBottom 值
  78. this.setData({
  79. inputBoxBottom: event.detail.inputBoxBottom,
  80. });
  81. // 更新 scroll-view 的 padding-bottom,确保内容不被输入框遮挡
  82. this.setData({
  83. paddingBottom: event.detail.inputBoxBottom,
  84. });
  85. },
  86. nextType(event: NextTypeEvent) {
  87. this.setData({ messageType: event.detail.MessageType });
  88. },
  89. getCount(event: GetCountEvent) {
  90. this.setData({ analysisCount: event.detail.analysisCount });
  91. },
  92. handle(event: HandleEvent) {
  93. const index = Object.keys(this.data.messages).length;
  94. this.setData({
  95. [`messages.${index}`]: <Message>{
  96. id: `${this.is.replace(/\//g, "_")}-${index}`,
  97. component: event.detail?.component,
  98. },
  99. });
  100. // 当切换到 guide 时,强制显示 guide
  101. if (event.detail?.component === "guide") {
  102. this.setData({ isShowGuide: true });
  103. }
  104. if (event.detail?.scroll) this.scrollIntoView();
  105. },
  106. scrollIntoView(event?: ScrollIntoViewEvent) {
  107. clearTimeout(getScrollcontext.call(this).timer);
  108. const id = event?.detail ?? this.data.lastId;
  109. const doScroll = (
  110. scroll: WechatMiniprogram.ScrollViewContext | undefined
  111. ) => {
  112. if (!scroll) return;
  113. if (id === "bottom") {
  114. // 使用 scrollIntoView 滚动到 #bottom 元素,实现平滑滚动
  115. scroll.scrollIntoView(`#bottom`, { alignment: "end" });
  116. } else if (id) {
  117. scroll.scrollIntoView(`#${id}`, { alignment: "end" });
  118. }
  119. // 再补一次,处理内容异步渲染晚于滚动触发的情况
  120. setTimeout(() => {
  121. if (id === "bottom") {
  122. // 使用 scrollIntoView 滚动到 #bottom 元素,实现平滑滚动
  123. scroll.scrollIntoView(`#bottom`, { alignment: "end" });
  124. } else if (id) {
  125. scroll.scrollIntoView(`#${id}`, { alignment: "end" });
  126. }
  127. }, 200);
  128. };
  129. getScrollcontext.call(this).timer = setTimeout(() => {
  130. let scroll = getScrollcontext.call(this).scroll;
  131. if (!scroll) {
  132. // 若还未拿到scroll上下文,立即尝试获取后再滚动
  133. wx.createSelectorQuery()
  134. .select("#scrollview")
  135. .node()
  136. .exec((res) => {
  137. getScrollcontext.call(this).scroll = res?.[0]?.node;
  138. doScroll(getScrollcontext.call(this).scroll);
  139. });
  140. } else {
  141. doScroll(scroll);
  142. }
  143. }, 300);
  144. },
  145. },
  146. });