chat.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. <span class="chatText" v-text="i.content"></span>
  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, inject, defineEmits } 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. const user = inject("user");
  47. const emit = defineEmits(["toLogin"]);
  48. let time = undefined;
  49. let time1 = undefined;
  50. let id = -1;
  51. const AnimationFrame = requestAnimationFrame();
  52. time = setTimeout(() => {
  53. clearTimeout(time);
  54. getChat().then(formmater);
  55. time = setInterval(() => {
  56. if (id == -1) return clearTimeout(time);
  57. getChat({ id }).then(formmater);
  58. }, 5000);
  59. }, 200);
  60. function formmater(li) {
  61. const list = li ? li || [] : [];
  62. chatList.value.push(...list);
  63. nextTick(() => {
  64. AnimationFrame(scorllFun);
  65. });
  66. if (!chatList.value.length) return;
  67. let max = -1;
  68. let index = -1;
  69. for (let i = 0; i < chatList.value.length; i++) {
  70. const v = chatList.value[i];
  71. if (v.id > max) {
  72. max = Math.max(max, v.id);
  73. index = i;
  74. }
  75. }
  76. id = chatList.value[index].id;
  77. }
  78. function scorllFun() {
  79. if (time1) clearTimeout(time1);
  80. time1 = setTimeout(() => {
  81. clearTimeout(time1);
  82. AnimationFrame(scorllFun);
  83. }, 25);
  84. let T = mT.value;
  85. const chat1 = document.querySelector("#chat" + chatList.value[0].id);
  86. if (chat1.offsetHeight <= T * -1) {
  87. T = 0;
  88. let last = chatList.value.shift();
  89. chatList.value.push(last);
  90. } else T -= 1;
  91. mT.value = T;
  92. }
  93. function onSearch() {
  94. if (!chat.value) return;
  95. if (!user.phone) return emit("toLogin", oriSave);
  96. else oriSave();
  97. }
  98. function oriSave() {
  99. console.log(user, user.phone);
  100. setChat({
  101. phone: user.phone,
  102. content: chat.value,
  103. }).then(r => {
  104. console.log(r);
  105. chat.value = "";
  106. Toast("发送成功");
  107. });
  108. }
  109. </script>
  110. <style lang="scss">
  111. .chat {
  112. width: 100%;
  113. .van-search {
  114. background-color: transparent;
  115. }
  116. .chatList {
  117. background-image: url("../../../assets/img/dmbg.jpg");
  118. background-size: 100% 100%;
  119. background-position: 0% 0%;
  120. height: 250px;
  121. padding: 10px 0 10px 0.5em;
  122. width: 100%;
  123. .kuang {
  124. height: 100%;
  125. overflow: hidden;
  126. .chatText {
  127. background: #fff;
  128. border-radius: 2em;
  129. max-width: 80%;
  130. font-size: 14px;
  131. padding: 0.5em 1em;
  132. display: inline-block;
  133. min-width: 10%;
  134. }
  135. }
  136. }
  137. .van-search__action {
  138. position: relative;
  139. width: 5em;
  140. :active {
  141. background-color: transparent;
  142. }
  143. .saveBtn {
  144. width: 62px;
  145. height: 34px;
  146. position: absolute;
  147. top: 50%;
  148. left: 50%;
  149. transform: translate(-50%, -50%);
  150. }
  151. }
  152. .myswiper {
  153. height: 250px;
  154. }
  155. }
  156. </style>