index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. this.handle({ detail: { component, scroll: true } });
  31. // 计算底部bottom
  32. const systemInfo = wx.getSystemInfoSync();
  33. const windowHeight = systemInfo.windowHeight;
  34. const safeAreaBottom = systemInfo.safeArea?.bottom ?? windowHeight; // 没有safeArea时,bottom等于windowHeight
  35. const safeBottom = windowHeight - safeAreaBottom; // 安全区底部距离(px)
  36. const safeBottomRpx = (750 / systemInfo.windowWidth) * safeBottom; // rpx
  37. console.log(safeBottom, "safeBottom");
  38. // const tabbarHeight = 40; // 你的tabbar高度,单位px(如80rpx转px,或直接用rpx)
  39. // let paddingBottom = tabbarHeight + safeBottom;
  40. this.setData({
  41. paddingBottom: this.data.paddingBottom+safeBottomRpx,
  42. });
  43. console.log(this.data.paddingBottom, "paddingBottom");
  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. // calc(100rpx + env(safe-area-inset-bottom))
  75. },
  76. methods: {
  77. boxBottom(event: boxBottom) {
  78. console.log(event.detail.inputBoxBottom, "监听页面index高度boxbottom");
  79. this.setData({
  80. inputBoxBottom: event.detail.inputBoxBottom,
  81. });
  82. // const bottom=this.data.paddingBottom;
  83. this.setData({
  84. paddingBottom: event.detail.inputBoxBottom,
  85. });
  86. console.log(this.data.inputBoxBottom, "监听页面index高度boxbottom");
  87. },
  88. nextType(event: NextTypeEvent) {
  89. this.setData({ messageType: event.detail.MessageType });
  90. },
  91. getCount(event: GetCountEvent) {
  92. this.setData({ analysisCount: event.detail.analysisCount });
  93. console.log(this.data.analysisCount, "监听页面index高度getCount");
  94. },
  95. handle(event: HandleEvent) {
  96. const index = Object.keys(this.data.messages).length;
  97. this.setData({
  98. [`messages.${index}`]: <Message>{
  99. id: `${this.is.replace(/\//g, "_")}-${index}`,
  100. component: event.detail?.component,
  101. },
  102. });
  103. if (event.detail?.scroll) this.scrollIntoView();
  104. },
  105. scrollIntoView(event?: ScrollIntoViewEvent) {
  106. console.log(event, "index-scrollIntoView");
  107. clearTimeout(getScrollcontext.call(this).timer);
  108. const id = event?.detail ?? this.data.lastId;
  109. console.log(id, "index-scrollIntoView");
  110. getScrollcontext.call(this).timer = setTimeout(() => {
  111. const scroll = getScrollcontext
  112. .call(this)
  113. .scroll;
  114. if(id === "bottom"){
  115. scroll?.scrollTo({
  116. top: Number.MAX_SAFE_INTEGER,
  117. animated: true
  118. })
  119. }else{
  120. scroll?.scrollIntoView(`#${id}`, { alignment: "end" });
  121. }
  122. }, 300);
  123. },
  124. },
  125. });