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

234 lines
6.3KB

  1. // pages/guessPoster/guessPoster.js
  2. const app = getApp()
  3. let _savePostBg = "";//用于绘制海报的背景
  4. let _totalDownloadTask = 2;//绘制需要下载的图片总数
  5. let _currentSuccessDownloadTask = 0;//已完成下载的图片数
  6. let windowScale = 0;//屏幕缩放比
  7. let canvasContron = null;//canvas
  8. let posterUrl = "";//合成后的海报路径
  9. let _saveImg = false;
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. imgUrl: app.globalData.urlStatic,//图片路径
  16. selectType:1,//选中的分享类型
  17. userPrice:0,//用户竞猜价格
  18. userName:"",//用户昵称
  19. qrCodeUrl:"",//用户二维码
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. app.globalData.nowPage = 3;
  26. this.setData({
  27. userName:app.globalData.userInfoData.nickName,
  28. userPrice:app.globalData.getSecondGuessInfo.secondData.guess_price
  29. })
  30. },
  31. loadFun:function(){
  32. this.getGuessPriceQrCode();
  33. },
  34. /**
  35. * 生命周期函数--监听页面初次渲染完成
  36. */
  37. onReady: function () {
  38. canvasContron = wx.createCanvasContext('myCanvas');
  39. if (app.globalData.openid) {
  40. this.loadFun();
  41. } else {
  42. app.globalData.openidSuccessFuc = this.loadFun;
  43. }
  44. },
  45. /**
  46. * 生命周期函数--监听页面显示
  47. */
  48. onShow: function () {
  49. _totalDownloadTask = 2;
  50. _currentSuccessDownloadTask = 0;
  51. _saveImg = false;
  52. },
  53. /**
  54. * 生命周期函数--监听页面隐藏
  55. */
  56. onHide: function () {
  57. },
  58. /**
  59. * 生命周期函数--监听页面卸载
  60. */
  61. onUnload: function () {
  62. },
  63. /**
  64. * 页面相关事件处理函数--监听用户下拉动作
  65. */
  66. onPullDownRefresh: function () {
  67. },
  68. /**
  69. * 页面上拉触底事件的处理函数
  70. */
  71. onReachBottom: function () {
  72. },
  73. /**
  74. * 用户点击右上角分享
  75. */
  76. onShareAppMessage: function () {
  77. return {
  78. title: '召唤预言帝!快来和我一起猜启辰星售价,赢升舱大奖!',
  79. imageUrl:this.data.imgUrl+'/newImages5/56.jpg',
  80. path: "/pages/guess/guess"
  81. }
  82. },
  83. changeType:function(){//切换分享类型
  84. this.setData({
  85. selectType:this.data.selectType==1?2:1
  86. })
  87. },
  88. getGuessPriceQrCode:function(){//获取竞猜分享小程序码
  89. app.wxRequest(app.globalData.urlRoot + "guessPrice/getGuessPriceQrCode", {}, res => {
  90. if (res.code == 200) {
  91. this.setData({
  92. qrCodeUrl:res.data.qrcodeUrl
  93. })
  94. this.cacheFun();
  95. }
  96. }, this);
  97. },
  98. cacheFun:function(){
  99. wx.getSystemInfo({
  100. success: option => {
  101. windowScale = option.windowWidth / 750;
  102. wx.getImageInfo({//缓存海报背景
  103. src: this.data.imgUrl + '/newImages5/66.png',
  104. success: res => {
  105. _savePostBg = res.path;
  106. _currentSuccessDownloadTask ++;
  107. this.posterDrawing();
  108. }
  109. })
  110. wx.getImageInfo({//缓存二维码
  111. src: this.data.qrCodeUrl,
  112. success: res => {
  113. this.data.qrCodeUrl = res.path;
  114. _currentSuccessDownloadTask++;
  115. this.posterDrawing();
  116. }
  117. })
  118. }
  119. })
  120. },
  121. posterDrawing: function (e) {//海报绘制
  122. //图片尚未下载完成,禁止绘制
  123. if(_currentSuccessDownloadTask < _totalDownloadTask){
  124. return;
  125. }
  126. var ctx = canvasContron;//canvas对象
  127. var scale = windowScale;//屏幕缩放比
  128. //背景
  129. ctx.drawImage(_savePostBg, 0, 0, scale * 750, scale * 1380);
  130. //绘制文字
  131. ctx.setFontSize(scale * 33);
  132. ctx.setFillStyle('#FFFFFF');
  133. ctx.setTextAlign('left');
  134. ctx.fillText(this.data.userName?'@'+this.data.userName:"@", scale * 128, scale *952);
  135. ctx.setFontSize(scale * 27);
  136. ctx.fillText("启辰星", scale * 128, scale *1006);
  137. ctx.fillText("260T48V", scale * 218, scale *1006);
  138. ctx.fillText("轻混增压星享版", scale * 336, scale *1006);
  139. ctx.fillText("我的预测上市价为:", scale * 128, scale *1045);
  140. ctx.fillText(this.data.userPrice, scale * 128, scale *1085);
  141. ctx.fillText("元", scale * 228, scale *1085);
  142. ctx.fillText("来和我一起竞猜吧!", scale * 128, scale *1125);
  143. //二维码
  144. ctx.drawImage(this.data.qrCodeUrl, scale * 574, scale * 1223, scale * 130, scale * 130);
  145. //绘制
  146. ctx.draw(false, setTimeout(() => {
  147. wx.canvasToTempFilePath({
  148. width:scale*750,
  149. height: scale * 1380,
  150. canvasId: 'myCanvas',
  151. success: res => {
  152. posterUrl = res.tempFilePath;
  153. if(_saveImg){
  154. this.savePic();
  155. }
  156. }
  157. })
  158. }, 300));
  159. },
  160. saveImg: function () {//保存到手机
  161. if (!posterUrl){
  162. wx.showLoading({
  163. title: '海报合成中',
  164. mask:true
  165. })
  166. _saveImg = true;
  167. }else{
  168. this.savePic();
  169. }
  170. },
  171. savePic: function () {
  172. wx.hideLoading();
  173. wx.saveImageToPhotosAlbum({
  174. filePath: posterUrl,
  175. success(res) {
  176. wx.showToast({
  177. title: '保存成功'
  178. })
  179. },
  180. fail: res => {
  181. wx.getSetting({
  182. success: res => {
  183. if (res.authSetting['scope.writePhotosAlbum']) {
  184. wx.showToast({
  185. title: '保存失败',
  186. icon: "none"
  187. })
  188. } else {
  189. wx.showModal({
  190. title: '授权设置',
  191. content: '请授权“保存到相册”',
  192. success: (opt) => {
  193. if (opt.confirm) {
  194. wx.openSetting({
  195. success: e => {
  196. if (e.authSetting['scope.writePhotosAlbum']) {
  197. wx.saveImageToPhotosAlbum({
  198. filePath: posterUrl,
  199. success(res) {
  200. wx.showToast({
  201. title: '保存成功'
  202. })
  203. }
  204. })
  205. } else {
  206. wx.showToast({
  207. title: '授权失败',
  208. icon: "none"
  209. })
  210. }
  211. }
  212. })
  213. }
  214. }
  215. })
  216. }
  217. }
  218. })
  219. }
  220. })
  221. }
  222. })