index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view style="position: relative;">
  3. <view v-if="shareUrl" class="d-flex align-center flex-column justify-center" style="height: 100vh;">
  4. <image v-if="!!shareUrl" :style="imgStyle" @click="handlePreviewSharePoster" :src="shareUrl" :show-menu-by-longpress="true"></image>
  5. <view class="color-999 ss-m-t-20 font-size-14 ss-m-b-50">点击图片预览,长按图片保存</view>
  6. </view>
  7. <canvas canvas-id="posterCanvas" class="shareCanvas" style="width: 330px; height: 600px;"></canvas>
  8. </view>
  9. </template>
  10. <script setup>
  11. import { ref, computed } from 'vue'
  12. import { userStore } from '@/store/user'
  13. import { onLoad } from '@dcloudio/uni-app'
  14. import { getUserAvatar } from '@/utils/avatar'
  15. const useUserStore = userStore()
  16. const baseInfo = computed(() => useUserStore?.baseInfo)
  17. const shareUrl = ref('')
  18. const windowWidth = ref(0)
  19. onLoad(() => {
  20. const windowInfo = wx.getWindowInfo()
  21. windowWidth.value = windowInfo.windowWidth
  22. console.log(windowWidth.value, '当前机型屏幕宽')
  23. })
  24. const imgStyle = computed(() => {
  25. if (windowWidth.value <= 320) return `width: 259px; height: 460px;`
  26. if (windowWidth.value > 320 && windowWidth.value <= 375) return `width: 313px; height: 560px;`
  27. if (windowWidth.value > 375) return `width: 328px; height: 600px;`
  28. })
  29. // 图片预览
  30. const handlePreviewSharePoster = () => {
  31. uni.previewImage({
  32. current: 0,
  33. longPressActions: {
  34. itemList: ['发送给朋友', '保存图片', '收藏']
  35. },
  36. urls: [shareUrl.value]
  37. })
  38. }
  39. const getImageTempRatio = (url) => {
  40. return new Promise((req, rej)=>{
  41. wx.getImageInfo({
  42. src:url,
  43. success:(res) =>{
  44. req(res)
  45. }
  46. })
  47. })
  48. }
  49. // 个人头像
  50. const drawAvatar = (ctx, imagePath, x, y, radius) => {
  51. ctx.save() // 保存当前绘图上下文
  52. // 绘制圆形裁剪路径
  53. ctx.beginPath();
  54. ctx.arc(x + radius, y + radius, radius, 0, 2 * Math.PI)
  55. ctx.clip();
  56. // 绘制图片
  57. ctx.drawImage(imagePath, x, y, 2 * radius, 2 * radius) // 注意这里的宽高是直径的2倍
  58. ctx.restore() // 恢复之前保存的绘图上下文
  59. }
  60. const createPoster = async () => {
  61. uni.showLoading({ title: '生成中...', mask: true })
  62. var context = uni.createCanvasContext('posterCanvas')
  63. //清空画布
  64. context.clearRect(0, 0, 330, 600)
  65. //背景图片
  66. const { path: bgUrl } = await getImageTempRatio('https://minio.menduner.com/dev/63a182780d26bd552819431b0ca94be8e3c90a90e2f933c61e4c83b4614f2fc6.jpg')
  67. context.drawImage(bgUrl, 0, 0, 330, 600) // 路径、x、y、宽、高
  68. // 头像
  69. const avatarUrl = getUserAvatar(baseInfo.value?.avatar, baseInfo.value?.sex)
  70. const { path } = await getImageTempRatio(avatarUrl)
  71. drawAvatar(context, path, 124, 22, 40.5) // canvas、图片路径、x、y、半径
  72. //分享二维码
  73. const { path: qrCode } = await getImageTempRatio('https://minio.citupro.com/dev/menduner/miniProgram.jpg')
  74. context.drawImage(qrCode, 105.5, 312.5, 120, 120)
  75. context.draw(false, () =>{
  76. wx.canvasToTempFilePath({
  77. canvasId: 'posterCanvas',
  78. success:(res)=>{
  79. shareUrl.value = res.tempFilePath
  80. console.log('canvas-success', shareUrl.value)
  81. uni.hideLoading({})
  82. },
  83. fail:(err)=>{
  84. uni.hideLoading({})
  85. console.log('canvas-fail', err)
  86. }
  87. })
  88. })
  89. }
  90. createPoster()
  91. </script>
  92. <style scoped lang="scss">
  93. .shareCanvas {
  94. position: fixed;
  95. top: -99999upx;
  96. left: -99999upx;
  97. z-index: -99999;
  98. }
  99. </style>