content.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <template>
  2. <view v-if="detaile">
  3. <view v-if="detaile&&detaile.contentType!=3">
  4. <view class="title">{{ detaile.title }}</view>
  5. <view class="det-sta ddflex">
  6. <image style="width: 48rpx;height: 48rpx;margin-right: 10rpx;" :src="config.CONFIG_PROJECT_LOGO"></image>
  7. <view class="sitename" @click="toIndex('')">{{config.CONFIG_PROJECT_TITLE}}</view>
  8. <view class="time">{{getDateTimeStamp(detaile.time)}}</view>
  9. </view>
  10. <view class="video" v-if="detaile.resourceUri&&JSON.parse(detaile.resourceUri).uri">
  11. <video :src="JSON.parse(detaile.resourceUri).uri" :show-center-play-btn="false" :controls="true" :autoplay="true" id="myVideo" @pause="videoPause" @ended="videoEnded"></video>
  12. <view class="video-play ddflex" @click="videoPlay()" v-if="detaile.isShowPlayBtn"><image src="../../static/images/play.png"></image></view>
  13. </view>
  14. <view class="content">
  15. <view class="viewMore">
  16. <mp-html :content="detaile.text" :lazy-load="true" @imgtap="choose"></mp-html>
  17. </view>
  18. </view>
  19. <view style="font-size: 24rpx;color: #999999;line-height: 36rpx;padding: 0 30rpx;">
  20. 部分素材来源:中国人寿寿险app、国寿e店、云助理等。
  21. </view>
  22. <view class="operate dflex">
  23. <view class="zan dflex">
  24. 共{{detaile.browse}}次浏览</view>
  25. <view class="ddflex">
  26. <button style="margin-right: 20rpx !important;" class="share ddflex" hover-class="none" @click="userBehavior()"><image :src="detaile.isThumbs?'/static/images/like_h.png':'/static/images/like.png'"></image>{{detaile.thumbsNumber}}</button>
  27. <button class="share ddflex" hover-class="none" open-type="share"><image src="/static/images/share.png"></image>分享</button>
  28. </view>
  29. </view>
  30. <view class="box" v-if="contentList && contentList.length > 0">
  31. <view class="tit dflex">为您推荐</view>
  32. <view class="rec">
  33. <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">
  34. <view class="flex">
  35. <view class="rec-tit tovers">{{item.title}}</view>
  36. <view class="dflex">
  37. <!-- <view class="author ddflex"><image src="../../static/pages/images/zbgw.png" mode="aspectFill"></image>欧衡</view> -->
  38. <view class="rec-time">{{item.createDate}}</view>
  39. </view>
  40. </view>
  41. <image :src="item.pic" mode="aspectFill" class="rec-img"></image>
  42. </navigator>
  43. </view>
  44. </view>
  45. <serviceContact :userId="query.userId?query.userId:''" v-if="userInfo.userType!=1"></serviceContact>
  46. </view>
  47. <view v-if="detaile&&detaile.contentType==3">
  48. <web-view :src="webUrl" v-if="webUrl!==''"></web-view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. const app = getApp();
  54. const req = require('../../utils/request.js');
  55. const api = require('../../utils/api.js');
  56. const util = require('../../utils/util.js');
  57. import mpHtml from "../../components/mp-html/components/mp-html/mp-html";
  58. import serviceContact from "../../components/service-contact/index.vue";
  59. export default {
  60. components: {
  61. mpHtml,
  62. serviceContact
  63. },
  64. data() {
  65. return {
  66. detaile: '',
  67. type: 2, //详情类型:1 普通内容 2 自定义页面内容
  68. form: {
  69. page: 1,
  70. limit: 4,
  71. },
  72. contentList: [],
  73. viewMore: false,
  74. config: {},
  75. userInfo:{},
  76. query:{},
  77. id:null,
  78. webUrl:'',
  79. tempFilePath:''
  80. };
  81. },
  82. onLoad: async function(options) {
  83. this.query = options
  84. this.id = options.id;
  85. if(options.shareId) req.setStorage('shareId',options.shareId)
  86. // 朋友圈页面不调用登陆
  87. if(!this.query.isSharePYQ){
  88. await req.silenceLogin(options.userId, '');
  89. }
  90. setTimeout(()=>{
  91. this.userInfo = req.getStorage('userInfo')
  92. },500)
  93. uni.showShareMenu({
  94. withShareTicket: true,
  95. menus: ['shareAppMessage', 'shareTimeline']
  96. });
  97. await this.getDetail();
  98. this.getContentList();
  99. // await this.monitor();
  100. },
  101. async onShow(){
  102. this.getConfig()
  103. this.getBrowse();
  104. },
  105. onShareAppMessage() {
  106. req.saveBehaviorNew(this.id,3,6)
  107. return {
  108. title: this.detaile.title,
  109. imageUrl: this.detaile.pic,
  110. path: '/topic/content/content?id=' + this.id +'&isShare=' + true+(this.userInfo.userType==1?('&shareId='+this.userInfo.saleNo):'')
  111. };
  112. },
  113. onShareTimeline() {
  114. req.saveBehaviorNew(this.id,3,6)
  115. return {
  116. title: this.detaile.title,
  117. imageUrl: this.tempFilePath?this.tempFilePath:this.detaile.pic,
  118. path: '/topic/content/content',
  119. query:'id=' + this.id+'&isSharePYQ=' + true +'&isShare=' + true+(this.userInfo.userType==1?('&shareId='+this.userInfo.saleNo):'')
  120. };
  121. },
  122. methods: {
  123. /*获取分享图 */
  124. async getShareImg(){
  125. let tempFilePath = await new Promise((r,j)=>{
  126. try{
  127. uni.getImageInfo({
  128. src:this.detaile.pic,
  129. success: (data) => {
  130. uni.compressImage({
  131. src:data.path,
  132. quality:0.5,
  133. success:(res)=>{
  134. console.log('压缩成功',res.tempFilePath)
  135. r(res.tempFilePath)
  136. },
  137. fail: (error) => {
  138. console.log('压缩失败',error)
  139. r('')
  140. }
  141. })
  142. },
  143. fail: () => {
  144. r('')
  145. }
  146. })
  147. }catch(e){
  148. //TODO handle the exception
  149. r('')
  150. }
  151. })
  152. this.tempFilePath = tempFilePath
  153. },
  154. getConfig() {
  155. var _this = this;
  156. return new Promise((res, rej) => {
  157. req.g(
  158. '/api/other/config',
  159. data => {
  160. req.setStorage('configRes', JSON.stringify(data));
  161. this.config = data;
  162. res(data);
  163. },
  164. true
  165. );
  166. });
  167. },
  168. monitor() {
  169. let that = this;
  170. let system = uni.getSystemInfoSync();
  171. let query = uni.createSelectorQuery();
  172. query.select('.viewMore').boundingClientRect(data=>{
  173. let height = data.height;
  174. if(height > system.screenHeight) {
  175. this.viewMore = true
  176. }
  177. }).exec();
  178. },
  179. viewMores(){
  180. this.viewMore = true;
  181. },
  182. choose: function () {
  183. let freshen = false;
  184. this.$emit('freshen', {
  185. detail: freshen
  186. });
  187. },
  188. toIndex(){
  189. uni.switchTab({
  190. url: '/pages/index/index'
  191. })
  192. },
  193. getDateTimeStamp(dateStr){
  194. return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
  195. },
  196. getDetail() {
  197. let that = this;
  198. let apiUrl = '/api/content/detail';
  199. return new Promise((resolve,reject)=>{
  200. req.getRequest(apiUrl,{ id: that.id },async res => {
  201. if(res.productCategory){
  202. res.productCategory = res.productCategory.split(',')
  203. }
  204. that.detaile = res;
  205. that.getShareImg()
  206. that.webUrl = that.detaile.linkUrl?that.detaile.linkUrl:''
  207. let arr = [];
  208. if(res.productCategory && res.productCategory.length > 0){
  209. for (var i = 0; i < res.productCategory.length; i++) {
  210. await that.getProductList(res.productCategory[i], i).then(res=>{
  211. arr = arr.concat(res);
  212. that.productList = arr;
  213. console.log('相关产品=='+JSON.stringify(arr),arr.length)
  214. });
  215. }
  216. }
  217. resolve();
  218. },true);
  219. })
  220. },
  221. getBrowse(){
  222. var dataP = {};
  223. dataP.type =
  224. 4 //、产品 2、活动 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 12、用户须知 13、素材 15、医院科室 16、海报 20、医生 21 新闻 23名片
  225. dataP.behavior = 4; //1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  226. dataP.bindId = this.id;
  227. req.postRequestLoding('/api/v3/behavior/save', dataP, data => {
  228. req.saveBehaviorNew(this.id,3,4)
  229. });
  230. },
  231. // 用户行为
  232. userBehavior() {
  233. var dataP = {};
  234. dataP.type =
  235. 4 //、产品 2、活动 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 12、用户须知 13、素材 15、医院科室 16、海报 20、医生 21 新闻 23名片
  236. dataP.behavior = 3; //1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  237. dataP.bindId = this.id;
  238. req.postRequestLoding('/api/v3/behavior/save', dataP, data => {
  239. if(this.detaile.isThumbs){
  240. this.detaile.thumbsNumber = this.detaile.thumbsNumber-1
  241. }else{
  242. this.detaile.thumbsNumber = this.detaile.thumbsNumber+1
  243. }
  244. this.detaile.isThumbs = !this.detaile.isThumbs
  245. if(this.detaile.isThumbs){
  246. req.saveBehaviorNew(this.id,3,3)
  247. }else{
  248. req.saveBehaviorNew(this.id,3,11)
  249. }
  250. });
  251. },
  252. getContentList() {
  253. let form = this.form;
  254. form.cid = this.detaile.cid;
  255. if(req.getStorage("shareId")){
  256. form.shareSaleNo = req.getStorage("shareId")
  257. }
  258. req.getRequest('/api/content/list',form,res => {
  259. this.contentList = res;
  260. for (var i = 0; i < this.contentList.length; i++) {
  261. if (this.contentList[i].id == this.id){
  262. this.contentList.splice(i,1);
  263. }
  264. }
  265. });
  266. },
  267. },
  268. filters: {
  269. /**
  270. * 处理富文本里的图片宽度自适应
  271. * 1.去掉img标签里的style、width、height属性
  272. * 2.img标签添加style属性:max-width:100%;height:auto
  273. * 3.修改所有style里的width属性为max-width:100%
  274. * 4.去掉<br/>标签
  275. * @param html
  276. * @returns {void|string|*}
  277. */
  278. formatRichText(html) {
  279. //控制小程序中图片大小
  280. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  281. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  282. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  283. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  284. return match;
  285. });
  286. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  287. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  288. return match;
  289. });
  290. // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  291. newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
  292. newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
  293. newContent = newContent.replace(/font-size:[^;]+;?/g,'');
  294. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
  295. return newContent;
  296. }
  297. }
  298. };
  299. </script>
  300. <style>
  301. @import './content.css';
  302. </style>