东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 line
7.3KB

  1. // pages/poster/poster.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. imgUrl: app.globalData.urlStatic,//图片路径
  9. canvasContron:null,//canvas
  10. posterBg:[],//海报背景
  11. qrCodeUrl:"https://www.jiyou-tech.com/2020/496_qichen/static/images/testQrCode.png",//二维码图片
  12. userHead: app.globalData.userInfoData ? app.globalData.userInfoData.avatarUrl : null,//用户头像
  13. rankNum: '8908',//缓存数字
  14. windowScale:0,//屏幕缩放比
  15. windowW: 0,//屏幕宽度
  16. windowH: 0,//屏幕高度
  17. posterUrl:[],//海报图片
  18. canvasShow: true,//是否渲染canvas
  19. swiperCurrent: 0,//swiper选中的元素下标
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. app.globalData.nowPage = 2;
  26. },
  27. /**
  28. * 生命周期函数--监听页面初次渲染完成
  29. */
  30. onReady: function () {
  31. this.data.canvasContron = wx.createCanvasContext('myCanvas');
  32. this.getCertificationCount();
  33. },
  34. cacheFun:function(){
  35. wx.getSystemInfo({
  36. success: option => {
  37. this.data.windowScale = option.windowWidth / 750;
  38. this.data.windowW = option.windowWidth;
  39. this.data.windowH = option.windowHeight;
  40. var countNum = 0;
  41. wx.getImageInfo({//缓存海报背景
  42. src: this.data.imgUrl + '/images/tempImg1.jpg',
  43. success: res => {
  44. this.data.posterBg.push(res.path);
  45. countNum += 1;
  46. }
  47. })
  48. wx.getImageInfo({//缓存二维码
  49. src: this.data.qrCodeUrl,
  50. success: res => {
  51. this.data.qrCodeUrl = res.path;
  52. countNum += 1;
  53. }
  54. })
  55. if (this.data.userHead) {
  56. wx.getImageInfo({//缓存头像
  57. src: this.data.userHead,
  58. success: res => {
  59. this.data.userHead = res.path;
  60. countNum += 1;
  61. }
  62. })
  63. }
  64. wx.getImageInfo({//缓存数字背景
  65. src: this.data.imgUrl + '/images/posterNum.png',
  66. success: res => {
  67. this.data.numBg = res.path;
  68. countNum += 1;
  69. }
  70. })
  71. this.data.rankNum = this.data.rankNum.toString().split("");
  72. for (let i = 0; i < this.data.rankNum.length; i++) {
  73. wx.getImageInfo({//缓存数字
  74. src: this.data.imgUrl + '/images/nums/' + this.data.rankNum[i] + '.png',
  75. success: res => {
  76. this.data.rankNum[i] = res.path;
  77. countNum += 1;
  78. }
  79. })
  80. }
  81. let setInt = setInterval(() => {
  82. if (countNum >= 8) {
  83. this.posterDrawing(1);
  84. clearInterval(setInt);
  85. }
  86. }, 50);
  87. }
  88. })
  89. },
  90. /**
  91. * 生命周期函数--监听页面显示
  92. */
  93. onShow: function () {
  94. },
  95. /**
  96. * 生命周期函数--监听页面隐藏
  97. */
  98. onHide: function () {
  99. },
  100. /**
  101. * 生命周期函数--监听页面卸载
  102. */
  103. onUnload: function () {
  104. },
  105. /**
  106. * 页面相关事件处理函数--监听用户下拉动作
  107. */
  108. onPullDownRefresh: function () {
  109. },
  110. /**
  111. * 页面上拉触底事件的处理函数
  112. */
  113. onReachBottom: function () {
  114. },
  115. /**
  116. * 用户点击右上角分享
  117. */
  118. onShareAppMessage: function () {
  119. return {
  120. title: '您有一份启辰星专属礼品待领取',
  121. imageUrl: this.data.imgUrl + "/images/posterShareImg" + (this.data.swiperCurrent+1)+".jpg",
  122. path: "/pages/coupon/coupon?friendOpenid=" + app.globalData.openid
  123. }
  124. },
  125. saveImg:function(){//保存到手机
  126. wx.saveImageToPhotosAlbum({
  127. filePath: this.data.posterUrl[this.data.swiperCurrent],
  128. success(res) {
  129. wx.showToast({
  130. title: '保存成功'
  131. })
  132. }
  133. })
  134. },
  135. posterDrawing: function (e) {//海报绘制
  136. var ctx = this.data.canvasContron;//canvas对象
  137. var scale = this.data.windowScale;//屏幕缩放比
  138. //背景
  139. ctx.drawImage(this.data.posterBg[e-1], 0, 0, scale * 444, scale * 817);//
  140. ctx.setFillStyle("#FFFFFF");
  141. ctx.save();
  142. ctx.beginPath(); //开始绘制
  143. //先画个圆 前两个参数确定了圆心 (x,y) 坐标 第三个参数是圆的半径 四参数是绘图方向 默认是false,即顺时针
  144. ctx.arc(scale * 48 / 2 + scale * 49, scale * 48 / 2 + scale * 538, scale * 48 / 2, 0, Math.PI * 2, false);
  145. ctx.clip(); //剪切
  146. ctx.drawImage(this.data.userHead, scale * 49, scale * 538, scale * 48, scale * 48); //头像
  147. ctx.restore(); //恢复之前保存的绘图上下文
  148. //名称
  149. ctx.setFontSize(scale * 28);
  150. ctx.setFillStyle('#FFFFFF');
  151. ctx.setTextAlign('left');
  152. ctx.fillText(app.globalData.userInfoData ? app.globalData.userInfoData.nickName : "", scale * 102, scale *572);
  153. //我是第XXX星探
  154. ctx.drawImage(this.data.numBg, scale * 50, scale * 598, scale * 333, scale * 74);
  155. //排名数字
  156. ctx.drawImage(this.data.rankNum[0], scale * 168, scale * 602, scale * 32, scale * 40);
  157. ctx.drawImage(this.data.rankNum[1], scale * 202, scale * 602, scale * 32, scale * 40);
  158. ctx.drawImage(this.data.rankNum[2], scale * 236, scale * 602, scale * 32, scale * 40);
  159. ctx.drawImage(this.data.rankNum[3], scale * 270, scale * 602, scale * 32, scale * 40);
  160. //二维码
  161. ctx.drawImage(this.data.qrCodeUrl, scale * 282, scale * 672, scale * 105, scale* 106);
  162. //绘制
  163. ctx.draw(false, setTimeout(() => {
  164. wx.canvasToTempFilePath({
  165. width:scale*444,
  166. height: scale * 817,
  167. canvasId: 'myCanvas',
  168. success: res => {
  169. this.data.posterUrl.push(res.tempFilePath);
  170. this.setData({
  171. posterUrl: this.data.posterUrl
  172. })
  173. if (e < 3) {
  174. wx.getImageInfo({
  175. src: this.data.imgUrl + '/images/tempImg'+(e+1)+'.jpg',
  176. success: res => {
  177. this.data.posterBg.push(res.path);
  178. this.posterDrawing(e + 1);
  179. }
  180. })
  181. }else{
  182. this.setData({
  183. canvasShow:false
  184. })
  185. }
  186. }
  187. })
  188. }, 300));
  189. },
  190. savePoster:function(){//预览海报
  191. wx.previewImage({
  192. current: this.data.posterUrl[this.data.swiperCurrent],
  193. urls: this.data.posterUrl // 需要预览的图片http链接列表
  194. })
  195. },
  196. prevImg() {//上一张图片
  197. if (this.data.swiperCurrent > 0) {
  198. this.setData({
  199. swiperCurrent: this.data.swiperCurrent -= 1,
  200. })
  201. }
  202. },
  203. nextImg() {//下一张图片
  204. if (this.data.swiperCurrent < this.data.posterUrl.length - 1) {
  205. this.setData({
  206. swiperCurrent: this.data.swiperCurrent += 1,
  207. })
  208. }
  209. },
  210. swiperChange(e) {//通过鼠标滑动改变swiper时
  211. if (e.detail.source == "touch") {
  212. this.setData({
  213. swiperCurrent: e.detail.current,
  214. })
  215. }
  216. },
  217. getCertificationCount: function () {//获取星探计划人数
  218. app.wxRequest(app.globalData.urlRoot +"userInfo/getCertificationCount",{},res=>{
  219. this.createQrcode();
  220. if(res.code==200){
  221. this.data.rankNum = res.data.total;
  222. }
  223. },this)
  224. },
  225. createQrcode: function () {//获取个人小程序分享码
  226. app.wxRequest(app.globalData.urlRoot + "wxInfo/getQrcode", {}, res => {
  227. if (res.code = 200) {
  228. this.data.qrCodeUrl = res.data.qrcodeUrl;
  229. }
  230. this.cacheFun();
  231. }, this)
  232. }
  233. })