honour.vue 8.5 KB

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