index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. import { getJobAdvertisedShareQrcode } from '@/api/user'
  16. const useUserStore = userStore()
  17. const baseInfo = computed(() => useUserStore?.baseInfo)
  18. const shareUrl = ref('')
  19. const windowWidth = ref(0)
  20. onLoad(() => {
  21. const windowInfo = wx.getWindowInfo()
  22. windowWidth.value = windowInfo.windowWidth
  23. console.log(windowWidth.value, '当前机型屏幕宽')
  24. })
  25. const imgStyle = computed(() => {
  26. if (windowWidth.value <= 320) return `width: 259px; height: 460px;`
  27. if (windowWidth.value > 320 && windowWidth.value <= 375) return `width: 313px; height: 560px;`
  28. if (windowWidth.value > 375) return `width: 328px; height: 600px;`
  29. })
  30. // 图片预览
  31. const handlePreviewSharePoster = () => {
  32. uni.previewImage({
  33. current: 0,
  34. longPressActions: {
  35. itemList: ['发送给朋友', '保存图片', '收藏']
  36. },
  37. urls: [shareUrl.value]
  38. })
  39. }
  40. const getImageTempRatio = (url) => {
  41. return new Promise((req, rej)=>{
  42. wx.getImageInfo({
  43. src:url,
  44. success:(res) =>{
  45. req(res)
  46. }
  47. })
  48. })
  49. }
  50. // 个人头像
  51. const drawAvatar = (ctx, imagePath, x, y, radius) => {
  52. ctx.save() // 保存当前绘图上下文
  53. // 绘制圆形裁剪路径
  54. ctx.beginPath();
  55. ctx.arc(x + radius, y + radius, radius, 0, 2 * Math.PI)
  56. ctx.clip();
  57. // 绘制图片
  58. ctx.drawImage(imagePath, x, y, 2 * radius, 2 * radius) // 注意这里的宽高是直径的2倍
  59. ctx.restore() // 恢复之前保存的绘图上下文
  60. }
  61. const getToLocal = (base64data) => {
  62. const fsm = wx.getFileSystemManager()
  63. const FILE_BASE_NAME = 'tmp_base64src'; //自定义文件名
  64. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
  65. if (!format) {
  66. return (new Error('ERROR_BASE64SRC_PARSE'))
  67. }
  68. const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`
  69. const buffer = wx.base64ToArrayBuffer(bodyData);
  70. fsm.writeFile({
  71. filePath,
  72. data: buffer,
  73. encoding: 'binary',
  74. success(r) {
  75. console.log(filePath,'filePath')
  76. qrCode.value = filePath
  77. },
  78. fail() {
  79. return (new Error('ERROR_BASE64SRC_WRITE'))
  80. }
  81. })
  82. }
  83. // 生成分享二维码
  84. const qrCode = ref()
  85. const handleShareCode = async () => {
  86. const userId = useUserStore?.accountInfo?.userId || ''
  87. const query = {
  88. scene: 'shareId=' + userId,
  89. path: 'pages/login/index',
  90. width: 200,
  91. autoColor: false,
  92. checkPath: true,
  93. hyaline: true
  94. }
  95. const res = await getJobAdvertisedShareQrcode(query)
  96. const data = res?.data ? 'data:image/png;base64,' + res.data : ''
  97. getToLocal(data)
  98. }
  99. const createPoster = async () => {
  100. uni.showLoading({ title: '生成中...', mask: true })
  101. await handleShareCode() // 生产分享二维码
  102. var context = uni.createCanvasContext('posterCanvas')
  103. //清空画布
  104. context.clearRect(0, 0, 330, 600)
  105. //背景图片
  106. const { path: bgUrl } = await getImageTempRatio('https://minio.menduner.com/dev/63a182780d26bd552819431b0ca94be8e3c90a90e2f933c61e4c83b4614f2fc6.jpg')
  107. context.drawImage(bgUrl, 0, 0, 330, 600) // 路径、x、y、宽、高
  108. // 头像
  109. const avatarUrl = getUserAvatar(baseInfo.value?.avatar, baseInfo.value?.sex)
  110. const { path } = await getImageTempRatio(avatarUrl)
  111. drawAvatar(context, path, 124, 22, 40.5) // canvas、图片路径、x、y、半径
  112. //分享二维码
  113. // const { path: qrCode } = await getImageTempRatio('https://minio.citupro.com/dev/menduner/miniProgram.jpg')
  114. context.drawImage(qrCode.value, 105.5, 312.5, 120, 120)
  115. context.draw(false, () =>{
  116. wx.canvasToTempFilePath({
  117. canvasId: 'posterCanvas',
  118. success:(res)=>{
  119. shareUrl.value = res.tempFilePath
  120. console.log('canvas-success', shareUrl.value)
  121. uni.hideLoading({})
  122. },
  123. fail:(err)=>{
  124. uni.hideLoading({})
  125. console.log('canvas-fail', err)
  126. }
  127. })
  128. })
  129. }
  130. createPoster()
  131. </script>
  132. <style scoped lang="scss">
  133. .shareCanvas {
  134. position: fixed;
  135. top: -99999upx;
  136. left: -99999upx;
  137. z-index: -99999;
  138. }
  139. </style>