content.vue 4.9 KB

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