小程序重构,采用 uniapp 框架。记录一下踩过的坑。关于用户拒绝再次调起授权,及如何识别语音识别、微信地址、附近地址的处理。

语音识别 组件

  • 语音识别,小程序只有录音功能,若要识别录音文件,常规做法是把录音文件传递给后端,然后由后端调用百度或讯飞语音识别接口,然后返回结果。
  • 但是微信小程序官方提供了“同声传译”插件,支持前端直接识别。可参考:插件介绍插件使用文档
  • uniapp 插件配置,在 manifest.json 文件中,源码模式,加入:
...
"mp-weixin": {
...
"plugins" : {
// 语音识别 - 同声传译
"WechatSI" : {
"version" : "0.3.1",
"provider" : "wx069ba97219f66d99"
}
}
}
  • 调用
<template>
<view @click="asrStart">语音识别</view> <view>{{arsRes}}</view> <!-- 语音识别 -->
<wechat-asr ref="weixinAsr" @callback="asrResult"/>
</template> <script>
import WechatAsr from '@/components/wechatASR.vue'; export default {
components: {WechatAsr},
data () {
return {
arsRes: ''
}
},
methods: {
// 语音识别
asrStart () {
this.$refs.weixinAsr.show();
},
asrResult (res) {
this.arsRes = res;
}
}
}
</script>
  • 编写组件,其中关于授权,用户若拒绝了,再次点击,本来是会一直进入失败的回调中,导致没法再次打开授权界面。所以先获取授权信息,针对没有授权、授权拒绝、授权成功,分别再次处理。若授权拒绝,需要打开授权设置界面,让用户再次授权处理。
<template>
<!-- 微信语音识别 -->
<view class="mask" v-show="isShow">
<view class="weixin-asr">
<view class="title">语音识别</view>
<!-- 动画 -->
<view class="spinner">
<view class="rect rect1"></view>
<view class="rect rect2"></view>
<view class="rect rect3"></view>
<view class="rect rect4"></view>
<view class="rect rect5"></view>
</view>
<view class="tip">说出姓名、电话和详细地址</view>
<button class="btn" type="default" @click="recordStop">说完了</button>
</view>
</view>
</template> <script>
const WechatSI = requirePlugin("WechatSI");
const ASRManager = WechatSI.getRecordRecognitionManager(); export default {
data () {
return {
isShow: false
}
},
onReady () {
// 录音开启成功回调
ASRManager.onStart = function (res) {
_this.isShow = true;
} const _this = this;
// 识别错误事件
ASRManager.onError = (res) => {
_this.isShow = false;
console.log(res.msg);
} // 录音停止回调
ASRManager.onStop = function (res) {
if (res && res.result) {
_this.$emit('callback', res.result);
} else {
uni.showToast({
icon: 'none',
title: '抱歉,没听到您的声音哦'
})
}
}
},
methods: {
data () {
return {
isShow: false,
}
},
show () {
const _this = this;
// 获取是否授权信息
uni.getSetting({
success(res) {
if (res && res.authSetting && res.authSetting.hasOwnProperty('scope.record')) {
if (res.authSetting['scope.record']) {
start();
} else { // 拒绝授权,打开授权设置
uni.openSetting({
success() {
start();
}
})
}
} else {
start();
}
}
}) function start () {
ASRManager.start({
lang: "zh_CN"
});
}
},
// 录音停止
recordStop () {
this.isShow = false;
ASRManager.stop();
}
}
}
</script> <style lang="scss" scoped>
.mask {
position: fixed;
top: 0;
left: 0;
z-index: 300;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .54);
}
.weixin-asr {
position: absolute;
top: calc(50% - #{477upx / 2});
left: 0;
right: 0;
margin: 0 auto;
width: 560upx;
height: 477upx;
background: #fff;
text-align: center;
transform: .5s ease-out .5s;
.title {
margin-top: 42upx;
color: #000;
font-size: 34upx;
font-weight: 500;
}
.spinner {
margin: 50upx;
height: 100upx;
}
.tip {
color: #787878;
}
.btn {
margin-top: 28upx;
width: 225upx;
height: 82upx;
background: $theme1;
color: #fff;
font-size: 34upx;
line-height: 82upx;
border-radius: 82upx;
}
} .spinner {
text-align: center;
} .spinner > .rect {
background-color: #EDAA35;
height: 100%;
border-radius: 13upx;
width: 13upx;
display: inline-block; -webkit-animation: stretchdelay 1.2s infinite ease-in-out;
animation: stretchdelay 1.2s infinite ease-in-out; & + .rect {
margin-left: 15upx;
}
} .spinner .rect2 {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
} .spinner .rect3 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
} .spinner .rect4 {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
} .spinner .rect5 {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
} @-webkit-keyframes stretchdelay {
0%, 40%, 100% { -webkit-transform: scaleY(0.4) }
20% { -webkit-transform: scaleY(1.0) }
} @keyframes stretchdelay {
0%, 40%, 100% {
transform: scaleY(0.4);
-webkit-transform: scaleY(0.4);
} 20% {
transform: scaleY(1.0);
-webkit-transform: scaleY(1.0);
}
}
</style>

