index.js 4.7 KB

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