whatsns社区
whatsns社区
  • 发布:2019-12-26 12:24
  • 更新:2021-03-09 15:32
  • 阅读:5792

授权弹出误点或者测试点击拒绝授权后无法在弹出授权窗口auth deny终极解决方案

分类:uni-app

先贴代码
视图层部分

<template>  
    <view>  
        <button @tap="startRecord">开始录音</button>  
        <button @tap="endRecord">停止录音</button>  
        <button @tap="playVoice">播放录音</button>  
        <button @tap="uploadvoice">上传文件</button>  
    </view>  
</template>

js部分

<script>  
    let recorderManager = uni.getRecorderManager();  
    let innerAudioContext = uni.createInnerAudioContext();  

    innerAudioContext.autoplay = true;  

    export default {  
        data: {  
            text: 'uni-app',  
            voicePath: ''  
        },  
        onLoad() {  

        },  
        methods: {  
            uploadvoice() {  
                var target = this  
                let haslogin = false  
                let accesstoken = ''  
                try {  
                    accesstoken = uni.getStorageSync('accesstoken');  
                    if (accesstoken) {  

                        haslogin = true  
                    } else {  
                        uni.showToast({  
                            title: '你还没有登录',  
                            icon: 'none'  
                        })  
                        return  
                    }  

                } catch (e) {  
                    // error  
                }  
                if (target.voicePath == '') {  
                    uni.showToast({  
                        title: '你还没录音',  
                        icon: 'none'  
                    })  
                    return  
                }  
                //上传音频文件  
                uni.uploadFile({  
                    url: target.$api.UploadvoiceFile, //接口地址  
                    filePath: target.voicePath, //临时音频文件  
                    name: 'appfile',  
                    formData: {  
                        'accesstoken': accesstoken,  
                        'viewfee': 5 //付费金额  
                    },  
                    success: (uploadFileRes) => {  
                        console.log(uploadFileRes.data);  
                    }  
                });  
            },  
            startRecord() {  
                //录音先判断是否有录音权限  
                uni.getSetting({  
                    success(res) {  
                        //获取设置成功  
                        console.log(res.authSetting)  
                        if (!res.authSetting['scope.record']) {  
                            //如果没开启录音麦克风权限提示打开  
                            uni.openSetting({  
                                success(res) {  
                                    //打开成功,提示获取录音权限  
                                    console.log(res.authSetting)  
                                    uni.authorize({  
                                        scope: 'scope.record',  
                                        success() {  
                                            //录音权限获取成功开始录音  
                                            console.log('开始录音2');  

                                            recorderManager.start({  
                                                format: 'mp3',  
                                                duration: 600000  
                                            });  
                                        },  
                                        complete() {  
                                            console.log('调用接口完成');  
                                        },  
                                        fail(res) {  
                                            uni.showToast({  
                                                title: '您拒绝了应用获取麦克风权限',  
                                                icon: 'none'  
                                            })  
                                            console.log(res);  
                                        }  
                                    })  
                                }  
                            });  
                        } else {  
                            recorderManager.start({  
                                format: 'mp3',  
                                duration: 600000  
                            });  
                        }  
                    }  
                })  
            },  
            endRecord() {  
                //默认官方把此onStop方法写onload里,但是如果被拒绝的状态后续录音成功也会不执行停止回调,onload只在加载状态执行一次  
                //保险起见,用户后续开启麦克风权限还能正常回调写停止方法里  
                let self = this;  
                recorderManager.onStop(function(res) {  
                    console.log('recorder stop' + JSON.stringify(res));  
                    self.voicePath = res.tempFilePath;  
                });  
                console.log('录音结束');  
                recorderManager.stop();  

            },  
            playVoice() {  
                console.log('播放录音');  

                if (this.voicePath) {  
                    innerAudioContext.src = this.voicePath;  
                    innerAudioContext.play();  
                }  
            }  
        }  
    }  
</script>

音频文件设置了mp3格式,默认10分钟
上面demo是根据官方录音功能改进的,主要是小程序端,startRecord()录音方法里写了如何解决不弹出授权问题,demo可以直接使用测试,包含上传音频文件方法

php接收音频文件
function uploavoice() {
$message = array ();
$accesstoken = $_POST ['accesstoken'];

    $user = $this->check_token ( $accesstoken );  
    if ($user) {  
        if (! empty ( $_FILES ['appfile'] )) {  
            // 获取扩展名  
            $pathinfo = pathinfo ( $_FILES ['appfile'] ['name'] );  

            $exename = strtolower ( $pathinfo ['extension'] );  
            if ($exename != 'mp3' ) {  
                $message ['code'] = 2001;  
                $message ['data'] = null;  
                $message ['message'] = "音频扩展不支持";  
                echo json_encode ( $message );  
                exit ();  
            }  
            $imageSavePath = 'data/weixinrecord/' . uniqid () . '.' . $exename;  
            if (move_uploaded_file ( $_FILES ['appfile'] ['tmp_name'], FCPATH . $imageSavePath )) {  
                $message ['code'] = 2000;  
                $message ['data'] = SITE_URL . $imageSavePath;  
                $message ['message'] = "音频上传成功";  
                echo json_encode ( $message );  
                exit ();  
            }  
        } else {  
            $message ['code'] = 2002;  
            $message ['data'] = null;  
            $message ['message'] = "请上传音频文件";  
            echo json_encode ( $message );  
            exit ();  
        }  
    } else {  
        $message ['code'] = 2088;  
        $message ['data'] = null;  
        $message ['message'] = "用户信息过期";  
        echo json_encode ( $message );  
        exit ();  
    }  
}  
2 关注 分享
gaus 1***@163.com

要回复文章请先登录注册

水手

水手

回复 8***@qq.com :
是的。 openSetting 不行了
2021-03-09 15:32
8***@qq.com

8***@qq.com

用户点拒绝授权后 uni.openSetting就不生效了
2021-01-26 10:53