微信地址、附近地址

它们的处理,和上面逻辑一样,只是调用的 api 不一样。

逻辑也是先获取授权信息,未授权、用户拒绝授权、授权成功,在用户拒绝授权时,打开授权设置页面,没授权由小程序主动调起授权弹窗。

主要处理逻辑如下:

  • 微信地址
chooseAddress (type) {
const _this = this;
if (type === 'weixin') {
// 处理拒绝再次打开调用设置
uni.getSetting({
success (res) {
if (res && res.authSetting && res.authSetting.hasOwnProperty('scope.address')) {
if (res.authSetting['scope.address']) {
choose();
} else {
uni.openSetting({
success () {
choose();
}
})
}
} else {
choose();
}
}
}); function choose () {
uni.chooseAddress({
success(res) {
if (res) {
// 调用接口将省市区转换成项目需要的,带id的,然后进行后续处理
}
}
})
}
}
}
  • 附近地址
nearAddress () {
const _this = this;
// 处理拒绝再次打开调用设置
uni.getSetting({
success (res) {
if (res && res.authSetting && res.authSetting.hasOwnProperty('scope.userLocation')) {
if (res.authSetting['scope.userLocation']) {
chooseLocation();
} else {
uni.openSetting({
success () {
chooseLocation();
}
})
}
} else {
chooseLocation();
}
}
}) function chooseLocation () {
uni.chooseLocation({
success: function (res) {
if (res) {
// 调用接口将省市区转换成项目需要的,带id的,然后进行后续处理
}
}
});
}
}

uniapp 用户拒绝授权再次调起授权-语音识别、微信地址、附近地址的更多相关文章

  1. 微信小程序开发问答《五十四》同步请求授权 & 用户拒绝授权,重新调起授权 ... ...

    1.同步请求授权 需求分析: 1.在小程序首次打开的时候,我需要同时请求获取多个权限,由用户逐一授权. (['scope.userInfo','scope.userLocation','scope.a ...

  2. 微信小程序----用户拒绝授权,重新调起授权

    获取用户信息 wx.getUserInfo({ withCredentials: true, success: function (res) { var nickName = res.userInfo ...

  3. 微信小程序-用户拒绝授权使用 wx.openSetting({}) 重新调起授权用户信息

    场景模拟:用户进入微信小程序-程序调出授权 选择拒绝之后,需要用到用户授权才能正常使用的页面,就无法正常使用了. 解决方法:在用户选择拒绝之后,弹窗提示用户 拒绝授权之后无法使用,让用户重新授权(微信 ...

  4. 微信小程序button授权页面,用户拒绝后仍可再次授权

    微信小程序授权页面,进入小程序如果没授权跳转到授权页面,授权后跳转到首页,如果用户点拒绝下次进入小程序还是能跳转到授权页面,授权页面如下 app.js  中的 onLaunch或onShow中加如下代 ...

  5. Android Permissions管理之用户拒绝授权

    Android Permissions管理之用户拒绝授权,在Marshmallow之前的安卓版本,应用的权限只需要注册一下,应用就会获取到,在Marshmallow之后,为了安全,全新的权限模型出现, ...

  6. IdentityServer4 常见问题 - 用户拒绝授权后报错

    1.问题说明 在 IdentityServer4 Web 授权中,一般会有一个显示客户端需要获取用户的那些信息的页面,询问用户是否同意: 在这个页面如果我们点击"No, Do Not All ...

  7. 微信小程序判断用户是否需要再次授权获取个人信息

    一.index.js设置如下 //获取用户的授权信息,放到本地缓存中 wx.getSetting({ success: (res) => { if(res.authSetting['scope. ...

  8. iOS提醒用户进入设置界面进行重新授权通知定位等功能

    iOS 8及以上版本最不为人知的一个特点是与应用设置的深层链接,用户可以根据APP的需要授权启用位置.通知.联系人.相机.日历以及健康等设置. 大多数应用程序仅仅是弹出一个包含操作指令的警示窗口,如“ ...

  9. ASP.NET Core 3.0 一个 jwt 的轻量角色/用户、单个API控制的授权认证库

    目录 说明 一.定义角色.API.用户 二.添加自定义事件 三.注入授权服务和中间件 三.如何设置API的授权 四.添加登录颁发 Token 五.部分说明 六.验证 说明 ASP.NET Core 3 ...

随机推荐

  1. SpringBoot + JPA问题汇总

    实体类有继承父类,但父类没有单独标明注解 异常表现 Caused by: org.hibernate.AnnotationException: No identifier specified for ...

  2. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

  3. WordCount2.2

    说明: 合作者:201631062507  201631062526(学号) 代码地址:https://gitee.com/WordCountMC/WordCountTeam 本次作业链接:https ...

  4. 阿里短信封装SDK TP3.2

    1.阿里短信接口需要企业认证: 2.短信需要短信模板 <?php /** * 阿里云短信验证码发送类 * @param string $accessKeyId key * @param stri ...

  5. [0]尝试用Unity3d制作一个王者荣耀(持续更新)->游戏规划

    太得闲了于是想写个农药,虽然可能会失败但是还是要试一试. 因为是自学的不是Unity专业的可能表达语言会有些不标准!望见谅! 结构: 以组件式(比如说摇杆控制和玩家部分的编写是分离的,可以自由拼装)作 ...

  6. [Vue 牛刀小试]:第十七章 - 优化 Vue CLI 3 构建的前端项目模板(1)- 基础项目模板介绍

    一.前言 在上一章中,我们开始通过 Vue CLI 去搭建属于自己的前端 Vue 项目模板,就像我们 .NET 程序员在使用 asp.net core 时一样,我们更多的会在框架基础上按照自己的开发习 ...

  7. 一次搞懂建模语言UML

    Unified Modeling Language (UML)又称统一建模语言或标准建模语言,它是一个支持模型化和软件系统开发的图形化语言,为软件开发的所有阶段提供模型化和可视化支持,包括由需求分析到 ...

  8. Android实现广告页图片无限轮播

    一.概述 对于一个联网的Android应用, 首页广告无限轮播基本已经成为标配了. 那么它是怎么实现的呢? 有几种实现方式呢? 二.无限轮播的实现 1.最常规的手段是用 ViewPager来实现 2. ...

  9. Escape (BFS + 模拟)

    Problem Description The students of the HEU are maneuvering for their military training. The red arm ...

  10. Spring Boot 入门之整合 log4jdbc 篇(六)

    博客地址:http://www.moonxy.com 一.前言 Spring Data JPA 默认采用 Hibernate 实现.Hibernate 的 showSql 配置只打印 SQL,但并不打 ...