AiCsMessageBubble.vue 7.48 KB
<template>
  <div class="cs-bubble-row" :class="message.role">
    <!-- AI 头像(流光精灵) -->
    <div v-if="message.role === 'assistant'" class="cs-avatar assistant-avatar">
      <svg viewBox="0 0 90 106" fill="none" xmlns="http://www.w3.org/2000/svg" class="cs-dragon-avatar">
        <defs>
          <radialGradient id="spAG" cx="36%" cy="28%" r="72%">
            <stop offset="0%" stop-color="#F8FAFF"/>
            <stop offset="38%" stop-color="#93C5FD"/>
            <stop offset="100%" stop-color="#7C3AED"/>
          </radialGradient>
          <linearGradient id="spAE" x1="0%" y1="100%" x2="0%" y2="0%">
            <stop offset="0%" stop-color="#A78BFA"/><stop offset="100%" stop-color="#DDD6FE"/>
          </linearGradient>
          <linearGradient id="spAT" x1="0%" y1="0%" x2="100%" y2="0%">
            <stop offset="0%" stop-color="#818CF8"/><stop offset="100%" stop-color="#DDD6FE"/>
          </linearGradient>
          <linearGradient id="spAR" x1="0%" y1="0%" x2="0%" y2="100%">
            <stop offset="0%" stop-color="#93C5FD" stop-opacity="0.8"/>
            <stop offset="100%" stop-color="#A78BFA" stop-opacity="0"/>
          </linearGradient>
        </defs>
        <path d="M 28,72 C 20,84 18,96 24,103" stroke="url(#spAR)" stroke-width="5.5" stroke-linecap="round" fill="none"/>
        <path d="M 52,72 C 60,84 62,96 56,103" stroke="url(#spAR)" stroke-width="5.5" stroke-linecap="round" fill="none"/>
        <path d="M 68,52 C 82,44 88,28 80,16 C 72,6 60,10 62,20 C 64,28 74,26 72,18"
          stroke="url(#spAT)" stroke-width="9" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
        <circle cx="71" cy="17" r="3.5" fill="#DDD6FE" opacity="0.85"/>
        <path d="M 24,22 L 15,5 L 33,17" fill="url(#spAE)"/>
        <path d="M 24,21 L 20,10 L 31,17" fill="#FCA5A5" opacity="0.5"/>
        <path d="M 56,22 L 65,5 L 47,17" fill="url(#spAE)"/>
        <path d="M 56,21 L 60,10 L 49,17" fill="#FCA5A5" opacity="0.5"/>
        <ellipse cx="40" cy="46" rx="28" ry="30" fill="url(#spAG)"/>
        <ellipse cx="29" cy="40" rx="9" ry="10" fill="white"/>
        <circle cx="30.5" cy="41.5" r="6.5" fill="#1E1B4B"/>
        <circle cx="31" cy="41" r="4.5" fill="#4F46E5"/>
        <circle cx="31.5" cy="40.5" r="2.5" fill="#818CF8"/>
        <circle cx="30.5" cy="42" r="3" fill="#050010"/>
        <circle cx="33.5" cy="37.5" r="2.2" fill="white"/>
        <ellipse cx="51" cy="40" rx="9" ry="10" fill="white"/>
        <circle cx="52.5" cy="41.5" r="6.5" fill="#1E1B4B"/>
        <circle cx="53" cy="41" r="4.5" fill="#4F46E5"/>
        <circle cx="53.5" cy="40.5" r="2.5" fill="#818CF8"/>
        <circle cx="52.5" cy="42" r="3" fill="#050010"/>
        <circle cx="55.5" cy="37.5" r="2.2" fill="white"/>
        <ellipse cx="40" cy="54" rx="2.2" ry="1.6" fill="#6D28D9" opacity="0.65"/>
        <path d="M 35,58 Q 40,64 45,58" stroke="#5B21B6" stroke-width="2" fill="none" stroke-linecap="round"/>
        <line x1="40" y1="55.5" x2="40" y2="58.5" stroke="#5B21B6" stroke-width="1.5" stroke-linecap="round"/>
        <ellipse cx="18" cy="52" rx="7" ry="4.5" fill="#FCA5A5" opacity="0.42"/>
        <ellipse cx="62" cy="52" rx="7" ry="4.5" fill="#FCA5A5" opacity="0.42"/>
      </svg>
    </div>

    <div class="cs-bubble-body">
      <!-- 消息气泡 -->
      <div class="cs-bubble" :class="[message.role, message.status]">
        <template v-if="message.status === 'streaming' && !message.content">
          <span class="cs-typing-dots">
            <span></span><span></span><span></span>
          </span>
        </template>
        <template v-else-if="message.status === 'error'">
          <span class="cs-error-text">{{ message.content || '回答出现错误,请重试。' }}</span>
        </template>
        <template v-else>
          <span class="cs-bubble-text">{{ message.content }}</span>
        </template>
      </div>
      <!-- 复制按钮(AI消息且已完成时显示) -->
      <div
        v-if="message.role === 'assistant' && message.status === 'done' && message.content"
        class="cs-bubble-actions"
      >
        <button class="cs-copy-btn" :class="{ copied: justCopied }" @click="copyContent" type="button">
          <i v-if="justCopied" class="fas fa-check"></i>
          <i v-else class="fas fa-copy"></i>
          {{ justCopied ? '已复制' : '复制' }}
        </button>
      </div>
    </div>

    <!-- 用户头像 -->
    <div v-if="message.role === 'user'" class="cs-avatar user-avatar">
      <i class="fas fa-user"></i>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { CsMessage } from "@/stores/customerService";

