qrcode-share.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <s-layout :title="t('share.share')" class="set-userinfo-wrap">
  3. <view class="ss-flex-col ss-col-center ss-row-center">
  4. <view v-if="poster.src === ''" class="poster-title ss-flex ss-row-center" :style="{
  5. height: poster.height + 'px',
  6. width: poster.width + 'px',
  7. }">
  8. {{ t('share.poster_loading') }}
  9. </view>
  10. <image v-else class="poster-img ss-m-20" :src="poster.src" :style="{
  11. height: poster.height + 'px',
  12. width: poster.width + 'px',
  13. }" :show-menu-by-longpress="true" />
  14. <canvas class="hideCanvas" :canvas-id="poster.canvasId" :id="poster.canvasId" :style="{
  15. height: poster.height + 'px',
  16. width: poster.width + 'px',
  17. }" />
  18. </view>
  19. <view class="modal-footer ss-flex ss-p-x-20">
  20. <button class="confirm-btn" @tap="onSavePoster">{{
  21. ['wechatOfficialAccount', 'H5'].includes(sheep.$platform.name)
  22. ? t('share.long_press_to_save')
  23. : t('share.long_press_to_save')
  24. }}</button>
  25. <!-- <button class="confirm-btn" @tap="showShareModal">分享</button> -->
  26. </view>
  27. </s-layout>
  28. </template>
  29. <script setup>
  30. import {
  31. computed,
  32. reactive,
  33. onBeforeMount,
  34. getCurrentInstance
  35. } from 'vue';
  36. import {
  37. onLoad
  38. } from '@dcloudio/uni-app';
  39. import sheep from '@/sheep';
  40. import useCanvas from '@/sheep/components/s-share-modal/canvas-poster/useCanvas';
  41. import {
  42. showAuthModal,
  43. showShareModal
  44. } from '@/sheep/hooks/useModal';
  45. import ShareApi from '@/sheep/api/distri/share';
  46. import { t } from '@/locale'
  47. const state = reactive({
  48. linkId:0
  49. })
  50. const poster = reactive({
  51. canvasId: 'canvasId',
  52. width: sheep.$platform.device.windowWidth * 0.9,
  53. height: 600,
  54. src: '',
  55. });
  56. const vm = getCurrentInstance();
  57. // 使用 canvas 生成海报
  58. async function getPoster(params) {
  59. poster.src = '';
  60. poster.shareInfo = {
  61. "title": "",
  62. "desc": "",
  63. "image": "",
  64. "path": "",
  65. "linkId": state.linkId,
  66. "link": import.meta.env.SHOPRO_BASE_URL+"/#/pages/index/user?linkId="+state.linkId,
  67. "poster": {
  68. "type": "user"
  69. }
  70. };
  71. // #ifdef APP-PLUS
  72. poster.canvasId = 'canvasId-' + new Date().getTime();
  73. // #endif
  74. const canvas = await useCanvas(poster, vm);
  75. return canvas;
  76. }
  77. async function getLink(){
  78. //加载分享时用到的linkId
  79. ShareApi.getLinkId(1).then((res) => {
  80. if (res.code !== 0) {
  81. return;
  82. }
  83. state.linkId = res.data.linkId
  84. getPoster()
  85. });
  86. }
  87. // 保存海报图片
  88. const onSavePoster = () => {
  89. if (['WechatOfficialAccount', 'H5'].includes(sheep.$platform.name)) {
  90. sheep.$helper.toast(t('share.long_press_to_save') );
  91. return;
  92. }
  93. uni.saveImageToPhotosAlbum({
  94. filePath: poster.src,
  95. success: (res) => {
  96. onClosePoster();
  97. sheep.$helper.toast(t('share.save_success') );
  98. },
  99. fail: (err) => {
  100. sheep.$helper.toast(t('share.save_failure') );
  101. console.log('图片保存失败:', err);
  102. },
  103. });
  104. };
  105. // onBeforeMount(() => {
  106. // });
  107. onLoad(async()=>{
  108. await getLink()
  109. })
  110. </script>
  111. <style lang="scss" scoped>
  112. .confirm-btn {
  113. width: 710rpx;
  114. margin-left: 20rpx;
  115. height: 80rpx;
  116. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  117. border-radius: 40rpx;
  118. color: #fff;
  119. }
  120. .confirm-btn:first-child{
  121. margin-left: 0;
  122. }
  123. .hideCanvas {
  124. position: fixed;
  125. top: -99999rpx;
  126. left: -99999rpx;
  127. z-index: -99999;
  128. }
  129. </style>