index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div class="intelligentQA">
  3. <div class="aside">历史记录</div>
  4. <div class="chat">
  5. <!-- <div class="header">Header</div> -->
  6. <div ref="main" class="main">
  7. <div class="chatLine">
  8. <div v-for="(item, index) in chatInfo" :key="index">
  9. <div class="chatRow" v-if="item.code === 200">
  10. <div class="answerData">
  11. <div class="answer">{{ item.data.answer }}</div>
  12. <!-- <div class="graph"></div> -->
  13. <graphECharts :graphData="item.data.graph" class="charts"></graphECharts>
  14. </div>
  15. </div>
  16. <div class="chatRow chatQ" v-if="item.userId">
  17. <div class="questionContent">{{ item.data }}</div>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="footer">
  23. <div class="footerContent">
  24. <textarea v-model="questionInput" :style="{ height: `${currentHeight}px`, overflowY: isScrollable ? 'auto' : 'hidden' }" placeholder="请输入您的问题..." @keydown="handleKeydown"> </textarea>
  25. <i class="el-icon-s-promotion icon" @click="sendQuestion"></i>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </template>
  31. <script>
  32. import store from '@/store'
  33. import graphECharts from '@/views/als/components/Charts/graph.vue'
  34. export default {
  35. name: 'IntelligentQA',
  36. components: { graphECharts },
  37. data() {
  38. return {
  39. userId: '',
  40. chatInfo: [],
  41. questionInput: '',
  42. currentHeight: 'auto',
  43. lastScrollHeight: 0,
  44. isScrollable: false
  45. }
  46. },
  47. mounted() {
  48. this.getUserInfo()
  49. this.adjustHeight()
  50. },
  51. watch: {},
  52. created() {},
  53. methods: {
  54. async getUserInfo() {
  55. const { user } = await store.dispatch('user/getUserInfo')
  56. this.userId = user.userId
  57. },
  58. adjustHeight() {
  59. const textarea = this.$el.querySelector('textarea')
  60. this.currentHeight = 'auto'
  61. if (textarea.scrollHeight < this.lastScrollHeight) {
  62. this.lastScrollHeight = this.lastScrollHeight - 16
  63. }
  64. this.$nextTick(() => {
  65. this.currentHeight = Math.min(this.lastScrollHeight, 200)
  66. this.lastScrollHeight = textarea.scrollHeight
  67. if (textarea.scrollHeight >= 200) {
  68. this.isScrollable = true
  69. } else {
  70. this.isScrollable = false
  71. }
  72. })
  73. },
  74. handleKeydown(event) {
  75. if (event.key === 'Enter') {
  76. // 检查是否按下了 Ctrl 键
  77. if (event.ctrlKey) {
  78. // 按下了 Ctrl+Enter,则插入换行符
  79. this.questionInput += '\n'
  80. this.adjustHeight()
  81. } else {
  82. // 只是按下了 Enter,阻止默认行为并发送消息
  83. event.preventDefault()
  84. this.sendQuestion()
  85. }
  86. }
  87. if (event.key === 'Backspace') {
  88. if (this.questionInput) {
  89. this.adjustHeight()
  90. } else {
  91. this.currentHeight = '50'
  92. }
  93. }
  94. },
  95. sendQuestion() {
  96. console.log('222', this.questionInput.trim())
  97. if (this.questionInput.trim() === '') {
  98. return
  99. }
  100. this.chatInfo.push(
  101. {
  102. data: this.questionInput,
  103. userId: this.userId
  104. },
  105. {
  106. code: 200,
  107. msg: '',
  108. data: {
  109. user_id: 'user',
  110. answer: '发生。。。。。。。xxxxxxx',
  111. graph: {
  112. data: [
  113. { name: '202310150010', category: 'HMC' },
  114. { name: '电视', category: '成品' },
  115. { name: '电视遥控器失灵', category: '故障描述' },
  116. { name: '家用电器', category: '系统' },
  117. { name: '更换电池或遥控器', category: '维修策略' }
  118. ],
  119. links: [
  120. { source: '202310150010', target: '电视', value: '成品' },
  121. { source: '202310150010', target: '电视遥控器失灵', value: '故障描述' },
  122. { source: '202310150010', target: '家用电器', value: '系统' },
  123. { source: '202310150010', target: '更换电池或遥控器', value: '维修策略' }
  124. ],
  125. categories: [{ name: 'HMC' }, { name: '成品' }, { name: '故障描述' }, { name: '维修策略' }, { name: '系统' }]
  126. }
  127. }
  128. }
  129. )
  130. // data: [
  131. // { name: '111', category: 'HMC' },
  132. // { name: '更换手机屏幕', category: '维修策略' }
  133. // ],
  134. // links: [{ source: '111', target: '更换手机屏幕', value: '维修策略' }],
  135. // categories: [{ name: 'HMC' }, { name: '维修策略' }]
  136. console.log('this.chatInfo', this.chatInfo)
  137. this.questionInput = ''
  138. // 等处理过返回数据后,调用使视图保持在最底部
  139. let main = this.$refs.main
  140. setTimeout(() => {
  141. main.scrollTop = main.scrollHeight
  142. }, 0)
  143. },
  144. showGraph(graphData) {
  145. const { data, links } = graphData
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss">
  151. @import './index.scss';
  152. </style>