detail.uvue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view :class="isDarkMode ? 'theme-dark' : 'theme-light'" class="detail-container" style="flex: 1">
  4. <!-- #endif -->
  5. <!-- 宽屏时不使用共享元素组件 -->
  6. <view v-if="isWideScreen">
  7. <image class="banner-img" :src="cover"></image>
  8. <text class="banner-title">{{title}}</text>
  9. </view>
  10. <share-element v-else :share-key="shareKey">
  11. <view>
  12. <image class="banner-img" :src="btCover"></image>
  13. <text class="banner-title">{{title}}</text>
  14. </view>
  15. </share-element>
  16. <rich-text class="rich-text" :nodes="htmlNodes" mode="native"></rich-text>
  17. <!-- #ifdef APP -->
  18. </scroll-view>
  19. <!-- #endif -->
  20. </template>
  21. <script>
  22. import { state } from '@/store/index.uts'
  23. export default {
  24. props: {
  25. // 作为页面渲染时,props会接收url中的参数
  26. // 作为组件使用时,可以正常传递props
  27. post_id: {
  28. type: String,
  29. default: ''
  30. },
  31. cover: {
  32. type: String,
  33. default: ''
  34. },
  35. title: {
  36. type: String,
  37. default: ''
  38. },
  39. isWideScreen: {
  40. type: Boolean,
  41. default: false
  42. },
  43. shareKey: {
  44. type: String,
  45. default: ''
  46. },
  47. // list-news预加载传入的详情内容,仅用于宽屏第一个新闻
  48. firstDetailContent: {
  49. type: String,
  50. default: ''
  51. }
  52. },
  53. data() {
  54. return {
  55. htmlNodes: "",
  56. // 标记是否已经用过firstDetailContent
  57. usedContent: false,
  58. btCover: ''
  59. }
  60. },
  61. onLoad() {
  62. if (!this.isWideScreen) {
  63. this.btCover = this.cover;
  64. // #ifdef APP-ANDROID
  65. this.btCover = atob(this.btCover);
  66. // #endif
  67. }
  68. },
  69. watch: {
  70. // 监听post_id变化,当id变化时加载数据
  71. post_id: {
  72. immediate: true,
  73. deep: true,
  74. handler(newVal) {
  75. if (newVal != '') {
  76. // 优先用传入的预载详情内容(只在第一个新闻时有值)
  77. if (this.firstDetailContent !== '') {
  78. this.htmlNodes = this.firstDetailContent
  79. this.usedContent = true
  80. } else {
  81. this.getDetail(newVal)
  82. this.usedContent = false
  83. }
  84. }
  85. }
  86. },
  87. firstDetailContent(newVal) {
  88. // 只有没用过firstDetailContent时才赋值,防止重复
  89. if (!this.usedContent && newVal !== '') {
  90. this.htmlNodes = newVal
  91. this.usedContent = true
  92. }
  93. }
  94. },
  95. computed: {
  96. isDarkMode():boolean {
  97. return state.isDarkMode
  98. }
  99. },
  100. methods: {
  101. // #ifdef APP
  102. processHtmlContent(content: string, color: string): string {
  103. // 先给已有 style 的 <p> 追加 color
  104. content = content.replace(
  105. new RegExp('(<p[^>]*style=")([^"]*)"', 'g'),
  106. `$1$2;color:${color};"`
  107. );
  108. // 再给没有 style 的 <p> 加 style
  109. content = content.replace(
  110. new RegExp('<p((?!style)[^>]*)>', 'g'),
  111. `<p$1 style="color:${color};">`
  112. );
  113. // 替换 <h2 ...> 为 <p style="color:...;"><big>
  114. content = content.replace(
  115. new RegExp('<h2[^>]*>', 'g'),
  116. `<p style="color:${color};"><big>`
  117. );
  118. // 替换 </h2> 为 </big></p>
  119. content = content.replace(
  120. new RegExp('</h2>', 'g'),
  121. '</big></p>'
  122. );
  123. // 替换 <h3 ...> 为 <p style="color:...;"><big>
  124. content = content.replace(
  125. new RegExp('<h3[^>]*>', 'g'),
  126. `<p style="color:${color};"><big>`
  127. );
  128. // 替换 </h3> 为 </big></p>
  129. content = content.replace(
  130. new RegExp('</h3>', 'g'),
  131. '</big></p>'
  132. );
  133. // 匹配以 </p>、</img> 结尾,后面紧跟裸文本的情况
  134. content = content.replace(
  135. new RegExp('(<\\/p>|<\\/img>)([^<\\s][^<]*)', 'g'),
  136. `$1<p style="color:${color};">$2</p>`
  137. );
  138. // 先给已有 style 的 <span> 追加 color
  139. content = content.replace(
  140. new RegExp('(<span[^>]*style=")([^"]*)"', 'g'),
  141. `$1$2;color:${color};"`
  142. );
  143. // 再给没有 style 的 <span> 加 style
  144. content = content.replace(
  145. new RegExp('<span((?!style)[^>]*)>', 'g'),
  146. `<span$1 style="color:${color};">`
  147. );
  148. return content;
  149. },
  150. // #endif
  151. getDetail(post_id : string) {
  152. uni.request({
  153. url: 'https://unidemo.dcloud.net.cn/api/news/36kr/' + post_id,
  154. success: (data) => {
  155. if (data.statusCode == 200) {
  156. const result = data.data as UTSJSONObject;
  157. let content = result["content"] as string;
  158. // #ifdef APP
  159. if (this.isDarkMode) {
  160. let color:string;
  161. // #ifdef APP-HARMONY
  162. color = 'rgb(255,255,255)'; // android 无效,rgb(255,255,255)和white
  163. // #endif
  164. // #ifndef APP-HARMONY
  165. color = '#ffffff'; // 鸿蒙的 webview 不显示 16 进制颜色 '#ffffff'
  166. // #endif
  167. content = this.processHtmlContent(content, color);
  168. }
  169. // #endif
  170. this.htmlNodes = content;
  171. }
  172. },
  173. fail: () => {
  174. console.log('fail');
  175. }
  176. });
  177. },
  178. }
  179. }
  180. </script>
  181. <style>
  182. .detail-container {
  183. background-color: var(--background-color,#f8f8f8);
  184. }
  185. .banner {
  186. height: 180px;
  187. overflow: hidden;
  188. position: relative;
  189. background-color: var(--background-color,#f8f8f8);
  190. }
  191. .banner-img {
  192. width: 100%;
  193. }
  194. .banner-title {
  195. max-height: 42px;
  196. overflow: hidden;
  197. position: absolute;
  198. left: 15px;
  199. bottom: 15px;
  200. width: 90%;
  201. font-size: 16px;
  202. font-weight: 400;
  203. line-height: 21px;
  204. color: white;
  205. }
  206. .rich-text {
  207. padding: 3px 3px 0px;
  208. color: var(--text-color,#333333);
  209. }
  210. </style>