qrcode-share.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <s-layout title="我的二维码/分享页" 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. 海报加载中...
  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. ? '长按图片保存'
  23. : '长按图片保存'
  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. const state = reactive({
  47. linkId:0
  48. })
  49. const poster = reactive({
  50. canvasId: 'canvasId',
  51. width: sheep.$platform.device.windowWidth * 0.9,
  52. height: 600,
  53. src: '',
  54. });
  55. const vm = getCurrentInstance();
  56. // 使用 canvas 生成海报
  57. async function getPoster(params) {
  58. poster.src = '';
  59. poster.shareInfo = {
  60. "title": "",
  61. "desc": "",
  62. "image": "",
  63. "path": "",
  64. "link": import.meta.env.SHOPRO_BASE_URL+"/#/pages/index/user?linkId="+state.linkId,
  65. "poster": {
  66. "type": "user"
  67. }
  68. };
  69. // #ifdef APP-PLUS
  70. poster.canvasId = 'canvasId-' + new Date().getTime();
  71. // #endif
  72. const canvas = await useCanvas(poster, vm);
  73. return canvas;
  74. }
  75. async function getLink(){
  76. //加载分享时用到的linkId
  77. ShareApi.getLinkId(1).then((res) => {
  78. if (res.code !== 0) {
  79. return;
  80. }
  81. state.linkId = res.data.linkId
  82. getPoster()
  83. });
  84. }
  85. // 保存海报图片
  86. const onSavePoster = () => {
  87. if (['WechatOfficialAccount', 'H5'].includes(sheep.$platform.name)) {
  88. sheep.$helper.toast('请长按图片保存');
  89. return;
  90. }
  91. uni.saveImageToPhotosAlbum({
  92. filePath: poster.src,
  93. success: (res) => {
  94. onClosePoster();
  95. sheep.$helper.toast('保存成功');
  96. },
  97. fail: (err) => {
  98. sheep.$helper.toast('保存失败');
  99. console.log('图片保存失败:', err);
  100. },
  101. });
  102. };
  103. // onBeforeMount(() => {
  104. // });
  105. onLoad(async()=>{
  106. await getLink()
  107. })
  108. </script>
  109. <style lang="scss" scoped>
  110. .confirm-btn {
  111. width: 710rpx;
  112. margin-left: 20rpx;
  113. height: 80rpx;
  114. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  115. border-radius: 40rpx;
  116. color: #fff;
  117. }
  118. .confirm-btn:first-child{
  119. margin-left: 0;
  120. }
  121. .hideCanvas {
  122. position: fixed;
  123. top: -99999rpx;
  124. left: -99999rpx;
  125. z-index: -99999;
  126. }
  127. </style>