index.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. console.log("id-index",this.data.messages);
  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. console.log(safeBottom, "safeBottom");
  43. // const tabbarHeight = 40; // 你的tabbar高度,单位px(如80rpx转px,或直接用rpx)
  44. // let paddingBottom = tabbarHeight + safeBottom;
  45. this.setData({
  46. paddingBottom: this.data.paddingBottom+safeBottomRpx,
  47. });
  48. console.log(this.data.paddingBottom, "paddingBottom");
  49. },
  50. ready() {
  51. wx.createSelectorQuery()
  52. .select("#scrollview")
  53. .node()
  54. .exec((res) => {
  55. getScrollcontext.call(this).scroll = res[0].node;
  56. });
  57. },
  58. },
  59. properties: {
  60. component: { type: String, value: "guide" },
  61. messageType: { type: Number, value: 0 },
  62. isShowGuide: { type: Boolean, value: false },
  63. id: { type: Number, value: 0 },
  64. },
  65. data: {
  66. date: "",
  67. messages: {} as Record<number, Message>,
  68. lastId: "",
  69. inputBoxBottom: 0,
  70. tabbarValue: "/module/chats/pages/index/index",
  71. analysisCount: 0,
  72. paddingBottom: 100, // 底部tabbar高度是100
  73. },
  74. observers: {
  75. "messages.**"(messages) {
  76. const message = Object.values(messages).pop() as Message;
  77. this.setData({ lastId: message?.id });
  78. },
  79. },
  80. methods: {
  81. boxBottom(event: boxBottom) {
  82. console.log(event.detail.inputBoxBottom, "监听页面index高度boxbottom");
  83. this.setData({
  84. inputBoxBottom: event.detail.inputBoxBottom + 60,
  85. });
  86. // const bottom=this.data.paddingBottom;
  87. this.setData({
  88. paddingBottom: event.detail.inputBoxBottom,
  89. });
  90. console.log(this.data.inputBoxBottom, "监听页面index高度boxbottom");
  91. },
  92. nextType(event: NextTypeEvent) {
  93. this.setData({ messageType: event.detail.MessageType });
  94. },
  95. getCount(event: GetCountEvent) {
  96. this.setData({ analysisCount: event.detail.analysisCount });
  97. },
  98. handle(event: HandleEvent) {
  99. const index = Object.keys(this.data.messages).length;
  100. this.setData({
  101. [`messages.${index}`]: <Message>{
  102. id: `${this.is.replace(/\//g, "_")}-${index}`,
  103. component: event.detail?.component,
  104. },
  105. });
  106. // 当切换到 guide 时,强制显示 guide
  107. if (event.detail?.component === "guide") {
  108. this.setData({ isShowGuide: true });
  109. }
  110. if (event.detail?.scroll) this.scrollIntoView();
  111. },
  112. scrollIntoView(event?: ScrollIntoViewEvent) {
  113. clearTimeout(getScrollcontext.call(this).timer);
  114. const id = event?.detail ?? this.data.lastId;
  115. const doScroll = (scroll: WechatMiniprogram.ScrollViewContext | undefined) => {
  116. if (!scroll) return;
  117. if (id === "bottom") {
  118. scroll.scrollTo({ top: Number.MAX_SAFE_INTEGER, animated: true });
  119. } else if (id) {
  120. scroll.scrollIntoView(`#${id}`, { alignment: "end" });
  121. }
  122. // 再补一次,处理内容异步渲染晚于滚动触发的情况
  123. setTimeout(() => {
  124. if (id === "bottom") {
  125. scroll.scrollTo({ top: Number.MAX_SAFE_INTEGER, animated: true });
  126. } else if (id) {
  127. scroll.scrollIntoView(`#${id}`, { alignment: "end" });
  128. }
  129. }, 200);
  130. };
  131. getScrollcontext.call(this).timer = setTimeout(() => {
  132. let scroll = getScrollcontext.call(this).scroll;
  133. if (!scroll) {
  134. // 若还未拿到scroll上下文,立即尝试获取后再滚动
  135. wx.createSelectorQuery()
  136. .select("#scrollview")
  137. .node()
  138. .exec((res) => {
  139. getScrollcontext.call(this).scroll = res?.[0]?.node;
  140. doScroll(getScrollcontext.call(this).scroll);
  141. });
  142. } else {
  143. doScroll(scroll);
  144. }
  145. }, 300);
  146. },
  147. },
  148. });