index.vue 3.3 KB

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