ShareSpell.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <div>
  3. <tui-bottom-popup :show="shareShow" @close="cancel(1)">
  4. <view class="share">
  5. <text class="h3">邀请好友</text>
  6. <view class="share-list">
  7. <view class="ul">
  8. <!-- #ifdef APP-PLUS -->
  9. <view class="li" @click="share('weixin')">
  10. <image class="icon" src="../../static/images/new-business/shop/weixin2x.png"></image>
  11. <label class="label">微信</label>
  12. </view>
  13. <view class="li" @click="share('weixinpyq')">
  14. <image class="icon" src="../../static/images/new-business/shop/pyq.png"></image>
  15. <label class="label">朋友圈</label>
  16. </view>
  17. <!-- #endif -->
  18. <!-- #ifdef MP-WEIXIN -->
  19. <view class="li">
  20. <button open-type="share" :data-obj="wxShareData" class="share-button" @share="onShareAppMessage">
  21. <image class="icon" src="../../static/images/new-business/shop/forward.png"></image>
  22. <label class="label">好友</label>
  23. </button>
  24. </view>
  25. <view class="li" @click="share('weixinpyq')">
  26. <image class="icon" src="../../static/images/new-business/shop/pyq.png"></image>
  27. <label class="label">朋友圈</label>
  28. </view>
  29. <!-- #endif -->
  30. <view class="li" @click="share('lianjie')">
  31. <image class="icon" src="../../static/images/new-business/shop/lianjie2.png"></image>
  32. <label class="label">链接</label>
  33. </view>
  34. </view>
  35. </view>
  36. <view class="btn-close" @click="cancel(1)">取消</view>
  37. </view>
  38. </tui-bottom-popup>
  39. <tui-bottom-popup :show="wapShow" @close="cancel(2)">
  40. <view class="share-h5">
  41. <view class="text">
  42. 点击浏览器下方
  43. <view class="icon">
  44. <tui-icon :size="28" color="#ffffff" name="more-fill"></tui-icon>
  45. </view>
  46. 即可进行分享
  47. </view>
  48. </view>
  49. </tui-bottom-popup>
  50. </div>
  51. </template>
  52. <script>
  53. import { A_TF_MAIN } from '../../config/index'
  54. export default {
  55. name: 'ShareSpell',
  56. props: {
  57. img: {
  58. type: String,
  59. default: ''
  60. },
  61. url: {
  62. type: String,
  63. dafault: ''
  64. },
  65. title: {
  66. type: String,
  67. dafault: ''
  68. },
  69. urlParms: {
  70. type: String,
  71. default: ''
  72. }
  73. },
  74. onShareAppMessage(e) {
  75. if (e.from == 'button') {
  76. // 点击button
  77. }
  78. if (e.from == 'menu') {
  79. // 点击右上角按钮
  80. }
  81. // 获取按钮传进来的参数 data 中的item值
  82. const params = e.target.dataset.obj// 获取的为 data 中定义的item值
  83. return {
  84. path: `/another-tf/another-serve/inviteSpell/index?${params.url}`
  85. }
  86. },
  87. data() {
  88. return {
  89. shareShow: false,
  90. wapShow: false,
  91. wxShareData: {
  92. url: ''
  93. }
  94. }
  95. },
  96. computed: {
  97. longUrl() {
  98. return A_TF_MAIN + '/#' + this.url
  99. }
  100. },
  101. mounted() {
  102. this.wxShareData = this.url
  103. },
  104. methods: {
  105. cancel(key) {
  106. if (key === 1) {
  107. this.shareShow = false
  108. this.$emit('shareCancel')
  109. } else if (key === 2) {
  110. this.wapShow = false
  111. }
  112. },
  113. wxShare(type) {
  114. // #ifdef APP
  115. uni.share({
  116. provider: 'weixin',
  117. type: 0,
  118. title: this.title,
  119. scene: type, // WXSceneSession会话 WXSceneTimeline朋友圈
  120. imageUrl: this.img,
  121. href: this.longUrl,
  122. success: () => {
  123. },
  124. fail: (err) => {
  125. throw Error(err)
  126. }
  127. })
  128. // #endif
  129. // #ifdef MP-WEIXIN
  130. uni.showToast({
  131. title: '请点击右上角打开菜单进行朋友圈分享',
  132. icon: 'none'
  133. })
  134. // #endif
  135. },
  136. share(key) {
  137. switch (key) {
  138. case 'weixin':
  139. this.wxShare('WXSceneSession')
  140. break
  141. case 'weixinpyq':
  142. this.wxShare('WXSceneTimeline')
  143. break
  144. case 'qq':
  145. break
  146. case 'weibo':
  147. break
  148. case 'lianjie':
  149. uni.setClipboardData({
  150. data: this.title + this.longUrl,
  151. success: () => {
  152. uni.showToast({
  153. title: '复制成功'
  154. })
  155. }
  156. })
  157. break
  158. }
  159. },
  160. // 重写分享方法
  161. overShare() {
  162. // 监听路由切换
  163. // 间接实现全局设置分享内容
  164. wx.onAppRoute(function (res) {
  165. // 获取加载的页面
  166. const pages = getCurrentPages()
  167. // 获取当前页面的对象
  168. const view = pages[pages.length - 1]
  169. let data
  170. if (view) {
  171. data = view.data
  172. if (!data.isOverShare) {
  173. data.isOverShare = true
  174. view.onShareAppMessage = function () {
  175. // 你的分享配置
  176. return {
  177. title: '标题',
  178. path: '/pages/xxxxx'
  179. }
  180. }
  181. }
  182. }
  183. })
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. .share {
  190. background-color: #F8F8F8;
  191. text-align: center;
  192. .h3 {
  193. font-size: 30rpx;
  194. color: #333333;
  195. line-height: 42rpx;
  196. padding: 30rpx 0;
  197. border-bottom: 2px solid #F0F0F0;
  198. display: block;
  199. }
  200. .share-list {
  201. padding: 40rpx 0 54rpx;
  202. .ul {
  203. display: flex;
  204. justify-content: space-around;
  205. .li {
  206. &::after {
  207. border: none;
  208. }
  209. .icon {
  210. display: block;
  211. width: 92rpx;
  212. height: 92rpx;
  213. }
  214. .label {
  215. padding-top: 22rpx;
  216. font-size: 24rpx;
  217. line-height: 34rpx;
  218. color: #333333;
  219. display: block;
  220. }
  221. }
  222. }
  223. }
  224. .btn-close {
  225. background-color: #fff;
  226. padding: 30rpx 0;
  227. font-size: 26rpx;
  228. color: #333;
  229. }
  230. }
  231. .share-h5 {
  232. padding: 0 20rpx;
  233. display: flex;
  234. justify-content: center;
  235. .text {
  236. line-height: 100rpx;
  237. font-size: 30px;
  238. .icon {
  239. background-color: #333;
  240. border-radius: 50%;
  241. display: inline-block;
  242. width: 40rpx;
  243. height: 40rpx;
  244. text-align: center;
  245. line-height: 40rpx;
  246. margin: 0 10rpx;
  247. }
  248. }
  249. }
  250. .share-button::after {
  251. border: none;
  252. }
  253. </style>