index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. console.log("id-index",this.data.messages);
  32. // 计算底部bottom
  33. const systemInfo = wx.getSystemInfoSync();
  34. const windowHeight = systemInfo.windowHeight;
  35. const safeAreaBottom = systemInfo.safeArea?.bottom ?? windowHeight; // 没有safeArea时,bottom等于windowHeight
  36. const safeBottom = windowHeight - safeAreaBottom; // 安全区底部距离(px)
  37. const safeBottomRpx = (750 / systemInfo.windowWidth) * safeBottom; // rpx
  38. console.log(safeBottom, "safeBottom");
  39. // const tabbarHeight = 40; // 你的tabbar高度,单位px(如80rpx转px,或直接用rpx)
  40. // let paddingBottom = tabbarHeight + safeBottom;
  41. this.setData({
  42. paddingBottom: this.data.paddingBottom+safeBottomRpx,
  43. });
  44. console.log(this.data.paddingBottom, "paddingBottom");
  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. date: "",
  63. messages: {} as Record<number, Message>,
  64. lastId: "",
  65. inputBoxBottom: 0,
  66. tabbarValue: "/module/chats/pages/index/index",
  67. analysisCount: 0,
  68. paddingBottom: 100, // 底部tabbar高度是100
  69. },
  70. observers: {
  71. "messages.**"(messages) {
  72. const message = Object.values(messages).pop() as Message;
  73. this.setData({ lastId: message?.id });
  74. },
  75. },
  76. methods: {
  77. boxBottom(event: boxBottom) {
  78. console.log(event.detail.inputBoxBottom, "监听页面index高度boxbottom");
  79. this.setData({
  80. inputBoxBottom: event.detail.inputBoxBottom + 60,
  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. },
  94. handle(event: HandleEvent) {
  95. const index = Object.keys(this.data.messages).length;
  96. this.setData({
  97. [`messages.${index}`]: <Message>{
  98. id: `${this.is.replace(/\//g, "_")}-${index}`,
  99. component: event.detail?.component,
  100. },
  101. });
  102. if (event.detail?.scroll) this.scrollIntoView();
  103. },
  104. scrollIntoView(event?: ScrollIntoViewEvent) {
  105. clearTimeout(getScrollcontext.call(this).timer);
  106. const id = event?.detail ?? this.data.lastId;
  107. getScrollcontext.call(this).timer = setTimeout(() => {
  108. const scroll = getScrollcontext
  109. .call(this)
  110. .scroll;
  111. if(id === "bottom"){
  112. scroll?.scrollTo({
  113. top: Number.MAX_SAFE_INTEGER,
  114. animated: true
  115. })
  116. }else{
  117. scroll?.scrollIntoView(`#${id}`, { alignment: "end" });
  118. }
  119. }, 300);
  120. },
  121. },
  122. });