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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <template>
  2. <view :class="'interlayer '+(c||'')" :style="s">
  3. <block v-for="(n, i) in nodes" v-bind:key="i">
  4. <!--图片-->
  5. <view v-if="n.name=='img'" :class="'_img '+n.attrs.class" :style="n.attrs.style" :data-attrs="n.attrs" @tap.stop="imgtap">
  6. <rich-text v-if="ctrl[i]!=0" :nodes="[{attrs:{src:loading&&(ctrl[i]||0)<2?loading:(lazyLoad&&!ctrl[i]?placeholder:(ctrl[i]==3?errorImg:n.attrs.src||'')),alt:n.attrs.alt||'',width:n.attrs.width||'',style:'-webkit-touch-callout:none;max-width:100%;display:block'+(n.attrs.height?';height:'+n.attrs.height:'')},name:'img'}]" />
  7. <image class="_image" :src="lazyLoad&&!ctrl[i]?placeholder:n.attrs.src" :lazy-load="lazyLoad"
  8. :show-menu-by-longpress="!n.attrs.ignore" :data-i="i" :data-index="n.attrs.i" data-source="img" @load="loadImg"
  9. @error="error" />
  10. </view>
  11. <!--文本-->
  12. <text v-else-if="n.type=='text'" decode>{{n.text}}</text>
  13. <!--#ifndef MP-BAIDU-->
  14. <text v-else-if="n.name=='br'">\n</text>
  15. <!--#endif-->
  16. <!--视频-->
  17. <view v-else-if="((n.lazyLoad&&!n.attrs.autoplay)||(n.name=='video'&&!loadVideo))&&ctrl[i]==undefined" :id="n.attrs.id"
  18. :class="'_video '+(n.attrs.class||'')" :style="n.attrs.style" :data-i="i" @tap.stop="_loadVideo" />
  19. <video v-else-if="n.name=='video'" :id="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :autoplay="n.attrs.autoplay||ctrl[i]==0"
  20. :controls="n.attrs.controls" :loop="n.attrs.loop" :muted="n.attrs.muted" :poster="n.attrs.poster" :src="n.attrs.source[ctrl[i]||0]"
  21. :unit-id="n.attrs['unit-id']" :data-id="n.attrs.id" :data-i="i" data-source="video" @error="error" @play="play" />
  22. <!--音频-->
  23. <audio v-else-if="n.name=='audio'" :ref="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :author="n.attrs.author"
  24. :autoplay="n.attrs.autoplay" :controls="n.attrs.controls" :loop="n.attrs.loop" :name="n.attrs.name" :poster="n.attrs.poster"
  25. :src="n.attrs.source[ctrl[i]||0]" :data-i="i" :data-id="n.attrs.id" data-source="audio" @error.native="error"
  26. @play.native="play" />
  27. <!--链接-->
  28. <view v-else-if="n.name=='a'" :id="n.attrs.id" :class="'_a '+(n.attrs.class||'')" hover-class="_hover" :style="n.attrs.style"
  29. :data-attrs="n.attrs" @tap.stop="linkpress">
  30. <trees class="_span" c="_span" :nodes="n.children" />
  31. </view>
  32. <!--广告-->
  33. <!--<ad v-else-if="n.name=='ad'" :class="n.attrs.class" :style="n.attrs.style" :unit-id="n.attrs['unit-id']" :appid="n.attrs.appid" :apid="n.attrs.apid" :type="n.attrs.type" :adpid="n.attrs.adpid" data-source="ad" @error="error" />-->
  34. <!--列表-->
  35. <view v-else-if="n.name=='li'" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:flex;flex-direction:row'">
  36. <view v-if="n.type=='ol'" class="_ol-bef">{{n.num}}</view>
  37. <view v-else class="_ul-bef">
  38. <view v-if="n.floor%3==0" class="_ul-p1">█</view>
  39. <view v-else-if="n.floor%3==2" class="_ul-p2" />
  40. <view v-else class="_ul-p1" style="border-radius:50%">█</view>
  41. </view>
  42. <trees class="_li" c="_li" :nodes="n.children" :lazyLoad="lazyLoad" :loading="loading" />
  43. </view>
  44. <!--表格-->
  45. <view v-else-if="n.name=='table'&&n.c&&n.flag" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:grid'">
  46. <trees v-for="(cell,n) in n.children" v-bind:key="n" :class="cell.attrs.class" :c="cell.attrs.class" :style="cell.attrs.style"
  47. :s="cell.attrs.style" :nodes="cell.children" />
  48. </view>
  49. <view v-else-if="n.name=='table'&&n.c" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:table'">
  50. <view v-for="(tbody, o) in n.children" v-bind:key="o" :class="tbody.attrs.class" :style="(tbody.attrs.style||'')+(tbody.name[0]=='t'?';display:table-'+(tbody.name=='tr'?'row':'row-group'):'')">
  51. <view v-for="(tr, p) in tbody.children" v-bind:key="p" :class="tr.attrs.class" :style="(tr.attrs.style||'')+(tr.name[0]=='t'?';display:table-'+(tr.name=='tr'?'row':'cell'):'')">
  52. <trees v-if="tr.name=='td'" :nodes="tr.children" />
  53. <trees v-else v-for="(td, q) in tr.children" v-bind:key="q" :class="td.attrs.class" :c="td.attrs.class" :style="(td.attrs.style||'')+(td.name[0]=='t'?';display:table-'+(td.name=='tr'?'row':'cell'):'')"
  54. :s="(td.attrs.style||'')+(td.name[0]=='t'?';display:table-'+(td.name=='tr'?'row':'cell'):'')" :nodes="td.children" />
  55. </view>
  56. </view>
  57. </view>
  58. <!--#ifdef APP-PLUS-->
  59. <iframe v-else-if="n.name=='iframe'" :style="n.attrs.style" :allowfullscreen="n.attrs.allowfullscreen" :frameborder="n.attrs.frameborder"
  60. :width="n.attrs.width" :height="n.attrs.height" :src="n.attrs.src" />
  61. <embed v-else-if="n.name=='embed'" :style="n.attrs.style" :width="n.attrs.width" :height="n.attrs.height" :src="n.attrs.src" />
  62. <!--#endif-->
  63. <!--富文本-->
  64. <!--#ifdef MP-WEIXIN || MP-QQ || APP-PLUS-->
  65. <rich-text v-else-if="use(n)" :id="n.attrs.id" :class="'_p __'+n.name" :nodes="[n]" />
  66. <!--#endif-->
  67. <!--#ifndef MP-WEIXIN || MP-QQ || APP-PLUS-->
  68. <rich-text v-else-if="!n.c" :id="n.attrs.id" :nodes="[n]" style="display:inline" />
  69. <!--#endif-->
  70. <trees v-else :class="(n.attrs.id||'')+' _'+n.name+' '+(n.attrs.class||'')" :c="(n.attrs.id||'')+' _'+n.name+' '+(n.attrs.class||'')"
  71. :style="n.attrs.style" :s="n.attrs.style" :nodes="n.children" :lazyLoad="lazyLoad" :loading="loading" />
  72. </block>
  73. </view>
  74. </template>
  75. <script>
  76. var inline = {
  77. abbr: 1,
  78. b: 1,
  79. big: 1,
  80. code: 1,
  81. del: 1,
  82. em: 1,
  83. i: 1,
  84. ins: 1,
  85. label: 1,
  86. q: 1,
  87. small: 1,
  88. span: 1,
  89. strong: 1,
  90. sub: 1,
  91. sup: 1
  92. }
  93. let global = {};
  94. global.Parser = {};
  95. import trees from './trees'
  96. import cfg from '../libs/config.js'
  97. const errorImg = cfg.errorImg;
  98. export default {
  99. components: {
  100. trees
  101. },
  102. name: 'trees',
  103. data() {
  104. return {
  105. ctrl: [],
  106. placeholder: 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="300" height="225"/>',
  107. errorImg,
  108. loadVideo: typeof plus == 'undefined',
  109. // #ifndef MP-ALIPAY
  110. c: '',
  111. s: ''
  112. // #endif
  113. }
  114. },
  115. props: {
  116. nodes: Array,
  117. lazyLoad: Boolean,
  118. loading: String,
  119. // #ifdef MP-ALIPAY
  120. c: String,
  121. s: String
  122. // #endif
  123. },
  124. mounted() {
  125. for (this.top = this.$parent; this.top.$options.name != 'parser'; this.top = this.top.$parent);
  126. this.init();
  127. },
  128. // #ifdef APP-PLUS
  129. // #ifndef VUE3
  130. // 组件销毁前,将实例从u-form的缓存中移除
  131. beforeDestroy() {
  132. this.observer && this.observer.disconnect();
  133. },
  134. // #endif
  135. // #ifdef VUE3
  136. beforeUnmount() {
  137. this.observer && this.observer.disconnect();
  138. },
  139. // #endif
  140. // #endif
  141. methods: {
  142. init() {
  143. for (var i = this.nodes.length, n; n = this.nodes[--i];) {
  144. if (n.name == 'img') {
  145. this.top.imgList.setItem(n.attrs.i, n.attrs['original-src'] || n.attrs.src);
  146. // #ifdef APP-PLUS
  147. if (this.lazyLoad && !this.observer) {
  148. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  149. top: 500,
  150. bottom: 500
  151. });
  152. setTimeout(() => {
  153. this.observer.observe('._img', res => {
  154. if (res.intersectionRatio) {
  155. for (var j = this.nodes.length; j--;)
  156. if (this.nodes[j].name == 'img')
  157. this.$set(this.ctrl, j, 1);
  158. this.observer.disconnect();
  159. }
  160. })
  161. }, 0)
  162. }
  163. // #endif
  164. } else if (n.name == 'video' || n.name == 'audio') {
  165. var ctx;
  166. if (n.name == 'video') {
  167. ctx = uni.createVideoContext(n.attrs.id
  168. // #ifndef MP-BAIDU
  169. , this
  170. // #endif
  171. );
  172. } else if (this.$refs[n.attrs.id])
  173. ctx = this.$refs[n.attrs.id][0];
  174. if (ctx) {
  175. ctx.id = n.attrs.id;
  176. this.top.videoContexts.push(ctx);
  177. }
  178. }
  179. }
  180. // #ifdef APP-PLUS
  181. // APP 上避免 video 错位需要延时渲染
  182. setTimeout(() => {
  183. this.loadVideo = true;
  184. }, 1000)
  185. // #endif
  186. },
  187. play(e) {
  188. var contexts = this.top.videoContexts;
  189. if (contexts.length > 1 && this.top.autopause)
  190. for (var i = contexts.length; i--;)
  191. if (contexts[i].id != e.currentTarget.dataset.id)
  192. contexts[i].pause();
  193. },
  194. imgtap(e) {
  195. var attrs = e.currentTarget.dataset.attrs;
  196. if (!attrs.ignore) {
  197. var preview = true,
  198. data = {
  199. id: e.target.id,
  200. src: attrs.src,
  201. ignore: () => preview = false
  202. };
  203. global.Parser.onImgtap && global.Parser.onImgtap(data);
  204. this.top.$emit('imgtap', data);
  205. if (preview) {
  206. var urls = this.top.imgList,
  207. current = urls[attrs.i] ? parseInt(attrs.i) : (urls = [attrs.src], 0);
  208. uni.previewImage({
  209. current,
  210. urls
  211. })
  212. }
  213. }
  214. },
  215. loadImg(e) {
  216. var i = e.currentTarget.dataset.i;
  217. if (this.lazyLoad && !this.ctrl[i]) {
  218. // #ifdef QUICKAPP-WEBVIEW
  219. this.$set(this.ctrl, i, 0);
  220. this.$nextTick(function() {
  221. // #endif
  222. // #ifndef APP-PLUS
  223. this.$set(this.ctrl, i, 1);
  224. // #endif
  225. // #ifdef QUICKAPP-WEBVIEW
  226. })
  227. // #endif
  228. } else if (this.loading && this.ctrl[i] != 2) {
  229. // #ifdef QUICKAPP-WEBVIEW
  230. this.$set(this.ctrl, i, 0);
  231. this.$nextTick(function() {
  232. // #endif
  233. this.$set(this.ctrl, i, 2);
  234. // #ifdef QUICKAPP-WEBVIEW
  235. })
  236. // #endif
  237. }
  238. },
  239. linkpress(e) {
  240. var jump = true,
  241. attrs = e.currentTarget.dataset.attrs;
  242. attrs.ignore = () => jump = false;
  243. global.Parser.onLinkpress && global.Parser.onLinkpress(attrs);
  244. this.top.$emit('linkpress', attrs);
  245. if (jump) {
  246. // #ifdef MP
  247. if (attrs['app-id']) {
  248. return uni.navigateToMiniProgram({
  249. appId: attrs['app-id'],
  250. path: attrs.path
  251. })
  252. }
  253. // #endif
  254. if (attrs.href) {
  255. if (attrs.href[0] == '#') {
  256. if (this.top.useAnchor)
  257. this.top.navigateTo({
  258. id: attrs.href.substring(1)
  259. })
  260. } else if (attrs.href.indexOf('http') == 0 || attrs.href.indexOf('//') == 0) {
  261. // #ifdef APP-PLUS
  262. plus.runtime.openWeb(attrs.href);
  263. // #endif
  264. // #ifndef APP-PLUS
  265. uni.setClipboardData({
  266. data: attrs.href,
  267. success: () =>
  268. uni.showToast({
  269. title: '链接已复制'
  270. })
  271. })
  272. // #endif
  273. } else
  274. uni.navigateTo({
  275. url: attrs.href,
  276. fail() {
  277. uni.switchTab({
  278. url: attrs.href,
  279. })
  280. }
  281. })
  282. }
  283. }
  284. },
  285. error(e) {
  286. var target = e.currentTarget,
  287. source = target.dataset.source,
  288. i = target.dataset.i;
  289. if (source == 'video' || source == 'audio') {
  290. // 加载其他 source
  291. var index = this.ctrl[i] ? this.ctrl[i].i + 1 : 1;
  292. if (index < this.nodes[i].attrs.source.length)
  293. this.$set(this.ctrl, i, index);
  294. if (e.detail.__args__)
  295. e.detail = e.detail.__args__[0];
  296. } else if (errorImg && source == 'img') {
  297. this.top.imgList.setItem(target.dataset.index, errorImg);
  298. this.$set(this.ctrl, i, 3);
  299. }
  300. this.top && this.top.$emit('error', {
  301. source,
  302. target,
  303. errMsg: e.detail.errMsg
  304. });
  305. },
  306. _loadVideo(e) {
  307. this.$set(this.ctrl, e.target.dataset.i, 0);
  308. },
  309. use(item) {
  310. return !item.c && !inline[item.name] && (item.attrs.style || '').indexOf('display:inline') == -1
  311. }
  312. }
  313. }
  314. </script>
  315. <style>
  316. /* 在这里引入自定义样式 */
  317. /* 链接和图片效果 */
  318. ._a {
  319. display: inline;
  320. padding: 1.5px 0 1.5px 0;
  321. color: #366092;
  322. word-break: break-all;
  323. }
  324. ._hover {
  325. text-decoration: underline;
  326. opacity: 0.7;
  327. }
  328. ._img {
  329. display: inline-block;
  330. max-width: 100%;
  331. overflow: hidden;
  332. }
  333. /* #ifdef MP-WEIXIN */
  334. :host {
  335. display: inline;
  336. }
  337. /* #endif */
  338. /* #ifndef MP-ALIPAY || APP-PLUS */
  339. .interlayer {
  340. display: inherit;
  341. flex-direction: inherit;
  342. flex-wrap: inherit;
  343. align-content: inherit;
  344. align-items: inherit;
  345. justify-content: inherit;
  346. width: 100%;
  347. white-space: inherit;
  348. }
  349. /* #endif */
  350. ._b,
  351. ._strong {
  352. font-weight: bold;
  353. }
  354. /* #ifndef MP-ALIPAY */
  355. ._blockquote,
  356. ._div,
  357. ._p,
  358. ._ol,
  359. ._ul,
  360. ._li {
  361. display: block;
  362. }
  363. /* #endif */
  364. ._code {
  365. font-family: monospace;
  366. }
  367. ._del {
  368. text-decoration: line-through;
  369. }
  370. ._em,
  371. ._i {
  372. font-style: italic;
  373. }
  374. ._h1 {
  375. font-size: 2em;
  376. }
  377. ._h2 {
  378. font-size: 1.5em;
  379. }
  380. ._h3 {
  381. font-size: 1.17em;
  382. }
  383. ._h5 {
  384. font-size: 0.83em;
  385. }
  386. ._h6 {
  387. font-size: 0.67em;
  388. }
  389. ._h1,
  390. ._h2,
  391. ._h3,
  392. ._h4,
  393. ._h5,
  394. ._h6 {
  395. display: block;
  396. font-weight: bold;
  397. }
  398. ._image {
  399. display: block;
  400. width: 100%;
  401. height: 360px;
  402. margin-top: -360px;
  403. opacity: 0;
  404. }
  405. ._ins {
  406. text-decoration: underline;
  407. }
  408. ._li {
  409. flex: 1;
  410. width: 0;
  411. }
  412. ._ol-bef {
  413. width: 36px;
  414. margin-right: 5px;
  415. text-align: right;
  416. }
  417. ._ul-bef {
  418. display: block;
  419. margin: 0 12px 0 23px;
  420. line-height: normal;
  421. }
  422. ._ol-bef,
  423. ._ul-bef {
  424. flex: none;
  425. user-select: none;
  426. }
  427. ._ul-p1 {
  428. display: inline-block;
  429. width: 0.3em;
  430. height: 0.3em;
  431. overflow: hidden;
  432. line-height: 0.3em;
  433. }
  434. ._ul-p2 {
  435. display: inline-block;
  436. width: 0.23em;
  437. height: 0.23em;
  438. border: 0.05em solid black;
  439. border-radius: 50%;
  440. }
  441. ._q::before {
  442. content: '"';
  443. }
  444. ._q::after {
  445. content: '"';
  446. }
  447. ._sub {
  448. font-size: smaller;
  449. vertical-align: sub;
  450. }
  451. ._sup {
  452. font-size: smaller;
  453. vertical-align: super;
  454. }
  455. /* #ifdef MP-ALIPAY || APP-PLUS || QUICKAPP-WEBVIEW */
  456. ._abbr,
  457. ._b,
  458. ._code,
  459. ._del,
  460. ._em,
  461. ._i,
  462. ._ins,
  463. ._label,
  464. ._q,
  465. ._span,
  466. ._strong,
  467. ._sub,
  468. ._sup {
  469. display: inline;
  470. }
  471. /* #endif */
  472. /* #ifdef MP-WEIXIN || MP-QQ */
  473. .__bdo,
  474. .__bdi,
  475. .__ruby,
  476. .__rt {
  477. display: inline-block;
  478. }
  479. /* #endif */
  480. ._video {
  481. position: relative;
  482. display: inline-block;
  483. width: 300px;
  484. height: 225px;
  485. background-color: black;
  486. }
  487. ._video::after {
  488. position: absolute;
  489. top: 50%;
  490. left: 50%;
  491. margin: -15px 0 0 -15px;
  492. content: '';
  493. border-color: transparent transparent transparent white;
  494. border-style: solid;
  495. border-width: 15px 0 15px 30px;
  496. }
  497. </style>