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