node.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <template>
  2. <view :id="attrs.id" :class="'_block _'+name+' '+attrs.class" :style="attrs.style">
  3. <block v-for="(n, i) in childs" v-bind:key="i">
  4. <!-- 图片 -->
  5. <!-- 占位图 -->
  6. <image v-if="n.name==='img'&&!n.t&&((opts[1]&&!ctrl[i])||ctrl[i]<0)" class="_img" :style="n.attrs.style" :src="ctrl[i]<0?opts[2]:opts[1]" mode="widthFix" />
  7. <!-- 显示图片 -->
  8. <!-- #ifdef H5 || (APP-PLUS && VUE2) -->
  9. <img v-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+n.attrs.style" :src="n.attrs.src||(ctrl.load?n.attrs['data-src']:'')" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  10. <!-- #endif -->
  11. <!-- #ifndef H5 || (APP-PLUS && VUE2) -->
  12. <!-- 表格中的图片,使用 rich-text 防止大小不正确 -->
  13. <rich-text v-if="n.name==='img'&&n.t" :style="'display:'+n.t" :nodes="'<img class=\'_img\' style=\''+n.attrs.style+'\' src=\''+n.attrs.src+'\'>'" :data-i="i" @tap.stop="imgTap" />
  14. <!-- #endif -->
  15. <!-- #ifndef H5 || APP-PLUS -->
  16. <image v-else-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+'width:'+(ctrl[i]||1)+'px;height:1px;'+n.attrs.style" :src="n.attrs.src" :mode="!n.h?'widthFix':(!n.w?'heightFix':'')" :lazy-load="opts[0]" :webp="n.webp" :show-menu-by-longpress="opts[3]&&!n.attrs.ignore" :image-menu-prevent="!opts[3]||n.attrs.ignore" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  17. <!-- #endif -->
  18. <!-- #ifdef APP-PLUS && VUE3 -->
  19. <image v-else-if="n.name==='img'" :id="n.attrs.id" :class="'_img '+n.attrs.class" :style="(ctrl[i]===-1?'display:none;':'')+'width:'+(ctrl[i]||1)+'px;'+n.attrs.style" :src="n.attrs.src||(ctrl.load?n.attrs['data-src']:'')" :mode="!n.h?'widthFix':(!n.w?'heightFix':'')" :data-i="i" @load="imgLoad" @error="mediaError" @tap.stop="imgTap" @longpress="imgLongTap" />
  20. <!-- #endif -->
  21. <!-- 文本 -->
  22. <!-- #ifdef MP-WEIXIN -->
  23. <text v-else-if="n.text" :user-select="opts[4]=='force'&&isiOS" decode>{{n.text}}</text>
  24. <!-- #endif -->
  25. <!-- #ifndef MP-WEIXIN || MP-BAIDU || MP-ALIPAY || MP-TOUTIAO -->
  26. <text v-else-if="n.text" decode>{{n.text}}</text>
  27. <!-- #endif -->
  28. <text v-else-if="n.name==='br'">\n</text>
  29. <!-- 链接 -->
  30. <view v-else-if="n.name==='a'" :id="n.attrs.id" :class="(n.attrs.href?'_a ':'')+n.attrs.class" hover-class="_hover" :style="'display:inline;'+n.attrs.style" :data-i="i" @tap.stop="linkTap">
  31. <node name="span" :childs="n.children" :opts="opts" style="display:inherit" />
  32. </view>
  33. <!-- 视频 -->
  34. <!-- #ifdef APP-PLUS -->
  35. <view v-else-if="n.html" :id="n.attrs.id" :class="'_video '+n.attrs.class" :style="n.attrs.style" v-html="n.html" @vplay.stop="play" />
  36. <!-- #endif -->
  37. <!-- #ifndef APP-PLUS -->
  38. <su-video v-else-if="n.name==='video'" :id="n.attrs.id" :class="n.attrs.class" style="width: 10%;" :style="n.attrs.style" :autoplay="n.attrs.autoplay" :controls="n.attrs.controls" :loop="n.attrs.loop" :muted="n.attrs.muted" :object-fit="n.attrs['object-fit']" :poster="n.attrs.poster" :src="n.src[ctrl[i]||0]" :data-i="i" @play="play" @error="mediaError" />
  39. <!-- <video v-else-if="n.name==='video'" :id="n.attrs.id" :class="n.attrs.class" style="width: 10%;" :style="n.attrs.style" :autoplay="n.attrs.autoplay" :controls="n.attrs.controls" :loop="n.attrs.loop" :muted="n.attrs.muted" :object-fit="n.attrs['object-fit']" :poster="n.attrs.poster" :src="n.src[ctrl[i]||0]" :data-i="i" @play="play" @error="mediaError" /> -->
  40. <!-- #endif -->
  41. <!-- #ifdef H5 || APP-PLUS -->
  42. <iframe v-else-if="n.name==='iframe'" :style="n.attrs.style" :allowfullscreen="n.attrs.allowfullscreen" :frameborder="n.attrs.frameborder" :src="n.attrs.src" />
  43. <embed v-else-if="n.name==='embed'" :style="n.attrs.style" :src="n.attrs.src" />
  44. <!-- #endif -->
  45. <!-- #ifndef MP-TOUTIAO || ((H5 || APP-PLUS) && VUE3) -->
  46. <!-- 音频 -->
  47. <audio v-else-if="n.name==='audio'" :id="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :author="n.attrs.author" :controls="n.attrs.controls" :loop="n.attrs.loop" :name="n.attrs.name" :poster="n.attrs.poster" :src="n.src[ctrl[i]||0]" :data-i="i" @play="play" @error="mediaError" />
  48. <!-- #endif -->
  49. <view v-else-if="(n.name==='table'&&n.c)||n.name==='li'" :id="n.attrs.id" :class="'_'+n.name+' '+n.attrs.class" :style="n.attrs.style">
  50. <node v-if="n.name==='li'" :childs="n.children" :opts="opts" />
  51. <view v-else v-for="(tbody, x) in n.children" v-bind:key="x" :class="'_'+tbody.name+' '+tbody.attrs.class" :style="tbody.attrs.style">
  52. <node v-if="tbody.name==='td'||tbody.name==='th'" :childs="tbody.children" :opts="opts" />
  53. <block v-else v-for="(tr, y) in tbody.children" v-bind:key="y">
  54. <view v-if="tr.name==='td'||tr.name==='th'" :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  55. <node :childs="tr.children" :opts="opts" />
  56. </view>
  57. <view v-else :class="'_'+tr.name+' '+tr.attrs.class" :style="tr.attrs.style">
  58. <view v-for="(td, z) in tr.children" v-bind:key="z" :class="'_'+td.name+' '+td.attrs.class" :style="td.attrs.style">
  59. <node :childs="td.children" :opts="opts" />
  60. </view>
  61. </view>
  62. </block>
  63. </view>
  64. </view>
  65. <!-- 富文本 -->
  66. <!-- #ifdef H5 || ((MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE2) -->
  67. <rich-text v-else-if="!n.c&&!handler.isInline(n.name, n.attrs.style)" :id="n.attrs.id" :style="n.f" :user-select="opts[4]" :nodes="[n]" />
  68. <!-- #endif -->
  69. <!-- #ifndef H5 || ((MP-WEIXIN || MP-QQ || APP-PLUS || MP-360) && VUE2) -->
  70. <rich-text v-else-if="!n.c" :id="n.attrs.id" :style="n.f+';display:inline'" :preview="false" :selectable="opts[4]" :user-select="opts[4]" :nodes="[n]" />
  71. <!-- #endif -->
  72. <!-- 继续递归 -->
  73. <view v-else-if="n.c===2" :id="n.attrs.id" :class="'_block _'+n.name+' '+n.attrs.class" :style="n.f+';'+n.attrs.style">
  74. <node v-for="(n2, j) in n.children" v-bind:key="j" :style="n2.f" :name="n2.name" :attrs="n2.attrs" :childs="n2.children" :opts="opts" />
  75. </view>
  76. <node v-else :style="n.f" :name="n.name" :attrs="n.attrs" :childs="n.children" :opts="opts" />
  77. </block>
  78. </view>
  79. </template>
  80. <script module="handler" lang="wxs">
  81. // 行内标签列表
  82. var inlineTags = {
  83. abbr: true,
  84. b: true,
  85. big: true,
  86. code: true,
  87. del: true,
  88. em: true,
  89. i: true,
  90. ins: true,
  91. label: true,
  92. q: true,
  93. small: true,
  94. span: true,
  95. strong: true,
  96. sub: true,
  97. sup: true
  98. }
  99. /**
  100. * @description 判断是否为行内标签
  101. */
  102. module.exports = {
  103. isInline: function (tagName, style) {
  104. return inlineTags[tagName] || (style || '').indexOf('display:inline') !== -1
  105. }
  106. }
  107. </script>
  108. <script>
  109. import node from './node'
  110. export default {
  111. name: 'node',
  112. options: {
  113. // #ifdef MP-WEIXIN
  114. virtualHost: true,
  115. // #endif
  116. // #ifdef MP-TOUTIAO
  117. addGlobalClass: false
  118. // #endif
  119. },
  120. data () {
  121. return {
  122. ctrl: {},
  123. // #ifdef MP-WEIXIN
  124. isiOS: uni.getSystemInfoSync().system.includes('iOS')
  125. // #endif
  126. }
  127. },
  128. props: {
  129. name: String,
  130. attrs: {
  131. type: Object,
  132. default () {
  133. return {}
  134. }
  135. },
  136. childs: Array,
  137. opts: Array
  138. },
  139. components: {
  140. // #ifndef (H5 || APP-PLUS) && VUE3
  141. node
  142. // #endif
  143. },
  144. mounted () {
  145. this.$nextTick(() => {
  146. for (this.root = this.$parent; this.root.$options.name !== 'mp-html'; this.root = this.root.$parent);
  147. })
  148. // #ifdef H5 || APP-PLUS
  149. if (this.opts[0]) {
  150. let i
  151. for (i = this.childs.length; i--;) {
  152. if (this.childs[i].name === 'img') break
  153. }
  154. if (i !== -1) {
  155. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  156. top: 500,
  157. bottom: 500
  158. })
  159. this.observer.observe('._img', res => {
  160. if (res.intersectionRatio) {
  161. this.$set(this.ctrl, 'load', 1)
  162. this.observer.disconnect()
  163. }
  164. })
  165. }
  166. }
  167. // #endif
  168. },
  169. beforeDestroy () {
  170. // #ifdef H5 || APP-PLUS
  171. if (this.observer) {
  172. this.observer.disconnect()
  173. }
  174. // #endif
  175. },
  176. methods:{
  177. // #ifdef MP-WEIXIN
  178. toJSON () { return this },
  179. // #endif
  180. /**
  181. * @description 播放视频事件
  182. * @param {Event} e
  183. */
  184. play (e) {
  185. this.root.$emit('play')
  186. // #ifndef APP-PLUS
  187. if (this.root.pauseVideo) {
  188. let flag = false
  189. const id = e.target.id
  190. for (let i = this.root._videos.length; i--;) {
  191. if (this.root._videos[i].id === id) {
  192. flag = true
  193. } else {
  194. this.root._videos[i].pause() // 自动暂停其他视频
  195. }
  196. }
  197. // 将自己加入列表
  198. if (!flag) {
  199. const ctx = uni.createVideoContext(id
  200. // #ifndef MP-BAIDU
  201. , this
  202. // #endif
  203. )
  204. ctx.id = id
  205. if (this.root.playbackRate) {
  206. ctx.playbackRate(this.root.playbackRate)
  207. }
  208. this.root._videos.push(ctx)
  209. }
  210. }
  211. // #endif
  212. },
  213. /**
  214. * @description 图片点击事件
  215. * @param {Event} e
  216. */
  217. imgTap (e) {
  218. const node = this.childs[e.currentTarget.dataset.i]
  219. console.log(node.attrs.src);
  220. console.log(node.attrs['data-href']);
  221. if(node.attrs.src != node.attrs['data-href']){
  222. uni.navigateTo({
  223. url: node.attrs['data-href'],
  224. fail() {
  225. // 如果普通页面跳转失败,尝试switchTab
  226. uni.switchTab({
  227. url: node.attrs['data-href'],
  228. fail() {
  229. // 如果是外部链接,可以使用系统浏览器打开
  230. if(node.attrs['data-href'].includes('http')) {
  231. // #ifdef H5
  232. location.href = node.attrs['data-href']
  233. // #endif
  234. // #ifdef APP-PLUS
  235. plus.runtime.openWeb(node.attrs['data-href'])
  236. // #endif
  237. // #ifdef MP
  238. uni.setClipboardData({
  239. data: node.attrs['data-href'],
  240. success: () => {
  241. uni.showToast({
  242. title: '链接已复制'
  243. })
  244. }
  245. })
  246. // #endif
  247. }
  248. }
  249. })
  250. }
  251. })
  252. return // 跳转后不执行后续预览逻辑
  253. }
  254. if (node.a) {
  255. this.linkTap(node.a)
  256. return
  257. }
  258. if (node.attrs.ignore) return
  259. // #ifdef H5 || APP-PLUS
  260. node.attrs.src = node.attrs.src || node.attrs['data-src']
  261. // #endif
  262. this.root.$emit('imgtap', node.attrs)
  263. // 自动预览图片
  264. if (this.root.previewImg) {
  265. uni.previewImage({
  266. // #ifdef MP-WEIXIN
  267. showmenu: this.root.showImgMenu,
  268. // #endif
  269. // #ifdef MP-ALIPAY
  270. enablesavephoto: this.root.showImgMenu,
  271. enableShowPhotoDownload: this.root.showImgMenu,
  272. // #endif
  273. current: parseInt(node.attrs.i),
  274. urls: this.root.imgList
  275. })
  276. }
  277. },
  278. /**
  279. * @description 图片长按
  280. */
  281. imgLongTap (e) {
  282. // #ifdef APP-PLUS
  283. const attrs = this.childs[e.currentTarget.dataset.i].attrs
  284. if (this.opts[3] && !attrs.ignore) {
  285. uni.showActionSheet({
  286. itemList: ['保存图片'],
  287. success: () => {
  288. const save = path => {
  289. uni.saveImageToPhotosAlbum({
  290. filePath: path,
  291. success () {
  292. uni.showToast({
  293. title: '保存成功'
  294. })
  295. }
  296. })
  297. }
  298. if (this.root.imgList[attrs.i].startsWith('http')) {
  299. uni.downloadFile({
  300. url: this.root.imgList[attrs.i],
  301. success: res => save(res.tempFilePath)
  302. })
  303. } else {
  304. save(this.root.imgList[attrs.i])
  305. }
  306. }
  307. })
  308. }
  309. // #endif
  310. },
  311. /**
  312. * @description 图片加载完成事件
  313. * @param {Event} e
  314. */
  315. imgLoad (e) {
  316. const i = e.currentTarget.dataset.i
  317. /* #ifndef H5 || (APP-PLUS && VUE2) */
  318. if (!this.childs[i].w) {
  319. // 设置原宽度
  320. this.$set(this.ctrl, i, e.detail.width)
  321. } else /* #endif */ if ((this.opts[1] && !this.ctrl[i]) || this.ctrl[i] === -1) {
  322. // 加载完毕,取消加载中占位图
  323. this.$set(this.ctrl, i, 1)
  324. }
  325. this.checkReady()
  326. },
  327. /**
  328. * @description 检查是否所有图片加载完毕
  329. */
  330. checkReady () {
  331. if (!this.root.lazyLoad) {
  332. this.root._unloadimgs -= 1
  333. if (!this.root._unloadimgs) {
  334. setTimeout(() => {
  335. this.root.getRect().then(rect => {
  336. this.root.$emit('ready', rect)
  337. }).catch(() => {
  338. this.root.$emit('ready', {})
  339. })
  340. }, 350)
  341. }
  342. }
  343. },
  344. /**
  345. * @description 链接点击事件
  346. * @param {Event} e
  347. */
  348. linkTap (e) {
  349. const node = e.currentTarget ? this.childs[e.currentTarget.dataset.i] : {}
  350. const attrs = node.attrs || e
  351. const href = attrs.href
  352. this.root.$emit('linktap', Object.assign({
  353. innerText: this.root.getText(node.children || []) // 链接内的文本内容
  354. }, attrs))
  355. if (href) {
  356. if (href[0] === '#') {
  357. // 跳转锚点
  358. this.root.navigateTo(href.substring(1)).catch(() => { })
  359. } else if (href.split('?')[0].includes('://')) {
  360. // 复制外部链接
  361. if (this.root.copyLink) {
  362. // #ifdef H5
  363. window.open(href)
  364. // #endif
  365. // #ifdef MP
  366. uni.setClipboardData({
  367. data: href,
  368. success: () =>
  369. uni.showToast({
  370. title: '链接已复制'
  371. })
  372. })
  373. // #endif
  374. // #ifdef APP-PLUS
  375. plus.runtime.openWeb(href)
  376. // #endif
  377. }
  378. } else {
  379. // 跳转页面
  380. uni.navigateTo({
  381. url: href,
  382. fail () {
  383. uni.switchTab({
  384. url: href,
  385. fail () { }
  386. })
  387. }
  388. })
  389. }
  390. }
  391. },
  392. /**
  393. * @description 错误事件
  394. * @param {Event} e
  395. */
  396. mediaError (e) {
  397. const i = e.currentTarget.dataset.i
  398. const node = this.childs[i]
  399. // 加载其他源
  400. if (node.name === 'video' || node.name === 'audio') {
  401. let index = (this.ctrl[i] || 0) + 1
  402. if (index > node.src.length) {
  403. index = 0
  404. }
  405. if (index < node.src.length) {
  406. this.$set(this.ctrl, i, index)
  407. return
  408. }
  409. } else if (node.name === 'img') {
  410. // #ifdef H5 && VUE3
  411. if (this.opts[0] && !this.ctrl.load) return
  412. // #endif
  413. // 显示错误占位图
  414. if (this.opts[2]) {
  415. this.$set(this.ctrl, i, -1)
  416. }
  417. this.checkReady()
  418. }
  419. if (this.root) {
  420. this.root.$emit('error', {
  421. source: node.name,
  422. attrs: node.attrs,
  423. // #ifndef H5 && VUE3
  424. errMsg: e.detail.errMsg
  425. // #endif
  426. })
  427. }
  428. }
  429. }
  430. }
  431. </script>
  432. <style>
  433. /* a 标签默认效果 */
  434. ._a {
  435. padding: 1.5px 0 1.5px 0;
  436. color: #366092;
  437. word-break: break-all;
  438. }
  439. /* a 标签点击态效果 */
  440. ._hover {
  441. text-decoration: underline;
  442. opacity: 0.7;
  443. }
  444. /* 图片默认效果 */
  445. ._img {
  446. max-width: 100%;
  447. -webkit-touch-callout: none;
  448. }
  449. /* 内部样式 */
  450. ._block {
  451. display: block;
  452. }
  453. ._b,
  454. ._strong {
  455. font-weight: bold;
  456. }
  457. ._code {
  458. font-family: monospace;
  459. }
  460. ._del {
  461. text-decoration: line-through;
  462. }
  463. ._em,
  464. ._i {
  465. font-style: italic;
  466. }
  467. ._h1 {
  468. font-size: 2em;
  469. }
  470. ._h2 {
  471. font-size: 1.5em;
  472. }
  473. ._h3 {
  474. font-size: 1.17em;
  475. }
  476. ._h5 {
  477. font-size: 0.83em;
  478. }
  479. ._h6 {
  480. font-size: 0.67em;
  481. }
  482. ._h1,
  483. ._h2,
  484. ._h3,
  485. ._h4,
  486. ._h5,
  487. ._h6 {
  488. display: block;
  489. font-weight: bold;
  490. }
  491. ._image {
  492. height: 1px;
  493. }
  494. ._ins {
  495. text-decoration: underline;
  496. }
  497. ._li {
  498. display: list-item;
  499. }
  500. ._ol {
  501. list-style-type: decimal;
  502. }
  503. ._ol,
  504. ._ul {
  505. display: block;
  506. padding-left: 40px;
  507. margin: 1em 0;
  508. }
  509. ._q::before {
  510. content: '"';
  511. }
  512. ._q::after {
  513. content: '"';
  514. }
  515. ._sub {
  516. font-size: smaller;
  517. vertical-align: sub;
  518. }
  519. ._sup {
  520. font-size: smaller;
  521. vertical-align: super;
  522. }
  523. ._thead,
  524. ._tbody,
  525. ._tfoot {
  526. display: table-row-group;
  527. }
  528. ._tr {
  529. display: table-row;
  530. }
  531. ._td,
  532. ._th {
  533. display: table-cell;
  534. vertical-align: middle;
  535. }
  536. ._th {
  537. font-weight: bold;
  538. text-align: center;
  539. }
  540. ._ul {
  541. list-style-type: disc;
  542. }
  543. ._ul ._ul {
  544. margin: 0;
  545. list-style-type: circle;
  546. }
  547. ._ul ._ul ._ul {
  548. list-style-type: square;
  549. }
  550. ._abbr,
  551. ._b,
  552. ._code,
  553. ._del,
  554. ._em,
  555. ._i,
  556. ._ins,
  557. ._label,
  558. ._q,
  559. ._span,
  560. ._strong,
  561. ._sub,
  562. ._sup {
  563. display: inline;
  564. }
  565. /* #ifdef APP-PLUS */
  566. ._video {
  567. width: 300px;
  568. height: 225px;
  569. }
  570. /* #endif */
  571. </style>