download.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // pages/download/download.ts
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. codeUrl: "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20221107134141195228914.png",
  8. nvabarData: {
  9. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  10. title: '下载', //导航栏 中间的标题
  11. },
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad() {
  17. init().then((res)=> {
  18. this.setData({
  19. codeUrl: res.appDownQrCode,
  20. })
  21. })
  22. },
  23. /**
  24. * 生命周期函数--监听页面初次渲染完成
  25. */
  26. onReady() {
  27. },
  28. /**
  29. * 生命周期函数--监听页面显示
  30. */
  31. onShow() {
  32. },
  33. /**
  34. * 生命周期函数--监听页面隐藏
  35. */
  36. onHide() {
  37. },
  38. /**
  39. * 生命周期函数--监听页面卸载
  40. */
  41. onUnload() {
  42. },
  43. /**
  44. * 页面相关事件处理函数--监听用户下拉动作
  45. */
  46. onPullDownRefresh() {
  47. },
  48. /**
  49. * 页面上拉触底事件的处理函数
  50. */
  51. onReachBottom() {
  52. },
  53. /**
  54. * 用户点击右上角分享
  55. */
  56. onShareAppMessage() {
  57. },
  58. // 保存海报
  59. saveImageToPhotosAlbum() {
  60. const _this = this;
  61. wx.showLoading({
  62. title: '保存中...'
  63. })
  64. wx.downloadFile({
  65. url: _this.data.codeUrl,
  66. success: function (res) {
  67. //图片保存到本地
  68. wx.saveImageToPhotosAlbum({
  69. filePath: res.tempFilePath,
  70. success: function (data) {
  71. wx.hideLoading()
  72. wx.showToast({
  73. title: '保存成功',
  74. icon: 'success',
  75. duration: 2000
  76. })
  77. },
  78. fail: function (err) {
  79. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  80. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  81. wx.showModal({
  82. title: '提示',
  83. content: '需要您授权保存相册',
  84. showCancel: false,
  85. success(){
  86. wx.openSetting({
  87. success(settingdata) {
  88. console.log("settingdata", settingdata)
  89. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  90. wx.showModal({
  91. title: '提示',
  92. content: '获取权限成功,再次点击图片即可保存',
  93. showCancel: false,
  94. })
  95. } else {
  96. wx.showModal({
  97. title: '提示',
  98. content: '获取权限失败,将无法保存到相册哦~',
  99. showCancel: false,
  100. })
  101. }
  102. },
  103. fail(failData) {
  104. console.log("failData",failData)
  105. },
  106. complete(finishData) {
  107. console.log("finishData", finishData)
  108. }
  109. })
  110. }
  111. })
  112. }
  113. },
  114. complete(res) {
  115. wx.hideLoading()
  116. }
  117. })
  118. }
  119. })
  120. },
  121. })