auth-card.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view class="auth-card">
  3. <custom-nav ref="ltm" :title="title" />
  4. <view class="content">
  5. <view class="auth-paper">
  6. <image 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. </view>
  9. <view class="save" @tap="save"><text class="cuIcon-down icon-right"></text>保存图片</view>
  10. <view class="show" @tap="share"><text class="cuIcon-news icon-right"></text>分享证书</view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import {
  16. judgeIosPermission, // 获取iOS设备上当前App是否有某项权限
  17. requestAndroidPermission, // 获取Android设备上当前App是否有某项权限
  18. } from '@/common/util/permission.js'
  19. import _share from '@/common/util/share.js'
  20. import { _API_Auth_Code, _API_Auth_Share } from '@/apis/user.js'
  21. export default {
  22. data() {
  23. return {
  24. title: '我的授权',
  25. drawOver: false,
  26. name: this.$store.state.userinfo.nickname,
  27. phone: this.$store.state.userinfo.mobile,
  28. canvasWidth: uni.upx2px(465),
  29. canvasHeight: uni.upx2px(820),
  30. url: '',
  31. }
  32. },
  33. mounted() {
  34. this.$refs.ltm.loading()
  35. _API_Auth_Code().then(res => {
  36. this.url = res.data.url
  37. const y = this.$options.filters.getYear(res.data.time)
  38. const m = this.$options.filters.getMonth(res.data.time)
  39. const d = this.$options.filters.getDate(res.data.time)
  40. const filters = this.$options.filters
  41. const ctx = uni.createCanvasContext('canvas')
  42. ctx.drawImage('../../static/index/my/auth-paper.png', 0, 0, this.canvasWidth, this.canvasHeight)
  43. ctx.setFontSize(uni.upx2px(14))
  44. ctx.setTextAlign('left')
  45. ctx.fillText(this.name, this.canvasWidth / 2 - uni.upx2px(30), uni.upx2px(265))
  46. ctx.fillText(this.phone, this.canvasWidth / 2 - uni.upx2px(30), uni.upx2px(305))
  47. ctx.setTextAlign('center')
  48. ctx.fillText(res.data.code, this.canvasWidth / 2 + uni.upx2px(30), uni.upx2px(485))
  49. ctx.fillText(`${y}年${m}月${d}日 至 ${y + 1}年${m}月${d}日`, this.canvasWidth / 2 + uni.upx2px(30), uni.upx2px(525))
  50. ctx.draw()
  51. this.drawOver = true
  52. })
  53. },
  54. onBackPress() { // 返回时收起分享
  55. if (uni._SHARE && uni._SHARE.isVisible()) {
  56. uni._MASK.hide()
  57. uni._SHARE.hide()
  58. return true
  59. }
  60. },
  61. methods: {
  62. previewImage () { //点击预览图片
  63. this.$refs.ltm.loading()
  64. uni.canvasToTempFilePath({ canvasId: 'canvas' }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  65. this.$refs.ltm.hideLoading()
  66. uni.previewImage({ urls: [res.tempFilePath] })
  67. })
  68. },
  69. save() { // 点击保存图片
  70. if (plus.os.name == "iOS") { // 判断设备
  71. if (judgeIosPermission("photoLibrary")) {
  72. this.$refs.ltm.loading()
  73. uni.canvasToTempFilePath({ canvasId: 'canvas' }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  74. uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath }).then(() => {
  75. this.$refs.ltm.hideLoading()
  76. this.$refs.ltm.toast('保存成功')
  77. })
  78. })
  79. } else {
  80. this.$refs.ltm.toast('大卫博士需要获取访问相册的权限,以保证图片能够保存到您的手机相册')
  81. }
  82. } else {
  83. requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE').then(res => {
  84. if (res === 1) {
  85. this.$refs.ltm.loading()
  86. uni.canvasToTempFilePath({ canvasId: 'canvas' }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  87. uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath }).then(() => {
  88. this.$refs.ltm.hideLoading()
  89. this.$refs.ltm.toast('保存成功')
  90. })
  91. })
  92. } else {
  93. this.$refs.ltm.modal('保存失败', ['大卫博士需要获取访问相册的权限,以保证图片能够保存到您的手机相册'], 'nocancel')
  94. }
  95. })
  96. }
  97. },
  98. share() { // 点击分享证书
  99. if (this.url) { // 如果证书已经上传到服务器,直接分享
  100. _share({ type: 2, imageUrl: this.url })
  101. } else { // 如果服务器没有证书,先上传再分享
  102. this.$refs.ltm.loading()
  103. uni.canvasToTempFilePath({ canvasId: 'canvas', quality: 0.5 }).then(([ , res]) => { //首先将 canvas 转化为一个图片,获得图片的临时路径
  104. plus.zip.compressImage({ src: res.tempFilePath, dst: '_doc/a.jpg', overwrite: true, quality: 20 }, e => {
  105. _API_Auth_Share({
  106. name: 'img',
  107. filePath: e.target
  108. }).then(res => {
  109. if (res.code == 200) {
  110. this.url = res.data.url
  111. this.$refs.ltm.hideLoading()
  112. _share({ type: 2, imageUrl: this.url })
  113. }
  114. })
  115. })
  116. })
  117. }
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .auth-card {
  124. @include page();
  125. .content {
  126. .auth-paper {
  127. @include flex();
  128. height: 922rpx;
  129. background: #FFFFFF;
  130. position: relative;
  131. image {
  132. width: 465rpx;
  133. height: 820rpx;
  134. }
  135. canvas {
  136. top: 50%;
  137. left: 50%;
  138. z-index: 1;
  139. position: absolute;
  140. transform: translate(-50%, -50%);
  141. }
  142. }
  143. .save, .show {
  144. @include flex();
  145. width: 315rpx;
  146. height: 88rpx;
  147. color: #FFFFFF;
  148. font-size: 34rpx;
  149. display: inline-flex;
  150. border-radius: 10rpx;
  151. margin: 88rpx 30rpx 0rpx;
  152. background: $app-base-color;
  153. text {
  154. font-size: 42rpx;
  155. }
  156. }
  157. }
  158. }
  159. </style>