|
@@ -0,0 +1,113 @@
|
|
|
+<template>
|
|
|
+ <div id="chat">
|
|
|
+ <p class="luyanTop">
|
|
|
+ <span>公共聊天</span>
|
|
|
+ <!-- <span class="icon"><i class="iconfont icon-xianhua" style="color:red"></i>送鲜花</span>
|
|
|
+ <span class="icon"><i class="iconfont icon-xin" style="color:red;"></i>发贺信</span> -->
|
|
|
+ </p>
|
|
|
+ <div class="chatList">
|
|
|
+ <ul>
|
|
|
+ <li v-for="(i, index) in list" :key="index">
|
|
|
+ <p>
|
|
|
+ <span>[{{ i.send_time | getTime }}]</span><span style="font-weight: bold;">{{ i.sender_name }}:</span>
|
|
|
+ <span> {{ i.content }}</span>
|
|
|
+ </p>
|
|
|
+ </li>
|
|
|
+ </ul>
|
|
|
+ <div class="input">
|
|
|
+ <input v-model="text" />
|
|
|
+ <button type="button" @click="send">发送</button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import _ from 'lodash';
|
|
|
+import { mapState, createNamespacedHelpers } from 'vuex';
|
|
|
+const { mapActions: chat } = createNamespacedHelpers('chat');
|
|
|
+var moment = require('moment');
|
|
|
+export default {
|
|
|
+ name: 'chat',
|
|
|
+ props: {},
|
|
|
+ components: {},
|
|
|
+ data: () => {
|
|
|
+ return {
|
|
|
+ list: [],
|
|
|
+ text: '',
|
|
|
+ };
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ //回车事件
|
|
|
+ // this.enterListen();
|
|
|
+ this.search();
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.channel();
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ ...chat(['query', 'create']),
|
|
|
+ async search() {
|
|
|
+ const res = await this.query({ skip: 0, limit: 10 });
|
|
|
+ if (this.$checkRes(res)) this.$set(this, `list`, _.reverse(res.data));
|
|
|
+ },
|
|
|
+ async send() {
|
|
|
+ if (this.text != '') {
|
|
|
+ let object = { sender_name: this.user.name, content: this.text };
|
|
|
+ if (this.user.id) object.sender_id = this.user.id;
|
|
|
+ //TODO接口
|
|
|
+ let res = await this.create(object);
|
|
|
+ this.$checkRes(res, null, res.errmsg || '发言失败');
|
|
|
+ } else this.$message.error('请输入信息后发送');
|
|
|
+ },
|
|
|
+ channel() {
|
|
|
+ this.$stomp({
|
|
|
+ [`/exchange/group_chat/1`]: this.onMessage,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ onMessage(message) {
|
|
|
+ console.log('receive a message: ', message.body);
|
|
|
+ let body = _.get(message, 'body');
|
|
|
+ if (body) {
|
|
|
+ body = JSON.parse(body);
|
|
|
+ this.list.push(body);
|
|
|
+ this.text = '';
|
|
|
+ }
|
|
|
+ // const { content, contenttype, sendid, sendname, icon, groupid, sendtime, type } = message.headers;
|
|
|
+ // let object = { content, contenttype, sendid, sendname, icon, groupid, sendtime, type };
|
|
|
+ // this.list.push(object);
|
|
|
+ },
|
|
|
+ enterListen() {
|
|
|
+ var lett = this;
|
|
|
+ document.onkeydown = function(e) {
|
|
|
+ var key = window.event.keyCode;
|
|
|
+ if (key == 13) {
|
|
|
+ lett.send();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ },
|
|
|
+ },
|
|
|
+ filters: {
|
|
|
+ getTime(date) {
|
|
|
+ if (!date) return '很久以前';
|
|
|
+ let today = moment().format('YYYY-MM-DD');
|
|
|
+ let dd = moment(date).format('YYYY-MM-DD');
|
|
|
+ let time;
|
|
|
+ if (today == dd) time = moment(date).format('HH:mm:ss');
|
|
|
+ else time = moment(date).format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ return time;
|
|
|
+ },
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ ...mapState(['user']),
|
|
|
+ pageTitle() {
|
|
|
+ return `${this.$route.meta.title}`;
|
|
|
+ },
|
|
|
+ },
|
|
|
+ metaInfo() {
|
|
|
+ return { title: this.$route.meta.title };
|
|
|
+ },
|
|
|
+};
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="less" scoped></style>
|