artDetail.vue 2.8 KB

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