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.id) object.sender_id = this.user.id;
  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. this.$stomp({
  68. [`/exchange/public_chat`]: this.onMessage,
  69. });
  70. },
  71. onMessage(message) {
  72. // console.log('receive a message: ', message.body);
  73. let body = _.get(message, 'body');
  74. if (body) {
  75. body = JSON.parse(body);
  76. this.list.push(body);
  77. this.text = '';
  78. }
  79. // const { content, contenttype, sendid, sendname, icon, groupid, sendtime, type } = message.headers;
  80. // let object = { content, contenttype, sendid, sendname, icon, groupid, sendtime, type };
  81. // this.list.push(object);
  82. },
  83. enterListen() {
  84. var lett = this;
  85. document.onkeydown = function(e) {
  86. var key = window.event.keyCode;
  87. if (key == 13) {
  88. lett.send();
  89. }
  90. };
  91. },
  92. },
  93. filters: {
  94. getTime(date) {
  95. if (!date) return '很久以前';
  96. let today = moment().format('YYYY-MM-DD');
  97. let dd = moment(date).format('YYYY-MM-DD');
  98. let time;
  99. if (today == dd) time = moment(date).format('HH:mm:ss');
  100. else time = moment(date).format('YYYY-MM-DD HH:mm:ss');
  101. return time;
  102. },
  103. },
  104. computed: {
  105. ...mapState(['user']),
  106. pageTitle() {
  107. return `${this.$route.meta.title}`;
  108. },
  109. },
  110. metaInfo() {
  111. return { title: this.$route.meta.title };
  112. },
  113. };
  114. </script>
  115. <style lang="less" scoped>
  116. .fangtan .chat {
  117. float: left;
  118. width: 30%;
  119. height: 545px;
  120. border-radius: 5px;
  121. box-shadow: 0 0 5px #c20808;
  122. padding: 0 10px 0px 10px;
  123. margin: 4px 0px 0 3px;
  124. }
  125. .chat .luyanTop {
  126. height: 30px;
  127. line-height: 30px;
  128. }
  129. .chat .luyanTop span:first-child {
  130. display: inline-block;
  131. padding: 0 10px;
  132. height: 30px;
  133. color: #fff;
  134. background-color: #ff8500;
  135. border-bottom-left-radius: 10px;
  136. border-bottom-right-radius: 10px;
  137. }
  138. .chat .luyanTop .icon {
  139. float: right;
  140. background: #f2f4f5;
  141. border-radius: 20px;
  142. color: #666;
  143. padding: 0 5px;
  144. margin: 0 0 0 5px;
  145. }
  146. .chat .luyanTop .icon {
  147. cursor: pointer;
  148. }
  149. .chat .chatList {
  150. height: 505px;
  151. background: #fff;
  152. }
  153. .chat .chatList ul {
  154. float: left;
  155. width: 100%;
  156. height: 454px;
  157. padding: 5px 0 0 0;
  158. overflow: auto;
  159. margin: 10px 0 0 0;
  160. }
  161. .chat .chatList ul li {
  162. padding: 0 10px;
  163. margin: 0 0 5px 0;
  164. }
  165. .chat .chatList ul li span:first-child {
  166. color: #666;
  167. padding: 0 5px 0 0;
  168. }
  169. .chat .chatList .input {
  170. height: 40px;
  171. line-height: 40px;
  172. float: left;
  173. width: 100%;
  174. padding: 55px 0 0 0;
  175. }
  176. .chat .chatList .input input {
  177. border: 1px solid #ccc;
  178. float: left;
  179. height: 54px;
  180. width: 75%;
  181. margin-right: 3%;
  182. }
  183. .chat .chatList .input button {
  184. float: left;
  185. background: #ff8500;
  186. color: #fff;
  187. height: 34px;
  188. width: 21%;
  189. max-width: 109px;
  190. border-radius: 5px;
  191. }
  192. .jiabinlist ul {
  193. margin: 0;
  194. padding: 0;
  195. }
  196. .anniu {
  197. float: left;
  198. background: #ff8500;
  199. color: #fff;
  200. height: 34px;
  201. width: 21%;
  202. max-width: 109px;
  203. margin: 20px 0 0 0;
  204. border-radius: 5px;
  205. }
  206. </style>