content.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view v-if="detaile">
  3. <view class="title">{{ detaile.title }}</view>
  4. <view class="det-sta dflex">
  5. <image style="width: 48rpx;height: 48rpx;margin-right: 10rpx;" src="config.CONFIG_PROJECT_LOGO"></image>
  6. <view class="sitename" @click="toIndex('')">{{config.CONFIG_PROJECT_TITLE}}</view>
  7. <view class="time">{{getDateTimeStamp(detaile.time)}}</view>
  8. </view>
  9. <view class="content">
  10. <view class="viewMore">
  11. <mp-html :content="detaile.text" :lazy-load="true" @imgtap="choose"></mp-html>
  12. </view>
  13. </view>
  14. <view style="font-size: 24rpx;color: #999999;line-height: 36rpx;padding: 0 30rpx;">
  15. 本文仅代表作者观点,版权归原创者所有,如需转载请在文中注明来源及作者名字。
  16. </view>
  17. <view class="operate dflex">
  18. <view class="zan dflex">
  19. 共{{detaile.browse}}次浏览</view>
  20. <button class="share ddflex" hover-class="none" open-type="share"><image src="/static/images/share.png"></image>分享</button>
  21. </view>
  22. <view class="box" v-if="contentList && contentList.length > 0">
  23. <view class="tit dflex">为您推荐</view>
  24. <view class="rec">
  25. <navigator :url="'/topic/content/content?id=' + item.id" hover-class="none" class="li dflex" v-for="(item,index) in contentList" :key="index" v-if="index < 3">
  26. <view class="flex">
  27. <view class="rec-tit tovers">{{item.title}}</view>
  28. <view class="dflex">
  29. <!-- <view class="author ddflex"><image src="../../static/pages/images/zbgw.png" mode="aspectFill"></image>欧衡</view> -->
  30. <view class="rec-time">{{item.createDate}}</view>
  31. </view>
  32. </view>
  33. <image :src="item.pic" mode="aspectFill" class="rec-img"></image>
  34. </navigator>
  35. </view>
  36. </view>
  37. <serviceContact></serviceContact>
  38. </view>
  39. </template>
  40. <script>
  41. const app = getApp();
  42. const req = require('../../utils/request.js');
  43. const api = require('../../utils/api.js');
  44. const util = require('../../utils/util.js');
  45. import mpHtml from "../../components/mp-html/components/mp-html/mp-html";
  46. import serviceContact from "../../components/service-contact/index.vue";
  47. export default {
  48. components: {
  49. mpHtml,
  50. serviceContact
  51. },
  52. data() {
  53. return {
  54. detaile: '',
  55. type: 2, //详情类型:1 普通内容 2 自定义页面内容
  56. form: {
  57. page: 1,
  58. limit: 4,
  59. },
  60. contentList: [],
  61. viewMore: false,
  62. config: {},
  63. };
  64. },
  65. onLoad: async function(options) {
  66. uni.showShareMenu({
  67. withShareTicket: true,
  68. menus: ['shareAppMessage', 'shareTimeline']
  69. });
  70. this.id = options.id;
  71. await this.getDetail();
  72. this.getContentList();
  73. // await this.monitor();
  74. },
  75. onShow(){
  76. this.config = JSON.parse(req.getStorage('configRes'))
  77. this.getBrowse();
  78. },
  79. onShareAppMessage() {
  80. return {
  81. title: this.detaile.title,
  82. imageUrl: this.detaile.pic,
  83. path: '/topic/content/content?id=' + this.id +'&isShare=' + true
  84. };
  85. },
  86. onShareTimeline() {
  87. return {
  88. title: this.detaile.title,
  89. imageUrl: this.detaile.pic,
  90. path: '/topic/content/content?id=' + this.id +'&isShareTimeline=' + true
  91. };
  92. },
  93. methods: {
  94. monitor() {
  95. let that = this;
  96. let system = uni.getSystemInfoSync();
  97. let query = uni.createSelectorQuery();
  98. query.select('.viewMore').boundingClientRect(data=>{
  99. let height = data.height;
  100. if(height > system.screenHeight) {
  101. this.viewMore = true
  102. }
  103. }).exec();
  104. },
  105. viewMores(){
  106. this.viewMore = true;
  107. },
  108. choose: function () {
  109. let freshen = false;
  110. this.$emit('freshen', {
  111. detail: freshen
  112. });
  113. },
  114. toIndex(){
  115. uni.switchTab({
  116. url: '/pages/index/index'
  117. })
  118. },
  119. getDateTimeStamp(dateStr){
  120. return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
  121. },
  122. getDetail() {
  123. let that = this;
  124. let apiUrl = '/api/content/detail';
  125. return new Promise((resolve,reject)=>{
  126. req.getRequest(apiUrl,{ id: that.id },async res => {
  127. if(res.productCategory){
  128. res.productCategory = res.productCategory.split(',')
  129. }
  130. that.detaile = res;
  131. let arr = [];
  132. if(res.productCategory && res.productCategory.length > 0){
  133. for (var i = 0; i < res.productCategory.length; i++) {
  134. await that.getProductList(res.productCategory[i], i).then(res=>{
  135. arr = arr.concat(res);
  136. that.productList = arr;
  137. console.log('相关产品=='+JSON.stringify(arr),arr.length)
  138. });
  139. }
  140. }
  141. resolve();
  142. },true);
  143. })
  144. },
  145. getBrowse(){
  146. let params={
  147. bindId: this.id,
  148. type: 2
  149. }
  150. req.postRequest('/api/browse', params, data => {});
  151. },
  152. getContentList() {
  153. let form = this.form;
  154. form.code = this.detaile.PCode;
  155. req.getRequest('/api/content/list',form,res => {
  156. this.contentList = res;
  157. for (var i = 0; i < this.contentList.length; i++) {
  158. if (this.contentList[i].id == this.id){
  159. this.contentList.splice(i,1);
  160. }
  161. }
  162. });
  163. },
  164. },
  165. filters: {
  166. /**
  167. * 处理富文本里的图片宽度自适应
  168. * 1.去掉img标签里的style、width、height属性
  169. * 2.img标签添加style属性:max-width:100%;height:auto
  170. * 3.修改所有style里的width属性为max-width:100%
  171. * 4.去掉<br/>标签
  172. * @param html
  173. * @returns {void|string|*}
  174. */
  175. formatRichText(html) {
  176. //控制小程序中图片大小
  177. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  178. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  179. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  180. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  181. return match;
  182. });
  183. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  184. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  185. return match;
  186. });
  187. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  188. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  189. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  190. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  191. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  192. return newContent;
  193. }
  194. }
  195. };
  196. </script>
  197. <style>
  198. @import './content.css';
  199. </style>