artDetail.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // onShareAppMessage(options) {
  25. // // 设置菜单中的转发按钮触发转发事件时的转发内容
  26. // let pages = getCurrentPages(); //获取加载的页面
  27. // let currentPage = pages[pages.length - 1]; //获取当前页面的对象
  28. // let url = currentPage.route; //当前页面url
  29. // let item = currentPage.options; //如果要获取url中所带的参数可以查看options
  30. // let shareObj = {
  31. // title: '华景美业', // 默认是小程序的名称(可以写slogan等)
  32. // path: url + '?id=' + item.id, // 默认是当前页面,必须是以‘/’开头的完整路径
  33. // imageUrl: '',
  34. // success: function(res) {
  35. // // 转发成功之后的回调
  36. // if (res.errMsg == 'shareAppMessage:ok') {
  37. // }
  38. // },
  39. // fail: function() {
  40. // // 转发失败之后的回调
  41. // if (res.errMsg == 'shareAppMessage:fail cancel') {
  42. // // 用户取消转发
  43. // } else if (res.errMsg == 'shareAppMessage:fail') {
  44. // // 转发失败,其中 detail message 为详细失败信息
  45. // }
  46. // }
  47. // };
  48. // return shareObj;
  49. // },
  50. methods: {
  51. loadData() {
  52. details({}, this.id).then(({ data }) => {
  53. console.log(data);
  54. data.content = data.content
  55. .replace(/<img/g, '<img class="rich-img"')
  56. .replace(/<p>\s*<img/g, '<p class="pHeight"><img')
  57. .replace(/<div/g, '<div style="max-width: 50% !important;"');
  58. data.content = this.getVideo(data.content);
  59. this.item = data;
  60. });
  61. },
  62. // 富文本视频解析
  63. getVideo(data) {
  64. let videoList = [];
  65. let videoReg = /<video.*?(?:>|\/>)/gi; //匹配到字符串中的 video 标签
  66. let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配到字符串中的 video 标签 的路径
  67. let arr = data.match(videoReg) || []; // arr 为包含所有video标签的数组
  68. let articleList = data.split('</video>'); // 把字符串 从视频标签分成数组
  69. arr.forEach((item, index) => {
  70. var src = item.match(srcReg);
  71. videoList.push(src[1]); //所要显示的字符串中 所有的video 标签 的路径
  72. });
  73. let needArticleList = [];
  74. articleList.forEach((item, index) => {
  75. if (item != '' && item != undefined) {
  76. // 常见的标签渲染
  77. needArticleList.push({
  78. type: 'rich-text',
  79. value: item + '</video>'
  80. });
  81. }
  82. let articleListLength = articleList.length; // 插入到原有video 标签位置
  83. if (index < articleListLength && videoList[index] != undefined) {
  84. needArticleList.push({
  85. type: 'video',
  86. value: videoList[index]
  87. });
  88. }
  89. });
  90. return needArticleList;
  91. }
  92. }
  93. };
  94. </script>
  95. <style lang="scss">
  96. page {
  97. background-color: #fff;
  98. min-height: 100%;
  99. height: auto;
  100. }
  101. .center {
  102. min-height: 100%;
  103. height: auto;
  104. background: #ffffff;
  105. padding: 10rpx 24rpx 0;
  106. }
  107. .title {
  108. font-size: 32rpx;
  109. font-family: PingFang SC;
  110. font-weight: bold;
  111. color: #333333;
  112. }
  113. .time {
  114. font-size: 24rpx;
  115. font-family: PingFangSC;
  116. font-weight: 500;
  117. color: #999999;
  118. margin-top: 40rpx;
  119. }
  120. .main {
  121. // margin-top: 60rpx;
  122. }
  123. /deep/ .main {
  124. .rich-img {
  125. width: 100% !important;
  126. height: auto;
  127. }
  128. * {
  129. max-width: 100% !important;
  130. }
  131. }
  132. </style>