content.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. <!--#ifdef H5-->
  11. <view style="padding: 0 30rpx;">
  12. <!--#endif-->
  13. <mp-html :content="detaile.text" :lazy-load="true" @imgtap="choose"></mp-html>
  14. <!--#ifdef H5-->
  15. </view>
  16. <!--#endif-->
  17. </view>
  18. <view class="view-more" @tap="viewMores()" v-if="!viewMore">查看全文</view>
  19. </view>
  20. <view class="operate dflex">
  21. <view class="zan dflex"><image src="../static/images/view.png"></image>{{detaile.browse}}</view>
  22. <view class="share dflex" @click="toShare()"><image src="../static/images/share.png"></image>分享</view>
  23. </view>
  24. <view class="box" v-if="contentList && contentList.length > 0">
  25. <view class="tit dflex">为您推荐</view>
  26. <view class="rec">
  27. <navigator @click="jumpUrl('/topic/content/content?id=' + item.id)" hover-class="none" class="li dflex" v-for="(item,index) in contentList" :key="index" v-if="index < 3">
  28. <view class="flex">
  29. <view class="rec-tit tovers">{{item.title}}</view>
  30. <view class="dflex">
  31. <!-- <view class="author ddflex"><image src="../../static/pages/images/zbgw.png" mode="aspectFill"></image>欧衡</view> -->
  32. <view class="rec-time">{{item.createDate}}</view>
  33. </view>
  34. </view>
  35. <image :src="item.pic" mode="aspectFill" class="rec-img"></image>
  36. </navigator>
  37. </view>
  38. </view>
  39. <!-- #ifdef H5 -->
  40. <wx-share ref="wxshare" />
  41. <!-- #endif -->
  42. </view>
  43. </template>
  44. <script>
  45. const app = getApp();
  46. const req = require('../../utils/request.js');
  47. const api = require('../../utils/api.js');
  48. const util = require('../../utils/util.js');
  49. import mpHtml from "../../components/mp-html/components/mp-html/mp-html";
  50. const h5util = require('../../utils/h5util.js');
  51. export default {
  52. components: {
  53. mpHtml,
  54. },
  55. data() {
  56. return {
  57. detaile: '',
  58. type: 2, //详情类型:1 普通内容 2 自定义页面内容
  59. form: {
  60. page: 1,
  61. limit: 4,
  62. },
  63. contentList: [],
  64. viewMore: false,
  65. config: {},
  66. code: ''
  67. };
  68. },
  69. onLoad: async function(options) {
  70. if(options.appId) req.setStorage('appId',options.appId);
  71. uni.showShareMenu({
  72. withShareTicket: true,
  73. menus: ['shareAppMessage', 'shareTimeline']
  74. });
  75. this.id = options.id;
  76. this.code = options.code;
  77. await this.getDetail();
  78. this.getContentList();
  79. // await this.monitor();
  80. },
  81. onShow(){
  82. this.getConfig();
  83. this.getBrowse();
  84. },
  85. async onShareAppMessage() {
  86. return await this.onShareMessage();
  87. },
  88. onShareTimeline() {
  89. return {
  90. title: this.detaile.title,
  91. imageUrl: this.detaile.pic,
  92. path: '/topic/content/content?id=' + this.id +'&isShareTimeline=' + true
  93. };
  94. },
  95. onReady() {
  96. // #ifdef H5
  97. var that = this;
  98. //初始化分享内容
  99. setTimeout(async function() {
  100. var shareContent = await that.onShareMessage();
  101. if (shareContent) {
  102. shareContent.path = window.location.origin + shareContent.path;
  103. }
  104. console.log('分享内容》》》》》', shareContent);
  105. that.$refs.wxshare.init(shareContent);
  106. }, 4 * 1000);
  107. // #endif
  108. },
  109. methods: {
  110. jumpUrl(url) {
  111. uni.navigateTo({
  112. url:url
  113. })
  114. // h5util.jumpUrlExternal({url:url})
  115. },
  116. async onShareMessage(){
  117. let path = '';
  118. let isSolution = '';
  119. isSolution = req.env[req.env.NODE_ENV].isSolution;
  120. let userInfo = req.getStorage('userInfo');
  121. if(isSolution){
  122. path = '/share/home/index?appId='+req.getStorage('appId')+'&userId='+userInfo.id;
  123. }else{
  124. path = '/topic/content/content?id=' + this.id +'&isShare=' + true+'&userId='+userInfo.id + '&appId=' + req.getStorage('appId');
  125. }
  126. path = this.SHARE_BEHAVIOR_FN({path:path,sendShare:false}).path
  127. console.log(path)
  128. return {
  129. title: this.detaile.title,
  130. imageUrl: this.detaile.pic,
  131. path: path
  132. };
  133. },
  134. getConfig(){
  135. if(req.getStorage('configRes')){
  136. this.config = JSON.parse(req.getStorage('configRes'))
  137. }else{
  138. req.getRequest('/api/config',{},data=>{
  139. this.config = data;
  140. req.setStorage('configRes',JSON.stringify(data));
  141. })
  142. }
  143. },
  144. monitor() {
  145. let that = this;
  146. let system = uni.getSystemInfoSync();
  147. let query = uni.createSelectorQuery();
  148. query.select('.viewMore').boundingClientRect(data=>{
  149. let height = data.height;
  150. if(height > system.screenHeight) {
  151. this.viewMore = true
  152. }
  153. }).exec();
  154. },
  155. viewMores(){
  156. this.viewMore = true;
  157. },
  158. choose: function () {
  159. let freshen = false;
  160. this.$emit('freshen', {
  161. detail: freshen
  162. });
  163. },
  164. toIndex(){
  165. h5util.jumpIndex('/pages/tabBar/index')
  166. // app.globalData.reLaunchTo('pages/index/index')
  167. },
  168. getDateTimeStamp(dateStr){
  169. return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
  170. },
  171. getDetail() {
  172. let that = this;
  173. let apiUrl = '/api/content/detail';
  174. return new Promise((resolve,reject)=>{
  175. req.getRequest(apiUrl,{ id: that.id },async res => {
  176. if(res.productCategory){
  177. res.productCategory = res.productCategory.split(',')
  178. }
  179. that.detaile = res;
  180. let shareD = await that.onShareMessage()
  181. that.postShareMessage(shareD.title,shareD.imageUrl,shareD.path,{
  182. BIND_TYPE : this.BIND_TYPE,
  183. BIND_ID : this.BIND_ID,
  184. USER_ID : this.USER_ID,
  185. SHARE_USER_ID : this.SHARE_USER_ID,
  186. PARENT_CODE : this.PARENT_CODE,
  187. CURRENT_CODE : this.CURRENT_CODE
  188. })
  189. let arr = [];
  190. if(res.productCategory && res.productCategory.length > 0){
  191. for (var i = 0; i < res.productCategory.length; i++) {
  192. await that.getProductList(res.productCategory[i], i).then(res=>{
  193. arr = arr.concat(res);
  194. that.productList = arr;
  195. console.log('相关产品=='+JSON.stringify(arr),arr.length)
  196. });
  197. }
  198. }
  199. resolve();
  200. },true);
  201. })
  202. },
  203. getBrowse(){
  204. let params={
  205. bindId: this.id,
  206. type: 2
  207. }
  208. req.postRequest('/api/browse', params, data => {});
  209. },
  210. getContentList() {
  211. let form = this.form;
  212. form.code = this.code;
  213. req.getRequest('/api/content/list',form,res => {
  214. this.contentList = res;
  215. for (var i = 0; i < this.contentList.length; i++) {
  216. if (this.contentList[i].id == this.id){
  217. this.contentList.splice(i,1);
  218. }
  219. }
  220. });
  221. },
  222. async toShare(){
  223. let shareD = await this.onShareMessage()
  224. h5util.jumpShare({
  225. title:shareD.title,
  226. imageUrl:shareD.imageUrl,
  227. path:shareD.path,
  228. shareType:1,
  229. id:this.detaile.id,
  230. sendBehaviorObj:{
  231. BIND_TYPE : this.BIND_TYPE,
  232. BIND_ID : this.BIND_ID,
  233. USER_ID : this.USER_ID,
  234. SHARE_USER_ID : this.SHARE_USER_ID,
  235. PARENT_CODE : this.PARENT_CODE,
  236. CURRENT_CODE : this.CURRENT_CODE
  237. }
  238. })
  239. }
  240. },
  241. filters: {
  242. /**
  243. * 处理富文本里的图片宽度自适应
  244. * 1.去掉img标签里的style、width、height属性
  245. * 2.img标签添加style属性:max-width:100%;height:auto
  246. * 3.修改所有style里的width属性为max-width:100%
  247. * 4.去掉<br/>标签
  248. * @param html
  249. * @returns {void|string|*}
  250. */
  251. formatRichText(html) {
  252. //控制小程序中图片大小
  253. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  254. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  255. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  256. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  257. return match;
  258. });
  259. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  260. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  261. return match;
  262. });
  263. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  264. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  265. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  266. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  267. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  268. return newContent;
  269. }
  270. }
  271. };
  272. </script>
  273. <style>
  274. @import './content.css';
  275. </style>