chat-html.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view>
  3. <view v-for="(chat, index) in messages" :key="index" class="cm_ai_answer"
  4. :class="chat.from === 'me' ? 'user' : ''">
  5. <view class="cm_ai_html" :class="chat.from === 'me' ? 'user' : ''">
  6. <template v-if="chat.from === 'me'">
  7. {{ chat.question }}
  8. </template>
  9. <template v-else>
  10. <!-- <image v-if="isLoading" class="cm_ai_html_loading" src="https://static.caimei365.com/app/img/chat/icon-loading@2x.gif" /> -->
  11. <!-- <template v-else> -->
  12. <text v-if="chat.typing">{{ chat.question.substring(0, chat.currentLength) }}</text>
  13. <text v-else>{{ chat.question }}</text>
  14. <!-- </template> -->
  15. </template>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'chatHtml',
  23. props: {
  24. messages: {
  25. type: Array
  26. }
  27. },
  28. data() {
  29. return {
  30. chatList: [],
  31. }
  32. },
  33. created() {
  34. },
  35. computed: {
  36. },
  37. watch: {
  38. messages: {
  39. handler: function(val) {
  40. console.log('messages', val)
  41. this.startTypingEffect(this.messages[this.messages.length - 1])
  42. }
  43. }
  44. },
  45. methods: {
  46. startTypingEffect(message) {
  47. if (!message.typing) {
  48. message.typing = true // 标记为正在打字
  49. let currentLength = 0
  50. const typeEffect = () => {
  51. if (currentLength < message.question.length) {
  52. message.currentLength++ // 增加已显示的文本长度
  53. // 如果需要,可以在这里添加一些性能优化或条件检查
  54. setTimeout(typeEffect, 50) // 每100毫秒增加一个字符
  55. } else {
  56. message.typing = false // 打字完成
  57. }
  58. }
  59. typeEffect() // 开始打字效果
  60. }
  61. }
  62. }
  63. }
  64. </script>
  65. <style lang="scss">
  66. .cm_ai_answer {
  67. width: 100%;
  68. height: auto;
  69. box-sizing: border-box;
  70. position: relative;
  71. margin: 20rpx 0;
  72. display: flex;
  73. justify-content: flex-start;
  74. &.user {
  75. justify-content: flex-end;
  76. }
  77. .cm_ai_html {
  78. min-height: 80rpx;
  79. border-radius: 0 30rpx 30rpx 30rpx;
  80. line-height: 1.5715;
  81. background-color: #FFFFFF;
  82. box-sizing: border-box;
  83. padding: 20rpx;
  84. font-size: 30rpx;
  85. color: #666666;
  86. &.user {
  87. min-height: 80rpx;
  88. border-radius: 30rpx 30rpx 0 30rpx;
  89. background-color: rgba(255, 91, 0, 0.1);
  90. font-size: 30rpx;
  91. color: #333333;
  92. }
  93. .cm_ai_html_loading {
  94. width: 60rpx;
  95. height: 14rpx;
  96. }
  97. }
  98. }
  99. </style>