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

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