152 lines
3.2 KiB
JavaScript
152 lines
3.2 KiB
JavaScript
import util from '../../../utils/util';
|
|
import apiArr from '../../../api/water_filter';
|
|
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
imagePath: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com/test_qrcode.jpg',
|
|
},
|
|
|
|
init() {
|
|
let that = this
|
|
util.postUrl(apiArr.createQrcode, {
|
|
page: '/page/index/index',
|
|
scene: `device=${wx.getStorageSync('device_id')}`
|
|
}, res => {
|
|
let img = util.img_url + res.data;
|
|
console.log('img', img)
|
|
that.setData({
|
|
imagePath: img
|
|
})
|
|
})
|
|
},
|
|
|
|
headerQrcodeClick() {
|
|
console.log('2');
|
|
const that = this;
|
|
wx.downloadFile({
|
|
url: that.data.imagePath, // 网络图片地址
|
|
success(res) {
|
|
console.log('11312312313', 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() {
|
|
console.log('222');
|
|
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) {
|
|
// this.init();
|
|
},
|
|
|
|
|
|
/**
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
onReady() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面隐藏
|
|
*/
|
|
onHide() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面上拉触底事件的处理函数
|
|
*/
|
|
onReachBottom() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage() {
|
|
|
|
}
|
|
}) |