chat.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <div id="chat">
  3. <el-row class="chat">
  4. <div class="chatList">
  5. <ul>
  6. <li v-for="(i, index) in list" :key="index">
  7. <p>
  8. <span>[{{ i.send_time | getTime }}]</span><span style="font-weight: bold;">{{ i.sender_name }}:</span>
  9. <span> {{ i.content }}</span>
  10. </p>
  11. </li>
  12. </ul>
  13. <el-row type="flex" :gutter="10" style="padding-top:10px;">
  14. <el-col :span="19">
  15. <el-input v-model="text" size="mini"></el-input>
  16. </el-col>
  17. <el-col :span="5">
  18. <el-button @click="send" size="mini" round style="background: #ff8500;color: #fff;">发送</el-button>
  19. </el-col>
  20. </el-row>
  21. </div>
  22. </el-row>
  23. </div>
  24. </template>
  25. <script>
  26. import _ from 'lodash';
  27. import { mapState, createNamespacedHelpers } from 'vuex';
  28. const { mapActions: chat } = createNamespacedHelpers('chat');
  29. var moment = require('moment');
  30. export default {
  31. name: 'chat',
  32. props: {},
  33. components: {},
  34. data: () => {
  35. return {
  36. list: [],
  37. text: '',
  38. };
  39. },
  40. created() {
  41. //回车事件
  42. // this.enterListen();
  43. this.search();
  44. },
  45. mounted() {
  46. this.channel();
  47. },
  48. methods: {
  49. ...chat(['query', 'create']),
  50. async search() {
  51. const res = await this.query({ skip: 0, limit: 10 });
  52. if (this.$checkRes(res)) this.$set(this, `list`, _.reverse(res.data));
  53. },
  54. async send() {
  55. if (!this.user.uid) {
  56. this.$message.error('游客不能发言,请先注册');
  57. return;
  58. }
  59. if (this.text != '') {
  60. let object = { sender_name: this.user.name, content: this.text };
  61. if (this.user.uid) object.sender_id = this.user.uid;
  62. //TODO接口
  63. let res = await this.create(object);
  64. this.$checkRes(res, null, res.errmsg || '发言失败');
  65. } else this.$message.error('请输入信息后发送');
  66. },
  67. channel() {
  68. console.log('in function:');
  69. this.$stomp({
  70. [`/exchange/public_chat`]: this.onMessage,
  71. });
  72. },
  73. onMessage(message) {
  74. console.log('lasjdas');
  75. // console.log('receive a message: ', message.body);
  76. let body = _.get(message, 'body');
  77. if (body) {
  78. body = JSON.parse(body);
  79. this.list.push(body);
  80. this.text = '';
  81. }
  82. // const { content, contenttype, sendid, sendname, icon, groupid, sendtime, type } = message.headers;
  83. // let object = { content, contenttype, sendid, sendname, icon, groupid, sendtime, type };
  84. // this.list.push(object);
  85. },
  86. enterListen() {
  87. var lett = this;
  88. document.onkeydown = function(e) {
  89. var key = window.event.keyCode;
  90. if (key == 13) {
  91. lett.send();
  92. }
  93. };
  94. },
  95. },
  96. filters: {
  97. getTime(date) {
  98. if (!date) return '很久以前';
  99. let today = moment().format('YYYY-MM-DD');
  100. let dd = moment(date).format('YYYY-MM-DD');
  101. let time;
  102. if (today == dd) time = moment(date).format('HH:mm:ss');
  103. else time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  104. return time;
  105. },
  106. },
  107. computed: {
  108. ...mapState(['user']),
  109. pageTitle() {
  110. return `${this.$route.meta.title}`;
  111. },
  112. },
  113. metaInfo() {
  114. return { title: this.$route.meta.title };
  115. },
  116. };
  117. </script>
  118. <style lang="less" scoped>
  119. .chat {
  120. float: left;
  121. width: 100%;
  122. height: 340px;
  123. border-radius: 5px;
  124. box-shadow: 0 0 5px #c20808;
  125. padding: 0 10px 0px 10px;
  126. margin: 4px 0px 0 3px;
  127. }
  128. .chat .luyanTop {
  129. height: 30px;
  130. line-height: 30px;
  131. }
  132. .chat .luyanTop span:first-child {
  133. display: inline-block;
  134. padding: 0 10px;
  135. height: 30px;
  136. color: #fff;
  137. background-color: #ff8500;
  138. border-bottom-left-radius: 10px;
  139. border-bottom-right-radius: 10px;
  140. }
  141. .chat .luyanTop .icon {
  142. float: right;
  143. background: #f2f4f5;
  144. border-radius: 20px;
  145. color: #666;
  146. padding: 0 5px;
  147. margin: 0 0 0 5px;
  148. }
  149. .chat .luyanTop .icon {
  150. cursor: pointer;
  151. }
  152. .chat .chatList {
  153. background: #fff;
  154. float: left;
  155. width: 100%;
  156. height: 340px;
  157. overflow: hidden;
  158. }
  159. .chat .chatList ul {
  160. float: left;
  161. width: 100%;
  162. height: 275px;
  163. padding: 5px 0 0 0;
  164. overflow: auto;
  165. margin: 10px 0 0 0;
  166. }
  167. .chat .chatList ul li {
  168. padding: 0 10px;
  169. margin: 0 0 5px 0;
  170. }
  171. .chat .chatList ul li span:first-child {
  172. color: #666;
  173. padding: 0 5px 0 0;
  174. }
  175. .chat .chatList .input {
  176. height: 40px;
  177. line-height: 40px;
  178. float: left;
  179. width: 100%;
  180. }
  181. .chat .chatList .input input[data-v-5189f7b7] {
  182. border: 1px solid #ccc;
  183. float: left;
  184. height: 33px;
  185. width: 75%;
  186. margin: 19px 3% 0 0;
  187. }
  188. .chat .chatList .input button {
  189. float: left;
  190. background: #ff8500;
  191. color: #fff;
  192. height: 34px;
  193. width: 21%;
  194. max-width: 109px;
  195. border-radius: 5px;
  196. }
  197. .jiabinlist ul {
  198. margin: 0;
  199. padding: 0;
  200. }
  201. .anniu {
  202. float: left;
  203. background: #ff8500;
  204. color: #fff;
  205. height: 34px;
  206. width: 21%;
  207. max-width: 109px;
  208. margin: 20px 0 0 0;
  209. border-radius: 5px;
  210. }
  211. </style>