index.vue 3.5 KB

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