chat.vue 5.4 KB

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