chat.vue 3.8 KB

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