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

350 lines
10KB

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