东风启辰小程序端
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 lines
7.1KB

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