chat.vue 4.8 KB

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