index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <view class="honour" v-if="isAjaxed">
  3. <custom-nav title="排行版"></custom-nav>
  4. <custom-toast ref="toast"></custom-toast>
  5. <view class="container">
  6. <view class="certificate"><canvas id="canvas" canvas-id="canvas" :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"></canvas></view>
  7. <view class="share">
  8. <view class="item" @tap="saveImg"><view>保存图片</view></view>
  9. <text>保存图片去朋友圈晒晒吧~</text>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import { share_data } from '../../api.js';
  16. import * as filters from '../../common/js/filters.js';
  17. export default {
  18. data() {
  19. return {
  20. pageTitle: '荣耀殿堂', //页面名
  21. canvasWidth: 0, //canvas 宽
  22. canvasHeight: 0, //canvas 高
  23. name: '', //名字
  24. season: '',
  25. grade: '', //学位
  26. year: '', //年
  27. month: '', //月
  28. day: '', //日
  29. fromIndex: true, //是否非分享页面,
  30. header: '',
  31. headerImg: '',
  32. deg: '',
  33. isAjaxed: false,
  34. imgList: [
  35. { imgUrl: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/cjd_bg1.png', drawUrl: '' },
  36. { imgUrl: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/cjd_bg3.png', drawUrl: '' },
  37. { imgUrl: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/cjd_bg4.png', drawUrl: '' },
  38. { imgUrl: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/honor_bg.png', drawUrl: '' },
  39. { imgUrl: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/honor_bg_no.png', drawUrl: '' },
  40. { imgUrl: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/honor_bg2.png', drawUrl: '' },
  41. { imgUrl: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/honor_bg2_no.png', drawUrl: '' }
  42. ]
  43. };
  44. },
  45. computed: {
  46. userServerInfo() {
  47. return this.$store.state.userServerInfo;
  48. },
  49. userWeixinInfo() {
  50. return this.$store.state.userWeixinInfo;
  51. }
  52. },
  53. async onLoad(opt) {
  54. //当进入页面之后判断是否带带有参数
  55. uni.hideShareMenu();
  56. this.getImg();
  57. },
  58. methods: {
  59. drawPoster() {
  60. let img = this.imgList;
  61. console.log(img, 'img');
  62. this.$ajax.get(`${share_data}`).then(([, { data: res }]) => {
  63. this.isAjaxed = true;
  64. if (res.code === 200) {
  65. this.canvasWidth = uni.upx2px(750);
  66. let y = 1188 + 950;
  67. if (res.data.medal > 0) {
  68. y += 524;
  69. }
  70. if (res.data.max_inte) {
  71. y += 421;
  72. }
  73. this.canvasHeight = uni.upx2px(y);
  74. this.name = '刘德华' || this.userServerInfo.name; //姓名
  75. this.name = this.name.slice(0, 6);
  76. this.season = 23 || this.userServerInfo.season; // 赛季季数
  77. const ctx = uni.createCanvasContext('canvas');
  78. ctx.drawImage(img[0].drawUrl, 0, 0, this.canvasWidth, uni.upx2px(1188));
  79. ctx.setTextAlign('center');
  80. ctx.setFontSize(uni.upx2px(30));
  81. ctx.setFillStyle('#F85820');
  82. ctx.fillText(`${res.data.rank}名`, uni.upx2px(160), uni.upx2px(920));
  83. ctx.fillText(`${res.data.integral}分`, uni.upx2px(380), uni.upx2px(920));
  84. ctx.fillText(`${res.data.num}套`, uni.upx2px(590), uni.upx2px(920));
  85. ctx.fillText(`${res.data.bonus}元`, uni.upx2px(160), uni.upx2px(1120));
  86. ctx.fillText(`${res.data.glory}个`, uni.upx2px(380), uni.upx2px(1120));
  87. ctx.fillText(this.jp(res.data.medal), uni.upx2px(590), uni.upx2px(1120));
  88. ctx.setFillStyle('#FFFFFF');
  89. ctx.setFontSize(uni.upx2px(40));
  90. ctx.fillText(`大卫博士学位争霸赛第${res.data.season}季`, uni.upx2px(375), uni.upx2px(385));
  91. // 学霸
  92. if (res.data.max_inte) {
  93. ctx.drawImage('../../static/new/cjd_bg2.png', 0, uni.upx2px(1188), this.canvasWidth, uni.upx2px(421));
  94. ctx.setFontSize(uni.upx2px(54));
  95. ctx.setFillStyle('#E95737');
  96. // 隐藏等级
  97. // ctx.fillText(`${res.data.level_name}学霸`, uni.upx2px(375), uni.upx2px(1530));
  98. ctx.fillText(`学霸`, uni.upx2px(375), uni.upx2px(1530));
  99. }
  100. //证书
  101. let zsY = res.data.max_inte ? 421 + 1188 : 1188;
  102. ctx.drawImage(img[1].drawUrl, 0, uni.upx2px(zsY), this.canvasWidth, uni.upx2px(950));
  103. ctx.drawImage(res.data.integral >= 200 ? img[3].drawUrl : img[4].drawUrl, uni.upx2px(155), uni.upx2px(zsY + 171), uni.upx2px(153), uni.upx2px(234));
  104. ctx.drawImage(res.data.integral >= 800 ? img[5].drawUrl : img[6].drawUrl, uni.upx2px(436), uni.upx2px(zsY + 171), uni.upx2px(153), uni.upx2px(234));
  105. ctx.drawImage(res.data.integral >= 2500 ? img[5].drawUrl : img[6].drawUrl, uni.upx2px(436), uni.upx2px(zsY + 541), uni.upx2px(153), uni.upx2px(234));
  106. ctx.drawImage(res.data.integral >= 1600 ? img[5].drawUrl : img[6].drawUrl, uni.upx2px(155), uni.upx2px(zsY + 541), uni.upx2px(153), uni.upx2px(234));
  107. // 奖牌
  108. if (res.data.medal > 0) {
  109. let jpY = res.data.max_inte ? 421 + 1188 + 950 : 1188 + 950;
  110. ctx.drawImage(img[3].drawUrl, 0, uni.upx2px(jpY), this.canvasWidth, uni.upx2px(524));
  111. ctx.setFontSize(uni.upx2px(32));
  112. ctx.setFillStyle('#FFFFFF');
  113. ctx.fillText('金牌奖章', uni.upx2px(375), uni.upx2px(jpY + 446));
  114. ctx.drawImage(`../../static/new/jp${res.data.medal + 1}.png`, uni.upx2px(280), uni.upx2px(jpY + 141), uni.upx2px(200), uni.upx2px(223));
  115. }
  116. ctx.draw();
  117. }
  118. });
  119. },
  120. loadImage: function(item) {
  121. return new Promise(resolve => {
  122. wx.getImageInfo({
  123. src: item.imgUrl,
  124. success: res => {
  125. item.drawUrl = res.path;
  126. resolve(item); //回掉函数的返回值
  127. }
  128. });
  129. });
  130. },
  131. getImg: function() {
  132. let imageArr = this.imgList;
  133. let arr = [];
  134. imageArr.forEach(item => {
  135. arr.push(this.loadImage(item));
  136. });
  137. Promise.all(arr).then(newList => {
  138. this.imgList = newList;
  139. this.drawPoster();
  140. });
  141. },
  142. jp(num) {
  143. let out = '';
  144. switch (num) {
  145. case 1:
  146. out = '铜牌';
  147. break;
  148. case 2:
  149. out = '银牌';
  150. break;
  151. case 3:
  152. out = '金牌';
  153. break;
  154. default:
  155. out = '未获得';
  156. }
  157. return out;
  158. },
  159. saveImg() {
  160. //保存证书到本地
  161. uni.canvasToTempFilePath({
  162. //首先将 canvas 转化为一个图片
  163. canvasId: 'canvas'
  164. })
  165. .then(([, res]) => {
  166. //获得图片的临时路径
  167. return uni.saveImageToPhotosAlbum({
  168. //将图片保存到系统相册
  169. filePath: res.tempFilePath
  170. });
  171. })
  172. .then(([err, res]) => {
  173. if (err && (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg === 'saveImageToPhotosAlbum:fail auth deny')) {
  174. //如果用户拒绝授权保存图片到相册
  175. const that = this;
  176. uni.showModal({
  177. title: '"大卫博士学位争霸赛"需要保存图片或视频到你的相册',
  178. content: '',
  179. success(res) {
  180. if (res.confirm) {
  181. wx.openSetting({
  182. success(res) {
  183. that.saveImg();
  184. return;
  185. }
  186. });
  187. }
  188. }
  189. });
  190. } else if (err && err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
  191. uni.showModal({
  192. content: '保存图片失败,请手动截图保存',
  193. showCancel: false
  194. });
  195. }
  196. if (res && res.errMsg === 'saveImageToPhotosAlbum:ok') {
  197. uni.showModal({
  198. content: '图片已经保存成功',
  199. showCancel: false
  200. });
  201. }
  202. });
  203. }
  204. }
  205. };
  206. </script>
  207. <style lang="scss">
  208. .container {
  209. .certificate {
  210. .bg {
  211. width: 100%;
  212. height: 100%;
  213. }
  214. }
  215. .share {
  216. padding: 22rpx 90rpx;
  217. background-color: #ffd352;
  218. display: flex;
  219. justify-content: center;
  220. flex-direction: column;
  221. align-items: center;
  222. color: #fa6b3d;
  223. font-size: 32rpx;
  224. .item {
  225. width: 100%;
  226. height: 110rpx;
  227. line-height: 110rpx;
  228. text-align: center;
  229. background-color: #f85820;
  230. border-radius: 110rpx;
  231. padding: 0;
  232. margin: 0;
  233. box-shadow: 0 0 5px rgba(233, 87, 55, 0.45);
  234. &::after,
  235. &::before {
  236. border: none;
  237. }
  238. color: #ffd252;
  239. font-size: 50rpx;
  240. margin-bottom: 16rpx;
  241. }
  242. }
  243. }
  244. .honour {
  245. @include page();
  246. height: auto;
  247. min-height: 100%;
  248. overflow: inherit;
  249. &.share {
  250. border-top-color: #493225;
  251. }
  252. .content {
  253. display: flex;
  254. flex-direction: column;
  255. .top {
  256. width: 100%;
  257. height: 680rpx;
  258. background: #493225;
  259. border-bottom-right-radius: 375rpx 123rpx;
  260. border-bottom-left-radius: 375rpx 123rpx;
  261. position: relative;
  262. &.share {
  263. height: 890rpx;
  264. }
  265. #canvas {
  266. position: absolute;
  267. top: 56rpx;
  268. left: 50%;
  269. transform: translateX(-50%);
  270. &.share {
  271. top: 0;
  272. }
  273. }
  274. .bg {
  275. position: absolute;
  276. width: 566rpx;
  277. height: 464rpx;
  278. top: 35rpx;
  279. left: 50%;
  280. z-index: 1;
  281. transform: translateX(-50%);
  282. }
  283. .all {
  284. position: absolute;
  285. right: 32rpx;
  286. bottom: -13rpx;
  287. font-size: 26rpx;
  288. color: #fa6342;
  289. }
  290. }
  291. .bot {
  292. flex: 1;
  293. box-sizing: border-box;
  294. padding: 100rpx;
  295. .title {
  296. text-align: center;
  297. height: 45rpx;
  298. display: flex;
  299. justify-content: center;
  300. align-items: center;
  301. font-size: 36rpx;
  302. color: #fa6342;
  303. text {
  304. margin: 0 40rpx;
  305. }
  306. image {
  307. width: 48rpx;
  308. height: 45rpx;
  309. }
  310. }
  311. .text {
  312. margin-top: 80rpx;
  313. font-size: 30rpx;
  314. color: #666666;
  315. }
  316. .share-save {
  317. height: 60rpx;
  318. margin-top: 80rpx;
  319. display: flex;
  320. justify-content: space-between;
  321. .button {
  322. height: 100%;
  323. width: 240rpx;
  324. box-sizing: border-box;
  325. border: 2rpx solid #fa6342;
  326. border-radius: 30rpx;
  327. line-height: 56rpx;
  328. text-align: center;
  329. color: #fa6342;
  330. background: #ffffff;
  331. padding: 0;
  332. font-size: 24rpx;
  333. text {
  334. margin: 0 6rpx;
  335. vertical-align: middle;
  336. &.cuIcon {
  337. font-size: 36rpx;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. </style>