content.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. uni.showModal({
  85. content:req.getStorage('AUTH_TOKEN')
  86. })
  87. },
  88. async onShareAppMessage() {
  89. return await this.onShareMessage();
  90. },
  91. onShareTimeline() {
  92. return {
  93. title: this.detaile.title,
  94. imageUrl: this.detaile.pic,
  95. path: '/topic/content/content?id=' + this.id +'&isShareTimeline=' + true
  96. };
  97. },
  98. onReady() {
  99. // #ifdef H5
  100. var that = this;
  101. //初始化分享内容
  102. setTimeout(async function() {
  103. var shareContent = await that.onShareMessage();
  104. if (shareContent) {
  105. shareContent.path = window.location.origin + shareContent.path;
  106. }
  107. console.log('分享内容》》》》》', shareContent);
  108. that.$refs.wxshare.init(shareContent);
  109. }, 4 * 1000);
  110. // #endif
  111. },
  112. methods: {
  113. jumpUrl(url) {
  114. uni.navigateTo({
  115. url:url
  116. })
  117. // h5util.jumpUrlExternal({url:url})
  118. },
  119. async onShareMessage(){
  120. let path = '';
  121. let userInfo = req.getStorage('userInfo');
  122. path = '/topic/content/content?id=' + this.id + '&userId='+userInfo.id
  123. if(this.code) path = path + '&code=' + this.code
  124. path = this.SHARE_BEHAVIOR_FN({path:path,sendShare:false}).path
  125. console.log(path)
  126. return {
  127. title: this.detaile.title,
  128. imageUrl: this.detaile.pic,
  129. path: path
  130. };
  131. },
  132. getConfig(){
  133. if(req.getStorage('configRes')){
  134. this.config = JSON.parse(req.getStorage('configRes'))
  135. }else{
  136. req.getRequest('/api/config',{},data=>{
  137. this.config = data;
  138. req.setStorage('configRes',JSON.stringify(data));
  139. })
  140. }
  141. },
  142. monitor() {
  143. let that = this;
  144. let system = uni.getSystemInfoSync();
  145. let query = uni.createSelectorQuery();
  146. query.select('.viewMore').boundingClientRect(data=>{
  147. let height = data.height;
  148. if(height > system.screenHeight) {
  149. this.viewMore = true
  150. }
  151. }).exec();
  152. },
  153. viewMores(){
  154. this.viewMore = true;
  155. },
  156. choose: function () {
  157. let freshen = false;
  158. this.$emit('freshen', {
  159. detail: freshen
  160. });
  161. },
  162. toIndex(){
  163. h5util.jumpIndex('/pages/tabBar/index')
  164. // app.globalData.reLaunchTo('pages/index/index')
  165. },
  166. getDateTimeStamp(dateStr){
  167. return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
  168. },
  169. getDetail() {
  170. let that = this;
  171. let apiUrl = '/api/content/detail';
  172. return new Promise((resolve,reject)=>{
  173. req.getRequest(apiUrl,{ id: that.id },async res => {
  174. if(res.productCategory){
  175. res.productCategory = res.productCategory.split(',')
  176. }
  177. that.detaile = res;
  178. let shareD = await that.onShareMessage()
  179. that.postShareMessage(shareD.title,shareD.imageUrl,shareD.path,{
  180. BIND_TYPE : this.BIND_TYPE,
  181. BIND_ID : this.BIND_ID,
  182. USER_ID : this.USER_ID,
  183. SHARE_USER_ID : this.SHARE_USER_ID,
  184. PARENT_CODE : this.PARENT_CODE,
  185. CURRENT_CODE : this.CURRENT_CODE
  186. })
  187. let arr = [];
  188. if(res.productCategory && res.productCategory.length > 0){
  189. for (var i = 0; i < res.productCategory.length; i++) {
  190. await that.getProductList(res.productCategory[i], i).then(res=>{
  191. arr = arr.concat(res);
  192. that.productList = arr;
  193. console.log('相关产品=='+JSON.stringify(arr),arr.length)
  194. });
  195. }
  196. }
  197. resolve();
  198. },true);
  199. })
  200. },
  201. getBrowse(){
  202. let params={
  203. bindId: this.id,
  204. type: 2
  205. }
  206. req.postRequest('/api/browse', params, data => {});
  207. },
  208. getContentList() {
  209. let form = this.form;
  210. form.code = this.code;
  211. req.getRequest('/api/content/list',form,res => {
  212. this.contentList = res;
  213. for (var i = 0; i < this.contentList.length; i++) {
  214. if (this.contentList[i].id == this.id){
  215. this.contentList.splice(i,1);
  216. }
  217. }
  218. });
  219. },
  220. async toShare(){
  221. let islogin = await req.isAuthFn(true)
  222. if(islogin){
  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. },
  242. filters: {
  243. /**
  244. * 处理富文本里的图片宽度自适应
  245. * 1.去掉img标签里的style、width、height属性
  246. * 2.img标签添加style属性:max-width:100%;height:auto
  247. * 3.修改所有style里的width属性为max-width:100%
  248. * 4.去掉<br/>标签
  249. * @param html
  250. * @returns {void|string|*}
  251. */
  252. formatRichText(html) {
  253. //控制小程序中图片大小
  254. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  255. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  256. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  257. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  258. return match;
  259. });
  260. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  261. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  262. return match;
  263. });
  264. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  265. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  266. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  267. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  268. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  269. return newContent;
  270. }
  271. }
  272. };
  273. </script>
  274. <style>
  275. @import './content.css';
  276. </style>