东风启辰小程序端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

325 行
9.8KB

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