chat.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div class="chat">
  3. <div class="chatList">
  4. <div class="kuang">
  5. <div ref="allChat" :style="'transform: translateY(' + mT + 'px);'">
  6. <div :key="i.id" :id="'chat' + i.id" v-for="i in chatList">
  7. <div class="chat" v-text="i.content"></div>
  8. </div>
  9. </div>
  10. </div>
  11. </div>
  12. <van-search
  13. left-icon=""
  14. right-icon=""
  15. v-model="chat"
  16. show-action
  17. :clearable="false"
  18. placeholder="说点什么"
  19. >
  20. <template #action>
  21. <img
  22. class="saveBtn"
  23. @click="onSearch"
  24. :src="require('../../../assets/img/save.png')"
  25. />
  26. </template>
  27. </van-search>
  28. </div>
  29. </template>
  30. <script setup>
  31. import { ref, nextTick } from "vue";
  32. import { getChat, setChat } from "@/api/worldCup.js";
  33. import { requestAnimationFrame } from "@/utils/tool.js";
  34. import { Toast } from "vant";
  35. // import { onMounted, reactive } from "vue";
  36. // import { isIpad, isIpod, isIphone } from "../../utils/isTerminal";
  37. /**
  38. * window.$originData.orginParames.title 页面标题
  39. * window.$originData.orginParames.parameters 固定参数值
  40. * window.$originData.urlParames url参数
  41. */
  42. const chatList = ref([]);
  43. const allChat = ref(null);
  44. const chat = ref("");
  45. const mT = ref(0);
  46. let time = undefined;
  47. let time1 = undefined;
  48. let page = 1;
  49. const AnimationFrame = requestAnimationFrame();
  50. time = setTimeout(() => {
  51. clearTimeout(time);
  52. getChat({
  53. page,
  54. pagesize: 10,
  55. }).then(formmater);
  56. time = setInterval(() => {
  57. if (page == -1) return clearTimeout(time);
  58. getChat({
  59. page,
  60. pagesize: 10,
  61. }).then(formmater);
  62. }, 5000);
  63. }, 200);
  64. function formmater(li) {
  65. const list = li ? li.records || [] : [];
  66. chatList.value.push(...list);
  67. page++;
  68. if (chatList.value.length >= li.total) page = -1;
  69. nextTick(() => {
  70. AnimationFrame(scorllFun);
  71. });
  72. }
  73. function scorllFun() {
  74. if (time1) clearTimeout(time1);
  75. time1 = setTimeout(() => {
  76. clearTimeout(time1);
  77. AnimationFrame(scorllFun);
  78. }, 25);
  79. let T = mT.value;
  80. const chat1 = document.querySelector("#chat" + chatList.value[0].id);
  81. if (chat1.offsetHeight <= T * -1) {
  82. T = 0;
  83. let last = chatList.value.shift();
  84. chatList.value.push(last);
  85. } else T -= 1;
  86. mT.value = T;
  87. }
  88. function onSearch() {
  89. if (!chat.value) return;
  90. setChat({
  91. phone: "18439106376",
  92. content: chat.value,
  93. }).then(r => {
  94. console.log(r);
  95. chat.value = "";
  96. Toast("发送成功")
  97. });
  98. }
  99. </script>
  100. <style lang="scss">
  101. .chat {
  102. .van-search {
  103. background-color: transparent;
  104. }
  105. .chatList {
  106. background-image: url("../../../assets/img/dmbg.jpg");
  107. background-size: 100% 100%;
  108. background-position: 0% 0%;
  109. height: 250px;
  110. padding: 10px 0 10px 0.5em;
  111. width: 100%;
  112. .kuang {
  113. height: 100%;
  114. overflow: hidden;
  115. .chat {
  116. background: #fff;
  117. border-radius: 2em;
  118. max-width: 80%;
  119. font-size: 14px;
  120. padding: 0.5em;
  121. display: inline-block;
  122. min-width: 3em;
  123. }
  124. }
  125. }
  126. .van-search__action {
  127. position: relative;
  128. width: 4em;
  129. :active {
  130. background-color: transparent;
  131. }
  132. .saveBtn {
  133. width: 62px;
  134. height: 34px;
  135. position: absolute;
  136. top: 50%;
  137. left: 50%;
  138. transform: translate(-50%, -50%);
  139. }
  140. }
  141. .myswiper {
  142. height: 250px;
  143. }
  144. }
  145. </style>