content.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view v-if="detaile">
  3. <view class="title">{{ detaile.title }}</view>
  4. <view class="content">
  5. <view class="viewMore">
  6. <rich-text :nodes="detaile.text | formatRichText"></rich-text>
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. const req = require('../../utils/request.js');
  13. const api = require('../../utils/api.js');
  14. const util = require('../../utils/util.js');
  15. export default {
  16. data() {
  17. return {
  18. detaile: '',
  19. type: 2, //详情类型:1 普通内容 2 自定义页面内容
  20. };
  21. },
  22. onLoad: function(options) {
  23. uni.showShareMenu({
  24. withShareTicket: true,
  25. menus: ['shareAppMessage', 'shareTimeline']
  26. });
  27. this.id = options.id;
  28. this.type = options.type ? options.type : 2;
  29. this.getDetail();
  30. },
  31. onShow(){
  32. },
  33. onShareAppMessage() {
  34. return {
  35. title: this.detaile.title,
  36. imageUrl: this.detaile.pic,
  37. path: '/activity/content/content?id=' + this.id
  38. };
  39. },
  40. onShareTimeline() {
  41. return {
  42. title: this.detaile.title,
  43. imageUrl: this.detaile.pic,
  44. path: '/activity/content/content?id=' + this.id
  45. };
  46. },
  47. methods: {
  48. getDetail() {
  49. let apiUrl = '';
  50. if(this.type == 1){
  51. apiUrl = '/api/content/detail'
  52. }else{
  53. apiUrl = '/api/help/detailV2'
  54. }
  55. let param = {}
  56. if(this.type == 1){
  57. param.id = this.id;
  58. }else{
  59. param.code = this.id;
  60. }
  61. req.getRequest(
  62. apiUrl,
  63. param,
  64. res => {
  65. this.detaile = res;
  66. }
  67. );
  68. },
  69. },
  70. filters: {
  71. /**
  72. * 处理富文本里的图片宽度自适应
  73. * 1.去掉img标签里的style、width、height属性
  74. * 2.img标签添加style属性:max-width:100%;height:auto
  75. * 3.修改所有style里的width属性为max-width:100%
  76. * 4.去掉<br/>标签
  77. * @param html
  78. * @returns {void|string|*}
  79. */
  80. formatRichText(html) {
  81. //控制小程序中图片大小
  82. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  83. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  84. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  85. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  86. return match;
  87. });
  88. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  89. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  90. return match;
  91. });
  92. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  93. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  94. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  95. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  96. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  97. return newContent;
  98. }
  99. }
  100. };
  101. </script>
  102. <style>
  103. @import './content.css';
  104. </style>