东风启辰小程序端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

5 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. fail:res=>{
  134. // wx.getSetting({
  135. // })
  136. // wx.showModal({
  137. // title: '设置',
  138. // content: '请授权“保存到相册”',
  139. // success:()=>{
  140. // wx.openSetting({
  141. // success(e){
  142. // if (e.scope.writePhotosAlbum){
  143. // wx.saveImageToPhotosAlbum({
  144. // filePath: this.data.posterUrl[this.data.swiperCurrent],
  145. // success(res) {
  146. // wx.showToast({
  147. // title: '保存成功'
  148. // })
  149. // }
  150. // })
  151. // }else{
  152. // wx.showToast({
  153. // title: '授权失败',
  154. // icon:"none"
  155. // })
  156. // }
  157. // }
  158. // })
  159. // }
  160. // })
  161. }
  162. })
  163. },
  164. posterDrawing: function (e) {//海报绘制
  165. var ctx = this.data.canvasContron;//canvas对象
  166. var scale = this.data.windowScale;//屏幕缩放比
  167. //背景
  168. ctx.drawImage(this.data.posterBg[e-1], 0, 0, scale * 444, scale * 817);//
  169. ctx.setFillStyle("#FFFFFF");
  170. ctx.save();
  171. ctx.beginPath(); //开始绘制
  172. //先画个圆 前两个参数确定了圆心 (x,y) 坐标 第三个参数是圆的半径 四参数是绘图方向 默认是false,即顺时针
  173. ctx.arc(scale * 48 / 2 + scale * 49, scale * 48 / 2 + scale * 538, scale * 48 / 2, 0, Math.PI * 2, false);
  174. ctx.clip(); //剪切
  175. ctx.drawImage(this.data.userHead, scale * 49, scale * 538, scale * 48, scale * 48); //头像
  176. ctx.restore(); //恢复之前保存的绘图上下文
  177. //名称
  178. ctx.setFontSize(scale * 28);
  179. ctx.setFillStyle('#FFFFFF');
  180. ctx.setTextAlign('left');
  181. ctx.fillText(app.globalData.userInfoData ? app.globalData.userInfoData.nickName : "", scale * 102, scale *572);
  182. //我是第XXX星探
  183. ctx.drawImage(this.data.numBg, scale * 50, scale * 598, scale * 333, scale * 74);
  184. //排名数字
  185. ctx.drawImage(this.data.rankNum[0], scale * 168, scale * 602, scale * 32, scale * 40);
  186. ctx.drawImage(this.data.rankNum[1], scale * 202, scale * 602, scale * 32, scale * 40);
  187. ctx.drawImage(this.data.rankNum[2], scale * 236, scale * 602, scale * 32, scale * 40);
  188. ctx.drawImage(this.data.rankNum[3], scale * 270, scale * 602, scale * 32, scale * 40);
  189. //二维码
  190. ctx.drawImage(this.data.qrCodeUrl, scale * 282, scale * 672, scale * 105, scale* 106);
  191. //绘制
  192. ctx.draw(false, setTimeout(() => {
  193. wx.canvasToTempFilePath({
  194. width:scale*444,
  195. height: scale * 817,
  196. canvasId: 'myCanvas',
  197. success: res => {
  198. this.data.posterUrl.push(res.tempFilePath);
  199. this.setData({
  200. posterUrl: this.data.posterUrl
  201. })
  202. if (e < 3) {
  203. wx.getImageInfo({
  204. src: this.data.imgUrl + '/images/tempImg'+(e+1)+'.jpg',
  205. success: res => {
  206. this.data.posterBg.push(res.path);
  207. this.posterDrawing(e + 1);
  208. }
  209. })
  210. }else{
  211. this.setData({
  212. canvasShow:false
  213. })
  214. }
  215. }
  216. })
  217. }, 300));
  218. },
  219. savePoster:function(){//预览海报
  220. wx.previewImage({
  221. current: this.data.posterUrl[this.data.swiperCurrent],
  222. urls: this.data.posterUrl // 需要预览的图片http链接列表
  223. })
  224. },
  225. prevImg() {//上一张图片
  226. if (this.data.swiperCurrent > 0) {
  227. this.setData({
  228. swiperCurrent: this.data.swiperCurrent -= 1,
  229. })
  230. }
  231. },
  232. nextImg() {//下一张图片
  233. if (this.data.swiperCurrent < this.data.posterUrl.length - 1) {
  234. this.setData({
  235. swiperCurrent: this.data.swiperCurrent += 1,
  236. })
  237. }
  238. },
  239. swiperChange(e) {//通过鼠标滑动改变swiper时
  240. if (e.detail.source == "touch") {
  241. this.setData({
  242. swiperCurrent: e.detail.current,
  243. })
  244. }
  245. },
  246. getCertificationCount: function () {//获取星探计划人数
  247. app.wxRequest(app.globalData.urlRoot +"userInfo/getCertificationCount",{},res=>{
  248. this.createQrcode();
  249. if(res.code==200){
  250. this.data.rankNum = res.data.total;
  251. }
  252. },this)
  253. },
  254. createQrcode: function () {//获取个人小程序分享码
  255. app.wxRequest(app.globalData.urlRoot + "wxInfo/getQrcode", {}, res => {
  256. if (res.code = 200) {
  257. this.data.qrCodeUrl = res.data.qrcodeUrl;
  258. }
  259. this.cacheFun();
  260. }, this)
  261. }
  262. })