index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // pages/downZS/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. ls_url: ""
  8. },
  9. systemInfo: wx.getSystemInfoSync(),
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad(options) {
  14. // 获取远程图片
  15. wx.showLoading({
  16. title: '加载中',
  17. mask: true
  18. })
  19. wx.getImageInfo({
  20. src: "https://cxzx.smcic.net/topic/tool/img/%E5%B0%91%E5%84%BF%E4%B9%A6%E7%94%BB%E5%A4%A7%E8%B5%9B/6.jpg",
  21. }).then((res) => {
  22. const {
  23. width,
  24. height,
  25. path
  26. } = res;
  27. const canvas = wx.createOffscreenCanvas({
  28. type: '2d',
  29. width,
  30. height
  31. });
  32. const ctx = canvas.getContext('2d');
  33. const img = canvas.createImage();
  34. img.onload = () => {
  35. ctx.drawImage(img, 0, 0, width, height);
  36. ctx.fillStyle = "#91725D";
  37. ctx.font = (width / 20) + 'px 微软雅黑';
  38. ctx.fillText("李勇利", width * 0.33, height * 0.4);
  39. wx.canvasToTempFilePath({
  40. x: 0,
  41. y: 0,
  42. width,
  43. height,
  44. destWidth: width,
  45. destHeight: height,
  46. canvas,
  47. fileType: 'jpg',
  48. success: res => {
  49. this.setData({
  50. ls_url: res.tempFilePath,
  51. width,
  52. height
  53. });
  54. },
  55. complete: () => {
  56. wx.hideLoading();
  57. }
  58. })
  59. }
  60. img.onerror = () => wx.hideLoading();
  61. img.src = path // 要加载的图片 url
  62. }).catch(err=>{
  63. wx.hideLoading();
  64. });
  65. },
  66. download() {
  67. if (!this.data.ls_url) return
  68. wx.saveImageToPhotosAlbum({
  69. filePath: this.data.ls_url, // 使用下载得到的临时文件路径
  70. success: function (saveRes) {
  71. wx.showToast({
  72. title: '已保存至相册',
  73. icon: 'success',
  74. duration: 2000
  75. });
  76. },
  77. fail: function (err) {
  78. if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied') {
  79. wx.showModal({
  80. title: '保存失败',
  81. content: '请前往设置页面授权保存图片到相册',
  82. showCancel: false,
  83. success: function (modalRes) {
  84. wx.openSetting({
  85. withSubscriptions: true,
  86. success: function (res) {
  87. if (res.authSetting['scope.writePhotosAlbum']) {
  88. // 用户重新授权后,再次尝试保存图片
  89. wx.saveImageToPhotosAlbum({
  90. filePath: res.tempFilePath,
  91. success: function (saveRes) {
  92. wx.showToast({
  93. title: '保存成功',
  94. icon: 'success',
  95. duration: 2000
  96. });
  97. },
  98. fail: function (err) {
  99. wx.showToast({
  100. title: '保存失败',
  101. icon: 'none',
  102. duration: 2000
  103. });
  104. }
  105. });
  106. }
  107. },
  108. fail: function (err) {
  109. wx.showToast({
  110. title: '打开设置页面失败',
  111. icon: 'none',
  112. duration: 2000
  113. });
  114. }
  115. });
  116. }
  117. });
  118. } else {
  119. wx.showToast({
  120. title: '保存失败',
  121. icon: 'none',
  122. duration: 2000
  123. });
  124. }
  125. }
  126. });
  127. },
  128. /**
  129. * 生命周期函数--监听页面初次渲染完成
  130. */
  131. onReady() {
  132. },
  133. /**
  134. * 生命周期函数--监听页面显示
  135. */
  136. onShow() {
  137. },
  138. /**
  139. * 生命周期函数--监听页面隐藏
  140. */
  141. onHide() {
  142. },
  143. /**
  144. * 生命周期函数--监听页面卸载
  145. */
  146. onUnload() {
  147. },
  148. /**
  149. * 页面相关事件处理函数--监听用户下拉动作
  150. */
  151. onPullDownRefresh() {
  152. },
  153. /**
  154. * 页面上拉触底事件的处理函数
  155. */
  156. onReachBottom() {
  157. },
  158. /**
  159. * 用户点击右上角分享
  160. */
  161. onShareAppMessage() {
  162. }
  163. })