artDetail.vue 3.7 KB

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