jobFairEntShare.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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: 330px; height: 600px;"></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 } 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 shareImg = '' // 分享背景图片-底图
  22. // let shareImg = 'https://menduner.citupro.com:3443/dev/4dd7dd3c9ef350d62fcbc814b88c1ac1916f484a3ee8d8531ae7a2698a289670.jpg' // 分享背景图片-底图
  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 = ''
  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: 2,
  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 = list[0]?.enterprise?.anotherName || ''
  57. debugger
  58. } catch (error) {}
  59. }
  60. const imgStyle = computed(() => {
  61. if (windowWidth.value <= 320) return `width: 259px; height: 460px;`
  62. if (windowWidth.value > 320 && windowWidth.value <= 375) return `width: 313px; height: 560px;`
  63. if (windowWidth.value > 375) return `width: 328px; height: 600px;`
  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)
  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, 330, 600)
  129. //背景图片
  130. const { path: bgUrl } = await getImageTempRatio(shareImg)
  131. context.drawImage(bgUrl, 0, 0, 330, 600) // 路径、x、y、宽、高
  132. //分享二维码
  133. context.drawImage(qrCode.value, 105.5, 380, 120, 120)
  134. context.draw(false, () =>{
  135. wx.canvasToTempFilePath({
  136. canvasId: 'posterCanvas',
  137. success:(res)=>{
  138. shareUrl.value = res.tempFilePath
  139. console.log('canvas-success', shareUrl.value)
  140. uni.hideLoading({})
  141. },
  142. fail:(err)=>{
  143. uni.hideLoading({})
  144. console.log('canvas-fail', err)
  145. }
  146. })
  147. })
  148. }
  149. </script>
  150. <style scoped lang="scss">
  151. .shareCanvas {
  152. position: fixed;
  153. top: -99999upx;
  154. left: -99999upx;
  155. z-index: -99999;
  156. }
  157. </style>