proof.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="container">
  3. <view class="info" id="infoImg">
  4. <image class="info_photo" v-if="msg.picture" :src="msg.picture" mode="aspectFill" @click="clickImg(msg.picture)"></image>
  5. <image class="info_photo" v-else src="/static/4.png" mode="aspectFill"></image>
  6. <image class="info_img" src="/static/logo2.png" mode="aspectFill"></image>
  7. <view class="info_box">
  8. <view class="box_key">学院:</view>
  9. <view class="box_value">{{ msg.college }}</view>
  10. </view>
  11. <view class="info_box">
  12. <view class="box_key">专业:</view>
  13. <view class="box_value">{{ msg.major }}</view>
  14. </view>
  15. <view class="info_box">
  16. <view class="box_key">班级:</view>
  17. <view class="box_value">{{ msg.classstr }}</view>
  18. </view>
  19. <view class="info_box">
  20. <view class="box_key">姓名:</view>
  21. <view class="box_value">{{ msg.name }}</view>
  22. </view>
  23. <view class="info_box">
  24. <view class="box_key">录取号:</view>
  25. <view class="box_value">{{ msg.admissNum }}</view>
  26. </view>
  27. <view class="info_box">
  28. <view class="box_key">性别:</view>
  29. <view class="box_value">{{ msg.sex }}</view>
  30. </view>
  31. <view class="info_box">
  32. <view class="box_key">联系电话:</view>
  33. <view class="box_value">{{ msg.phone }}</view>
  34. </view>
  35. <view class="info_box">
  36. <view class="box_key">楼栋名称:</view>
  37. <view class="box_value color">{{ msg.build }}</view>
  38. </view>
  39. <view class="info_box">
  40. <view class="box_key">宿舍床号:</view>
  41. <view class="box_value color">{{ msg.dormitory + '-' + msg.number }}</view>
  42. </view>
  43. </view>
  44. <!-- 按钮区域 -->
  45. <view class="btn">
  46. <view class="btn_box" @click="handleDownload">下载凭证</view>
  47. </view>
  48. <!-- 预览图片弹窗 -->
  49. <uni-popup ref="popup_img" :is-mask-click="false">
  50. <view class="pop_img">
  51. <!-- 关闭图标 -->
  52. <uni-icons class="pop_close" type="closeempty" color="#808080" size="20" @click="handleClose_img"></uni-icons>
  53. <image class="img" v-if="previewUrl" :src="previewUrl" mode="aspectFill"></image>
  54. <view class="text">长按图片保存到相册</view>
  55. </view>
  56. </uni-popup>
  57. </view>
  58. </template>
  59. <script setup>
  60. import { onLoad } from '@dcloudio/uni-app'
  61. import { ref } from 'vue'
  62. import html2canvas from 'html2canvas'
  63. const msg = ref({})
  64. const previewUrl = ref()
  65. const popup_img = ref()
  66. onLoad((options) => {
  67. msg.value = JSON.parse(decodeURIComponent(options.msg))
  68. // console.log(msg.value)
  69. })
  70. // 点击图片回调
  71. const clickImg = (url) => {
  72. // console.log(url)
  73. uni.previewImage({
  74. urls: [url]
  75. })
  76. }
  77. const handleClose_img = () => {
  78. popup_img.value.close()
  79. }
  80. // 点击下载凭证按钮回调
  81. const handleDownload = async () => {
  82. try {
  83. uni.showLoading({ title: '生成中...', mask: true })
  84. // 获取DOM元素
  85. const element = document.getElementById('infoImg')
  86. // 使用html2canvas生成图片
  87. const canvas = await html2canvas(element, {
  88. useCORS: true, // 允许跨域图片
  89. scale: 2, // 提高清晰度
  90. backgroundColor: '#fff', // 背景色
  91. logging: false // 关闭日志
  92. })
  93. // 转换为图片URL
  94. const imgData = canvas.toDataURL('image/png')
  95. if (isWechatBrowser()) {
  96. // 显示预览图
  97. previewUrl.value = imgData
  98. // console.log(previewUrl.value)
  99. popup_img.value.open()
  100. } else {
  101. // 下载图片
  102. downloadImage(imgData, '页面截图.png')
  103. uni.showToast({ title: '保存成功', icon: 'success' })
  104. }
  105. } catch (error) {
  106. console.error('截图失败:', error)
  107. uni.showToast({ title: '保存失败', icon: 'none' })
  108. } finally {
  109. uni.hideLoading()
  110. }
  111. }
  112. // 判断是否在微信浏览器中
  113. const isWechatBrowser = () => {
  114. const ua = navigator.userAgent.toLowerCase()
  115. return ua.indexOf('micromessenger') !== -1
  116. }
  117. const downloadImage = (dataUrl, fileName) => {
  118. const link = document.createElement('a')
  119. link.href = dataUrl
  120. link.download = fileName
  121. link.style.display = 'none'
  122. document.body.appendChild(link)
  123. link.click()
  124. document.body.removeChild(link)
  125. }
  126. </script>
  127. <style lang="scss" scoped>
  128. .container {
  129. box-sizing: border-box;
  130. padding-top: 12rpx;
  131. min-height: 100vh;
  132. font-size: 28rpx;
  133. background-color: #fff;
  134. .info {
  135. position: relative;
  136. padding: 20rpx;
  137. box-sizing: border-box;
  138. margin: auto;
  139. width: 690rpx;
  140. border-radius: 8rpx;
  141. background-image: url(@/static/11.png);
  142. background-size: cover;
  143. background-repeat: no-repeat;
  144. background-position: center;
  145. .info_img {
  146. width: 168rpx;
  147. height: 45rpx;
  148. }
  149. .info_photo {
  150. position: absolute;
  151. top: 30rpx;
  152. right: 20rpx;
  153. width: 213rpx;
  154. height: 259rpx;
  155. border-radius: 8rpx;
  156. }
  157. .info_box {
  158. display: flex;
  159. align-items: center;
  160. height: 60rpx;
  161. .box_key {
  162. color: #4d4d4d;
  163. }
  164. .box_value {
  165. font-weight: bold;
  166. }
  167. .color {
  168. font-size: 40rpx;
  169. color: #0061ff;
  170. }
  171. }
  172. }
  173. .btn {
  174. padding-top: 120rpx;
  175. padding-bottom: 80rpx;
  176. background-color: #fff;
  177. .btn_box {
  178. display: flex;
  179. justify-content: center;
  180. align-items: center;
  181. margin: auto;
  182. width: 710rpx;
  183. height: 100rpx;
  184. color: #fff;
  185. font-size: 32rpx;
  186. border-radius: 8rpx;
  187. background-color: #0061ff;
  188. }
  189. }
  190. .pop_img {
  191. position: relative;
  192. box-sizing: border-box;
  193. padding: 80rpx 20rpx 50rpx;
  194. display: flex;
  195. flex-direction: column;
  196. align-items: center;
  197. width: 90vw;
  198. height: 70vh;
  199. border-radius: 15rpx;
  200. background: linear-gradient(180deg, #ebf2ff 0%, #ffffff 100%);
  201. .pop_close {
  202. position: absolute;
  203. top: 19rpx;
  204. right: 29rpx;
  205. }
  206. .img {
  207. margin-top: 50rpx;
  208. width: 563rpx;
  209. height: 525rpx;
  210. }
  211. .text {
  212. margin-top: 50rpx;
  213. font-size: 32rpx;
  214. font-weight: bold;
  215. }
  216. }
  217. }
  218. </style>