AiAnalysis.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div class="ai-analysis">
  3. <div class="ai-analysis-empty" v-if="loading">
  4. <Empty :autoFinish="autoFinish" :fail="fail"></Empty>
  5. </div>
  6. <div class="ai-analysis-text" v-html="renderedMarkdown" v-else></div>
  7. </div>
  8. </template>
  9. <script lang="ts" setup>
  10. import DOMPurify from 'dompurify';
  11. import MarkdownIt from 'markdown-it';
  12. import { checkLimit, incrementCount } from '@/utils/limit';
  13. import { ref, computed } from 'vue';
  14. import { useMainStore } from '@/store';
  15. import Empty from '@/components/CommonEmpty.vue';
  16. const mainStore = useMainStore();
  17. const loading = ref<boolean>(true);
  18. const autoFinish = ref<boolean>(false);
  19. const fail = ref<boolean>(false);
  20. const rawText = ref<string>('');
  21. const md = new MarkdownIt({
  22. html: true,
  23. linkify: true,
  24. typographer: true
  25. });
  26. // 去掉最外层 ```,渲染内部 Markdown
  27. const renderedMarkdown = computed(() => {
  28. let content = rawText.value.trim();
  29. content = content.replace(/^```[\s\S]*?\n?/, '').replace(/```$/, '');
  30. return DOMPurify.sanitize(md.render(content));
  31. });
  32. const getQualitativeInfo = () => {
  33. return mainStore
  34. .getQualitative()
  35. .then((res) => {
  36. rawText.value = res.msg;
  37. autoFinish.value = true;
  38. setTimeout(() => {
  39. loading.value = false;
  40. }, 300);
  41. })
  42. .catch((err) => {
  43. fail.value = true;
  44. }).finally(async () => {
  45. mainStore.setIsLoadOver(true);
  46. await incrementCount();
  47. })
  48. };
  49. getQualitativeInfo();
  50. </script>
  51. <style lang="scss" scoped>
  52. .ai-analysis {
  53. width: 100%;
  54. height: auto;
  55. padding: 40px;
  56. box-sizing: border-box;
  57. background-color: #ffffff;
  58. &-empty {
  59. width: 100%;
  60. height: 376px;
  61. }
  62. &-text {
  63. width: 100%;
  64. word-break: break-all;
  65. white-space: pre-wrap;
  66. overflow: hidden;
  67. font-weight: 400;
  68. font-size: 20px;
  69. color: #282e30;
  70. }
  71. }
  72. </style>