wx.checkIsAddedToMyMiniProgram({
success: (res) => {
console.log('res',res)
if(!res.added){
currentPage.setData({
showNotice:true
})
setTimeout(()=>{
currentPage.setData({
showNotice:false
})
},7000)
}
},
fail:(error)=>{
console.log('error',error)
}
wx.checkIsAddedToMyMiniProgram 查看该小程序是否已经加入我的小程序、success表示查看成功,res.added的布尔值表示是否加入我的小程序
微信小程序的APP与页面的生成速度也不同,在具体使用的时候可以通过打印控制台了解先后生成的顺序。必要的话可以使用settimeout来进行延时
getcurrentpages() 获取当前页面栈,数组中第一个元素为首页,最后一个元素为当前页面
wx.downloadFile 只支持保存本地路径
wx.downloadFile({
url: this.data.codeImg, //仅为示例,并非真实的资源
success: (res) => {
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
if (res.statusCode === 200) {
wx.createInnerAudioContext({
filePath: res.tempFilePath
})
this.setData({
shareImg: res.tempFilePath
})
console.log('res', res.tempFilePath)
//判断用户确认权限结果
wx.getSetting({
success: (res) => {
console.log(res.authSetting)
if (res.authSetting['scope.writePhotosAlbum'] === true) {
console.log('用户请求权限成功', 'res', res.tempFilePath, this.data.shareImg)
this.getimage()
} else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: (res) => {
wx.saveImageToPhotosAlbum({
filePath: this.data.shareImg,
success: function (t) {
wx.showToast({
title: "保存成功",
icon: "none"
});
},
fail: function (t) {
wx.showToast({
title: "保存失败,请重试",
icon: "none"
});
console.log('filePath', t)
}
})
},
fail() {
wx.showToast({
title: '您没有授权,无法保存到相册',
icon: 'none'
})
}
})
} else {
wx.openSetting({
success:(res)=>{
if (res.authSetting['scope.writePhotosAlbum']) {
this.getimage()
} else {
wx.showToast({
title: '您没有授权,无法保存到相册',
icon: 'none'
})
}
}
})
}
}
})
}
}
})
可以先用wx.createInnerAudioContext 将线上图片地址转换为临时路径
wx.authorize 在调用保存图片等权限时,只能调起弹窗一次,也就是说如果用户拒绝了,那就默认他无法再进行保存图片等操作、因此我们在点击事件时需要判断用户的权限状态。
使用 wx.getSetting 判断用户是否给权限了,
1.给权限了(调起权限弹窗), 同意了,直接进行后续操作
2.给权限了(调起权限弹窗),没同意,但是需要再次打开权限,使用wx.openSetting
3.没给权限(没调起权限弹窗) res.authSetting['scope.writePhotosAlbum'] === undefined 需要调起权限 wx.authorize