content.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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+'&code='+code)" 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 userInfo = req.getStorage('userInfo');
  119. path = '/topic/content/content?id=' + this.id + '&userId='+userInfo.id
  120. if(this.code) path = path + '&code=' + this.code
  121. path = this.SHARE_BEHAVIOR_FN({path:path,sendShare:false}).path
  122. console.log(path)
  123. return {
  124. title: this.detaile.title,
  125. imageUrl: this.detaile.pic,
  126. path: path
  127. };
  128. },
  129. getConfig(){
  130. if(req.getStorage('configRes')){
  131. this.config = JSON.parse(req.getStorage('configRes'))
  132. }else{
  133. req.getRequest('/api/config',{},data=>{
  134. this.config = data;
  135. req.setStorage('configRes',JSON.stringify(data));
  136. })
  137. }
  138. },
  139. monitor() {
  140. let that = this;
  141. let system = uni.getSystemInfoSync();
  142. let query = uni.createSelectorQuery();
  143. query.select('.viewMore').boundingClientRect(data=>{
  144. let height = data.height;
  145. if(height > system.screenHeight) {
  146. this.viewMore = true
  147. }
  148. }).exec();
  149. },
  150. viewMores(){
  151. this.viewMore = true;
  152. },
  153. choose: function () {
  154. let freshen = false;
  155. this.$emit('freshen', {
  156. detail: freshen
  157. });
  158. },
  159. toIndex(){
  160. h5util.jumpIndex('/pages/tabBar/index')
  161. // app.globalData.reLaunchTo('pages/index/index')
  162. },
  163. getDateTimeStamp(dateStr){
  164. return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
  165. },
  166. getDetail() {
  167. let that = this;
  168. let apiUrl = '/api/content/detail';
  169. return new Promise((resolve,reject)=>{
  170. req.getRequest(apiUrl,{ id: that.id },async res => {
  171. if(res.productCategory){
  172. res.productCategory = res.productCategory.split(',')
  173. }
  174. that.detaile = res;
  175. let shareD = await that.onShareMessage()
  176. that.postShareMessage(shareD.title,shareD.imageUrl,shareD.path,{
  177. BIND_TYPE : this.BIND_TYPE,
  178. BIND_ID : this.BIND_ID,
  179. USER_ID : this.USER_ID,
  180. SHARE_USER_ID : this.SHARE_USER_ID,
  181. PARENT_CODE : this.PARENT_CODE,
  182. CURRENT_CODE : this.CURRENT_CODE
  183. })
  184. let arr = [];
  185. if(res.productCategory && res.productCategory.length > 0){
  186. for (var i = 0; i < res.productCategory.length; i++) {
  187. await that.getProductList(res.productCategory[i], i).then(res=>{
  188. arr = arr.concat(res);
  189. that.productList = arr;
  190. console.log('相关产品=='+JSON.stringify(arr),arr.length)
  191. });
  192. }
  193. }
  194. resolve();
  195. },true);
  196. })
  197. },
  198. getBrowse(){
  199. let params={
  200. bindId: this.id,
  201. type: 2
  202. }
  203. req.postRequest('/api/browse', params, data => {});
  204. },
  205. getContentList() {
  206. let form = this.form;
  207. form.code = this.code;
  208. req.getRequest('/api/content/list',form,res => {
  209. this.contentList = res;
  210. for (var i = 0; i < this.contentList.length; i++) {
  211. if (this.contentList[i].id == this.id){
  212. this.contentList.splice(i,1);
  213. }
  214. }
  215. });
  216. },
  217. async toShare(){
  218. let islogin = await req.isAuthFn(true)
  219. if(islogin){
  220. let shareD = await this.onShareMessage()
  221. h5util.jumpShare({
  222. title:shareD.title,
  223. imageUrl:shareD.imageUrl,
  224. path:shareD.path,
  225. shareType:1,
  226. id:this.detaile.id,
  227. sendBehaviorObj:{
  228. BIND_TYPE : this.BIND_TYPE,
  229. BIND_ID : this.BIND_ID,
  230. USER_ID : this.USER_ID,
  231. SHARE_USER_ID : this.SHARE_USER_ID,
  232. PARENT_CODE : this.PARENT_CODE,
  233. CURRENT_CODE : this.CURRENT_CODE
  234. }
  235. })
  236. }
  237. }
  238. },
  239. filters: {
  240. /**
  241. * 处理富文本里的图片宽度自适应
  242. * 1.去掉img标签里的style、width、height属性
  243. * 2.img标签添加style属性:max-width:100%;height:auto
  244. * 3.修改所有style里的width属性为max-width:100%
  245. * 4.去掉<br/>标签
  246. * @param html
  247. * @returns {void|string|*}
  248. */
  249. formatRichText(html) {
  250. //控制小程序中图片大小
  251. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  252. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  253. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  254. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  255. return match;
  256. });
  257. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  258. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  259. return match;
  260. });
  261. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  262. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  263. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  264. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  265. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  266. return newContent;
  267. }
  268. }
  269. };
  270. </script>
  271. <style>
  272. @import './content.css';
  273. </style>