content.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. <!-- #ifdef H5 -->
  10. <wx-share ref="wxshare" />
  11. <!-- #endif -->
  12. </view>
  13. </template>
  14. <script>
  15. const req = require('../../utils/request.js');
  16. const api = require('../../utils/api.js');
  17. const util = require('../../utils/util.js');
  18. export default {
  19. data() {
  20. return {
  21. detaile: '',
  22. type: 2, //详情类型:1 普通内容 2 自定义页面内容
  23. };
  24. },
  25. onLoad: function(options) {
  26. if(options.appId) req.setStorage('appId',options.appId);
  27. uni.showShareMenu({
  28. withShareTicket: true,
  29. menus: ['shareAppMessage', 'shareTimeline']
  30. });
  31. this.id = options.id;
  32. this.type = options.type ? options.type : 2;
  33. this.getDetail();
  34. },
  35. onShow(){
  36. },
  37. onShareAppMessage() {
  38. return this.onShareMessage();
  39. },
  40. onShareTimeline() {
  41. return {
  42. title: this.detaile.title,
  43. imageUrl: this.detaile.pic,
  44. path: '/activity/content/content?id=' + this.id+'&appId='+req.getStorage('appId')
  45. };
  46. },
  47. onReady() {
  48. // #ifdef H5
  49. var that = this;
  50. //初始化分享内容
  51. setTimeout(function() {
  52. var shareContent = that.onShareMessage();
  53. if (shareContent) {
  54. shareContent.path = window.location.origin + shareContent.path;
  55. }
  56. console.log('分享内容》》》》》', shareContent);
  57. that.$refs.wxshare.init(shareContent);
  58. }, 4 * 1000);
  59. // #endif
  60. },
  61. methods: {
  62. onShareMessage(){
  63. let path = '';
  64. let isSolution = '';
  65. // #ifndef H5
  66. isSolution = req.env[req.env.NODE_ENV].isSolution;
  67. // #endif
  68. // #ifdef H5
  69. isSolution = false;
  70. // #endif
  71. if(isSolution){
  72. path = '/share/home/index?appId='+req.getStorage('appId')+'&userId='+userInfo.id;
  73. }else{
  74. path = '/activity/content/content?id=' + this.id+ '&appId=' + req.getStorage('appId');
  75. }
  76. return {
  77. title: this.detaile.title,
  78. imageUrl: this.detaile.pic,
  79. path: path
  80. };
  81. },
  82. getDetail() {
  83. let apiUrl = '';
  84. if(this.type == 1){
  85. apiUrl = '/api/content/detail'
  86. }else{
  87. apiUrl = '/api/help/detailV2'
  88. }
  89. let param = {}
  90. if(this.type == 1){
  91. param.id = this.id;
  92. }else{
  93. param.code = this.id;
  94. }
  95. req.getRequest(
  96. apiUrl,
  97. param,
  98. res => {
  99. this.detaile = res;
  100. }
  101. );
  102. },
  103. },
  104. filters: {
  105. /**
  106. * 处理富文本里的图片宽度自适应
  107. * 1.去掉img标签里的style、width、height属性
  108. * 2.img标签添加style属性:max-width:100%;height:auto
  109. * 3.修改所有style里的width属性为max-width:100%
  110. * 4.去掉<br/>标签
  111. * @param html
  112. * @returns {void|string|*}
  113. */
  114. formatRichText(html) {
  115. //控制小程序中图片大小
  116. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  117. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  118. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  119. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  120. return match;
  121. });
  122. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  123. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  124. return match;
  125. });
  126. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  127. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  128. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  129. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  130. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  131. return newContent;
  132. }
  133. }
  134. };
  135. </script>
  136. <style>
  137. @import './content.css';
  138. </style>