index.vue 3.6 KB

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