artDetail.vue 3.7 KB

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