小程序获取手机相机权限


使用到的方法:

  1.uni.getSetting

  2.uni.authorize

  3.uni.showActionSheet

  4.uni.showModal

  5.uni.openSetting

uni.getSetting({
                    success: (res) => {
                        let authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
                        // 如果没有授权相册
                        if(!authStatus){
                            //向用户发起授权请求
                            uni.authorize({
                                scope: "scope.camera",
                                success: () => {
                                    uni.showActionSheet({
                                        title: '选择上传类型',
                                        itemList: ['图片', '视频'],
                                        success: res => {
                                            console.log(res);
                                            if (res.tapIndex == 0) {
                                                this.chooseImages();
                                            } else {
                                                this.chooseVideo();
                                            }
                                        }
                                    });
                                },
                                fail: () => {
                                    uni.showModal({
                                        title: '授权失败',
                                        content: '需要从您的相机或相册获取图片,请在设置界面打开相关权限',
                                        success: (res) => {
                                            if (res.confirm) {
                                                uni.openSetting()
                                            }
                                        }
                                    })
                                }
                            })
                        }else{
                            uni.showActionSheet({
                                title: '选择上传类型',
                                itemList: ['图片', '视频'],
                                success: res => {
                                    console.log(res);
                                    if (res.tapIndex == 0) {
                                        this.chooseImages();
                                    } else {
                                        this.chooseVideo();
                                    }
                                }
                            });
                        }
                    }
                })