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

poster.js 9.5KB

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