四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.0 KiB

  1. <template>
  2. <view class="richtext-container">
  3. <!-- 状态栏安全区域 -->
  4. <uv-status-bar></uv-status-bar>
  5. <!-- 富文本内容区域 -->
  6. <view class="content-wrapper">
  7. <view class="content-container">
  8. <!-- 使用 uv-parse 组件渲染富文本 -->
  9. <uv-parse
  10. v-if="htmlContent"
  11. :content="htmlContent"
  12. :tag-style="tagStyle"
  13. :show-with-animation="true"
  14. :animation-duration="300"
  15. ></uv-parse>
  16. <!-- 加载状态 -->
  17. <view v-else class="loading-container">
  18. <uv-loading-icon mode="circle"></uv-loading-icon>
  19. <text class="loading-text">内容加载中...</text>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. htmlContent: '',
  30. // 富文本样式配置
  31. tagStyle: {
  32. p: 'margin: 16rpx 0; line-height: 1.6; color: #333;',
  33. img: 'max-width: 100%; height: auto; border-radius: 8rpx; margin: 16rpx 0;',
  34. h1: 'font-size: 36rpx; font-weight: bold; margin: 24rpx 0 16rpx 0; color: #222;',
  35. h2: 'font-size: 32rpx; font-weight: bold; margin: 20rpx 0 12rpx 0; color: #333;',
  36. h3: 'font-size: 28rpx; font-weight: bold; margin: 16rpx 0 8rpx 0; color: #444;',
  37. strong: 'font-weight: bold; color: #222;',
  38. em: 'font-style: italic; color: #666;',
  39. ul: 'margin: 16rpx 0; padding-left: 32rpx;',
  40. ol: 'margin: 16rpx 0; padding-left: 32rpx;',
  41. li: 'margin: 8rpx 0; line-height: 1.5;',
  42. blockquote: 'margin: 16rpx 0; padding: 16rpx; background: #f5f5f5; border-left: 4rpx solid #ddd; border-radius: 4rpx;',
  43. code: 'background: #f5f5f5; padding: 4rpx 8rpx; border-radius: 4rpx; font-family: monospace;',
  44. pre: 'background: #f5f5f5; padding: 16rpx; border-radius: 8rpx; overflow-x: auto; margin: 16rpx 0;'
  45. }
  46. }
  47. },
  48. onLoad(options) {
  49. // 获取传递的富文本内容
  50. if (options.content) {
  51. this.htmlContent = decodeURIComponent(options.content)
  52. } else {
  53. uni.showToast({
  54. title: '内容加载失败',
  55. icon: 'error'
  56. })
  57. setTimeout(() => {
  58. this.goBack()
  59. }, 1500)
  60. }
  61. },
  62. methods: {
  63. // 返回上一页
  64. goBack() {
  65. uni.navigateBack({
  66. delta: 1
  67. })
  68. }
  69. }
  70. }
  71. </script>
  72. <style lang="scss" scoped>
  73. .richtext-container {
  74. min-height: 100vh;
  75. background: #fff;
  76. }
  77. .content-wrapper {
  78. padding: 0 32rpx 40rpx;
  79. }
  80. .content-container {
  81. background: #fff;
  82. border-radius: 16rpx;
  83. overflow: hidden;
  84. }
  85. .loading-container {
  86. display: flex;
  87. flex-direction: column;
  88. align-items: center;
  89. justify-content: center;
  90. padding: 120rpx 0;
  91. .loading-text {
  92. margin-top: 24rpx;
  93. font-size: 28rpx;
  94. color: #999;
  95. }
  96. }
  97. // 富文本内容样式优化
  98. :deep(.uv-parse) {
  99. font-size: 28rpx;
  100. line-height: 1.6;
  101. color: #333;
  102. // 图片样式
  103. img {
  104. max-width: 100% !important;
  105. height: auto !important;
  106. border-radius: 8rpx;
  107. margin: 16rpx 0;
  108. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  109. }
  110. // 段落样式
  111. p {
  112. margin: 16rpx 0;
  113. text-align: justify;
  114. word-break: break-word;
  115. }
  116. // 标题样式
  117. h1, h2, h3, h4, h5, h6 {
  118. margin: 24rpx 0 16rpx 0;
  119. font-weight: bold;
  120. line-height: 1.4;
  121. }
  122. // 列表样式
  123. ul, ol {
  124. margin: 16rpx 0;
  125. padding-left: 32rpx;
  126. li {
  127. margin: 8rpx 0;
  128. line-height: 1.5;
  129. }
  130. }
  131. // 引用样式
  132. blockquote {
  133. margin: 16rpx 0;
  134. padding: 16rpx;
  135. background: #f8f9fa;
  136. border-left: 4rpx solid $primary-color;
  137. border-radius: 4rpx;
  138. font-style: italic;
  139. }
  140. // 代码样式
  141. code {
  142. background: #f1f3f4;
  143. padding: 4rpx 8rpx;
  144. border-radius: 4rpx;
  145. font-family: 'Courier New', monospace;
  146. font-size: 24rpx;
  147. }
  148. pre {
  149. background: #f1f3f4;
  150. padding: 16rpx;
  151. border-radius: 8rpx;
  152. overflow-x: auto;
  153. margin: 16rpx 0;
  154. code {
  155. background: none;
  156. padding: 0;
  157. }
  158. }
  159. }
  160. </style>