index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.views.length === 0"
  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. <!-- v-if 保障等待所有的海报信息都加载完后才开始渲染 -->
  16. <template v-if="poster.views.length > 0">
  17. <l-painter :board="poster" ref="painterRef"/>
  18. </template>
  19. </view>
  20. <view
  21. class="poster-btn-box ss-m-t-20 ss-flex ss-row-between ss-col-center"
  22. v-if="poster.views.length > 0"
  23. >
  24. <button class="cancel-btn ss-reset-button" @tap="onClosePoster">取消</button>
  25. <button class="save-btn ss-reset-button ui-BG-Main" @tap="onSavePoster">
  26. {{
  27. ['wechatOfficialAccount', 'H5'].includes(sheep.$platform.name)
  28. ? '长按图片保存'
  29. : '保存图片'
  30. }}
  31. </button>
  32. </view>
  33. </su-popup>
  34. </template>
  35. <script setup>
  36. import { reactive, ref } from 'vue';
  37. import sheep from '@/sheep';
  38. import { getPosterData } from '@/sheep/components/s-share-modal/canvas-poster/poster';
  39. const props = defineProps({
  40. show: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. shareInfo: {
  45. type: Object,
  46. default() {
  47. },
  48. },
  49. });
  50. const poster = reactive({
  51. css: {
  52. // 根节点若无尺寸,自动获取父级节点
  53. width: sheep.$platform.device.windowWidth * 0.9,
  54. height: 600
  55. },
  56. views: [],
  57. });
  58. const emits = defineEmits(['success', 'close']);
  59. const onClosePoster = () => {
  60. emits('close');
  61. };
  62. const painterRef = ref()
  63. // 保存海报图片
  64. const onSavePoster = () => {
  65. if (['WechatOfficialAccount', 'H5'].includes(sheep.$platform.name)) {
  66. sheep.$helper.toast('请长按图片保存');
  67. return;
  68. }
  69. painterRef.value.canvasToTempFilePathSync({
  70. fileType: "jpg",
  71. // 如果返回的是base64是无法使用 saveImageToPhotosAlbum,需要设置 pathType为url
  72. pathType: 'url',
  73. quality: 1,
  74. success: (res) => {
  75. console.log(res.tempFilePath);
  76. // 非H5 保存到相册
  77. uni.saveImageToPhotosAlbum({
  78. filePath: res.tempFilePath,
  79. success: (res) => {
  80. onClosePoster();
  81. sheep.$helper.toast('保存成功');
  82. },
  83. fail: (err) => {
  84. sheep.$helper.toast('保存失败');
  85. console.log('图片保存失败:', err);
  86. },
  87. });
  88. },
  89. });
  90. };
  91. // 获得海报数据
  92. async function getPoster() {
  93. poster.views = await getPosterData({
  94. width: poster.css.width,
  95. shareInfo: props.shareInfo,
  96. });
  97. }
  98. defineExpose({
  99. getPoster,
  100. });
  101. </script>
  102. <style lang="scss" scoped>
  103. .popup-box {
  104. position: relative;
  105. }
  106. .poster-title {
  107. color: #999;
  108. }
  109. // 分享海报
  110. .poster-btn-box {
  111. width: 600rpx;
  112. position: absolute;
  113. left: 50%;
  114. transform: translateX(-50%);
  115. bottom: -80rpx;
  116. .cancel-btn {
  117. width: 240rpx;
  118. height: 70rpx;
  119. line-height: 70rpx;
  120. background: $white;
  121. border-radius: 35rpx;
  122. font-size: 28rpx;
  123. font-weight: 500;
  124. color: $dark-9;
  125. }
  126. .save-btn {
  127. width: 240rpx;
  128. height: 70rpx;
  129. line-height: 70rpx;
  130. border-radius: 35rpx;
  131. font-size: 28rpx;
  132. font-weight: 500;
  133. }
  134. }
  135. .poster-img {
  136. border-radius: 20rpx;
  137. }
  138. </style>