content.vue 5.9 KB

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