notice.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="notice">
  3. <custom-nav title="录取通知书"></custom-nav>
  4. <view class="canvas_container content">
  5. <!-- <image src="https://api.jiuweiyun.cn/public/uploads/icon/new_tzs.png" mode="" class="bg"></image> -->
  6. <canvas id="canvas" canvas-id="canvas" :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }" @longpress="saveImage()" />
  7. </view>
  8. <view class="save">(长按保存图片)</view>
  9. </view>
  10. </template>
  11. <script>
  12. import { mapState } from 'vuex';
  13. export default {
  14. data() {
  15. return {
  16. canvasWidth: 0, //canvas 宽
  17. canvasHeight: 0, //canvas 高
  18. header: '',
  19. headerImg: ''
  20. };
  21. },
  22. computed: {
  23. ...mapState(['userServerInfo'])
  24. // userServerInfo() {
  25. // return this.$store.state.userServerInfo
  26. // }
  27. },
  28. onShow() {
  29. uni.hideLoading()
  30. this.initCanvas()
  31. },
  32. methods: {
  33. initCanvas(cb) {
  34. const _this = this
  35. // this.header = this.userServerInfo.avatar
  36. // const nickname = uni.getStorageSync('nickName').slice(0,8)
  37. // const nickname = '柳德湖'
  38. this.header = this.userServerInfo.avatar
  39. const nickname = this.userServerInfo.name.slice(0,8)
  40. uni.showLoading({
  41. title: '加载中',
  42. mask: true
  43. })
  44. uni.downloadFile({
  45. url: this.header,
  46. success: ({ statusCode, tempFilePath }) => {
  47. if (statusCode === 200) {
  48. this.headerImg = tempFilePath
  49. }
  50. },
  51. fail:(e) => {
  52. uni.showModal({
  53. content: JSON.stringify(e),
  54. showCancel: false
  55. })
  56. // uni.showToast({
  57. // icon: 'none',
  58. // title: '获取用户头像失败',
  59. // duration: 3000
  60. // })
  61. },
  62. complete: () => {
  63. uni.getImageInfo({
  64. src: 'https://api.jiuweiyun.cn/public/uploads/icon/new_tzs.png',
  65. success: res => {
  66. uni.hideLoading() //停止 loading
  67. _this.canvasWidth = uni.upx2px(690);
  68. _this.canvasHeight = uni.upx2px(1100);
  69. let ctx = uni.createCanvasContext('canvas');
  70. ctx.drawImage(res.path, 0, 0, uni.upx2px(690), uni.upx2px(1100))
  71. ctx.save()
  72. ctx.beginPath()
  73. ctx.arc(uni.upx2px(342), uni.upx2px(430), uni.upx2px(60), 0, 2 * Math.PI, false)
  74. ctx.clip()
  75. ctx.drawImage(_this.headerImg, uni.upx2px(282), uni.upx2px(370), uni.upx2px(120), uni.upx2px(120))
  76. ctx.restore()
  77. ctx.font = 'normal normal 20px PingFang SC'
  78. let year = new Date(_this.userServerInfo.enroll_time).getFullYear();
  79. let month = new Date(_this.userServerInfo.enroll_time).getMonth() + 1;
  80. let day = new Date(_this.userServerInfo.enroll_time).getDate();
  81. ctx.setFillStyle("#333333")
  82. ctx.textAlign = "left"
  83. ctx.setFontSize(uni.upx2px(32));
  84. ctx.fillText(_this.userServerInfo.season, uni.upx2px(200), uni.upx2px(678));
  85. ctx.setFontSize(uni.upx2px(28));
  86. ctx.fillText(year, uni.upx2px(380), uni.upx2px(918));
  87. ctx.fillText(month > 10 ? month : `0${month}`, uni.upx2px(478), uni.upx2px(918));
  88. ctx.fillText(day > 10 ? day : `0${day}`, uni.upx2px(535), uni.upx2px(918));
  89. ctx.setFontSize(uni.upx2px(30));
  90. ctx.setFillStyle("#193669")
  91. ctx.font = 'normal bold 15px PingFang SC'
  92. ctx.textAlign = "center"
  93. ctx.fillText(nickname, uni.upx2px(338), uni.upx2px(560));
  94. ctx.draw();
  95. }
  96. })
  97. }
  98. })
  99. },
  100. saveImage() {
  101. this.getCanvas()
  102. },
  103. getCanvas() {
  104. uni.showLoading({
  105. title: '下载中...'
  106. });
  107. uni.canvasToTempFilePath({
  108. width: uni.upx2px(690),
  109. height: uni.upx2px(1100),
  110. canvasId: 'canvas',
  111. success: function(res) {
  112. uni.saveImageToPhotosAlbum({
  113. filePath: res.tempFilePath,
  114. success: res => {
  115. uni.showModal({
  116. content: '录取通知书保存成功',
  117. showCancel: false
  118. });
  119. },
  120. fail: err => {
  121. if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
  122. uni.showModal({
  123. title: '提示',
  124. content: '您好,请先授权,再保存此图片。',
  125. showCancel: false,
  126. success: res => {
  127. if (res.confirm) {
  128. uni.openSetting({
  129. success(settingdata) {
  130. console.log(settingdata);
  131. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  132. uni.showModal({
  133. content: '请重新长按保存图片',
  134. showCancel: false
  135. });
  136. } else {
  137. uni.showModal({
  138. content: '您拒绝了保存到相册权限',
  139. showCancel: false
  140. });
  141. }
  142. },
  143. fail: err => {
  144. uni.showModal({
  145. content: '手动打开相册权限失败',
  146. showCancel: false
  147. });
  148. }
  149. });
  150. }
  151. }
  152. });
  153. } else {
  154. _this.initCanvas()
  155. }
  156. },
  157. complete() {
  158. uni.hideLoading();
  159. }
  160. });
  161. }
  162. });
  163. }
  164. }
  165. };
  166. </script>
  167. <style scoped lang="scss">
  168. .notice {
  169. // @include page();
  170. width: 100%;
  171. height: 100%;
  172. min-height: 100%;
  173. // display: flex;
  174. // justify-content: space-between;
  175. // flex-direction: column;
  176. background-color: #ededed;
  177. .canvas_container {
  178. // flex: 1;
  179. padding: 170rpx 30rpx 0rpx 30rpx;
  180. position: relative;
  181. .bg {
  182. width: 690rpx;
  183. height: 1100rpx;
  184. }
  185. #canvas {
  186. width: 690rpx;
  187. height: 1100rpx;
  188. // position: absolute;
  189. // top: 169rpx;
  190. // bottom: 0;
  191. // left: 30rpx;
  192. // right: 0;
  193. }
  194. }
  195. }
  196. .save {
  197. width: 100%;
  198. text-align: center;
  199. margin-top:10rpx;
  200. }
  201. </style>