auth-card.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="auth-card">
  3. <custom-nav noback="noback" transparent="transparent" ref="ltm" title=" " />
  4. <view class="content">
  5. <view class="auth-paper">
  6. <image v-if="!src" src="../../static/index/my/auth-paper.png"></image>
  7. <!-- <canvas @tap="previewImage" v-if="drawOver" id="canvas" canvas-id="canvas" :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px'}"></canvas> -->
  8. <canvas ref="canvas"></canvas>
  9. <image class="showCan" v-if="src" :src="src" ></image>
  10. <!-- <text class="info name">{{ name }}</text>
  11. <text class="info phone">{{ phone }}</text>
  12. <text class="info code">{{ code}}</text>
  13. <text class="info long">{{ long }}</text> -->
  14. <!-- <text class="info y">{{ y }}</text>
  15. <text class="info m">{{ m }}</text>
  16. <text class="info d">{{ d }}</text> -->
  17. </view>
  18. <!-- <view class="save" @tap="save"><text class="cuIcon-down icon-right"></text>保存图片</view>
  19. <view class="show" @tap="share"><text class="cuIcon-news icon-right"></text>分享证书</view> -->
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. judgeIosPermission, // 获取iOS设备上当前App是否有某项权限
  26. requestAndroidPermission, // 获取Android设备上当前App是否有某项权限
  27. } from '@/common/util/permission.js'
  28. import _share from '@/common/util/share.js'
  29. import { _API_Auth_Code, _API_Auth_Share } from '@/apis/user.js'
  30. export default {
  31. data() {
  32. return {
  33. title: '我的授权',
  34. drawOver: false,
  35. name: this.$store.state.userinfo.nickname,
  36. phone: this.$store.state.userinfo.mobile,
  37. canvasWidth: uni.upx2px(750),
  38. canvasHeight: uni.upx2px(1323),
  39. url: '',
  40. code: '',
  41. long: '',
  42. src: ''
  43. // y: '',
  44. // m: '',
  45. // d: ''
  46. }
  47. },
  48. onLoad({ id }) {
  49. uni.showLoading({ mask: true })
  50. _API_Auth_Code({ id: id ? id : this.$store.state.userinfo.id }).then(res => {
  51. uni.showLoading({ mask: true })
  52. setTimeout(() => {
  53. const can = document.getElementsByTagName('canvas')[0]
  54. const ctx = can.getContext('2d')
  55. const img = document.getElementsByTagName('img')[0]
  56. const { windowWidth, windowHeight } = uni.getSystemInfoSync();
  57. const regW = windowWidth / 375
  58. const regH = windowHeight / 603
  59. ctx.drawImage(img, 0, 0, windowWidth, windowHeight)
  60. ctx.font="14px Arial";
  61. ctx.fillText(res.data.realname, 170 * regW, 212 * regH)
  62. ctx.fillText(res.data.phone, 170 * regW , 240 * regH)
  63. ctx.font="25px Arial";
  64. ctx.fillText(res.data.level_name, 130 * regW , 310 * regH)
  65. ctx.font="11px Arial";
  66. ctx.fillText(res.data.code, 120 * regW, 365 * regH)
  67. const y1 = this.y = this.$options.filters.getYear(res.data.auth_startime)
  68. const m1 = this.m = this.$options.filters.getMonth(res.data.auth_startime)
  69. const d1 = this.d = this.$options.filters.getDate(res.data.auth_startime)
  70. const y2 = this.y = this.$options.filters.getYear(res.data.auth_endtime)
  71. const m2 = this.m = this.$options.filters.getMonth(res.data.auth_endtime)
  72. const d2 = this.d = this.$options.filters.getDate(res.data.auth_endtime)
  73. ctx.fillText(`${y1}年${m1}月${d1}日 - ${y2}年${m2}月${d2}日`, 120 * regW, 395 * regH)
  74. this.src = can.toDataURL("image/png")
  75. }, 333)
  76. })
  77. },
  78. onBackPress() { // 返回时收起分享
  79. if (uni._SHARE && uni._SHARE.isVisible()) {
  80. uni._MASK.hide()
  81. uni._SHARE.hide()
  82. return true
  83. }
  84. },
  85. methods: {
  86. previewImage () { //点击预览图片
  87. uni.showLoading({ mask: true })
  88. uni.canvasToTempFilePath({ canvasId: 'canvas' }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  89. uni.hideLoading()
  90. uni.previewImage({ urls: [res.tempFilePath] })
  91. })
  92. },
  93. save() { // 点击保存图片
  94. if (plus.os.name == "iOS") { // 判断设备
  95. if (judgeIosPermission("photoLibrary")) {
  96. uni.showLoading({ mask: true })
  97. uni.canvasToTempFilePath({ canvasId: 'canvas' }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  98. uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath }).then(() => {
  99. uni.hideLoading()
  100. uni.toast('保存成功')
  101. })
  102. })
  103. } else {
  104. uni.toast('大卫博士需要获取访问相册的权限,以保证图片能够保存到您的手机相册')
  105. }
  106. } else {
  107. requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE').then(res => {
  108. if (res === 1) {
  109. uni.showLoading({ mask: true })
  110. uni.canvasToTempFilePath({ canvasId: 'canvas' }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  111. uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath }).then(() => {
  112. uni.hideLoading()
  113. uni.toast('保存成功')
  114. })
  115. })
  116. } else {
  117. this.$refs.ltm.modal('保存失败', ['大卫博士需要获取访问相册的权限,以保证图片能够保存到您的手机相册'], 'nocancel')
  118. }
  119. })
  120. }
  121. },
  122. share() { // 点击分享证书
  123. if (this.url) { // 如果证书已经上传到服务器,直接分享
  124. _share({ type: 2, imageUrl: this.url })
  125. } else { // 如果服务器没有证书,先上传再分享
  126. uni.showLoading({ mask: true })
  127. uni.canvasToTempFilePath({ canvasId: 'canvas', quality: 0.5 }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  128. plus.zip.compressImage({ src: res.tempFilePath, dst: '_doc/a.jpg', overwrite: true, quality: 20 }, e => {
  129. _API_Auth_Share({ name: 'img', filePath: e.target }).then(res => {
  130. if (res.code == 200) {
  131. this.url = res.data.url
  132. _share({ type: 2, imageUrl: this.url })
  133. }
  134. })
  135. })
  136. })
  137. }
  138. }
  139. }
  140. }
  141. </script>
  142. <style lang="scss">
  143. .auth-card {
  144. @include page();
  145. .content {
  146. .auth-paper {
  147. // width: 750rpx;
  148. // height: 1322.58rpx;
  149. // background: #FFFFFF;
  150. // position: relative;
  151. width: 100%;
  152. height: 100%;
  153. background: #CCCCCC;
  154. image {
  155. width: 100%;
  156. height: 100%;
  157. z-index: -999;
  158. position: absolute;
  159. &.showCan {
  160. z-index: 9999;
  161. }
  162. }
  163. canvas {
  164. width: 100%;
  165. height: 100%;
  166. position: absolute;
  167. z-index: -99;
  168. left: 0;
  169. top: 0;
  170. }
  171. .info {
  172. position: absolute;
  173. font-size: 18rpx;
  174. &.name {
  175. top: 396rpx;
  176. left: 333rpx;
  177. }
  178. &.phone {
  179. top: 460rpx;
  180. left: 345rpx;
  181. }
  182. &.code {
  183. top: 752rpx;
  184. left: 228rpx;
  185. }
  186. &.long {
  187. top: 820rpx;
  188. left: 228rpx;
  189. }
  190. }
  191. }
  192. .save, .show {
  193. @include flex();
  194. width: 315rpx;
  195. height: 88rpx;
  196. color: #FFFFFF;
  197. font-size: 34rpx;
  198. display: inline-flex;
  199. border-radius: 10rpx;
  200. margin: 88rpx 30rpx 0rpx;
  201. background: $app-base-color;
  202. text {
  203. font-size: 42rpx;
  204. }
  205. }
  206. }
  207. }
  208. </style>