download.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. Page({
  2. data: {
  3. codeUrl: "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20221107134141195228914.png",
  4. nvabarData: {
  5. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  6. title: '下载', //导航栏 中间的标题
  7. },
  8. },
  9. onLoad() {
  10. init().then((res) => {
  11. this.setData({
  12. codeUrl: res.appDownQrCode,
  13. })
  14. })
  15. },
  16. // 保存海报
  17. saveImageToPhotosAlbum() {
  18. const _this = this;
  19. wx.showLoading({
  20. title: '保存中...'
  21. })
  22. wx.downloadFile({
  23. url: _this.data.codeUrl,
  24. success: function (res) {
  25. //图片保存到本地
  26. wx.saveImageToPhotosAlbum({
  27. filePath: res.tempFilePath,
  28. success: function (data) {
  29. wx.hideLoading()
  30. wx.showToast({
  31. title: '保存成功',
  32. icon: 'success',
  33. duration: 2000
  34. })
  35. },
  36. fail: function (err) {
  37. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  38. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  39. wx.showModal({
  40. title: '提示',
  41. content: '需要您授权保存相册',
  42. showCancel: false,
  43. success() {
  44. wx.openSetting({
  45. success(settingdata) {
  46. console.log("settingdata", settingdata)
  47. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  48. wx.showModal({
  49. title: '提示',
  50. content: '获取权限成功,再次点击图片即可保存',
  51. showCancel: false,
  52. })
  53. } else {
  54. wx.showModal({
  55. title: '提示',
  56. content: '获取权限失败,将无法保存到相册哦~',
  57. showCancel: false,
  58. })
  59. }
  60. },
  61. fail(failData) {
  62. console.log("failData", failData)
  63. },
  64. complete(finishData) {
  65. console.log("finishData", finishData)
  66. }
  67. })
  68. }
  69. })
  70. }
  71. },
  72. complete(res) {
  73. wx.hideLoading()
  74. }
  75. })
  76. }
  77. })
  78. },
  79. })