chat.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div id="chats">
  3. <el-row :span="24" class="chat">
  4. <el-col :span="24" style="padding-bottom:5px">
  5. <el-button size="mini" @click="$emit('toRoom')" icon="el-icon-arrow-left" type="primary">返回</el-button>
  6. </el-col>
  7. <el-col :span="24" class="chatInfo" id="chatBorder" ref="chatBorder">
  8. <template v-for="(i, index) in talk">
  9. <template v-if="isSender(i)">
  10. <span :key="`senderTime${i.id}${index}`">[{{ i.send_time }}] {{ i.sender_name }}</span>
  11. <span v-html="i.content" :key="`senderContent${i.id}${index}`"></span>
  12. </template>
  13. <template v-else>
  14. <span :key="`receverTime${i.id}${index}`">[{{ i.send_time }}] {{ i.receiver_name }}</span>
  15. <span v-html="i.content" :key="`receverContent${i.id}${index}`"></span>
  16. </template>
  17. </template>
  18. </el-col>
  19. <el-col :span="24" class="chatBtn">
  20. <wang-editor v-model="content" style="height:130px;padding-bottom:120px" ref="editor"></wang-editor>
  21. <el-button type="primary" @click="chatClick">发送</el-button>
  22. </el-col>
  23. </el-row>
  24. </div>
  25. </template>
  26. <script>
  27. import _ from 'lodash';
  28. import { mapState, createNamespacedHelpers } from 'vuex';
  29. import wangEditor from '@/components/wang-editor.vue';
  30. const { mapActions: personalChat } = createNamespacedHelpers('personalchat');
  31. export default {
  32. name: 'chats',
  33. props: {
  34. room: { type: Object, default: () => {} },
  35. },
  36. components: {
  37. wangEditor,
  38. },
  39. data: () => {
  40. return {
  41. content: '',
  42. talk: [],
  43. };
  44. },
  45. created() {},
  46. mounted() {
  47. this.channel();
  48. },
  49. methods: {
  50. ...personalChat(['create', 'query']),
  51. async search() {
  52. let res = await this.query({ personroom_id: this.room.id });
  53. if (this.$checkRes(res)) {
  54. this.$set(this, `talk`, res.data);
  55. this.turnBottom();
  56. }
  57. },
  58. async chatClick() {
  59. if (this.content != '') {
  60. let obj = { personroom_id: this.room.id, content: this.content, sender_id: this.user.uid, sender_name: this.user.name, send_time: '13:00' };
  61. let keys = Object.keys(this.room);
  62. let fres = keys.find(f => this.room[f] == this.user.uid);
  63. obj.receiver_id = fres === 'buyer_id' ? this.room['seller_id'] : this.room['buyer_id'];
  64. obj.receiver_name = fres === 'buyer_id' ? this.room['seller_name'] : this.room['buyer_name'];
  65. let res = await this.create(obj);
  66. this.$refs.editor.setContent();
  67. this.$set(this, `content`, '');
  68. this.$forceUpdate();
  69. if (this.$checkRes(res, null, res.errmsg || '发言失败')) {
  70. console.log(`in zhong tai chat`);
  71. this.talk.push(res.data);
  72. this.turnBottom();
  73. }
  74. } else this.$message.error('请输入信息后发送');
  75. },
  76. turnBottom() {
  77. this.$nextTick(() => {
  78. document.getElementById('chatBorder').scrollTop = document.getElementById('chatBorder').scrollHeight + 150;
  79. });
  80. },
  81. isSender(data) {
  82. return this.user.uid == data.sender_id;
  83. },
  84. channel() {
  85. //TODO 修改订阅地址
  86. if (!this.room.id) {
  87. console.warn('未获取到房间id,无法进行订阅');
  88. return;
  89. }
  90. console.log(`this.user.uid`);
  91. console.log(this.user.uid);
  92. console.log(`${this.room.id}_${this.user.uid}`);
  93. this.$stomp({
  94. [`/exchange/person_chat/${this.room.id}_${this.user.uid}`]: this.onMessage,
  95. });
  96. },
  97. onMessage(message) {
  98. console.log(`in zhong tai message`);
  99. let body = _.get(message, 'body');
  100. if (body) {
  101. body = JSON.parse(body);
  102. this.talk.push(body);
  103. this.turnBottom();
  104. }
  105. },
  106. },
  107. watch: {
  108. room: {
  109. handler(val) {
  110. if (val.id) this.search();
  111. },
  112. immediate: true,
  113. deep: true,
  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. padding: 20px;
  132. }
  133. .chat .chatInfo {
  134. float: left;
  135. width: 100%;
  136. height: 280px;
  137. overflow: hidden;
  138. border: 1px solid #ccc;
  139. margin: 0 0 30px 0;
  140. overflow-y: auto;
  141. }
  142. .chatBtn {
  143. float: left;
  144. width: 100%;
  145. text-align: center;
  146. }
  147. /deep/.chatBtn .el-button {
  148. padding: 10px 80px;
  149. font-size: 20px;
  150. }
  151. </style>