ShareBtn.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="wrap safe-area" :style="[getStyle]">
  3. <view class="btn share-btn" @click="handleShare">
  4. <image :src="getFileUrl('home/share-icon.png')" mode="aspectFill"></image>
  5. </view>
  6. </view>
  7. </template>
  8. <script setup>
  9. import { computed, nextTick, ref, unref } from 'vue';
  10. import { getFileUrl } from '@/utils/file.js';
  11. import { getCurrentPageUrlWithArgs } from '@/utils/request.js'
  12. import { SHARE_ID, objectTOString, useShare } from './hook.js'
  13. import { SharePosterGetShareQrcode } from "@/api"
  14. import { omit } from 'lodash-es';
  15. const { getShareInfo } = useShare()
  16. const props = defineProps({
  17. // 1首页,2店铺详情,3产品详情,4景点详情
  18. type: { type: [String, Number], default: 1 },
  19. offsetY: { type: Number, default: 0 },
  20. })
  21. const tempFilePath = ref('') // 本地缓存路径
  22. const getStyle = computed(() => {
  23. const style = {};
  24. style.bottom = `${180+props.offsetY}rpx`;
  25. return style;
  26. })
  27. // 分享
  28. async function handleShare(){
  29. // #ifdef MP-WEIXIN
  30. if(unref(tempFilePath)){
  31. handleShowShareImageMenu()
  32. return;
  33. }
  34. const pages = getCurrentPages();
  35. const currentPage = pages[pages.length - 1];
  36. const route = currentPage.route; // 页面路径
  37. const options = omit(currentPage.options, ['scene']); // 页面参数
  38. if(getShareInfo(SHARE_ID)) options[SHARE_ID] = getShareInfo(SHARE_ID);
  39. console.log('options', options, objectTOString(options))
  40. uni.showLoading({ title: '海报生成中···', mask: true });
  41. try {
  42. const { data } = await SharePosterGetShareQrcode({
  43. pathName: route,
  44. search: objectTOString(options),
  45. type: props.type
  46. })
  47. wx.downloadFile({
  48. url: data,// getFileUrl(imgUrl),
  49. success: (res) => {
  50. console.log('SharePosterGetShareQrcode', res)
  51. if(res.statusCode === 200){
  52. tempFilePath.value = res.tempFilePath
  53. handleShowShareImageMenu()
  54. uni.hideLoading();
  55. }else{
  56. uni.hideLoading();
  57. nextTick(() => {
  58. uni.showToast({ title: '分享失败', icon: 'none' })
  59. })
  60. }
  61. },
  62. fail: (res) => {
  63. console.log('res', res)
  64. }
  65. })
  66. } catch (error) {
  67. uni.hideLoading();
  68. //TODO handle the exception
  69. }
  70. // #endif
  71. }
  72. // 调起wx分享
  73. function handleShowShareImageMenu(){
  74. wx.showShareImageMenu({
  75. path: unref(tempFilePath),
  76. style: 'v2',
  77. needShowEntrance: true,
  78. success: (res1) => {
  79. console.log("分享成功", res1);
  80. },
  81. fail: (error) => {
  82. console.log("分享失败", error);
  83. }
  84. })
  85. }
  86. </script>
  87. <style scoped lang="scss">
  88. image { height: 100%; width: 100%; }
  89. .wrap{
  90. position: fixed;
  91. right: 32rpx;
  92. z-index: 9;
  93. display: flex;
  94. align-items: flex-end;
  95. .share-btn{
  96. width: 80rpx;
  97. height: 80rpx;
  98. }
  99. }
  100. </style>