index.vue 3.4 KB

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