const props = defineProps<{ message: CsMessage }>();

const justCopied = ref(false);
let copyTimer: ReturnType<typeof setTimeout> | null = null;

function copyContent() {
  if (!props.message.content) return;
  navigator.clipboard.writeText(props.message.content).then(() => {
    justCopied.value = true;
    if (copyTimer) clearTimeout(copyTimer);
    copyTimer = setTimeout(() => { justCopied.value = false; }, 2000);
  });
}

onUnmounted(() => { if (copyTimer) clearTimeout(copyTimer); });
</script>

<style scoped>
.cs-bubble-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 2px 0;
}

.cs-bubble-row.user {
  flex-direction: row-reverse;
}

.cs-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  overflow: hidden;
}

.assistant-avatar {
  background: linear-gradient(135deg, #EFF6FF, #EDE9FE);
  border: 1px solid #BFDBFE;
}

.cs-dragon-avatar {
  width: 26px;
  height: 31px;
  display: block;
}

.user-avatar {
  background: #409eff;
  color: #ffffff;
}

.cs-bubble-body {
  max-width: calc(100% - 88px);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cs-bubble-row.user .cs-bubble-body {
  align-items: flex-end;
}

.cs-bubble {
  display: inline-block;
  padding: 9px 13px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.65;
  word-break: break-word;
  transition: box-shadow 0.15s ease;
}

.cs-bubble.assistant {
  background: #ffffff;
  color: #303133;
  border: 1px solid #ebeef5;
  border-top-left-radius: 3px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.cs-bubble.user {
  background: linear-gradient(135deg, #409eff, #36a3f7);
  color: #ffffff;
  border-top-right-radius: 3px;
  box-shadow: 0 2px 8px rgba(64,158,255,0.3);
}

.cs-bubble-text {
  white-space: pre-wrap;
}

.cs-error-text {
  color: #f56c6c;
}

/* 气泡操作栏 */
.cs-bubble-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.cs-bubble-row:hover .cs-bubble-actions {
  opacity: 1;
}

.cs-copy-btn {
  border: none;
  background: transparent;
  color: #909399;
  font-size: 11px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 2px 6px;
  border-radius: 4px;
  transition: color 0.15s ease, background 0.15s ease;
}

.cs-copy-btn:hover {
  background: #f0f2f5;
  color: #606266;
}

.cs-copy-btn.copied {
  color: #67c23a;
}

/* 打点动画 */
.cs-typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 4px;
}

.cs-typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #a8d5c2;
  animation: cs-dot-bounce 1.2s infinite;
}

.cs-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.cs-typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes cs-dot-bounce {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}
</style>