detail.vue 2.4 KB

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