东风启辰小程序端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

293 lines
8.8KB

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