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