index.vue 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view class="content-info-container">
  3. <view class="title-container">
  4. <view class="title-content-text">{{ title }}</view>
  5. <view class="title-content-date">{{ date }}</view>
  6. </view>
  7. <mp-html :content="content|formatRichText" selectable="true" show-img-menu="true" />
  8. </view>
  9. </template>
  10. <script>
  11. import { fetchContentDetail } from '@/common/api';
  12. export default {
  13. name: 'content',
  14. props: {},
  15. data() {
  16. return {
  17. content: '',
  18. queryParams: {},
  19. title: '',
  20. date: '',
  21. };
  22. },
  23. created() {
  24. },
  25. onLoad(res) {
  26. console.log(res);
  27. this.queryParams = res;
  28. this.getContentInfo();
  29. },
  30. filters: {
  31. /**
  32. * 处理富文本里的图片宽度自适应
  33. * 1.去掉img标签里的style、width、height属性
  34. * 2.img标签添加style属性:max-width:100%;height:auto
  35. * 3.修改所有style里的width属性为max-width:100%
  36. * 4.去掉<br/>标签
  37. * @param html
  38. * @returns {void|string|*}
  39. *
  40. */
  41. formatRichText(html) { //控制小程序中图片大小
  42. let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
  43. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  44. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  45. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  46. return match;
  47. });
  48. newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
  49. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  50. 'max-width:100%;');
  51. return match;
  52. });
  53. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  54. newContent = newContent.replace(/<img src="\/dev-api/gi,
  55. `<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;" src="${process.env.UNI_FILE_PREFIX}`);
  56. newContent = newContent.replace(/<iframe(([\s\S])*?)<\/iframe>/ig, function (match, capture) {
  57. match = match.replace('iframe', 'video');
  58. match = match.replace(/<video/gi,
  59. '<video style="width:100%;height:auto;display:inline-block;margin:10rpx auto;"')
  60. .replace(/style='[^']+'/gi, 'style=\'max-width:100%;height:auto;display:inline-block;margin:10rpx auto;\'');
  61. match = match.replace(/src="/gi, `src="${process.env.UNI_FILE_PREFIX}`)
  62. .replace(/src='/gi, `src='${process.env.UNI_FILE_PREFIX}`);
  63. return match;
  64. });
  65. return newContent;
  66. },
  67. },
  68. methods: {
  69. async getContentInfo() {
  70. const {
  71. data: {
  72. content,
  73. title,
  74. createTime,
  75. },
  76. } = await fetchContentDetail(this.queryParams.id);
  77. this.content = content;
  78. this.title = title;
  79. this.date = uni.$u.timeFormat(createTime, 'yyyy-mm-dd');
  80. },
  81. },
  82. };
  83. </script>
  84. <style lang="scss" src="./index.scss" />;