LinkURLContent.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div class="link-content-wrap">
  3. <Transition name="fade">
  4. <div class="link-content" :class="{ active: url }">
  5. <a v-if="url" :href="url" target="_blank" rel="noopener" class="link-url-link">
  6. <img src="@/assets/zip-icon.png" class="link-icon" />
  7. <span class="link-text">{{ displayName }}</span>
  8. </a>
  9. </div>
  10. </Transition>
  11. </div>
  12. </template>
  13. <script setup>
  14. import { ref, watch, onMounted, computed } from "vue";
  15. const props = defineProps({
  16. content: {
  17. type: String,
  18. default: "",
  19. },
  20. });
  21. const loading = ref(false);
  22. const error = ref("");
  23. const url = ref("");
  24. const displayName = computed(() => {
  25. // 提取 <link-url> 标签中的内容作为显示名称
  26. if (url.value) {
  27. // 提取 URL 中的文件名部分
  28. const urlParts = url.value.split("/");
  29. const fileName = urlParts[urlParts.length - 1];
  30. // 如果 URL 中有文件名,则返回文件名,否则返回 URL
  31. if (fileName) {
  32. return fileName;
  33. }
  34. }
  35. // 如果没有 URL,则返回默认文本
  36. return "材料.zip";
  37. });
  38. function extractLinkURL(content) {
  39. // return 'http://1.95.178.212:8910/download_forms/6918bc00-e9ed-46d1-9714-f302cf285f02'
  40. // 匹配 <link-url>...</link-url>,允许前后有空格和换行
  41. const match = (content || "").match(/<link-url[^>]*>([\s\S]*?)<\/link-url>/i);
  42. if (match && match[1]) {
  43. // 去除首尾空白
  44. return match[1].trim();
  45. }
  46. return "";
  47. }
  48. async function fetchLinkURL() {
  49. loading.value = true;
  50. error.value = "";
  51. try {
  52. // 解析 url
  53. console.log("正在提取链接内容:", props.content);
  54. const extractedUrl = extractLinkURL(props.content);
  55. console.log("提取的表单链接:", props.content, extractedUrl);
  56. if (!extractedUrl) {
  57. error.value = "";
  58. url.value = "";
  59. } else {
  60. // 这里可以添加额外的校验或处理
  61. url.value = extractedUrl;
  62. }
  63. // 模拟异步加载
  64. await new Promise((resolve) => setTimeout(resolve, 300));
  65. } catch (e) {
  66. console.error("数据加载失败", e);
  67. error.value = "数据加载失败";
  68. url.value = "";
  69. } finally {
  70. loading.value = false;
  71. }
  72. }
  73. watch(
  74. () => props.content,
  75. () => {
  76. fetchLinkURL();
  77. },
  78. { immediate: true }
  79. );
  80. onMounted(() => {
  81. fetchLinkURL();
  82. });
  83. </script>
  84. <style lang="scss" scoped>
  85. .link-content-wrap {
  86. margin: 16px 0;
  87. position: relative;
  88. .link-content {
  89. display: inline-block;
  90. padding: 6px;
  91. border-radius: 12px;
  92. min-height: 48px;
  93. min-width: 200px;
  94. &.active {
  95. background-color: rgba(255, 255, 255, 1);
  96. }
  97. }
  98. }
  99. .loading {
  100. position: absolute;
  101. top: 0;
  102. left: 0;
  103. width: 100%;
  104. height: 100%;
  105. display: flex;
  106. justify-content: center;
  107. align-items: center;
  108. color: #888;
  109. text-align: center;
  110. padding: 20px 0;
  111. }
  112. .error {
  113. color: #fff;
  114. text-align: center;
  115. padding: 20px 0;
  116. }
  117. .link-content {
  118. display: inline-block;
  119. white-space: pre-wrap;
  120. word-break: break-word;
  121. transition: opacity 0.2s;
  122. opacity: 1;
  123. }
  124. .link-url-link {
  125. color: #1677ff;
  126. font-size: 16px;
  127. word-break: break-all;
  128. text-decoration: underline;
  129. display: flex;
  130. align-items: center;
  131. .link-icon {
  132. width: 36px;
  133. height: 36px;
  134. margin-right: 8px;
  135. vertical-align: middle;
  136. }
  137. .link-text {
  138. color: hsla(218, 100%, 40%, 1);
  139. font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  140. }
  141. }
  142. </style>
  143. <style>
  144. .fade-enter-active,
  145. .fade-leave-active {
  146. transition: opacity 0.3s ease-in-out;
  147. }
  148. .fade-enter-from,
  149. .fade-leave-to {
  150. opacity: 0;
  151. }
  152. .fade-enter-to,
  153. .fade-leave-from {
  154. opacity: 1;
  155. }
  156. </style>