index.vue 3.5 KB

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