index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, w, h) => {
  52. ctx.save() // 保存当前绘图上下文
  53. // 绘制圆形裁剪路径
  54. ctx.beginPath();
  55. ctx.arc(x + radius, y + radius, radius, 0, 2 * Math.PI)
  56. ctx.clip();
  57. const rate = w / h
  58. // 绘制图片
  59. if (rate > 1) {
  60. const _len = 2 * radius / rate // lenH
  61. const _yStart = radius - _len / 2 + y
  62. ctx.drawImage(imagePath, x, _yStart, 2 * radius, _len) // 注意这里的宽高是直径的2倍
  63. } else {
  64. const _len = 2 * radius * rate // lenW
  65. const _xStart = radius - _len / 2 + x
  66. ctx.drawImage(imagePath, _xStart, y, _len, 2 * radius) // 注意这里的宽高是直径的2倍
  67. }
  68. ctx.restore() // 恢复之前保存的绘图上下文
  69. }
  70. const getToLocal = (base64data) => {
  71. const fsm = wx.getFileSystemManager()
  72. const FILE_BASE_NAME = 'tmp_base64src'; //自定义文件名
  73. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
  74. if (!format) {
  75. return (new Error('ERROR_BASE64SRC_PARSE'))
  76. }
  77. const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`
  78. const buffer = wx.base64ToArrayBuffer(bodyData);
  79. fsm.writeFile({
  80. filePath,
  81. data: buffer,
  82. encoding: 'binary',
  83. success(r) {
  84. console.log(filePath,'filePath')
  85. qrCode.value = filePath
  86. },
  87. fail() {
  88. return (new Error('ERROR_BASE64SRC_WRITE'))
  89. }
  90. })
  91. }
  92. // 生成分享二维码
  93. const qrCode = ref()
  94. const handleShareCode = async () => {
  95. const userId = useUserStore?.accountInfo?.userId || ''
  96. const query = {
  97. scene: 'shareId=' + userId,
  98. path: 'pages/login/index',
  99. width: 200,
  100. autoColor: false,
  101. checkPath: true,
  102. hyaline: true
  103. }
  104. const res = await getJobAdvertisedShareQrcode(query)
  105. const data = res?.data ? 'data:image/png;base64,' + res.data : ''
  106. getToLocal(data)
  107. }
  108. const createPoster = async () => {
  109. uni.showLoading({ title: '生成中...', mask: true })
  110. await handleShareCode() // 生产分享二维码
  111. var context = uni.createCanvasContext('posterCanvas')
  112. //清空画布
  113. context.clearRect(0, 0, 330, 600)
  114. //背景图片
  115. const { path: bgUrl } = await getImageTempRatio('https://minio.menduner.com/dev/63a182780d26bd552819431b0ca94be8e3c90a90e2f933c61e4c83b4614f2fc6.jpg')
  116. context.drawImage(bgUrl, 0, 0, 330, 600) // 路径、x、y、宽、高
  117. // 头像
  118. const avatarUrl = getUserAvatar(baseInfo.value?.avatar, baseInfo.value?.sex)
  119. const { path, width, height } = await getImageTempRatio(avatarUrl)
  120. drawAvatar(context, path, 124, 22, 40.5, width, height) // canvas、图片路径、x、y、半径
  121. //分享二维码
  122. // const { path: qrCode } = await getImageTempRatio('https://minio.citupro.com/dev/menduner/miniProgram.jpg')
  123. context.drawImage(qrCode.value, 105.5, 312.5, 120, 120)
  124. context.draw(false, () =>{
  125. wx.canvasToTempFilePath({
  126. canvasId: 'posterCanvas',
  127. success:(res)=>{
  128. shareUrl.value = res.tempFilePath
  129. console.log('canvas-success', shareUrl.value)
  130. uni.hideLoading({})
  131. },
  132. fail:(err)=>{
  133. uni.hideLoading({})
  134. console.log('canvas-fail', err)
  135. }
  136. })
  137. })
  138. }
  139. createPoster()
  140. </script>
  141. <style scoped lang="scss">
  142. .shareCanvas {
  143. position: fixed;
  144. top: -99999upx;
  145. left: -99999upx;
  146. z-index: -99999;
  147. }
  148. </style>