content.vue 4.9 KB

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