artDetailTab.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <view class="center">
  3. <!-- <view class="title clamp">{{ item.title }}</view>
  4. <view class="time">{{ item.add_time }}</view> -->
  5. <view class="main" v-for="(ls, index) in item.content" :key="index">
  6. <view v-if="ls.type == 'rich-text'" v-html="ls.value" class="main"></view>
  7. <video v-if="ls.type == 'video' && ls.value" :src="ls.value" style="width:100%;height: 300px" frameborder="0"></video>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import { getArtDetail } from '@/api/index.js'
  13. export default {
  14. data() {
  15. return {
  16. id: '',
  17. item: {
  18. content:[]
  19. }
  20. };
  21. },
  22. onLoad(option) {
  23. this.id = option.id;
  24. this.loadData();
  25. },
  26. onShareAppMessage(options) {
  27. // 设置菜单中的转发按钮触发转发事件时的转发内容
  28. let pages = getCurrentPages(); //获取加载的页面
  29. let currentPage = pages[pages.length - 1]; //获取当前页面的对象
  30. let url = currentPage.route; //当前页面url
  31. let item = currentPage.options; //如果要获取url中所带的参数可以查看options
  32. let shareObj = {
  33. title: '母婴界数字名片', // 默认是小程序的名称(可以写slogan等)
  34. path: url + '?id=' + item.id, // 默认是当前页面,必须是以‘/’开头的完整路径
  35. imageUrl: '',
  36. success: function(res) {
  37. // 转发成功之后的回调
  38. if (res.errMsg == 'shareAppMessage:ok') {}
  39. },
  40. fail: function() {
  41. // 转发失败之后的回调
  42. if (res.errMsg == 'shareAppMessage:fail cancel') {
  43. // 用户取消转发
  44. } else if (res.errMsg == 'shareAppMessage:fail') {
  45. // 转发失败,其中 detail message 为详细失败信息
  46. }
  47. }
  48. };
  49. return shareObj;
  50. },
  51. methods: {
  52. loadData() {
  53. getArtDetail({id:this.id}).then(({ data }) => {
  54. let content = data.list[0]
  55. content.content = content.content.replace(/<img/g, '<img class="rich-img"').replace(/<p>\s*<img/g, '<p class="pHeight"><img').replace(/<div/g, '<div style="max-width: 50% !important;"');
  56. content.content = this.getVideo(content.content);
  57. this.item = content;
  58. uni.setNavigationBarTitle({
  59. title: this.item.title
  60. })
  61. });
  62. },
  63. // 富文本视频解析
  64. getVideo(data) {
  65. let videoList = [];
  66. let videoReg = /<video.*?(?:>|\/>)/gi; //匹配到字符串中的 video 标签
  67. let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配到字符串中的 video 标签 的路径
  68. let arr = data.match(videoReg) || []; // arr 为包含所有video标签的数组
  69. let articleList = data.split('</video>'); // 把字符串 从视频标签分成数组
  70. arr.forEach((item, index) => {
  71. var src = item.match(srcReg);
  72. videoList.push(src[1]); //所要显示的字符串中 所有的video 标签 的路径
  73. });
  74. let needArticleList = [];
  75. articleList.forEach((item, index) => {
  76. if (item != '' && item != undefined) {
  77. // 常见的标签渲染
  78. if((index+1)%2==0){
  79. item = item + '</video>';
  80. }
  81. needArticleList.push({
  82. type: 'rich-text',
  83. value:item
  84. });
  85. }
  86. let articleListLength = articleList.length; // 插入到原有video 标签位置
  87. if (index < articleListLength && videoList[index] != undefined) {
  88. needArticleList.push({
  89. type: 'video',
  90. value: videoList[index]
  91. });
  92. }
  93. });
  94. return needArticleList;
  95. }
  96. }
  97. };
  98. </script>
  99. <style lang="scss">
  100. page {
  101. background-color: #fff;
  102. min-height: 100%;
  103. height: auto;
  104. }
  105. .center {
  106. min-height: 100%;
  107. height: auto;
  108. background: #ffffff;
  109. padding: 30rpx 24rpx 0;
  110. }
  111. .title {
  112. font-size: 32rpx;
  113. font-family: PingFang SC;
  114. font-weight: bold;
  115. color: #333333;
  116. }
  117. .time {
  118. font-size: 24rpx;
  119. font-family: PingFangSC;
  120. font-weight: 500;
  121. color: #999999;
  122. margin-top: 40rpx;
  123. }
  124. .main {
  125. margin-top: 60rpx;
  126. }
  127. /deep/ .main {
  128. .rich-img {
  129. width: 100% !important;
  130. height: auto;
  131. }
  132. * {
  133. max-width: 100% !important;
  134. }
  135. }
  136. </style>