let util = require('../../../../utils/util') let apiArr = require('../../../../api/water_filter') // packages/WaterPurifier/pages/shareDerive/shareDerive.js Page({ /** * 页面的初始数据 */ data: { imagePath:"https://wechat-img-file.oss-cn-beijing.aliyuncs.com/test_qrcode.jpg", }, getImg(){ let that = this util.postUrl(apiArr.createQrcode,{ // page:'packages/WaterPurifier/pages/shareWaterFilter/shareWaterFilter', page:'/page/index/index', scene:`device=${wx.getStorageSync('device_id')}` },res=>{ let img = util.img_url + res.data that.setData({ imagePath:img }) return wx.downloadFile({ url: img, // 网络图片地址 success(res) { if (res.statusCode === 200) { const tempFilePath = res.tempFilePath; // 获取临时文件路径 console.log(tempFilePath); that.setData({ imagePath:tempFilePath }) } }, fail() { wx.showToast({ title: '图片下载失败', icon: 'none' }); } }); }) }, saveImg(){ const that = this; wx.downloadFile({ url: that.data.imagePath, // 网络图片地址 success(res) { if (res.statusCode === 200) { const tempFilePath = res.tempFilePath; // 获取临时文件路径 // 2. 检查用户授权 wx.getSetting({ success(settingRes) { if (!settingRes.authSetting['scope.writePhotosAlbum']) { // 未授权,请求授权 wx.authorize({ scope: 'scope.writePhotosAlbum', success() { // 授权成功,保存图片 that.saveImageToAlbum(tempFilePath); }, fail() { // 授权失败,提示用户手动打开授权 wx.showModal({ title: '提示', content: '需要授权保存图片到相册,请手动打开授权设置', success(modalRes) { if (modalRes.confirm) { wx.openSetting(); // 打开授权设置页面 } } }); } }); } else { // 已授权,保存图片 that.saveImageToAlbum(tempFilePath); } } }); } }, fail() { wx.showToast({ title: '图片下载失败', icon: 'none' }); } }); }, saveImageToAlbum(tempFilePath) { wx.saveImageToPhotosAlbum({ filePath: tempFilePath, success() { wx.showToast({ title: '保存成功', icon: 'success' }); }, fail() { wx.showToast({ title: '保存失败', icon: 'none' }); } }); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let that = this // that.getImg() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })