artDetail.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 { details } from '@/api/user.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. details({}, this.id).then(({ data }) => {
  52. console.log(data);
  53. data.content = data.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. data.content = this.getVideo(data.content);
  55. this.item = data;
  56. });
  57. },
  58. // 富文本视频解析
  59. getVideo(data) {
  60. let videoList = [];
  61. let videoReg = /<video.*?(?:>|\/>)/gi; //匹配到字符串中的 video 标签
  62. let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配到字符串中的 video 标签 的路径
  63. let arr = data.match(videoReg) || []; // arr 为包含所有video标签的数组
  64. let articleList = data.split('</video>'); // 把字符串 从视频标签分成数组
  65. arr.forEach((item, index) => {
  66. var src = item.match(srcReg);
  67. videoList.push(src[1]); //所要显示的字符串中 所有的video 标签 的路径
  68. });
  69. let needArticleList = [];
  70. articleList.forEach((item, index) => {
  71. if (item != '' && item != undefined) {
  72. // 常见的标签渲染
  73. needArticleList.push({
  74. type: 'rich-text',
  75. value: item + '</video>'
  76. });
  77. }
  78. let articleListLength = articleList.length; // 插入到原有video 标签位置
  79. if (index < articleListLength && videoList[index] != undefined) {
  80. needArticleList.push({
  81. type: 'video',
  82. value: videoList[index]
  83. });
  84. }
  85. });
  86. return needArticleList;
  87. }
  88. }
  89. };
  90. </script>
  91. <style lang="scss">
  92. page {
  93. background-color: #fff;
  94. min-height: 100%;
  95. height: auto;
  96. }
  97. .center {
  98. min-height: 100%;
  99. height: auto;
  100. background: #ffffff;
  101. padding: 10rpx 24rpx 0;
  102. }
  103. .title {
  104. font-size: 32rpx;
  105. font-family: PingFang SC;
  106. font-weight: bold;
  107. color: #333333;
  108. }
  109. .time {
  110. font-size: 24rpx;
  111. font-family: PingFangSC;
  112. font-weight: 500;
  113. color: #999999;
  114. margin-top: 40rpx;
  115. }
  116. .main {
  117. // margin-top: 60rpx;
  118. }
  119. /deep/ .main {
  120. .rich-img {
  121. width: 100% !important;
  122. height: auto;
  123. }
  124. * {
  125. max-width: 100% !important;
  126. }
  127. }
  128. </style>