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