artDetail.vue 2.7 KB

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