honour.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <view class="honour" :class="fromIndex ? '' : 'share'">
  3. <!-- <custom-nav :title="pageTitle" v-if="fromIndex"></custom-nav> -->
  4. <custom-toast ref="toast"></custom-toast>
  5. <view class="container">
  6. <view class="certificate" @tap="previewImage">
  7. <canvas style="position: absolute;top: 10%;left: 8%" id="canvas" canvas-id="canvas"
  8. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"></canvas>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. onShareAppMessage: function(res) {
  16. //分享荣誉证书
  17. uni.showLoading({
  18. title: '加载中',
  19. mask: true
  20. }); //显示loading
  21. if (res.from === 'button') {
  22. //如果点击分享给好友按钮,则分享页面为当前页面
  23. return {
  24. title: '我的学位证书',
  25. path: `/pages/honour/honour?name=${this.name}&season=${this.season}&grade=${this.grade}&year=${this.year}&month=${this.month}&day=${
  26. this.day
  27. }&header=${encodeURIComponent(this.header)}`
  28. };
  29. }
  30. return {
  31. //如果点击右上角分享按钮,则分享页面为小程序主页
  32. title: '大卫博士商学院卖货争霸',
  33. path: '/pages/zbsIndex/index'
  34. };
  35. },
  36. data() {
  37. return {
  38. pageTitle: '荣耀殿堂', //页面名
  39. canvasWidth: 0, //canvas 宽
  40. canvasHeight: 0, //canvas 高
  41. name: '', //名字
  42. season: '',
  43. grade: '', //学位
  44. year: '', //年
  45. month: '', //月
  46. day: '', //日
  47. fromIndex: true, //是否非分享页面,
  48. header: '',
  49. headerImg: '',
  50. deg: ''
  51. };
  52. },
  53. computed: {
  54. userServerInfo() {
  55. return uni.getStorageSync('newUserInfo');
  56. },
  57. },
  58. methods: {
  59. saveImg() {
  60. //保存证书到本地
  61. uni.canvasToTempFilePath({
  62. //首先将 canvas 转化为一个图片
  63. canvasId: 'canvas'
  64. })
  65. .then(([, res]) => {
  66. //获得图片的临时路径
  67. return uni.saveImageToPhotosAlbum({
  68. //将图片保存到系统相册
  69. filePath: res.tempFilePath
  70. });
  71. })
  72. .then(([err, res]) => {
  73. console.log(err, res);
  74. if (err && (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg ===
  75. 'saveImageToPhotosAlbum:fail auth deny')) {
  76. //如果用户拒绝授权保存图片到相册
  77. const that = this;
  78. uni.showModal({
  79. title: '"大卫博士学位争霸赛"需要保存图片或视频到你的相册',
  80. content: '',
  81. success(res) {
  82. if (res.confirm) {
  83. wx.openSetting({
  84. success(res) {
  85. that.saveImg();
  86. return;
  87. }
  88. });
  89. }
  90. }
  91. });
  92. } else if (err && err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
  93. uni.showModal({
  94. content: '保存图片失败,请手动截图保存',
  95. showCancel: false
  96. });
  97. }
  98. if (res && res.errMsg === 'saveImageToPhotosAlbum:ok') {
  99. uni.showModal({
  100. content: '图片已经保存成功',
  101. showCancel: false
  102. });
  103. }
  104. });
  105. },
  106. previewImage() {
  107. //点击 canvas 预览图片
  108. uni.showLoading({
  109. title: '加载中',
  110. mask: true
  111. }); //显示loading
  112. uni.canvasToTempFilePath({
  113. //首先将 canvas 转化为一个图片
  114. canvasId: 'canvas'
  115. }).then(([, res]) => {
  116. //获得图片的临时路径
  117. uni.hideLoading(); //app hide 时隐藏loading
  118. uni.previewImage({
  119. urls: [res.tempFilePath]
  120. });
  121. });
  122. },
  123. tohishonour() {
  124. //点击历史荣誉
  125. uni.navigateTo({
  126. url: '../hishonour/hishonour'
  127. });
  128. }
  129. },
  130. async onLoad(opt) {
  131. //当进入页面之后判断是否带带有参数
  132. const avatarUrl = this.userServerInfo.avatar
  133. opt.time = opt.time || this.userServerInfo.start_time;
  134. this.header = opt.name ? decodeURIComponent(opt.header) : avatarUrl;
  135. uni.showLoading({
  136. title: '加载中',
  137. mask: true
  138. });
  139. let _this = this;
  140. uni.downloadFile({
  141. url: this.header,
  142. success: ({
  143. statusCode,
  144. tempFilePath
  145. }) => {
  146. if (statusCode === 200) {
  147. this.headerImg = tempFilePath;
  148. }
  149. },
  150. fail: e => {
  151. console.log(e);
  152. uni.showToast({
  153. icon: 'none',
  154. title: '获取用户头像失败',
  155. duration: 3000
  156. });
  157. },
  158. complete: () => {
  159. uni.showLoading(); //停止 loading
  160. _this.canvasWidth = uni.upx2px(641);
  161. _this.canvasHeight = uni.upx2px(977);
  162. _this.fromIndex = opt.name ? false : true; //当前页面为分享页面
  163. _this.name = opt.name || _this.userServerInfo.nickname; //姓名
  164. _this.name = _this.name.slice(0, 6);
  165. _this.season = opt.season || _this.userServerInfo.season; // 赛季季数
  166. _this.grade = opt.grade || ''; //学位
  167. console.log(_this.grade);
  168. // _this.year = opt.year || filters.getYear(+opt.time) //年
  169. // _this.month = opt.month || filters.getMonth(+opt.time) //月
  170. // _this.day = opt.day || filters.getDate(+opt.time) //日
  171. const ctx = uni.createCanvasContext('canvas');
  172. ctx.drawImage(_this.grade === '大学毕业' ? '../../static/icon/zs2-min.jpg' :
  173. '../../static/icon/zs1-min.jpg', 0, 0, _this.canvasWidth, _this.canvasHeight);
  174. ctx.save();
  175. ctx.beginPath();
  176. ctx.arc(uni.upx2px(320), uni.upx2px(400), uni.upx2px(40), 0, 2 * Math.PI, false);
  177. ctx.clip();
  178. ctx.drawImage(_this.headerImg, uni.upx2px(280), uni.upx2px(360), uni.upx2px(80), uni
  179. .upx2px(80));
  180. ctx.restore();
  181. ctx.setTextAlign('center')
  182. ctx.setFontSize(uni.upx2px(26))
  183. ctx.fillText(_this.grade === '学霸' || _this.grade === '结业' ? '' : _this
  184. .grade, uni.upx2px(320), uni.upx2px(555))
  185. ctx.setFontSize(uni.upx2px(32))
  186. ctx.fillText(_this.name, uni.upx2px(315), uni.upx2px(495))
  187. ctx.setFontSize(uni.upx2px(28))
  188. ctx.fillText(_this.season, uni.upx2px(440), uni.upx2px(595))
  189. ctx.draw()
  190. _this.$hideLoading();
  191. uni.hideLoading()
  192. }
  193. });
  194. }
  195. };
  196. </script>
  197. <style scoped lang="scss">
  198. .container {
  199. padding: 36rpx 54rpx;
  200. .certificate {
  201. width: 642rpx;
  202. height: 998rpx;
  203. .bg {
  204. width: 100%;
  205. height: 100%;
  206. }
  207. }
  208. .share {
  209. padding: 22rpx 90rpx;
  210. display: flex;
  211. justify-content: center;
  212. align-items: center;
  213. .item {
  214. display: flex;
  215. justify-content: center;
  216. flex-direction: column;
  217. align-items: center;
  218. background: none;
  219. padding: 0;
  220. margin: 0;
  221. line-height: 70rpx !important;
  222. &::after,
  223. &::before {
  224. border: none;
  225. }
  226. .icon {
  227. width: 64rpx;
  228. height: 64rpx;
  229. }
  230. color: #333333;
  231. font-size: 28rpx;
  232. }
  233. }
  234. }
  235. .honour {
  236. @include page();
  237. &.share {
  238. border-top-color: #493225;
  239. }
  240. .content {
  241. display: flex;
  242. flex-direction: column;
  243. .top {
  244. width: 100%;
  245. height: 680rpx;
  246. background: #493225;
  247. border-bottom-right-radius: 375rpx 123rpx;
  248. border-bottom-left-radius: 375rpx 123rpx;
  249. position: relative;
  250. &.share {
  251. height: 890rpx;
  252. }
  253. #canvas {
  254. position: absolute;
  255. top: 100px;
  256. left: 50%;
  257. transform: translateX(-50%);
  258. &.share {
  259. top: 0;
  260. }
  261. }
  262. .bg {
  263. position: absolute;
  264. width: 566rpx;
  265. height: 464rpx;
  266. top: 100px;
  267. left: 50%;
  268. z-index: 1;
  269. transform: translateX(-50%);
  270. }
  271. .all {
  272. position: absolute;
  273. right: 32rpx;
  274. bottom: -13rpx;
  275. font-size: 26rpx;
  276. color: #fa6342;
  277. }
  278. }
  279. .bot {
  280. flex: 1;
  281. box-sizing: border-box;
  282. padding: 100rpx;
  283. .title {
  284. text-align: center;
  285. height: 45rpx;
  286. display: flex;
  287. justify-content: center;
  288. align-items: center;
  289. font-size: 36rpx;
  290. color: #fa6342;
  291. text {
  292. margin: 0 40rpx;
  293. }
  294. image {
  295. width: 48rpx;
  296. height: 45rpx;
  297. }
  298. }
  299. .text {
  300. margin-top: 80rpx;
  301. font-size: 30rpx;
  302. color: #666666;
  303. }
  304. .share-save {
  305. height: 60rpx;
  306. margin-top: 80rpx;
  307. display: flex;
  308. justify-content: space-between;
  309. .button {
  310. height: 100%;
  311. width: 240rpx;
  312. box-sizing: border-box;
  313. border: 2rpx solid #fa6342;
  314. border-radius: 30rpx;
  315. line-height: 56rpx;
  316. text-align: center;
  317. color: #fa6342;
  318. background: #ffffff;
  319. padding: 0;
  320. font-size: 24rpx;
  321. text {
  322. margin: 0 6rpx;
  323. vertical-align: middle;
  324. &.cuIcon {
  325. font-size: 36rpx;
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. </style>