Message.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <template>
  2. <div ref="messageContainer" style="height: 100%; overflow-y: auto; position: relative">
  3. <div class="chat-list" v-for="(item, index) in list" :key="index">
  4. <!-- 靠左 message -->
  5. <div class="left-message message-item" v-if="item.type !== 'user'">
  6. <div class="avatar">
  7. <el-avatar :src="roleAvatar" />
  8. </div>
  9. <div class="message">
  10. <div>
  11. <el-text class="time">{{ formatDate(item.createTime) }}</el-text>
  12. </div>
  13. <div class="left-text-container" ref="markdownViewRef">
  14. <MarkdownView class="left-text" :content="item.content" />
  15. </div>
  16. <div class="left-btns">
  17. <el-button class="btn-cus" link @click="noCopy(item.content)">
  18. <img class="btn-image" src="@/assets/ai/copy.svg" />
  19. </el-button>
  20. <el-button class="btn-cus" link @click="onDelete(item.id)">
  21. <img class="btn-image" src="@/assets/ai/delete.svg" style="height: 17px" />
  22. </el-button>
  23. </div>
  24. </div>
  25. </div>
  26. <!-- 靠右 message -->
  27. <div class="right-message message-item" v-if="item.type === 'user'">
  28. <div class="avatar">
  29. <el-avatar :src="userAvatar" />
  30. </div>
  31. <div class="message">
  32. <div>
  33. <el-text class="time">{{ formatDate(item.createTime) }}</el-text>
  34. </div>
  35. <div class="right-text-container">
  36. <div class="right-text">{{ item.content }}</div>
  37. </div>
  38. <div class="right-btns">
  39. <el-button class="btn-cus" link @click="noCopy(item.content)">
  40. <img class="btn-image" src="@/assets/ai/copy.svg" />
  41. </el-button>
  42. <el-button class="btn-cus" link @click="onDelete(item.id)">
  43. <img
  44. class="btn-image"
  45. src="@/assets/ai/delete.svg"
  46. style="height: 17px; margin-right: 12px"
  47. />
  48. </el-button>
  49. <el-button class="btn-cus" link @click="onRefresh(item)">
  50. <el-icon size="17"><RefreshRight /></el-icon>
  51. </el-button>
  52. <el-button class="btn-cus" link @click="onEdit(item)">
  53. <el-icon size="17"><Edit /></el-icon>
  54. </el-button>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. <!-- 回到底部 -->
  61. <div v-if="isScrolling" class="to-bottom" @click="handleGoBottom">
  62. <el-button :icon="ArrowDownBold" circle />
  63. </div>
  64. </template>
  65. <script setup lang="ts">
  66. import { formatDate } from '@/utils/formatTime'
  67. import MarkdownView from '@/components/MarkdownView/index.vue'
  68. import { ChatMessageApi, ChatMessageVO } from '@/api/ai/chat/message'
  69. import { useClipboard } from '@vueuse/core'
  70. import { PropType } from 'vue'
  71. import { ArrowDownBold, Edit, RefreshRight } from '@element-plus/icons-vue'
  72. import { ChatConversationVO } from '@/api/ai/chat/conversation'
  73. import {useUserStore} from '@/store/modules/user';
  74. import userAvatarDefaultImg from '@/assets/imgs/avatar.gif'
  75. import roleAvatarDefaultImg from '@/assets/ai/gpt.svg'
  76. const { copy } = useClipboard() // 初始化 copy 到粘贴板
  77. // 判断 消息列表 滚动的位置(用于判断是否需要滚动到消息最下方)
  78. const messageContainer: any = ref(null)
  79. const isScrolling = ref(false) //用于判断用户是否在滚动
  80. const userStore = useUserStore()
  81. const userAvatar = computed(() => userStore.user.avatar ?? userAvatarDefaultImg)
  82. const roleAvatar = computed(() => props.conversation.roleAvatar ?? roleAvatarDefaultImg)
  83. // 定义 props
  84. const props = defineProps({
  85. conversation: {
  86. type: Object as PropType<ChatConversationVO>,
  87. required: true
  88. },
  89. list: {
  90. type: Array as PropType<ChatMessageVO[]>,
  91. required: true
  92. }
  93. })
  94. // ============ 处理对话滚动 ==============
  95. const scrollToBottom = async (isIgnore?: boolean) => {
  96. await nextTick(() => {
  97. // TODO @fan:中文写作习惯,中英文之间要有空格;另外,nextick 哈,idea 如果有绿色波兰线,可以关注下
  98. //注意要使用nexttick以免获取不到dom
  99. if (isIgnore || !isScrolling.value) {
  100. messageContainer.value.scrollTop =
  101. messageContainer.value.scrollHeight - messageContainer.value.offsetHeight
  102. }
  103. })
  104. }
  105. function handleScroll() {
  106. const scrollContainer = messageContainer.value
  107. const scrollTop = scrollContainer.scrollTop
  108. const scrollHeight = scrollContainer.scrollHeight
  109. const offsetHeight = scrollContainer.offsetHeight
  110. if (scrollTop + offsetHeight < scrollHeight - 100) {
  111. // 用户开始滚动并在最底部之上,取消保持在最底部的效果
  112. isScrolling.value = true
  113. } else {
  114. // 用户停止滚动并滚动到最底部,开启保持到最底部的效果
  115. isScrolling.value = false
  116. }
  117. }
  118. /**
  119. * 复制
  120. */
  121. const noCopy = async (content) => {
  122. copy(content)
  123. ElMessage({
  124. message: '复制成功!',
  125. type: 'success'
  126. })
  127. }
  128. /**
  129. * 删除
  130. */
  131. const onDelete = async (id) => {
  132. // 删除 message
  133. await ChatMessageApi.delete(id)
  134. ElMessage({
  135. message: '删除成功!',
  136. type: 'success'
  137. })
  138. // 回调
  139. emits('onDeleteSuccess')
  140. }
  141. /**
  142. * 刷新
  143. */
  144. const onRefresh = async (message: ChatMessageVO) => {
  145. emits('onRefresh', message)
  146. }
  147. /**
  148. * 编辑
  149. */
  150. const onEdit = async (message: ChatMessageVO) => {
  151. emits('onEdit', message)
  152. }
  153. /**
  154. * 回到底部
  155. */
  156. const handleGoBottom = async () => {
  157. const scrollContainer = messageContainer.value
  158. scrollContainer.scrollTop = scrollContainer.scrollHeight
  159. }
  160. /**
  161. * 回到顶部
  162. */
  163. const handlerGoTop = async () => {
  164. const scrollContainer = messageContainer.value
  165. scrollContainer.scrollTop = 0
  166. }
  167. // 监听 list
  168. // TODO @fan:这个木有,是不是删除啦
  169. const { list, conversationId } = toRefs(props)
  170. watch(list, async (newValue, oldValue) => {
  171. console.log('watch list', list)
  172. })
  173. // 提供方法给 parent 调用
  174. defineExpose({ scrollToBottom, handlerGoTop })
  175. // 定义 emits
  176. const emits = defineEmits(['onDeleteSuccess', 'onRefresh', 'onEdit'])
  177. // onMounted
  178. onMounted(async () => {
  179. messageContainer.value.addEventListener('scroll', handleScroll)
  180. })
  181. </script>
  182. <style scoped lang="scss">
  183. .message-container {
  184. position: relative;
  185. //top: 0;
  186. //bottom: 0;
  187. //left: 0;
  188. //right: 0;
  189. //width: 100%;
  190. //height: 100%;
  191. overflow-y: scroll;
  192. //padding: 0 15px;
  193. //z-index: -1;
  194. }
  195. // 中间
  196. .chat-list {
  197. display: flex;
  198. flex-direction: column;
  199. overflow-y: hidden;
  200. padding: 0 20px;
  201. .message-item {
  202. margin-top: 50px;
  203. }
  204. .left-message {
  205. display: flex;
  206. flex-direction: row;
  207. }
  208. .right-message {
  209. display: flex;
  210. flex-direction: row-reverse;
  211. justify-content: flex-start;
  212. }
  213. .avatar {
  214. //height: 170px;
  215. //width: 170px;
  216. }
  217. .message {
  218. display: flex;
  219. flex-direction: column;
  220. text-align: left;
  221. margin: 0 15px;
  222. .time {
  223. text-align: left;
  224. line-height: 30px;
  225. }
  226. .left-text-container {
  227. position: relative;
  228. display: flex;
  229. flex-direction: column;
  230. overflow-wrap: break-word;
  231. background-color: rgba(228, 228, 228, 0.8);
  232. box-shadow: 0 0 0 1px rgba(228, 228, 228, 0.8);
  233. border-radius: 10px;
  234. padding: 10px 10px 5px 10px;
  235. .left-text {
  236. color: #393939;
  237. font-size: 0.95rem;
  238. }
  239. }
  240. .right-text-container {
  241. display: flex;
  242. flex-direction: row-reverse;
  243. .right-text {
  244. font-size: 0.95rem;
  245. color: #fff;
  246. display: inline;
  247. background-color: #267fff;
  248. color: #fff;
  249. box-shadow: 0 0 0 1px #267fff;
  250. border-radius: 10px;
  251. padding: 10px;
  252. width: auto;
  253. overflow-wrap: break-word;
  254. white-space: pre-wrap;
  255. }
  256. }
  257. .left-btns {
  258. display: flex;
  259. flex-direction: row;
  260. margin-top: 8px;
  261. }
  262. .right-btns {
  263. display: flex;
  264. flex-direction: row-reverse;
  265. margin-top: 8px;
  266. }
  267. }
  268. // 复制、删除按钮
  269. .btn-cus {
  270. display: flex;
  271. background-color: transparent;
  272. align-items: center;
  273. .btn-image {
  274. height: 20px;
  275. }
  276. }
  277. .btn-cus:hover {
  278. cursor: pointer;
  279. background-color: #f6f6f6;
  280. }
  281. }
  282. // 回到底部
  283. .to-bottom {
  284. position: absolute;
  285. z-index: 1000;
  286. bottom: 0;
  287. right: 50%;
  288. }
  289. </style>