saveImg: function () { var that = this; if ((that.data.codePic == '') || (that.data.codePic == null)) { app.basic_dialog('图片加载失败'); return false; } wx.getSetting({ success: function(res) { if (res.authSetting["scope.writePhotosAlbum"] == false) { app.confirm_dialog('受权提醒', '您还未受权保存相册,请受权', function(res) { if (res.confirm == true) { wx.openSetting({ success: function(res) { if (res.authSetting["scope.writePhotosAlbum"] == true) { // 保存相册 that.saveImg(); } else { app.toast_none('受权失败'); that.setData({ chat: false }); } } }); } }) } else { // 下载这个图片 wx.downloadFile({ url: that.data.codePic, success: function (res) { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function (response) { if (response.errMsg == "saveImageToPhotosAlbum:ok") { app.toast_none('下载成功', function () { that.setData({ chat: false }); }); } } }); }, fail: function() { app.toast_none('下载资源失败'); } }); } } }); },
第二版优化, 解决第一次点击,反应时间比较长的问题javascript
var that = this; if ((that.data.codePic == '') || (that.data.codePic == null)) { app.basic_dialog('图片加载失败'); return false; } if (that.data.loading) { return false; } that.data.loading = true; that.setData({ loading: that.data.loading }); // 判断用户是否受权 wx.getSetting({ success: function(res) { if (res.authSetting["scope.writePhotosAlbum"] == false) { app.confirm_dialog('受权提醒', '您还未受权保存相册,请受权', function(res) { if (res.confirm == true) { wx.openSetting({ success: function(res) { if (res.authSetting["scope.writePhotosAlbum"] == true) { // 保存相册 that.saveImg(); } else { app.toast_none('受权失败'); that.setData({ chat: false }); } } }); } that.data.loading = false; that.setData({ loading: that.data.loading }); }) } else { // 下载这个图片 wx.downloadFile({ url: that.data.codePic, success: function (res) { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function (response) { if (response.errMsg == "saveImageToPhotosAlbum:ok") { app.toast_none('下载成功', function () { that.setData({ chat: false }); }); } } }); that.data.loading = false; that.setData({ loading: that.data.loading }); }, fail: function() { app.toast_none('下载资源失败'); that.data.loading = false; that.setData({ loading: that.data.loading }); } }); } } });