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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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:"https://www.jiyou-tech.com/2020/496_qichen/static/images/testQrCode.png",//二维码图片
  12. userHead: app.globalData.userInfoData ? app.globalData.userInfoData.avatarUrl : null,//用户头像
  13. rankNum: '8908',//缓存数字
  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',
  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',
  71. success: res => {
  72. this.data.numBg = res.path;
  73. countNum += 1;
  74. }
  75. })
  76. this.data.rankNum = this.data.rankNum.toString().split("");
  77. for (let i = 0; i < this.data.rankNum.length; i++) {
  78. wx.getImageInfo({//缓存数字
  79. src: this.data.imgUrl + '/images/nums/' + this.data.rankNum[i] + '.png',
  80. success: res => {
  81. this.data.rankNum[i] = res.path;
  82. countNum += 1;
  83. }
  84. })
  85. }
  86. let setInt = setInterval(() => {
  87. if (countNum >= 8) {
  88. this.posterDrawing(1);
  89. clearInterval(setInt);
  90. }
  91. }, 50);
  92. }
  93. })
  94. },
  95. /**
  96. * 生命周期函数--监听页面显示
  97. */
  98. onShow: function () {
  99. },
  100. /**
  101. * 生命周期函数--监听页面隐藏
  102. */
  103. onHide: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面卸载
  107. */
  108. onUnload: function () {
  109. },
  110. /**
  111. * 页面相关事件处理函数--监听用户下拉动作
  112. */
  113. onPullDownRefresh: function () {
  114. },
  115. /**
  116. * 页面上拉触底事件的处理函数
  117. */
  118. onReachBottom: function () {
  119. },
  120. /**
  121. * 用户点击右上角分享
  122. */
  123. onShareAppMessage: function () {
  124. return {
  125. title: '您有一份启辰星专属礼品待领取',
  126. imageUrl: this.data.imgUrl + "/images/posterShareImg" + (this.data.swiperCurrent+1)+".jpg",
  127. path: "/pages/coupon/coupon?friendOpenid=" + app.globalData.openid
  128. }
  129. },
  130. saveImg:function(){//保存到手机
  131. wx.saveImageToPhotosAlbum({
  132. filePath: this.data.posterUrl[this.data.swiperCurrent],
  133. success(res) {
  134. wx.showToast({
  135. title: '保存成功'
  136. })
  137. },
  138. fail:res=>{
  139. wx.getSetting({
  140. success: res => {
  141. if (res.authSetting['scope.writePhotosAlbum']) {
  142. wx.showToast({
  143. title: '保存失败',
  144. icon:"none"
  145. })
  146. } else {
  147. wx.showModal({
  148. title: '授权设置',
  149. content: '请授权“保存到相册”',
  150. success: (opt) => {
  151. if (opt.confirm) {
  152. wx.openSetting({
  153. success:e=>{
  154. if (e.authSetting['scope.writePhotosAlbum']) {
  155. wx.saveImageToPhotosAlbum({
  156. filePath: this.data.posterUrl[this.data.swiperCurrent],
  157. success(res) {
  158. wx.showToast({
  159. title: '保存成功'
  160. })
  161. }
  162. })
  163. } else {
  164. wx.showToast({
  165. title: '授权失败',
  166. icon: "none"
  167. })
  168. }
  169. }
  170. })
  171. }
  172. }
  173. })
  174. }
  175. }
  176. })
  177. }
  178. })
  179. },
  180. posterDrawing: function (e) {//海报绘制
  181. var ctx = this.data.canvasContron;//canvas对象
  182. var scale = this.data.windowScale;//屏幕缩放比
  183. //背景
  184. ctx.drawImage(this.data.posterBg[e-1], 0, 0, scale * 444, scale * 817);//
  185. ctx.setFillStyle("#FFFFFF");
  186. ctx.save();
  187. ctx.beginPath(); //开始绘制
  188. //先画个圆 前两个参数确定了圆心 (x,y) 坐标 第三个参数是圆的半径 四参数是绘图方向 默认是false,即顺时针
  189. ctx.arc(scale * 48 / 2 + scale * 49, scale * 48 / 2 + scale * 538, scale * 48 / 2, 0, Math.PI * 2, false);
  190. ctx.clip(); //剪切
  191. ctx.drawImage(this.data.userHead, scale * 49, scale * 538, scale * 48, scale * 48); //头像
  192. ctx.restore(); //恢复之前保存的绘图上下文
  193. //名称
  194. ctx.setFontSize(scale * 28);
  195. ctx.setFillStyle('#FFFFFF');
  196. ctx.setTextAlign('left');
  197. ctx.fillText(app.globalData.userInfoData ? app.globalData.userInfoData.nickName : "", scale * 102, scale *572);
  198. //我是第XXX星探
  199. ctx.drawImage(this.data.numBg, scale * 50, scale * 598, scale * 333, scale * 74);
  200. //排名数字
  201. ctx.drawImage(this.data.rankNum[0], scale * 168, scale * 602, scale * 32, scale * 40);
  202. ctx.drawImage(this.data.rankNum[1], scale * 202, scale * 602, scale * 32, scale * 40);
  203. ctx.drawImage(this.data.rankNum[2], scale * 236, scale * 602, scale * 32, scale * 40);
  204. ctx.drawImage(this.data.rankNum[3], scale * 270, scale * 602, scale * 32, scale * 40);
  205. //二维码
  206. ctx.drawImage(this.data.qrCodeUrl, scale * 282, scale * 672, scale * 105, scale* 106);
  207. //绘制
  208. ctx.draw(false, setTimeout(() => {
  209. wx.canvasToTempFilePath({
  210. width:scale*444,
  211. height: scale * 817,
  212. canvasId: 'myCanvas',
  213. success: res => {
  214. this.data.posterUrl.push(res.tempFilePath);
  215. this.setData({
  216. posterUrl: this.data.posterUrl
  217. })
  218. if (e < 3) {
  219. wx.getImageInfo({
  220. src: this.data.imgUrl + '/images/tempImg'+(e+1)+'.jpg',
  221. success: res => {
  222. this.data.posterBg.push(res.path);
  223. this.posterDrawing(e + 1);
  224. }
  225. })
  226. }else{
  227. this.setData({
  228. canvasShow:false
  229. })
  230. }
  231. }
  232. })
  233. }, 300));
  234. },
  235. savePoster:function(){//预览海报
  236. wx.previewImage({
  237. current: this.data.posterUrl[this.data.swiperCurrent],
  238. urls: this.data.posterUrl // 需要预览的图片http链接列表
  239. })
  240. },
  241. prevImg() {//上一张图片
  242. if (this.data.swiperCurrent > 0) {
  243. this.setData({
  244. swiperCurrent: this.data.swiperCurrent -= 1,
  245. })
  246. }
  247. },
  248. nextImg() {//下一张图片
  249. if (this.data.swiperCurrent < this.data.posterUrl.length - 1) {
  250. this.setData({
  251. swiperCurrent: this.data.swiperCurrent += 1,
  252. })
  253. }
  254. },
  255. swiperChange(e) {//通过鼠标滑动改变swiper时
  256. if (e.detail.source == "touch") {
  257. this.setData({
  258. swiperCurrent: e.detail.current,
  259. })
  260. }
  261. },
  262. getCertificationCount: function () {//获取星探计划人数
  263. app.wxRequest(app.globalData.urlRoot +"userInfo/getCertificationCount",{},res=>{
  264. this.createQrcode();
  265. if(res.code==200){
  266. this.data.rankNum = res.data.total;
  267. }
  268. },this)
  269. },
  270. createQrcode: function () {//获取个人小程序分享码
  271. app.wxRequest(app.globalData.urlRoot + "wxInfo/getQrcode", {}, res => {
  272. if (res.code = 200) {
  273. this.data.qrCodeUrl = res.data.qrcodeUrl;
  274. }
  275. this.cacheFun();
  276. }, this)
  277. }
  278. })