alContent.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view v-if="detaile">
  3. <image mode="widthFix" style="width: 100%;margin-bottom: 40rpx;" v-if="detaile.fieldValues&&detaile.fieldValues.pic" :src="detaile.fieldValues.pic"></image>
  4. <block v-else>
  5. </block>
  6. <view class="content">
  7. <view class="viewMore">
  8. <mp-html :content="detaile.text" :lazy-load="true" @imgtap="choose"></mp-html>
  9. </view>
  10. </view>
  11. <serviceContact></serviceContact>
  12. </view>
  13. </template>
  14. <script>
  15. const app = getApp();
  16. const req = require('../../utils/request.js');
  17. const api = require('../../utils/api.js');
  18. const util = require('../../utils/util.js');
  19. import mpHtml from "../../components/mp-html/components/mp-html/mp-html";
  20. import serviceContact from "../../components/service-contact/index.vue";
  21. export default {
  22. components: {
  23. mpHtml,
  24. serviceContact
  25. },
  26. data() {
  27. return {
  28. detaile: '',
  29. type: 2, //详情类型:1 普通内容 2 自定义页面内容
  30. config: {},
  31. };
  32. },
  33. onLoad: async function(options) {
  34. uni.showShareMenu({
  35. withShareTicket: true,
  36. menus: ['shareAppMessage', 'shareTimeline']
  37. });
  38. this.id = options.id;
  39. await this.getDetail();
  40. // await this.monitor();
  41. },
  42. onShow(){
  43. this.config = JSON.parse(req.getStorage('configRes'))
  44. this.getBrowse();
  45. },
  46. onShareAppMessage() {
  47. return {
  48. title: this.detaile.title,
  49. imageUrl: this.detaile.pic,
  50. path: '/topic/content/content?id=' + this.id +'&isShare=' + true
  51. };
  52. },
  53. onShareTimeline() {
  54. return {
  55. title: this.detaile.title,
  56. imageUrl: this.detaile.pic,
  57. path: '/topic/content/content?id=' + this.id +'&isShareTimeline=' + true
  58. };
  59. },
  60. methods: {
  61. monitor() {
  62. let that = this;
  63. let system = uni.getSystemInfoSync();
  64. let query = uni.createSelectorQuery();
  65. query.select('.viewMore').boundingClientRect(data=>{
  66. let height = data.height;
  67. if(height > system.screenHeight) {
  68. this.viewMore = true
  69. }
  70. }).exec();
  71. },
  72. choose: function () {
  73. let freshen = false;
  74. this.$emit('freshen', {
  75. detail: freshen
  76. });
  77. },
  78. toIndex(){
  79. uni.switchTab({
  80. url: '/pages/index/index'
  81. })
  82. },
  83. getDateTimeStamp(dateStr){
  84. return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
  85. },
  86. getDetail() {
  87. let that = this;
  88. let apiUrl = '/api/content/detail';
  89. return new Promise((resolve,reject)=>{
  90. req.getRequest(apiUrl,{ id: that.id },async res => {
  91. if(res.productCategory){
  92. res.productCategory = res.productCategory.split(',')
  93. }
  94. that.detaile = res;
  95. let arr = [];
  96. if(res.productCategory && res.productCategory.length > 0){
  97. for (var i = 0; i < res.productCategory.length; i++) {
  98. await that.getProductList(res.productCategory[i], i).then(res=>{
  99. arr = arr.concat(res);
  100. that.productList = arr;
  101. console.log('相关产品=='+JSON.stringify(arr),arr.length)
  102. });
  103. }
  104. }
  105. resolve();
  106. },true);
  107. })
  108. },
  109. getBrowse(){
  110. var dataP = {};
  111. dataP.type =
  112. 4 //、产品 2、活动 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 12、用户须知 13、素材 15、医院科室 16、海报 20、医生 21 新闻 23名片
  113. dataP.behavior = 4; //1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  114. dataP.bindId = this.id;
  115. req.postRequestLoding('/api/v3/behavior/save', dataP, data => {
  116. });
  117. },
  118. },
  119. filters: {
  120. /**
  121. * 处理富文本里的图片宽度自适应
  122. * 1.去掉img标签里的style、width、height属性
  123. * 2.img标签添加style属性:max-width:100%;height:auto
  124. * 3.修改所有style里的width属性为max-width:100%
  125. * 4.去掉<br/>标签
  126. * @param html
  127. * @returns {void|string|*}
  128. */
  129. formatRichText(html) {
  130. //控制小程序中图片大小
  131. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  132. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  133. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  134. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  135. return match;
  136. });
  137. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  138. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  139. return match;
  140. });
  141. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  142. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  143. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  144. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  145. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  146. return newContent;
  147. }
  148. }
  149. };
  150. </script>
  151. <style>
  152. @import './alContent.css';
  153. </style>