list2detail-detail.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <view>
  3. <view class="banner">
  4. <image class="banner-img" :src="banner.cover"></image>
  5. <view class="banner-title">{{banner.title}}</view>
  6. </view>
  7. <view class="article-meta">
  8. <text class="article-author">{{banner.author_name}}</text>
  9. <text class="article-text">发表于</text>
  10. <text class="article-time">{{banner.published_at}}</text>
  11. </view>
  12. <view class="article-content">
  13. <rich-text :nodes="htmlNodes"></rich-text>
  14. </view>
  15. <!-- #ifdef MP-WEIXIN || MP-QQ -->
  16. <ad v-if="htmlNodes.length > 0" unit-id="adunit-01b7a010bf53d74e"></ad>
  17. <!-- #endif -->
  18. </view>
  19. </template>
  20. <script>
  21. const DETAIL_PAGE_PATH = '/pages/template/list2detail-detail/list2detail-detail';
  22. import htmlParser from '@/common/html-parser.js'
  23. function _handleShareChannels(provider) {
  24. let channels = [];
  25. for (let i = 0, len = provider.length; i < len; i++) {
  26. switch (provider[i]) {
  27. case 'weixin':
  28. channels.push({
  29. text: '分享到微信好友',
  30. id: 'weixin',
  31. sort: 0
  32. });
  33. channels.push({
  34. text: '分享到微信朋友圈',
  35. id: 'weixin',
  36. sort: 1
  37. });
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. channels.sort(function(x, y) {
  44. return x.sort - y.sort;
  45. });
  46. return channels;
  47. }
  48. export default {
  49. data() {
  50. return {
  51. title: '',
  52. banner: {},
  53. htmlNodes: []
  54. }
  55. },
  56. onLoad(event) {
  57. // TODO 后面把参数名替换成 payload
  58. const payload = event.detailDate || event.payload;
  59. // 目前在某些平台参数会被主动 decode,暂时这样处理。
  60. try {
  61. this.banner = JSON.parse(decodeURIComponent(payload));
  62. } catch (error) {
  63. this.banner = JSON.parse(payload);
  64. }
  65. uni.setNavigationBarTitle({
  66. title: this.banner.title
  67. });
  68. this.getDetail();
  69. },
  70. onShareAppMessage() {
  71. return {
  72. title: this.banner.title,
  73. path: DETAIL_PAGE_PATH + '?detailDate=' + JSON.stringify(this.banner)
  74. }
  75. },
  76. onNavigationBarButtonTap(event) {
  77. const buttonIndex = event.index;
  78. if (buttonIndex === 0) {
  79. // 分享 H5 的页面
  80. const shareProviders = [];
  81. uni.getProvider({
  82. service: 'share',
  83. success: (result) => {
  84. // 目前仅考虑分享到微信
  85. if (result.provider && result.provider.length && ~result.provider.indexOf('weixin')) {
  86. const channels = _handleShareChannels(result.provider);
  87. uni.showActionSheet({
  88. itemList: channels.map(channel => {
  89. return channel.text;
  90. }),
  91. success: (result) => {
  92. const tapIndex = result.tapIndex;
  93. uni.share({
  94. provider: 'weixin',
  95. type: 0,
  96. title: this.banner.title,
  97. scene: tapIndex === 0 ? 'WXSceneSession' : 'WXSenceTimeline',
  98. href: 'https://uniapp.dcloud.io/h5' + DETAIL_PAGE_PATH + '?detailDate=' + JSON.stringify(this.banner),
  99. imageUrl: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b6304f00-5168-11eb-bd01-97bc1429a9ff.png'
  100. });
  101. }
  102. });
  103. } else {
  104. uni.showToast({
  105. title: '未检测到可用的微信分享服务'
  106. });
  107. }
  108. },
  109. fail: (error) => {
  110. uni.showToast({
  111. title: '获取分享服务失败'
  112. });
  113. }
  114. });
  115. }
  116. },
  117. methods: {
  118. getDetail() {
  119. uni.request({
  120. url: 'https://unidemo.dcloud.net.cn/api/news/36kr/' + this.banner.post_id,
  121. success: (data) => {
  122. if (data.statusCode == 200) {
  123. var htmlString = data.data.content.replace(/\\/g, "").replace(/<img/g, "<img style=\"display:none;\"");
  124. this.htmlNodes = htmlParser(htmlString);
  125. }
  126. },
  127. fail: () => {
  128. console.log('fail');
  129. }
  130. });
  131. }
  132. }
  133. }
  134. </script>
  135. <style>
  136. .banner {
  137. height: 360rpx;
  138. overflow: hidden;
  139. position: relative;
  140. background-color: #ccc;
  141. }
  142. .banner-img {
  143. width: 100%;
  144. }
  145. .banner-title {
  146. max-height: 84rpx;
  147. overflow: hidden;
  148. position: absolute;
  149. left: 30rpx;
  150. bottom: 30rpx;
  151. width: 90%;
  152. font-size: 32rpx;
  153. font-weight: 400;
  154. line-height: 42rpx;
  155. color: white;
  156. z-index: 11;
  157. }
  158. .article-meta {
  159. padding: 20rpx 40rpx;
  160. display: flex;
  161. flex-direction: row;
  162. justify-content: flex-start;
  163. color: gray;
  164. }
  165. .article-text {
  166. font-size: 26rpx;
  167. line-height: 50rpx;
  168. margin: 0 20rpx;
  169. }
  170. .article-author,
  171. .article-time {
  172. font-size: 30rpx;
  173. }
  174. .article-content {
  175. padding: 0 30rpx;
  176. overflow: hidden;
  177. font-size: 30rpx;
  178. margin-bottom: 30rpx;
  179. }
  180. </style>