alContent.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. this.getContentList();
  41. // await this.monitor();
  42. },
  43. onShow(){
  44. this.config = JSON.parse(req.getStorage('configRes'))
  45. this.getBrowse();
  46. },
  47. onShareAppMessage() {
  48. return {
  49. title: this.detaile.title,
  50. imageUrl: this.detaile.pic,
  51. path: '/topic/content/content?id=' + this.id +'&isShare=' + true
  52. };
  53. },
  54. onShareTimeline() {
  55. return {
  56. title: this.detaile.title,
  57. imageUrl: this.detaile.pic,
  58. path: '/topic/content/content?id=' + this.id +'&isShareTimeline=' + true
  59. };
  60. },
  61. methods: {
  62. monitor() {
  63. let that = this;
  64. let system = uni.getSystemInfoSync();
  65. let query = uni.createSelectorQuery();
  66. query.select('.viewMore').boundingClientRect(data=>{
  67. let height = data.height;
  68. if(height > system.screenHeight) {
  69. this.viewMore = true
  70. }
  71. }).exec();
  72. },
  73. choose: function () {
  74. let freshen = false;
  75. this.$emit('freshen', {
  76. detail: freshen
  77. });
  78. },
  79. toIndex(){
  80. uni.switchTab({
  81. url: '/pages/index/index'
  82. })
  83. },
  84. getDateTimeStamp(dateStr){
  85. return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
  86. },
  87. getDetail() {
  88. let that = this;
  89. let apiUrl = '/api/content/detail';
  90. return new Promise((resolve,reject)=>{
  91. req.getRequest(apiUrl,{ id: that.id },async res => {
  92. if(res.productCategory){
  93. res.productCategory = res.productCategory.split(',')
  94. }
  95. that.detaile = res;
  96. let arr = [];
  97. if(res.productCategory && res.productCategory.length > 0){
  98. for (var i = 0; i < res.productCategory.length; i++) {
  99. await that.getProductList(res.productCategory[i], i).then(res=>{
  100. arr = arr.concat(res);
  101. that.productList = arr;
  102. console.log('相关产品=='+JSON.stringify(arr),arr.length)
  103. });
  104. }
  105. }
  106. resolve();
  107. },true);
  108. })
  109. },
  110. getBrowse(){
  111. let params={
  112. bindId: this.id,
  113. type: 2
  114. }
  115. req.postRequest('/api/browse', params, data => {});
  116. },
  117. },
  118. filters: {
  119. /**
  120. * 处理富文本里的图片宽度自适应
  121. * 1.去掉img标签里的style、width、height属性
  122. * 2.img标签添加style属性:max-width:100%;height:auto
  123. * 3.修改所有style里的width属性为max-width:100%
  124. * 4.去掉<br/>标签
  125. * @param html
  126. * @returns {void|string|*}
  127. */
  128. formatRichText(html) {
  129. //控制小程序中图片大小
  130. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  131. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  132. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  133. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  134. return match;
  135. });
  136. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  137. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  138. return match;
  139. });
  140. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  141. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  142. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  143. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  144. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  145. return newContent;
  146. }
  147. }
  148. };
  149. </script>
  150. <style>
  151. @import './alContent.css';
  152. </style>