index.js 5.1 KB

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