indexPreview.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div class="tos-container">
  3. <el-card>
  4. <div class="tos-content" v-html="tosContent"></div>
  5. </el-card>
  6. </div>
  7. </template>
  8. <script setup lang="ts">
  9. import { ref, onMounted } from 'vue';
  10. import DOMPurify from 'dompurify';
  11. import { getTermsServiceListMax } from '@/api/system/business/ofService';
  12. const tosContent = ref('');
  13. const language = ref('zh-CN');
  14. onMounted(() => {
  15. fetchTos();
  16. });
  17. const fetchTos = async () => {
  18. try {
  19. const res = await getTermsServiceListMax();
  20. // 处理获取到的内容,确保格式良好
  21. const formattedContent = handleContentFormatting(res.data.content || '<p>暂无服务条款内容</p>');
  22. tosContent.value = DOMPurify.sanitize(formattedContent);
  23. } catch (error) {
  24. console.error('获取服务条款失败', error);
  25. tosContent.value = '<p>服务条款内容加载失败</p>';
  26. }
  27. };
  28. // 完善内容格式化处理
  29. const handleContentFormatting = (content: string): string => {
  30. if (!content) return '<p>暂无内容</p>';
  31. // 移除多余的空白字符,但保留必要的换行
  32. let formattedContent = content
  33. .replace(/\r\n/g, '\n')
  34. .replace(/\r/g, '\n')
  35. .replace(/\n\s*\n/g, '\n\n') // 合并多个空行为两个换行
  36. .trim();
  37. // 如果内容已经是HTML格式(包含标签),则不进行额外处理
  38. if (/<[^>]+>/.test(formattedContent)) {
  39. return formattedContent;
  40. }
  41. // 如果是纯文本,转换为基本的HTML格式
  42. // 将连续的换行转换为段落
  43. formattedContent = formattedContent
  44. .split('\n\n')
  45. .map((paragraph) => `<p>${paragraph.replace(/\n/g, '<br>')}</p>`)
  46. .join('');
  47. return formattedContent;
  48. };
  49. </script>
  50. <style scoped>
  51. .tos-container {
  52. padding: 20px;
  53. max-width: 800px;
  54. margin: 0 auto;
  55. }
  56. .tos-content {
  57. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  58. font-size: 14px;
  59. line-height: 1.6;
  60. color: #333;
  61. word-break: break-word;
  62. }
  63. /* 标题样式 */
  64. .tos-content h1 {
  65. font-size: 24px;
  66. font-weight: bold;
  67. margin: 24px 0 16px 0;
  68. color: #2c3e50;
  69. text-align: center;
  70. }
  71. .tos-content h2 {
  72. font-size: 20px;
  73. font-weight: bold;
  74. margin: 20px 0 12px 0;
  75. color: #34495e;
  76. border-bottom: 1px solid #eee;
  77. padding-bottom: 8px;
  78. }
  79. .tos-content h3 {
  80. font-size: 18px;
  81. font-weight: bold;
  82. margin: 16px 0 10px 0;
  83. color: #555;
  84. }
  85. /* 段落样式 */
  86. .tos-content p {
  87. margin: 12px 0;
  88. line-height: 1.7;
  89. }
  90. /* 列表样式 */
  91. .tos-content ul,
  92. .tos-content ol {
  93. margin: 12px 0;
  94. padding-left: 24px;
  95. }
  96. .tos-content li {
  97. margin: 8px 0;
  98. line-height: 1.6;
  99. }
  100. .tos-content ul li {
  101. list-style-type: disc;
  102. }
  103. .tos-content ol li {
  104. list-style-type: decimal;
  105. }
  106. /* 强调文本 */
  107. .tos-content strong {
  108. font-weight: bold;
  109. color: #2c3e50;
  110. }
  111. .tos-content em {
  112. font-style: italic;
  113. }
  114. /* 链接样式 */
  115. .tos-content a {
  116. color: #3498db;
  117. text-decoration: none;
  118. }
  119. .tos-content a:hover {
  120. text-decoration: underline;
  121. }
  122. /* 代码样式 */
  123. .tos-content code {
  124. background-color: #f8f9fa;
  125. padding: 2px 4px;
  126. border-radius: 3px;
  127. font-family: 'Courier New', monospace;
  128. font-size: 13px;
  129. }
  130. /* 引用样式 */
  131. .tos-content blockquote {
  132. border-left: 4px solid #ddd;
  133. margin: 16px 0;
  134. padding: 8px 16px;
  135. background-color: #f9f9f9;
  136. color: #666;
  137. }
  138. /* 响应式设计 */
  139. @media (max-width: 768px) {
  140. .tos-container {
  141. padding: 15px;
  142. }
  143. .tos-content {
  144. font-size: 13px;
  145. }
  146. .tos-content h1 {
  147. font-size: 20px;
  148. margin: 20px 0 14px 0;
  149. }
  150. .tos-content h2 {
  151. font-size: 18px;
  152. margin: 18px 0 10px 0;
  153. }
  154. .tos-content h3 {
  155. font-size: 16px;
  156. margin: 14px 0 8px 0;
  157. }
  158. .el-card {
  159. width: 100%;
  160. margin: 0 auto;
  161. }
  162. }
  163. /* 处理可能的空白内容 */
  164. .tos-content:empty::before {
  165. content: '暂无内容';
  166. color: #999;
  167. font-style: italic;
  168. }
  169. </style>