uniapp 用戶拒絕授權再次調起授權-語音識別、微信地址太闺、微信附近地址

小程序重構,采用 uniapp 框架嘁圈。記錄一下踩過的坑省骂。關于用戶拒絕再次調起授權,及如何識別語音識別最住、微信地址钞澳、附近地址的處理。

語音識別 組件

  • 語音識別涨缚,小程序只有錄音功能轧粟,若要識別錄音文件,常規(guī)做法是把錄音文件傳遞給后端脓魏,然后由后端調用百度或訊飛語音識別接口兰吟,然后返回結果。
  • 但是微信小程序官方提供了“同聲傳譯”插件茂翔,支持前端直接識別混蔼×柰#可參考:插件介紹罩句、插件使用文檔
  • 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) {
               // 調用接口將省市區(qū)轉換成項目需要的哺壶,帶id的屋吨,然后進行后續(xù)處理
            }
          }
        })
      }
    }
}
  • 附近地址
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) {
            // 調用接口將省市區(qū)轉換成項目需要的,帶id的山宾,然后進行后續(xù)處理
          }
        }
      });
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末至扰,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子塌碌,更是在濱河造成了極大的恐慌渊胸,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件台妆,死亡現(xiàn)場離奇詭異翎猛,居然都是意外死亡胖翰,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門切厘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來萨咳,“玉大人,你說我怎么就攤上這事疫稿∨嗨” “怎么了?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵遗座,是天一觀的道長舀凛。 經(jīng)常有香客問我,道長途蒋,這世上最難降的妖魔是什么猛遍? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮号坡,結果婚禮上懊烤,老公的妹妹穿的比我還像新娘。我一直安慰自己宽堆,他們只是感情好腌紧,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著畜隶,像睡著了一般壁肋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上籽慢,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天墩划,我揣著相機與錄音,去河邊找鬼嗡综。 笑死,一個胖子當著我的面吹牛杜漠,可吹牛的內容都是我干的极景。 我是一名探鬼主播,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼驾茴,長吁一口氣:“原來是場噩夢啊……” “哼盼樟!你這毒婦竟也來了?” 一聲冷哼從身側響起锈至,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤晨缴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后峡捡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體击碗,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡筑悴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了稍途。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阁吝。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖械拍,靈堂內的尸體忽然破棺而出突勇,到底是詐尸還是另有隱情,我是刑警寧澤坷虑,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布甲馋,位于F島的核電站,受9級特大地震影響迄损,放射性物質發(fā)生泄漏定躏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一海蔽、第九天 我趴在偏房一處隱蔽的房頂上張望共屈。 院中可真熱鬧,春花似錦党窜、人聲如沸拗引。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽矾削。三九已至,卻和暖如春豁护,著一層夾襖步出監(jiān)牢的瞬間哼凯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工楚里, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留断部,地道東北人。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓班缎,卻偏偏與公主長得像蝴光,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子达址,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,113評論 1 32
  • [宋代]趙師俠 梅花謝后櫻花綻蔑祟, 淺淺勻紅。 試手天工沉唠。 百卉千葩一信通疆虚。 余寒未許開舒妥, 怨雨愁風。 結子筠籠...
    修源正本閱讀 283評論 0 2
  • requirejs径簿、cmd罢屈、amd基本解釋
    嘗了又嘗閱讀 178評論 0 0