proof.vue 5.6 KB

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