product.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <view class="center">
  3. <view class="main" v-if="art != ''" v-for="(ls, index) in art.content" :key="index">
  4. <view v-if="ls.type == 'rich-text'" v-html="ls.value" class="main-item"></view>
  5. <view style="width:100%;height: 300px;" v-if="ls.type == 'video' && ls.value">
  6. <view v-if="ls.jd == 1" style="width:100%;height: 300px; background: black;" @click="djbf(ls)">
  7. <image style="width:100%;height: 300px;" src="../../static/img/spfm.png" mode=""></image>
  8. </view>
  9. <video :src="ls.value" autoplay v-if="ls.jd ==2" style="width:100%;height: 300px;">
  10. </video>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import {
  17. details
  18. } from '@/api/user.js';
  19. export default {
  20. data() {
  21. return {
  22. id: '',
  23. art: '',
  24. };
  25. },
  26. onLoad(option) {
  27. this.id = option.id;
  28. this.loadData();
  29. },
  30. methods: {
  31. loadData() {
  32. details({}, this.id).then(({
  33. data
  34. }) => {
  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.art = data;
  39. console.log(this.art, 'nierong');
  40. });
  41. },
  42. djbf(opt) {
  43. console.log(opt, '2222');
  44. if (opt.jd == 1) {
  45. opt.jd = 2
  46. }
  47. if (opt.jd == 2) {
  48. opt.jd = 1
  49. }
  50. },
  51. // 富文本视频解析
  52. getVideo(data) {
  53. console.log(data, '源数据')
  54. let videoList = [];
  55. let videoReg = /<video.*?(?:>|\/>)/gi; //匹配到字符串中的 video 标签
  56. let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配到字符串中的 video 标签 的路径
  57. let arr = data.match(videoReg) || []; // arr 为包含所有video标签的数组
  58. let articleList = data.split('</video>'); // 把字符串 从视频标签分成数组
  59. arr.forEach((item, index) => {
  60. var src = item.match(srcReg);
  61. videoList.push(src[1]); //所要显示的字符串中 所有的video 标签 的路径
  62. });
  63. let needArticleList = [];
  64. articleList.forEach((item, index) => {
  65. if (item != '' && item != undefined) {
  66. let wuvideo = item.split('<video')
  67. let js = wuvideo[0].replace('<video', '')
  68. // 常见的标签渲染
  69. needArticleList.push({
  70. type: 'rich-text',
  71. value: js
  72. });
  73. }
  74. let articleListLength = articleList.length; // 插入到原有video 标签位置
  75. if (index < articleListLength && videoList[index] != undefined) {
  76. needArticleList.push({
  77. type: 'video',
  78. jd: 1,
  79. value: videoList[index]
  80. });
  81. }
  82. });
  83. console.log(needArticleList, '完成');
  84. return needArticleList;
  85. }
  86. }
  87. };
  88. </script>
  89. <style lang="less">
  90. .center {
  91. width: 100%;
  92. height: 100%;
  93. }
  94. .main {
  95. padding: 10rpx;
  96. display: flex;
  97. flex-direction: column;
  98. }
  99. /deep/ .main {
  100. .rich-img {
  101. width: 100% !important;
  102. height: auto;
  103. }
  104. }
  105. </style>