jobFairShare.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!-- 分享招聘会 -->
  2. <template>
  3. <view style="position: relative;">
  4. <view v-if="shareUrl" class="d-flex align-center flex-column justify-center" style="height: 100vh;">
  5. <image v-if="!!shareUrl" :style="imgStyle" @click="handlePreviewSharePoster" :src="shareUrl" :show-menu-by-longpress="true"></image>
  6. <view class="color-999 ss-m-t-20 font-size-14 ss-m-b-50">点击图片预览,长按图片保存</view>
  7. </view>
  8. <canvas canvas-id="posterCanvas" class="shareCanvas" :style="`width: ${canvasWidth}px; height: ${canvasHeight}px;`"></canvas>
  9. </view>
  10. </template>
  11. <script setup>
  12. import { ref, computed } from 'vue'
  13. import { onLoad } from '@dcloudio/uni-app'
  14. import { getJobAdvertisedShareQrcode } from '@/api/user'
  15. import { getJobFair, saveShareQuery } from '@/api/jobFair'
  16. const canvasWidth = 500
  17. const canvasHeight = 900
  18. const shareUrl = ref('')
  19. const windowWidth = ref(0)
  20. let jobFairId = '' // 分享会ID
  21. let isEnt = false // 招聘会类型
  22. let shareImg = '' // 分享背景图片-底图
  23. // let shareImg = 'https://menduner.citupro.com:3443/dev/4dd7dd3c9ef350d62fcbc814b88c1ac1916f484a3ee8d8531ae7a2698a289670.jpg' // 分享背景图片-底图
  24. onLoad(async (options) => {
  25. jobFairId = options.jobFairId
  26. await getJobFairDetail()
  27. const windowInfo = wx.getWindowInfo()
  28. windowWidth.value = windowInfo.windowWidth
  29. console.log(windowWidth.value, '当前机型屏幕宽')
  30. })
  31. const getJobFairDetail = async () => {
  32. if (!jobFairId) return
  33. const { data } = await getJobFair(jobFairId)
  34. shareImg = data?.shareImg || ''
  35. isEnt = Number(data?.category)
  36. if (shareImg) createPoster()
  37. }
  38. const imgStyle = computed(() => {
  39. if (windowWidth.value <= 320) return `width: 259px; height: ${Math.round((canvasHeight*259)/canvasWidth)}px;`
  40. if (windowWidth.value > 320 && windowWidth.value <= 375) return `width: 313px; height: ${Math.round((canvasHeight*313)/canvasWidth)}px;`
  41. if (windowWidth.value > 375) return `width: 328px; height: ${Math.round((canvasHeight*328)/canvasWidth)}px;`
  42. })
  43. // 图片预览
  44. const handlePreviewSharePoster = () => {
  45. uni.previewImage({
  46. current: 0,
  47. longPressActions: {
  48. itemList: ['发送给朋友', '保存图片', '收藏']
  49. },
  50. urls: [shareUrl.value]
  51. })
  52. }
  53. const getImageTempRatio = (url) => {
  54. return new Promise((req, rej)=>{
  55. wx.getImageInfo({
  56. src:url,
  57. success:(res) =>{
  58. req(res)
  59. }
  60. })
  61. })
  62. }
  63. // const getToLocal = (base64data) => {
  64. // const fsm = wx.getFileSystemManager()
  65. // const FILE_BASE_NAME = 'tmp_base64src'; //自定义文件名
  66. // const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
  67. // if (!format) {
  68. // return (new Error('ERROR_BASE64SRC_PARSE'))
  69. // }
  70. // const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`
  71. // const buffer = wx.base64ToArrayBuffer(bodyData);
  72. // fsm.writeFile({
  73. // filePath,
  74. // data: buffer,
  75. // encoding: 'binary',
  76. // success(r) {
  77. // console.log(filePath,'filePath')
  78. // qrCode.value = filePath
  79. // },
  80. // fail() {
  81. // return (new Error('ERROR_BASE64SRC_WRITE'))
  82. // }
  83. // })
  84. // }
  85. const getToLocal = (base64data) => {
  86. return new Promise((resolve, reject) => {
  87. const fsm = wx.getFileSystemManager()
  88. const FILE_BASE_NAME = 'tmp_base64src' // 自定义文件名
  89. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
  90. if (!format) {
  91. reject(new Error('ERROR_BASE64SRC_PARSE'))
  92. return
  93. }
  94. const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`
  95. const buffer = wx.base64ToArrayBuffer(bodyData)
  96. fsm.writeFile({
  97. filePath,
  98. data: buffer,
  99. encoding: 'binary',
  100. success(r) {
  101. qrCode.value = filePath
  102. resolve(filePath) // 成功时返回 filePath
  103. },
  104. fail() {
  105. reject(new Error('ERROR_BASE64SRC_WRITE'))
  106. },
  107. })
  108. })
  109. }
  110. // 生成分享二维码
  111. const qrCode = ref()
  112. const handleShareCode = async () => {
  113. const result = await saveShareQuery({ jobFairId })
  114. const query = {
  115. scene: 'id=' + result.data,
  116. path: `pagesB/jobFair/${isEnt ? 'enterprisesClassification' : 'positionClassification'}`,
  117. width: 200,
  118. autoColor: false,
  119. checkPath: true,
  120. hyaline: false // 二维码背景颜色为透明
  121. }
  122. const res = await getJobAdvertisedShareQrcode(query, { noAuth: true })
  123. const data = res?.data ? 'data:image/png;base64,' + res.data : ''
  124. await getToLocal(data) // 等待 getToLocal 完成
  125. }
  126. const createPoster = async () => {
  127. uni.showLoading({ title: '生成中...', mask: true })
  128. await handleShareCode() // 生产分享二维码
  129. var context = uni.createCanvasContext('posterCanvas')
  130. //清空画布
  131. context.clearRect(0, 0, canvasWidth, canvasHeight)
  132. //背景图片
  133. const { path: bgUrl } = await getImageTempRatio(shareImg)
  134. context.drawImage(bgUrl, 0, 0, canvasWidth, canvasHeight) // 路径、x、y、宽、高
  135. //分享二维码
  136. const qrCodeWidth = 127
  137. const qrCodeHeight = 127
  138. const qrCodeX = 188
  139. const qrCodeY = 585
  140. context.drawImage(qrCode.value, qrCodeX, qrCodeY, qrCodeWidth, qrCodeHeight)
  141. context.font = 'bold 16px Arial'
  142. context.fillStyle = '#10325d'
  143. const text = '诚挚邀约 共享机遇'
  144. const textWidth = context.measureText(text).width
  145. const textX = qrCodeX + (qrCodeWidth - textWidth) / 2
  146. const textY = qrCodeY + qrCodeHeight + 30
  147. context.fillText(text, textX, textY)
  148. context.draw(false, () =>{
  149. wx.canvasToTempFilePath({
  150. canvasId: 'posterCanvas',
  151. success:(res)=>{
  152. shareUrl.value = res.tempFilePath
  153. console.log('canvas-success', shareUrl.value)
  154. uni.hideLoading({})
  155. },
  156. fail:(err)=>{
  157. uni.hideLoading({})
  158. console.log('canvas-fail', err)
  159. }
  160. })
  161. })
  162. }
  163. </script>
  164. <style scoped lang="scss">
  165. .shareCanvas {
  166. position: fixed;
  167. top: -99999upx;
  168. left: -99999upx;
  169. z-index: -99999;
  170. }
  171. </style>