qrcode-share.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "link": import.meta.env.SHOPRO_BASE_URL+"/#/pages/index/user?linkId="+state.linkId,
  66. "poster": {
  67. "type": "user"
  68. }
  69. };
  70. // #ifdef APP-PLUS
  71. poster.canvasId = 'canvasId-' + new Date().getTime();
  72. // #endif
  73. const canvas = await useCanvas(poster, vm);
  74. return canvas;
  75. }
  76. async function getLink(){
  77. //加载分享时用到的linkId
  78. ShareApi.getLinkId(1).then((res) => {
  79. if (res.code !== 0) {
  80. return;
  81. }
  82. state.linkId = res.data.linkId
  83. getPoster()
  84. });
  85. }
  86. // 保存海报图片
  87. const onSavePoster = () => {
  88. if (['WechatOfficialAccount', 'H5'].includes(sheep.$platform.name)) {
  89. sheep.$helper.toast(t('share.long_press_to_save') );
  90. return;
  91. }
  92. uni.saveImageToPhotosAlbum({
  93. filePath: poster.src,
  94. success: (res) => {
  95. onClosePoster();
  96. sheep.$helper.toast(t('share.save_success') );
  97. },
  98. fail: (err) => {
  99. sheep.$helper.toast(t('share.save_failure') );
  100. console.log('图片保存失败:', err);
  101. },
  102. });
  103. };
  104. // onBeforeMount(() => {
  105. // });
  106. onLoad(async()=>{
  107. await getLink()
  108. })
  109. </script>
  110. <style lang="scss" scoped>
  111. .confirm-btn {
  112. width: 710rpx;
  113. margin-left: 20rpx;
  114. height: 80rpx;
  115. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  116. border-radius: 40rpx;
  117. color: #fff;
  118. }
  119. .confirm-btn:first-child{
  120. margin-left: 0;
  121. }
  122. .hideCanvas {
  123. position: fixed;
  124. top: -99999rpx;
  125. left: -99999rpx;
  126. z-index: -99999;
  127. }
  128. </style>