东风启辰小程序端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

guessPoster.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.drawImage(this.data.qrCodeUrl, scale * 574, scale * 1223, scale * 130, scale * 130);
  132. //绘制
  133. ctx.draw(false, setTimeout(() => {
  134. wx.canvasToTempFilePath({
  135. width:scale*750,
  136. height: scale * 1380,
  137. canvasId: 'myCanvas',
  138. success: res => {
  139. posterUrl = res.tempFilePath;
  140. if(_saveImg){
  141. this.savePic();
  142. }
  143. }
  144. })
  145. }, 300));
  146. },
  147. saveImg: function () {//保存到手机
  148. if (!posterUrl){
  149. wx.showLoading({
  150. title: '海报合成中',
  151. mask:true
  152. })
  153. _saveImg = true;
  154. }else{
  155. this.savePic();
  156. }
  157. },
  158. savePic: function () {
  159. wx.hideLoading();
  160. wx.saveImageToPhotosAlbum({
  161. filePath: posterUrl,
  162. success(res) {
  163. wx.showToast({
  164. title: '保存成功'
  165. })
  166. },
  167. fail: res => {
  168. wx.getSetting({
  169. success: res => {
  170. if (res.authSetting['scope.writePhotosAlbum']) {
  171. wx.showToast({
  172. title: '保存失败',
  173. icon: "none"
  174. })
  175. } else {
  176. wx.showModal({
  177. title: '授权设置',
  178. content: '请授权“保存到相册”',
  179. success: (opt) => {
  180. if (opt.confirm) {
  181. wx.openSetting({
  182. success: e => {
  183. if (e.authSetting['scope.writePhotosAlbum']) {
  184. wx.saveImageToPhotosAlbum({
  185. filePath: posterUrl,
  186. success(res) {
  187. wx.showToast({
  188. title: '保存成功'
  189. })
  190. }
  191. })
  192. } else {
  193. wx.showToast({
  194. title: '授权失败',
  195. icon: "none"
  196. })
  197. }
  198. }
  199. })
  200. }
  201. }
  202. })
  203. }
  204. }
  205. })
  206. }
  207. })
  208. }
  209. })