detail.vue 2.4 KB

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