jobFairShare.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_${new Date().getTime()}` // 自定义文件名
  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. resolve(filePath) // 成功时返回 filePath
  102. },
  103. fail() {
  104. reject(new Error('ERROR_BASE64SRC_WRITE'))
  105. },
  106. })
  107. })
  108. }
  109. // 生成分享二维码
  110. const qrCode = ref()
  111. const handleShareCode = async () => {
  112. const result = await saveShareQuery({ jobFairId })
  113. const query = {
  114. scene: 'id=' + result.data,
  115. path: `pagesB/jobFair/${!isEnt ? 'enterprisesClassification' : 'positionClassification'}`,
  116. width: 200,
  117. autoColor: false,
  118. checkPath: true,
  119. hyaline: false // 二维码背景颜色为透明
  120. }
  121. console.log(result.data, query.scene, '=========================')
  122. const res = await getJobAdvertisedShareQrcode(query, { noAuth: true })
  123. const data = res?.data ? 'data:image/png;base64,' + res.data : ''
  124. const filePath = await getToLocal(data) // 等待 getToLocal 完成
  125. qrCode.value = filePath
  126. }
  127. const createPoster = async () => {
  128. uni.showLoading({ title: '生成中...', mask: true })
  129. await handleShareCode() // 生产分享二维码
  130. var context = uni.createCanvasContext('posterCanvas')
  131. //清空画布
  132. context.clearRect(0, 0, canvasWidth, canvasHeight)
  133. //背景图片
  134. const { path: bgUrl } = await getImageTempRatio(shareImg)
  135. context.drawImage(bgUrl, 0, 0, canvasWidth, canvasHeight) // 路径、x、y、宽、高
  136. //分享二维码
  137. const qrCodeWidth = 127
  138. const qrCodeHeight = 127
  139. const qrCodeX = 188
  140. const qrCodeY = 585
  141. console.log('dddd', qrCode.value)
  142. context.drawImage(qrCode.value, qrCodeX, qrCodeY, qrCodeWidth, qrCodeHeight)
  143. context.font = 'bold 16px Arial'
  144. context.fillStyle = '#10325d'
  145. const text = '扫码投递'
  146. const textWidth = context.measureText(text).width
  147. const textX = qrCodeX + (qrCodeWidth - textWidth) / 2
  148. const textY = qrCodeY + qrCodeHeight + 30
  149. context.fillText(text, textX, textY)
  150. context.draw(false, () =>{
  151. wx.canvasToTempFilePath({
  152. canvasId: 'posterCanvas',
  153. success:(res)=>{
  154. shareUrl.value = res.tempFilePath
  155. console.log('canvas-success', shareUrl.value)
  156. uni.hideLoading({})
  157. },
  158. fail:(err)=>{
  159. uni.hideLoading({})
  160. console.log('canvas-fail', err)
  161. }
  162. })
  163. })
  164. }
  165. </script>
  166. <style scoped lang="scss">
  167. .shareCanvas {
  168. position: fixed;
  169. top: -99999upx;
  170. left: -99999upx;
  171. z-index: -99999;
  172. }
  173. </style>