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

298 lines
9.1KB

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