index.ts 5.0 KB

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