index-old2.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <el-tabs stretch="true" tab-position="bottom" v-model="activeName" @tab-click="handleClick">
  3. <el-tab-pane name="chat-one" class="chat-tab-one">
  4. <span slot="label" class="chat-span">
  5. <i class="el-icon-chat-square"></i>
  6. 对话
  7. </span>
  8. <div class="chat-search">
  9. <el-input
  10. v-model="searchText"
  11. placeholder="搜索"
  12. >
  13. <el-button slot="append" icon="el-icon-search">搜索</el-button>
  14. </el-input>
  15. </div>
  16. <el-scrollbar class="chat-scrollbar"><!--:wrap-style="{ transform: 'scaleY(' + scrollbarScale + ')' }"-->
  17. <div class="message" v-for="(message, index) in currentChat.messages" :key="index"
  18. :class="{ received: message.isReceived, sent: !message.isReceived }">
  19. <img :src="message.avatar" alt="" class="avatar"/>
  20. <div class="bubble">
  21. <p>{{ message.content }}</p>
  22. </div>
  23. </div>
  24. </el-scrollbar>
  25. <div class="chat-input">
  26. <el-input v-model="newMessage" type="textarea" autosize @keyup.enter.native="sendMessage">
  27. </el-input>
  28. <el-button icon="el-icon-delete" :disabled="hasText" type="text" @click="newMessage=''"></el-button>
  29. <el-button round="true" :disabled="hasText" @click="sendMessage"
  30. :style="{ background: hasText ? '' : 'green', color: hasText ? '' : 'white' }">发送
  31. </el-button>
  32. </div>
  33. </el-tab-pane>
  34. <el-tab-pane>
  35. <span slot="label" class="chat-span">
  36. <i class="el-icon-user-solid"></i>
  37. 我的
  38. </span>
  39. </el-tab-pane>
  40. </el-tabs>
  41. </template>
  42. <script>
  43. export default {
  44. data() {
  45. return {
  46. chatList: [
  47. {
  48. id: '1',
  49. name: '小明',
  50. avatar: 'https://randomuser.me/api/portraits/men/32.jpg',
  51. lastMessage: '好的,一会儿见',
  52. time: '14:06',
  53. messages: [
  54. {
  55. id: '1',
  56. content: '嗨,你好啊',
  57. isReceived: true,
  58. avatar: 'https://randomuser.me/api/portraits/women/11.jpg',
  59. time: '14:02'
  60. },
  61. {
  62. id: '2',
  63. content: '你最近怎么样啊?',
  64. isReceived: false,
  65. avatar: 'https://randomuser.me/api/portraits/men/32.jpg',
  66. time: '14:03'
  67. },
  68. {
  69. id: '3',
  70. content: '我最近一直忙着工作',
  71. isReceived: true,
  72. avatar: 'https://randomuser.me/api/portraits/women/11.jpg',
  73. time: '14:04'
  74. },
  75. {
  76. id: '4',
  77. content: '好的,一会儿见',
  78. isReceived: false,
  79. avatar: 'https://randomuser.me/api/portraits/men/32.jpg',
  80. time: '14:06'
  81. }
  82. ]
  83. },
  84. {
  85. id: '2',
  86. name: '张三',
  87. avatar: 'https://randomuser.me/api/portraits/men/33.jpg',
  88. lastMessage: '我在开会,请稍等',
  89. time: '13:45',
  90. messages: [
  91. {
  92. id: '1',
  93. content: '你好啊',
  94. isReceived: true,
  95. avatar: 'https://randomuser.me/api/portraits/women/1.jpg',
  96. time: '13:40'
  97. },
  98. {
  99. id: '2',
  100. content: '你最近怎么样啊?',
  101. isReceived: false,
  102. avatar: 'https://randomuser.me/api/portraits/men/33.jpg',
  103. time: '13:41'
  104. },
  105. {
  106. id: '3',
  107. content: '我在开会,请稍等',
  108. isReceived: true,
  109. avatar: 'https://randomuser.me/api/portraits/women/1.jpg',
  110. time: '13:45'
  111. }
  112. ]
  113. }
  114. ],
  115. currentChat: null,
  116. newMessage: '',
  117. searchText: '',
  118. activeName: 'chat-one'
  119. }
  120. },
  121. methods: {
  122. setCurrentChat(chat) {
  123. this.currentChat = chat;
  124. },
  125. sendMessage() {
  126. const message = {
  127. id: String(Math.random()).substr(2),
  128. content: this.newMessage,
  129. isReceived: false,
  130. avatar: 'https://randomuser.me/api/portraits/men/32.jpg',
  131. time: this.getCurrentTime()
  132. };
  133. this.currentChat.messages.push(message);
  134. this.newMessage = '';
  135. },
  136. getCurrentTime() {
  137. const now = new Date();
  138. return now.toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'});
  139. },
  140. handleClick(tab, event) {
  141. console.log(tab, event);
  142. }
  143. /* updateInputHeight() {
  144. this.$nextTick(() => {
  145. this.inputHeight = this.$refs.input.$el.clientHeight;
  146. });
  147. }*/
  148. },
  149. created() {
  150. this.currentChat = this.chatList[0];
  151. },
  152. computed: {
  153. filteredChatList() {
  154. return this.chatList.filter(chat =>
  155. chat.name.toLowerCase().includes(this.searchText.toLowerCase())
  156. );
  157. },
  158. hasText() {
  159. return this.newMessage == '';
  160. },
  161. /* scrollbarScale() {
  162. return this.inputHeight !== 0 ? 1 / this.inputHeight : 1;
  163. }*/
  164. }
  165. }
  166. </script>
  167. <style scoped>
  168. .el-tabs {
  169. height: 100vh;
  170. }
  171. >>> .el-tabs__content {
  172. height: 100%;
  173. }
  174. /*>>> .el-tabs__header{
  175. position: absolute;
  176. bottom: 5px;
  177. }*/
  178. .chat-span {
  179. font-size: 16px;
  180. }
  181. .chat-tab {
  182. height: 93vh;
  183. display: flex; /*弹性盒子模型*/
  184. padding: 3vh 5vw;
  185. flex-direction: column; /*灵活的项目将垂直显示,正如一个列一样。*/
  186. justify-content: space-between;
  187. }
  188. .chat-search {
  189. position: absolute;
  190. top: 5px;
  191. }
  192. .chat-scrollbar {
  193. position: absolute;
  194. top: 5px;
  195. bottom: 5px;
  196. }
  197. .chat-input {
  198. position: absolute;
  199. bottom: 5px;
  200. display: flex;
  201. }
  202. >>> .el-textarea__inner {
  203. resize: none;
  204. }
  205. .avatar {
  206. display: block;
  207. width: 50px;
  208. height: 50px;
  209. margin-right: 10px;
  210. border-radius: 50%;
  211. }
  212. .message {
  213. display: flex;
  214. align-items: flex-end; /*元素位于容器的结尾。*/
  215. margin: 20px 5px;
  216. flex-grow: 1;
  217. }
  218. .message .avatar {
  219. width: 30px;
  220. height: 30px;
  221. margin-right: 5px;
  222. border-radius: 50%;
  223. }
  224. .message.received .bubble {
  225. background-color: #f1f1f1;
  226. }
  227. .message.sent .bubble {
  228. background-color: #dcf8c6;
  229. }
  230. .bubble {
  231. padding: 10px;
  232. border-radius: 8px;
  233. color: #333;
  234. max-width: 80%;
  235. }
  236. .bubble p {
  237. margin: 0;
  238. white-space: pre-line;
  239. word-wrap: break-word;
  240. word-break: break-all;
  241. }
  242. </style>