jobFairEntShare.vue 5.6 KB

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