messageInfo.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="center">
  3. <view class="title clamp">{{ item.title }}</view>
  4. <view class="tip flex">
  5. <view class="tip-left">作者:{{ item.author }}</view>
  6. <view class="tip-right">{{ item.add_time }}</view>
  7. </view>
  8. <view class="main" v-for="(ls, index) in item.content" :key="index">
  9. <view v-if="ls.type == 'rich-text'" v-html="ls.value" class="main"></view>
  10. <video v-if="ls.type == 'video' && ls.value" :src="ls.value" style="width:100%;height: 300px" frameborder="0"></video>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import { details } 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(({ data }) => {
  30. console.log(data);
  31. data.content = data.content.replace(/<img/g, '<img class="rich-img"').replace(/<p>\s*<img/g, '<p class="pHeight"><img');
  32. data.content = this.getVideo(data.content);
  33. this.item = data;
  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="less">
  70. .center {
  71. width: 100%;
  72. height: 100%;
  73. background-color: #FFF;
  74. }
  75. .title {
  76. padding: 30rpx 30rpx 0 24rpx;
  77. font-size: 32rpx;
  78. font-family: PingFang SC;
  79. font-weight: bold;
  80. color: #333333;
  81. }
  82. .tip {
  83. padding: 38rpx 32rpx 28rpx 24rpx;
  84. font-size: 24rpx;
  85. font-family: PingFang SC;
  86. font-weight: 500;
  87. color: #666666;
  88. border-bottom: 1px solid #e9e9e9;
  89. }
  90. /deep/ .main {
  91. .rich-img {
  92. width: 100% !important;
  93. height: auto;
  94. }
  95. }
  96. </style>