artDetail.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/user.js';
  16. export default {
  17. data() {
  18. return {
  19. id: '',
  20. item: '',
  21. type:'',
  22. };
  23. },
  24. onLoad(option) {
  25. this.id = option.id;
  26. this.type = option.type
  27. this.loadData();
  28. },
  29. methods: {
  30. loadData() {
  31. details({
  32. fl:this.type
  33. }, this.id).then(({
  34. data
  35. }) => {
  36. console.log(data);
  37. data.content = data.content.replace(/<img/g, '<img class="rich-img"').replace(/<p>\s*<img/g,
  38. '<p class="pHeight"><img');
  39. data.content = this.getVideo(data.content);
  40. this.item = data;
  41. });
  42. },
  43. // 富文本视频解析
  44. getVideo(data) {
  45. let videoList = [];
  46. let videoReg = /<video.*?(?:>|\/>)/gi; //匹配到字符串中的 video 标签
  47. let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配到字符串中的 video 标签 的路径
  48. let arr = data.match(videoReg) || []; // arr 为包含所有video标签的数组
  49. let articleList = data.split('</video>'); // 把字符串 从视频标签分成数组
  50. arr.forEach((item, index) => {
  51. var src = item.match(srcReg);
  52. videoList.push(src[1]); //所要显示的字符串中 所有的video 标签 的路径
  53. });
  54. let needArticleList = [];
  55. articleList.forEach((item, index) => {
  56. if (item != '' && item != undefined) {
  57. // 常见的标签渲染
  58. needArticleList.push({
  59. type: 'rich-text',
  60. value: item + '</video>'
  61. });
  62. }
  63. let articleListLength = articleList.length; // 插入到原有video 标签位置
  64. if (index < articleListLength && videoList[index] != undefined) {
  65. needArticleList.push({
  66. type: 'video',
  67. value: videoList[index]
  68. });
  69. }
  70. });
  71. return needArticleList;
  72. }
  73. }
  74. };
  75. </script>
  76. <style lang="scss">
  77. page {
  78. min-height: 100%;
  79. height: 0;
  80. }
  81. .center {
  82. min-height: 100%;
  83. height: auto;
  84. background: #ffffff;
  85. padding: 30rpx 24rpx 0;
  86. }
  87. .title {
  88. font-size: 32rpx;
  89. font-family: PingFang SC;
  90. font-weight: bold;
  91. color: #333333;
  92. text-align: center;
  93. }
  94. .time {
  95. font-size: 24rpx;
  96. font-family: PingFangSC;
  97. font-weight: 500;
  98. color: #999999;
  99. margin-top: 40rpx;
  100. }
  101. .main {
  102. margin-top: 60rpx;
  103. }
  104. /deep/ .main {
  105. .rich-img {
  106. width: 100% !important;
  107. height: auto;
  108. }
  109. }
  110. </style>