chat.vue 6.5 KB

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