artDetail.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="center">
  3. <view class="tip flex">
  4. <view class="tip-left">作者:{{ item.author }}</view>
  5. <view class="tip-right">{{ item.add_time }}</view>
  6. </view>
  7. <view class="main" v-for="(ls, index) in item.content" :key="index">
  8. <view v-if="ls.type == 'rich-text'" v-html="ls.value" class="main"></view>
  9. <video v-if="ls.type == 'video' && ls.value" :src="ls.value" style="width:100%;height: 300px"
  10. frameborder="0"></video>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import {
  16. details
  17. } from '@/api/user.js';
  18. export default {
  19. data() {
  20. return {
  21. id: '',
  22. item: ''
  23. };
  24. },
  25. onLoad(option) {
  26. this.id = option.id;
  27. this.loadData();
  28. },
  29. methods: {
  30. loadData() {
  31. details({}, this.id).then(({
  32. data
  33. }) => {
  34. console.log(data);
  35. data.content = data.content.replace(/<img/g, '<img class="rich-img"').replace(/<p>\s*<img/g,
  36. '<p class="pHeight"><img');
  37. data.content = this.getVideo(data.content);
  38. this.item = data;
  39. });
  40. },
  41. // 富文本视频解析
  42. getVideo(data) {
  43. let videoList = [];
  44. let videoReg = /<video.*?(?:>|\/>)/gi; //匹配到字符串中的 video 标签
  45. let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配到字符串中的 video 标签 的路径
  46. let arr = data.match(videoReg) || []; // arr 为包含所有video标签的数组
  47. let articleList = data.split('</video>'); // 把字符串 从视频标签分成数组
  48. arr.forEach((item, index) => {
  49. var src = item.match(srcReg);
  50. videoList.push(src[1]); //所要显示的字符串中 所有的video 标签 的路径
  51. });
  52. let needArticleList = [];
  53. articleList.forEach((item, index) => {
  54. if (item != '' && item != undefined) {
  55. // 常见的标签渲染
  56. needArticleList.push({
  57. type: 'rich-text',
  58. value: item + '</video>'
  59. });
  60. }
  61. let articleListLength = articleList.length; // 插入到原有video 标签位置
  62. if (index < articleListLength && videoList[index] != undefined) {
  63. needArticleList.push({
  64. type: 'video',
  65. value: videoList[index]
  66. });
  67. }
  68. });
  69. return needArticleList;
  70. }
  71. }
  72. };
  73. </script>
  74. <style lang="less">
  75. .center {
  76. width: 100%;
  77. height: 100%;
  78. }
  79. .vheigh {
  80. height: var(--status-bar-height);
  81. }
  82. .title {
  83. padding: 30rpx 30rpx 0 24rpx;
  84. font-size: 32rpx;
  85. font-family: PingFang SC;
  86. font-weight: bold;
  87. color: #333333;
  88. }
  89. .tip {
  90. padding: 38rpx 32rpx 28rpx 24rpx;
  91. font-size: 24rpx;
  92. font-family: PingFang SC;
  93. font-weight: 500;
  94. color: #666666;
  95. border-bottom: 1px solid #e9e9e9;
  96. }
  97. .main {
  98. padding: 10rpx;
  99. }
  100. /deep/ .main {
  101. .rich-img {
  102. width: 100% !important;
  103. height: auto;
  104. }
  105. }
  106. </style>