步骤:
获取用户当前授权状态,看是否已经授权,如果已经授权直接显示摄像头
如果用户还没有授权,则调起授权弹框,用户允许授权则显示摄像头
如果用户不允许,则提示用户去设置页面打开摄像头权限
// getSetting 获取用户的当前设置
wx.getSetting({
success: res => {
console.log('res.authSetting',res.authSetting);
// scope.camera 是否拥有摄像头权限
if (res.authSetting['scope.camera']) {
// 用户已经授权
_this.setData({
isAuth: true
})
} else {
// 用户还没有授权,向用户发起授权请求
wx.authorize({
// scope.camera 获取摄像头权限
scope: 'scope.camera',
success() { // 用户同意授权
_this.setData({
isAuth: true
})
},
fail() { // 用户不同意授权
// 调起客户端小程序设置界面,返回用户设置的操作结果
_this.openSetting().then(res => {
_this.setData({
isAuth: true
})
})
}
})
}
},
fail: res => {
console.log('获取用户授权信息失败')
}
})
// 打开授权设置界面
openSetting() {
const _this = this
let promise = new Promise((resolve, reject) => {
wx.showModal({
title: '授权',
content: '请先授权获取摄像头权限',
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
if (res.authSetting['scope.camera']) { // 用户打开了授权开关
resolve(true)
} else { // 用户没有打开授权开关, 继续打开设置页面
_this.openSetting().then(res => {
resolve(true)
})
}
},
fail(res) {
console.log(res)
}
})
} else if (res.cancel) {
_this.openSetting().then(res => {
resolve(true)
})
}
}
})
})
return promise;
}
打开相册或拍照逻辑
getlocalpicture() {
let that = this;
// 拍摄或从手机相册中选择图片或视频
wx.chooseMedia({
// 最多可以选择的文件个数
count: 1,
// 是否压缩所选文件
sizeType: ['original', 'compressed'],
// 图片和视频选择的来源 album(从相册选择) camera(使用相机拍摄)
sourceType: ['camera'],
success: function(res) {
// 选择图片后的完成确认操作
console.log('res',res);
// that.setData({
// aimgurl: res.tempFilePaths
// });
// console.log(that.data.aimgurl);
}
})
}