index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.getImageInfo({
  16. 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",
  17. }).then((res) => {
  18. const {
  19. width,
  20. height,
  21. path
  22. } = res;
  23. const canvas = wx.createOffscreenCanvas({
  24. type: '2d',
  25. width,
  26. height
  27. });
  28. const ctx = canvas.getContext('2d');
  29. const img = canvas.createImage();
  30. img.onload = () => {
  31. ctx.drawImage(img, 0, 0, width, height);
  32. ctx.fillStyle = "#91725D";
  33. ctx.font = (width / 20) + 'px 微软雅黑';
  34. ctx.fillText("李勇利", width * 0.33, height * 0.4);
  35. wx.canvasToTempFilePath({
  36. x: 0,
  37. y: 0,
  38. width,
  39. height,
  40. destWidth: width,
  41. destHeight: height,
  42. canvas,
  43. fileType: 'jpg',
  44. success: res => {
  45. this.setData({
  46. ls_url: res.tempFilePath,
  47. width,
  48. height
  49. });
  50. }
  51. })
  52. }
  53. img.src = path // 要加载的图片 url
  54. });
  55. },
  56. download() {
  57. if (!this.data.ls_url) return
  58. wx.downloadFile({
  59. url: this.data.ls_url, // 这里替换为你的图片链接
  60. success: function (res) {
  61. if (res.statusCode === 200) {
  62. wx.saveImageToPhotosAlbum({
  63. filePath: res.tempFilePath, // 使用下载得到的临时文件路径
  64. success: function (saveRes) {
  65. wx.showToast({
  66. title: '保存成功',
  67. icon: 'success',
  68. duration: 2000
  69. });
  70. },
  71. fail: function (err) {
  72. if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied') {
  73. wx.showModal({
  74. title: '保存失败',
  75. content: '请前往设置页面授权保存图片到相册',
  76. showCancel: false,
  77. success: function (modalRes) {
  78. wx.openSetting({
  79. withSubscriptions: true,
  80. success: function (res) {
  81. if (res.authSetting['scope.writePhotosAlbum']) {
  82. // 用户重新授权后,再次尝试保存图片
  83. wx.saveImageToPhotosAlbum({
  84. filePath: res.tempFilePath,
  85. success: function (saveRes) {
  86. wx.showToast({
  87. title: '保存成功',
  88. icon: 'success',
  89. duration: 2000
  90. });
  91. },
  92. fail: function (err) {
  93. wx.showToast({
  94. title: '保存失败',
  95. icon: 'none',
  96. duration: 2000
  97. });
  98. }
  99. });
  100. }
  101. },
  102. fail: function (err) {
  103. wx.showToast({
  104. title: '打开设置页面失败',
  105. icon: 'none',
  106. duration: 2000
  107. });
  108. }
  109. });
  110. }
  111. });
  112. } else {
  113. wx.showToast({
  114. title: '保存失败',
  115. icon: 'none',
  116. duration: 2000
  117. });
  118. }
  119. }
  120. });
  121. } else {
  122. wx.showToast({
  123. title: '下载失败',
  124. icon: 'none',
  125. duration: 2000
  126. });
  127. }
  128. },
  129. fail: function (err) {
  130. wx.showToast({
  131. title: '下载失败',
  132. icon: 'none',
  133. duration: 2000
  134. });
  135. }
  136. });
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady() {
  142. },
  143. /**
  144. * 生命周期函数--监听页面显示
  145. */
  146. onShow() {
  147. },
  148. /**
  149. * 生命周期函数--监听页面隐藏
  150. */
  151. onHide() {
  152. },
  153. /**
  154. * 生命周期函数--监听页面卸载
  155. */
  156. onUnload() {
  157. },
  158. /**
  159. * 页面相关事件处理函数--监听用户下拉动作
  160. */
  161. onPullDownRefresh() {
  162. },
  163. /**
  164. * 页面上拉触底事件的处理函数
  165. */
  166. onReachBottom() {
  167. },
  168. /**
  169. * 用户点击右上角分享
  170. */
  171. onShareAppMessage() {
  172. }
  173. })