index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!-- 海报弹窗 -->
  2. <template>
  3. <su-popup :show="show" round="10" @close="onClosePoster" type="center" class="popup-box">
  4. <view class="ss-flex-col ss-col-center ss-row-center">
  5. <!-- <image class="poster-img" :src="imageUrl"/> -->
  6. <view v-if="poster.src === ''" class="poster-title ss-flex ss-row-center" :style="{
  7. height: poster.height + 'px',
  8. width: poster.width + 'px',
  9. }">
  10. {{ t('share.poster_loading') }}
  11. </view>
  12. <image v-else class="poster-img" :src="poster.src" :style="{
  13. height: poster.height + 'px',
  14. width: poster.width + 'px',
  15. }" :show-menu-by-longpress="true" />
  16. <canvas class="hideCanvas" :canvas-id="poster.canvasId" :id="poster.canvasId" :style="{
  17. height: poster.height + 'px',
  18. width: poster.width + 'px',
  19. }" />
  20. <view class="poster-btn-box ss-m-t-20 ss-flex ss-row-between ss-col-center" v-if="poster.src !== ''">
  21. <button class="cancel-btn ss-reset-button" @tap="onClosePoster">{{ t('common.cancel') }}</button>
  22. <button class="save-btn ss-reset-button ui-BG-Main" @tap="onSavePoster">
  23. {{t('share.long_press_to_save') }}
  24. <!-- {{
  25. ['wechatOfficialAccount', 'H5'].includes(sheep.$platform.name)
  26. ? '保存图片'
  27. : '长按图片保存'
  28. }} -->
  29. </button>
  30. </view>
  31. </view>
  32. </su-popup>
  33. </template>
  34. <script setup>
  35. import {
  36. reactive,
  37. getCurrentInstance,
  38. ref
  39. } from 'vue';
  40. import sheep from '@/sheep';
  41. import useCanvas from './useCanvas';
  42. import { t } from '@/locale'
  43. // import FileApi from '@/sheep/api/infra/file';
  44. const props = defineProps({
  45. show: {
  46. type: Boolean,
  47. default: false,
  48. },
  49. shareInfo: {
  50. type: Object,
  51. default () {},
  52. },
  53. });
  54. const poster = reactive({
  55. canvasId: 'canvasId',
  56. width: sheep.$platform.device.windowWidth * 0.9,
  57. height: 600,
  58. src: '',
  59. imgData:''
  60. });
  61. const emits = defineEmits(['success', 'close']);
  62. const vm = getCurrentInstance();
  63. const onClosePoster = () => {
  64. emits('close');
  65. };
  66. // 保存海报图片
  67. const onSavePoster = () => {
  68. if (['WechatOfficialAccount', 'H5'].includes(sheep.$platform.name)) {
  69. sheep.$helper.toast(t('share.long_press_to_save') );
  70. return;
  71. }
  72. uni.saveImageToPhotosAlbum({
  73. filePath: poster.src,
  74. success: (res) => {
  75. onClosePoster();
  76. sheep.$helper.toast(t('share.save_success'));
  77. },
  78. fail: (err) => {
  79. sheep.$helper.toast(t('share.save_failure'));
  80. console.log('图片保存失败:', err);
  81. },
  82. });
  83. };
  84. const imageUrl = ref("")
  85. async function getPoster(params) {
  86. // console.log(props.shareInfo)
  87. poster.src = '';
  88. poster.shareInfo = props.shareInfo;
  89. // #ifdef APP-PLUS
  90. poster.canvasId = 'canvasId-' + new Date().getTime();
  91. // #endif
  92. const canvas = await useCanvas(poster, vm);
  93. return canvas;
  94. }
  95. defineExpose({
  96. getPoster,
  97. });
  98. </script>
  99. <style lang="scss" scoped>
  100. .popup-box {
  101. position: relative;
  102. }
  103. .poster-title {
  104. color: #999;
  105. }
  106. // 分享海报
  107. .poster-btn-box {
  108. width: 600rpx;
  109. position: absolute;
  110. left: 50%;
  111. transform: translateX(-50%);
  112. bottom: -80rpx;
  113. .cancel-btn {
  114. width: 280rpx;
  115. height: 70rpx;
  116. line-height: 70rpx;
  117. background: $white;
  118. border-radius: 35rpx;
  119. font-size: 28rpx;
  120. font-weight: 500;
  121. color: $dark-9;
  122. }
  123. .save-btn {
  124. width: 280rpx;
  125. height: 70rpx;
  126. line-height: 70rpx;
  127. border-radius: 35rpx;
  128. font-size: 28rpx;
  129. font-weight: 500;
  130. }
  131. }
  132. .poster-img {
  133. border-radius: 20rpx;
  134. }
  135. .hideCanvas {
  136. position: fixed;
  137. top: -99999rpx;
  138. left: -99999rpx;
  139. z-index: -99999;
  140. }
  141. </style>