jobFairEntShare.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 { getJobFair } from '@/api/jobFair'
  16. // import { getEnterpriseDetails } from '@/api/user'
  17. // import { getEnterpriseDetails } from '@/api/user'
  18. // const positionList = listData.value.length ? JSON.stringify(listData.value.slice(0, 2)) : "0"
  19. const shareUrl = ref('')
  20. const windowWidth = ref(0)
  21. let jobFairId = '' // 分享会ID
  22. let enterpriseId = '' // 企业ID
  23. let shareImg = '' // 分享背景图片-底图
  24. // let shareImg = 'https://menduner.citupro.com:3443/dev/4dd7dd3c9ef350d62fcbc814b88c1ac1916f484a3ee8d8531ae7a2698a289670.jpg' // 分享背景图片-底图
  25. onLoad(async (options) => {
  26. jobFairId = options.jobFairId
  27. enterpriseId = options.enterpriseId
  28. await getJobFairDetail()
  29. // await getJobFairEntDetail()
  30. const windowInfo = wx.getWindowInfo()
  31. windowWidth.value = windowInfo.windowWidth
  32. console.log(windowWidth.value, '当前机型屏幕宽')
  33. })
  34. const getJobFairDetail = async () => {
  35. if (!jobFairId) return
  36. const { data } = await getJobFair(jobFairId)
  37. shareImg = data?.contentImg || ''
  38. if (shareImg) createPoster()
  39. }
  40. const getEntPositionList = async () => {
  41. if (!query.jobFairId) {
  42. uni.showToast({ title: '获取企业岗位失败,请重试!', icon: 'none', duration: 2000 })
  43. return
  44. }
  45. try {
  46. const params = { ...query }
  47. const positionIdValue = tabIndex.value !== -1 ? tabList.value[tabIndex.value]?.value : []
  48. positionIdValue?.length && positionIdValue.forEach((value, index) => {
  49. params[`positionId[${index}]`] = value
  50. })
  51. const res = await getJobFairEntJobPage(params)
  52. const list = res?.data?.list || []
  53. // list.forEach(e => {
  54. // e.job = dealDictObjData({}, e)
  55. // e.enterprise = { ...e.enterprise, ...dealDictObjData({}, e.enterprise)}
  56. // })
  57. // listData.value = listData.value.concat(list)
  58. // showShareBtn.value = true
  59. // if (listData.value?.length === +res?.data?.total) {
  60. // more.value = 'noMore'
  61. // return
  62. // }
  63. } catch (error) {
  64. query.pageNo--
  65. more.value = 'more'
  66. }
  67. }
  68. const imgStyle = computed(() => {
  69. if (windowWidth.value <= 320) return `width: 259px; height: 460px;`
  70. if (windowWidth.value > 320 && windowWidth.value <= 375) return `width: 313px; height: 560px;`
  71. if (windowWidth.value > 375) return `width: 328px; height: 600px;`
  72. })
  73. // 图片预览
  74. const handlePreviewSharePoster = () => {
  75. uni.previewImage({
  76. current: 0,
  77. longPressActions: {
  78. itemList: ['发送给朋友', '保存图片', '收藏']
  79. },
  80. urls: [shareUrl.value]
  81. })
  82. }
  83. const getImageTempRatio = (url) => {
  84. return new Promise((req, rej)=>{
  85. wx.getImageInfo({
  86. src:url,
  87. success:(res) =>{
  88. req(res)
  89. }
  90. })
  91. })
  92. }
  93. const getToLocal = (base64data) => {
  94. const fsm = wx.getFileSystemManager()
  95. const FILE_BASE_NAME = 'tmp_base64src'; //自定义文件名
  96. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
  97. if (!format) {
  98. return (new Error('ERROR_BASE64SRC_PARSE'))
  99. }
  100. const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`
  101. const buffer = wx.base64ToArrayBuffer(bodyData);
  102. fsm.writeFile({
  103. filePath,
  104. data: buffer,
  105. encoding: 'binary',
  106. success(r) {
  107. console.log(filePath,'filePath')
  108. qrCode.value = filePath
  109. },
  110. fail() {
  111. return (new Error('ERROR_BASE64SRC_WRITE'))
  112. }
  113. })
  114. }
  115. // 生成分享二维码
  116. const qrCode = ref()
  117. const handleShareCode = async () => {
  118. const query = {
  119. scene: `jobFairId=${jobFairId}&enterpriseId=${enterpriseId}&entName=${entName}`,
  120. scene: 'jobFairId=' + jobFairId + '&enterpriseId=' + enterpriseId,
  121. path: 'pages/login/index',
  122. // path: 'pagesB/jobFair/positionClassification',
  123. width: 200,
  124. autoColor: false,
  125. checkPath: true,
  126. hyaline: true
  127. }
  128. const res = await getJobAdvertisedShareQrcode(query)
  129. const data = res?.data ? 'data:image/png;base64,' + res.data : ''
  130. getToLocal(data)
  131. }
  132. const createPoster = async () => {
  133. uni.showLoading({ title: '生成中...', mask: true })
  134. await handleShareCode() // 生产分享二维码
  135. var context = uni.createCanvasContext('posterCanvas')
  136. //清空画布
  137. context.clearRect(0, 0, 330, 600)
  138. //背景图片
  139. const { path: bgUrl } = await getImageTempRatio(shareImg)
  140. context.drawImage(bgUrl, 0, 0, 330, 600) // 路径、x、y、宽、高
  141. //分享二维码
  142. context.drawImage(qrCode.value, 105.5, 380, 120, 120)
  143. context.draw(false, () =>{
  144. wx.canvasToTempFilePath({
  145. canvasId: 'posterCanvas',
  146. success:(res)=>{
  147. shareUrl.value = res.tempFilePath
  148. console.log('canvas-success', shareUrl.value)
  149. uni.hideLoading({})
  150. },
  151. fail:(err)=>{
  152. uni.hideLoading({})
  153. console.log('canvas-fail', err)
  154. }
  155. })
  156. })
  157. }
  158. </script>
  159. <style scoped lang="scss">
  160. .shareCanvas {
  161. position: fixed;
  162. top: -99999upx;
  163. left: -99999upx;
  164. z-index: -99999;
  165. }
  166. </style>