东风启辰小程序端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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 роки тому
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. _totalDownloadTask = 6;
  47. _currentSuccessDownloadTask = 0;
  48. _saveImg = false;
  49. if(app.globalData.userInfoData && app.globalData.userInfoData.avatarUrl){
  50. this.data.userHead = app.globalData.userInfoData.avatarUrl;
  51. this.setData({
  52. userHead:app.globalData.userInfoData.avatarUrl,
  53. userName:app.globalData.userInfoData.nickName
  54. })
  55. }
  56. this.data.canvasContron = wx.createCanvasContext('myCanvas');
  57. if (app.globalData.openid) {
  58. this.createQrcode();
  59. // this.getCertificationCount();
  60. } else {
  61. app.globalData.openidSuccessFuc = this.createQrcode;
  62. }
  63. },
  64. cacheFun:function(){
  65. // _saveImg = true;
  66. wx.getSystemInfo({
  67. success: option => {
  68. this.data.windowScale = option.windowWidth / 750;
  69. this.data.windowW = option.windowWidth;
  70. this.data.windowH = option.windowHeight;
  71. for (let i = 0; i < 3; i++) {
  72. wx.getImageInfo({//缓存海报背景
  73. src: this.data.imgUrl + '/newImages7/16_'+(i+1)+'.png?v=003',
  74. success: res => {
  75. this.data.posterBg[i] = res.path;
  76. _savePostBg[i] = res.path;
  77. _currentSuccessDownloadTask ++;
  78. this.posterDrawing(this.data.swiperCurrent + 1);
  79. }
  80. })
  81. }
  82. wx.getImageInfo({//缓存二维码
  83. src: this.data.qrCodeUrl,
  84. success: res => {
  85. this.data.qrCodeUrl = res.path;
  86. _saveQrCodeUrl = res.path;
  87. _currentSuccessDownloadTask++;
  88. this.posterDrawing(this.data.swiperCurrent + 1);
  89. }
  90. })
  91. if (this.data.userHead) {
  92. wx.getImageInfo({//缓存头像
  93. src: this.data.userHead,
  94. success: res => {
  95. this.data.userHead = res.path;
  96. _saveUserHeadUrl = res.path;
  97. _currentSuccessDownloadTask++;
  98. this.posterDrawing(this.data.swiperCurrent + 1);
  99. }
  100. })
  101. }
  102. // wx.getImageInfo({//缓存数字背景
  103. // src: this.data.imgUrl + '/images/posterNum.png?v=004',
  104. // success: res => {
  105. // this.data.numBg = res.path;
  106. // }
  107. // })
  108. // wx.getImageInfo({//缓存数字背景1
  109. // src: this.data.imgUrl + '/newImages4/5.png?v=004',
  110. // success: res => {
  111. // this.data.numBg[0] = res.path;
  112. // _currentSuccessDownloadTask++;
  113. // this.posterDrawing(this.data.swiperCurrent + 1);
  114. // }
  115. // })
  116. // wx.getImageInfo({//缓存数字背景2
  117. // src: this.data.imgUrl + '/newImages4/6.png?v=004',
  118. // success: res => {
  119. // this.data.numBg[1] = res.path;
  120. // _currentSuccessDownloadTask++;
  121. // this.posterDrawing(this.data.swiperCurrent + 1);
  122. // }
  123. // })
  124. // wx.getImageInfo({//缓存数字背景3
  125. // src: this.data.imgUrl + '/newImages4/4.png?v=004',
  126. // success: res => {
  127. // this.data.numBg[2] = res.path;
  128. // _currentSuccessDownloadTask++;
  129. // this.posterDrawing(this.data.swiperCurrent + 1);
  130. // }
  131. // })
  132. // wx.getImageInfo({//缓存数字背景4
  133. // src: this.data.imgUrl + '/newImages3/51.png?v=005',
  134. // success: res => {
  135. // this.data.numBg[3] = res.path;
  136. // _currentSuccessDownloadTask++;
  137. // this.posterDrawing(this.data.swiperCurrent + 1);
  138. // }
  139. // })
  140. wx.getImageInfo({//缓存海报底部图片
  141. src: this.data.imgUrl + "/newImages7/15.png?v=002",
  142. success: res => {
  143. this.data.posterBottom = res.path;
  144. _currentSuccessDownloadTask++;
  145. this.posterDrawing(this.data.swiperCurrent + 1);
  146. }
  147. })
  148. // for (let i = 0; i < this.data.rankNum.length; i++) {
  149. // wx.getImageInfo({//缓存数字
  150. // src: this.data.imgUrl + '/images/nums/' + this.data.rankNum[i] + '.png',
  151. // success: res => {
  152. // this.data.rankNum[i] = res.path;
  153. // _currentSuccessDownloadTask ++;
  154. // this.posterDrawing(this.data.swiperCurrent + 1);
  155. // }
  156. // })
  157. // }
  158. }
  159. })
  160. },
  161. /**
  162. * 生命周期函数--监听页面显示
  163. */
  164. onShow: function () {
  165. },
  166. /**
  167. * 生命周期函数--监听页面隐藏
  168. */
  169. onHide: function () {
  170. },
  171. /**
  172. * 生命周期函数--监听页面卸载
  173. */
  174. onUnload: function () {
  175. },
  176. /**
  177. * 页面相关事件处理函数--监听用户下拉动作
  178. */
  179. onPullDownRefresh: function () {
  180. },
  181. /**
  182. * 页面上拉触底事件的处理函数
  183. */
  184. onReachBottom: function () {
  185. },
  186. /**
  187. * 用户点击右上角分享
  188. */
  189. onShareAppMessage: function () {
  190. return {
  191. title: '您有一份启辰星专属礼品待领取',
  192. // imageUrl: this.data.imgUrl + "/newImages3/28_" + (this.data.swiperCurrent+1)+".png?v=004",
  193. imageUrl: this.data.imgUrl + "/newImages7/1.png",
  194. path: "/pages/coupon/coupon?scene=" + app.globalData.openid
  195. }
  196. },
  197. saveImg: function () {//保存到手机
  198. if (!this.data.posterUrl[this.data.swiperCurrent]){
  199. wx.showLoading({
  200. title: '海报合成中',
  201. mask:true
  202. })
  203. _saveImg = true;
  204. // console.log("值1:"+_currentSuccessDownloadTask);
  205. this.posterDrawing(this.data.swiperCurrent + 1);
  206. }else{
  207. this.savePic();
  208. }
  209. },
  210. savePic: function () {
  211. wx.saveImageToPhotosAlbum({
  212. filePath: this.data.posterUrl[this.data.swiperCurrent],
  213. success(res) {
  214. wx.showToast({
  215. title: '保存成功'
  216. })
  217. },
  218. fail: res => {
  219. wx.getSetting({
  220. success: res => {
  221. if (res.authSetting['scope.writePhotosAlbum']) {
  222. wx.showToast({
  223. title: '保存失败',
  224. icon: "none"
  225. })
  226. } else {
  227. wx.showModal({
  228. title: '授权设置',
  229. content: '请授权“保存到相册”',
  230. success: (opt) => {
  231. if (opt.confirm) {
  232. wx.openSetting({
  233. success: e => {
  234. if (e.authSetting['scope.writePhotosAlbum']) {
  235. wx.saveImageToPhotosAlbum({
  236. filePath: this.data.posterUrl[this.data.swiperCurrent],
  237. success(res) {
  238. wx.showToast({
  239. title: '保存成功'
  240. })
  241. }
  242. })
  243. } else {
  244. wx.showToast({
  245. title: '授权失败',
  246. icon: "none"
  247. })
  248. }
  249. }
  250. })
  251. }
  252. }
  253. })
  254. }
  255. }
  256. })
  257. }
  258. })
  259. },
  260. posterDrawing: function (e) {//海报绘制
  261. // console.log("_totalDownloadTask = " + _totalDownloadTask)
  262. // console.log("_currentSuccessDownloadTask = " + _currentSuccessDownloadTask)
  263. //图片尚未下载完成,禁止绘制
  264. // console.log("值2:"+_currentSuccessDownloadTask);
  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. ctx.drawImage(this.data.userHead, scale * 121, scale * 62, scale * 50, scale * 50); //头像
  280. ctx.restore(); //恢复之前保存的绘图上下文
  281. //名称
  282. ctx.setFontSize(scale * 22);
  283. if(this.data.swiperCurrent==1){
  284. ctx.setFillStyle('#595757');
  285. }else{
  286. ctx.setFillStyle('#FFFFFF');
  287. }
  288. ctx.setTextAlign('left');
  289. ctx.fillText(app.globalData.userInfoData ? ("@"+app.globalData.userInfoData.nickName) : "", scale * 179, scale *93);
  290. //我是第XXX星探
  291. // ctx.drawImage(this.data.numBg, scale * 86, scale * 1010, scale * 563, scale * 124);
  292. // ctx.drawImage(this.data.numBg[0], scale * 86, scale * 1016, scale * 169, scale * 56);
  293. // ctx.drawImage(this.data.numBg[1], scale * (303+this.data.rankNum.length*58), scale * 1016, scale * 111, scale * 55);
  294. // ctx.drawImage(this.data.numBg[2], scale * 86, scale * 1098, scale * 307, scale * 33);
  295. // //排名数字
  296. // for(let i=0;i<this.data.rankNum.length;i++){
  297. // ctx.drawImage(this.data.numBg[3], scale * (283+i*58), scale * 1010, scale * 58, scale * 77);
  298. // ctx.drawImage(this.data.rankNum[i], scale * (285+i*59), scale * 1012, scale * 52, scale * 70);
  299. // }
  300. //二维码
  301. ctx.drawImage(this.data.posterBottom, 0, scale * 1163, scale * 750, scale * 170);
  302. ctx.drawImage(this.data.qrCodeUrl, scale * 579, scale * 1186, scale * 140, scale * 140);
  303. //绘制
  304. ctx.draw(false, setTimeout(() => {
  305. wx.canvasToTempFilePath({
  306. width:scale*750,
  307. height: scale * 1335,
  308. canvasId: 'myCanvas',
  309. success: res => {
  310. this.data.posterUrl[this.data.swiperCurrent] = res.tempFilePath;
  311. wx.hideLoading();
  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. })