jobFairEntShare.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: 540px; height: 788px;"></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 { getJobFairEntJobPage, getJobFair, saveShareQuery } from '@/api/jobFair'
  16. import { formatName } from '@/utils/getText'
  17. const shareUrl = ref('')
  18. const windowWidth = ref(0)
  19. let jobFairId = '' // 分享会ID
  20. let enterpriseId = '' // 企业ID
  21. let entName = '' // 企业名称
  22. let shareImg = '' // 分享背景图片-底图
  23. onLoad(async (options) => {
  24. jobFairId = options.jobFairId
  25. enterpriseId = options.enterpriseId
  26. await getJobFairDetail()
  27. await getEntPositionList()
  28. const windowInfo = wx.getWindowInfo()
  29. windowWidth.value = windowInfo.windowWidth
  30. console.log(windowWidth.value, '当前机型屏幕宽')
  31. if (shareImg) createPoster() // 生成海报
  32. })
  33. const getJobFairDetail = async () => {
  34. if (!jobFairId) return
  35. const { data } = await getJobFair(jobFairId)
  36. shareImg = data?.contentImg || ''
  37. }
  38. let positionNameList = []
  39. let entLogoUrl = 'https://minio.citupro.com/dev/menduner/company-avatar.png'
  40. const getEntPositionList = async () => {
  41. if (!jobFairId || !enterpriseId) {
  42. uni.showToast({ title: '获取企业岗位失败,请重试!', icon: 'none', duration: 2000 })
  43. return
  44. }
  45. try {
  46. const params = {
  47. pageSize: 3,
  48. pageNo: 1,
  49. jobFairId,
  50. enterpriseId,
  51. }
  52. const res = await getJobFairEntJobPage(params)
  53. const list = res?.data?.list?.length ? res.data.list : []
  54. positionNameList = list.map(e => { return formatName(e?.name) })
  55. entLogoUrl = list[0]?.enterprise?.logoUrl || ''
  56. entName = formatName(list[0]?.enterprise?.anotherName) || ''
  57. } catch (error) {}
  58. }
  59. const imgStyle = computed(() => {
  60. if (windowWidth.value <= 320) return `width: 259px; height: 377px;`
  61. if (windowWidth.value > 320 && windowWidth.value <= 375) return `width: 313px; height: 455px;`
  62. if (windowWidth.value > 375) return `width: 328px; height: 474px;`
  63. })
  64. // 图片预览
  65. const handlePreviewSharePoster = () => {
  66. uni.previewImage({
  67. current: 0,
  68. longPressActions: {
  69. itemList: ['发送给朋友', '保存图片', '收藏']
  70. },
  71. urls: [shareUrl.value]
  72. })
  73. }
  74. const getImageTempRatio = (url) => {
  75. return new Promise((req, rej)=>{
  76. wx.getImageInfo({
  77. src:url,
  78. success:(res) =>{
  79. req(res)
  80. }
  81. })
  82. })
  83. }
  84. const getToLocal = (base64data) => {
  85. const fsm = wx.getFileSystemManager()
  86. const FILE_BASE_NAME = `tmp_${new Date().getTime()}` // 自定义文件名
  87. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
  88. if (!format) {
  89. return (new Error('ERROR_BASE64SRC_PARSE'))
  90. }
  91. const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`
  92. const buffer = wx.base64ToArrayBuffer(bodyData);
  93. fsm.writeFile({
  94. filePath,
  95. data: buffer,
  96. encoding: 'binary',
  97. success(r) {
  98. console.log(filePath,'filePath')
  99. qrCode.value = filePath
  100. },
  101. fail() {
  102. return (new Error('ERROR_BASE64SRC_WRITE'))
  103. }
  104. })
  105. }
  106. // 生成分享二维码
  107. const qrCode = ref()
  108. const handleShareCode = async () => {
  109. const result = await saveShareQuery({ jobFairId, enterpriseId, entName })
  110. const query = {
  111. scene: 'id=' + result.data,
  112. path: 'pagesB/jobFair/positionClassification',
  113. width: 200,
  114. autoColor: false,
  115. checkPath: true,
  116. hyaline: false // 二维码背景颜色为透明
  117. }
  118. const res = await getJobAdvertisedShareQrcode(query, { noAuth: true })
  119. const data = res?.data ? 'data:image/png;base64,' + res.data : ''
  120. getToLocal(data)
  121. }
  122. const createPoster = async () => {
  123. uni.showLoading({ title: '生成中...', mask: true })
  124. await handleShareCode() // 生产分享二维码
  125. var context = uni.createCanvasContext('posterCanvas')
  126. //清空画布
  127. context.clearRect(0, 0, 540, 788)
  128. //背景图片
  129. const { path: bgUrl } = await getImageTempRatio(shareImg)
  130. context.drawImage(bgUrl, 0, 0, 540, 788) // 路径、x、y、宽、高
  131. // 企业头像
  132. context.save()
  133. const secondImgWidth = 120
  134. const secondImgHeight = 120
  135. const x = (540 - secondImgWidth) / 2
  136. const y = 788 - secondImgHeight - 460
  137. context.beginPath()
  138. context.arc(x + secondImgWidth / 2, y + secondImgHeight / 2, secondImgWidth / 2, 0, Math.PI * 2, true)
  139. context.clip()
  140. const { path: entLogoUrlPath } = await getImageTempRatio(entLogoUrl)
  141. context.drawImage(entLogoUrlPath, x, y, secondImgWidth, secondImgHeight)
  142. context.restore()
  143. // 企业名称
  144. const maxTextWidth = 400
  145. const fontStyle = 'bold 24px Arial'
  146. context.font = fontStyle
  147. let truncatedText = entName
  148. while (context.measureText(truncatedText + '...').width > maxTextWidth && truncatedText.length > 0) {
  149. truncatedText = truncatedText.slice(0, -1)
  150. }
  151. if (truncatedText !== entName) truncatedText += '...'
  152. const textX = x + (secondImgWidth - context.measureText(truncatedText).width) / 2
  153. const textY = y + secondImgHeight + 30
  154. context.fillStyle = '#000'
  155. context.fillText(truncatedText, textX, textY)
  156. // 职位标签
  157. const tagPaddingLeftRight = 20
  158. const tagPaddingTopBottom = 10
  159. const tagRadius = 8
  160. const tagSpacing = 22
  161. let tagY = textY + tagSpacing + 10
  162. positionNameList.forEach((tag) => {
  163. let truncatedTag = tag
  164. context.font = '18px Arial' // 绘制前设置字体,否则tagWidth受前面的font fontStyle影响
  165. while (context.measureText(truncatedTag + '...').width > maxTextWidth - 2 * tagPaddingLeftRight && truncatedTag.length > 0) {
  166. truncatedTag = truncatedTag.slice(0, -1)
  167. }
  168. if (truncatedTag !== tag) truncatedTag += '...'
  169. const tagWidth = context.measureText(truncatedTag).width + 2 * tagPaddingLeftRight
  170. const tagX = x + (secondImgWidth - tagWidth) / 2
  171. context.fillStyle = '#00B760'
  172. context.beginPath()
  173. context.moveTo(tagX + tagRadius, tagY)
  174. context.lineTo(tagX + tagWidth - tagRadius, tagY)
  175. context.quadraticCurveTo(tagX + tagWidth, tagY, tagX + tagWidth, tagY + tagRadius)
  176. context.lineTo(tagX + tagWidth, tagY + tagPaddingTopBottom * 2)
  177. context.quadraticCurveTo(tagX + tagWidth, tagY + tagPaddingTopBottom * 2 + tagRadius, tagX + tagWidth - tagRadius, tagY + tagPaddingTopBottom * 2 + tagRadius)
  178. context.lineTo(tagX + tagRadius, tagY + tagPaddingTopBottom * 2 + tagRadius)
  179. context.quadraticCurveTo(tagX, tagY + tagPaddingTopBottom * 2 + tagRadius, tagX, tagY + tagPaddingTopBottom * 2)
  180. context.lineTo(tagX, tagY + tagRadius);
  181. context.quadraticCurveTo(tagX, tagY, tagX + tagRadius, tagY)
  182. context.closePath()
  183. context.fill()
  184. context.fillStyle = '#fff'
  185. context.fillText(truncatedTag, tagX + tagPaddingLeftRight, tagY + tagPaddingTopBottom + 10)
  186. tagY += tagPaddingTopBottom * 2 + tagSpacing
  187. })
  188. //分享二维码
  189. context.drawImage(qrCode.value, 80, 550, 110, 110)
  190. context.font = 'bold 18px Arial'
  191. context.fillStyle = '#000'
  192. context.fillText('海量职位火热招聘中', 280, 605)
  193. context.font = '15px Arial'
  194. context.fillStyle = '#999'
  195. context.fillText('长按图片识别二维码', 290, 625)
  196. context.draw(false, () =>{
  197. wx.canvasToTempFilePath({
  198. canvasId: 'posterCanvas',
  199. success:(res)=>{
  200. shareUrl.value = res.tempFilePath
  201. console.log('canvas-success', shareUrl.value)
  202. uni.hideLoading({})
  203. },
  204. fail:(err)=>{
  205. uni.hideLoading({})
  206. console.log('canvas-fail', err)
  207. }
  208. })
  209. })
  210. }
  211. </script>
  212. <style scoped lang="scss">
  213. .shareCanvas {
  214. position: fixed;
  215. top: -99999upx;
  216. left: -99999upx;
  217. z-index: -99999;
  218. }
  219. </style>