index.js 4.7 KB

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