index.js 4.8 KB

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