chat.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div id="chat">
  3. <el-row class="main">
  4. <div class="chat">
  5. <el-col :span="24" class="chatlist">
  6. <el-col :span="24" class="list" v-for="(i, index) in list" :key="index">
  7. <span>[{{ i.send_time | getTime }}]</span><span style="font-weight: bold;" :class="i.role == '8' ? 'nameColor' : ''">{{ i.sender_name }}:</span>
  8. <span v-if="!isEmotion(i.content)"> {{ i.content }}</span>
  9. <span v-else v-html="i.content"> </span>
  10. </el-col>
  11. </el-col>
  12. <el-col :span="24" class="send">
  13. <el-col :span="19">
  14. <el-input v-model="text" size="mini"></el-input>
  15. </el-col>
  16. <el-col :span="5">
  17. <el-button @click="send" size="mini" type="primary">发送</el-button>
  18. </el-col>
  19. </el-col>
  20. </div>
  21. </el-row>
  22. </div>
  23. </template>
  24. <script>
  25. import _ from 'lodash';
  26. import { mapState, createNamespacedHelpers } from 'vuex';
  27. const { mapActions: chat } = createNamespacedHelpers('chat');
  28. var moment = require('moment');
  29. export default {
  30. name: 'chat',
  31. props: {},
  32. components: {},
  33. data: () => {
  34. return {
  35. list: [],
  36. text: '',
  37. dock_id: '',
  38. };
  39. },
  40. created() {
  41. //回车事件
  42. // this.enterListen();
  43. this.$set(this, 'dock_id', this.$route.query.id);
  44. this.search();
  45. },
  46. mounted() {
  47. this.channel();
  48. },
  49. methods: {
  50. ...chat(['query', 'create']),
  51. async search() {
  52. const res = await this.query({ skip: 0, limit: 10, dock_id: this.dock_id });
  53. if (this.$checkRes(res)) this.$set(this, `list`, _.reverse(res.data));
  54. },
  55. async send() {
  56. if (!this.user) {
  57. this.$notify({
  58. message: '游客不能发言',
  59. type: 'danger',
  60. });
  61. return;
  62. } else {
  63. if (this.text != '') {
  64. let object = { sender_name: this.user.name ? this.user.name : this.user.adminuser, content: this.text, dock_id: this.dock_id };
  65. if (this.user.uid) {
  66. object.sender_id = this.user.uid;
  67. object.role = this.user.role;
  68. }
  69. let res = await this.create(object);
  70. this.$checkRes(res, null, res.errmsg || '发言失败');
  71. } else this.$message.error('请输入信息后发送');
  72. }
  73. },
  74. isEmotion(word) {
  75. return word.startsWith('<img');
  76. },
  77. channel() {
  78. this.$stomp({
  79. [`/exchange/public_chat/${this.dock_id}`]: this.onMessage,
  80. });
  81. },
  82. onMessage(message) {
  83. let body = _.get(message, 'body');
  84. if (body) {
  85. body = JSON.parse(body);
  86. this.list.push(body);
  87. this.text = '';
  88. }
  89. },
  90. enterListen() {
  91. var lett = this;
  92. document.onkeydown = function(e) {
  93. var key = window.event.keyCode;
  94. if (key == 13) {
  95. lett.send();
  96. }
  97. };
  98. },
  99. },
  100. filters: {
  101. getTime(date) {
  102. if (!date) return '很久以前';
  103. let today = moment().format('YYYY-MM-DD');
  104. let dd = moment(date).format('YYYY-MM-DD');
  105. let time;
  106. if (today == dd) time = moment(date).format('HH:mm:ss');
  107. else time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  108. return time;
  109. },
  110. },
  111. computed: {
  112. ...mapState(['user']),
  113. pageTitle() {
  114. return `${this.$route.meta.title}`;
  115. },
  116. },
  117. metaInfo() {
  118. return { title: this.$route.meta.title };
  119. },
  120. };
  121. </script>
  122. <style lang="less" scoped>
  123. .main {
  124. width: 100%;
  125. height: 360px;
  126. overflow: hidden;
  127. padding: 0 16px;
  128. .chat {
  129. height: 359px;
  130. overflow: hidden;
  131. border-left: 1px dashed #f1f1f1;
  132. border-right: 1px dashed #f1f1f1;
  133. border-bottom: 1px dashed #f1f1f1;
  134. .chatlist {
  135. height: 320px;
  136. overflow-y: auto;
  137. .list {
  138. padding: 5px 5px;
  139. }
  140. }
  141. .send {
  142. height: 40px;
  143. line-height: 38px;
  144. padding: 0 0 0 12px;
  145. }
  146. }
  147. }
  148. .nameColor {
  149. color: red;
  150. }
  151. </style>