歡迎您光臨深圳塔燈網絡科技有限公司!
          電話圖標 余先生:13699882642

          網站百科

          為您解碼網站建設的點點滴滴

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

          發(fā)表日期:2019-11 文章編輯:小燈 瀏覽次數(shù):4953

          小程序重構,采用 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)系,并提供相關證據,工作人員會在5工作日內聯(lián)系您,一經查實,本站立刻刪除侵權內容。本文鏈接:http://www.cjxv.cn/25233.html
          相關小程序
           八年  行業(yè)經驗

          多一份參考,總有益處

          聯(lián)系深圳網站公司塔燈網絡,免費獲得網站建設方案及報價

          咨詢相關問題或預約面談,可以通過以下方式與我們聯(lián)系

          業(yè)務熱線:余經理:13699882642

          Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.    

          主站蜘蛛池模板: 日本不卡高清中文字幕免费| 皇夫被迫含玉势女尊高h| 国产综合成人久久大片91| 一本一本久久a久久精品综合麻豆| 日韩人妻无码精品专区| 亚洲大香人伊一本线| 狠狠爱天天综合色欲网| 动漫人物差差差动漫网站| 色爱区综合激情五月综合激情| 国产欧美一区二区三区在线看| 91丨九色丨首页在线观看| 天天插天天操天天射| 一本色道久久综合网| 无码国模国产在线观看| 久久精品国产精品亚洲蜜月| 欧美一级做一a做片性视频| 亚洲日韩久久综合中文字幕 | 中文字幕成人精品久久不卡| 日本高清va在线播放| 九九这里只有精品视频| 欧美一级片观看| 亚洲成a人v欧美综合天堂 | 欧美日韩亚洲中文字幕二区| 亚洲精品欧美日本中文字幕| 第一区免费在线观看| 印度爱经hd在线观看| 美女张开腿黄网站免费| 国产99久9在线视频| 色狠狠婷婷97| 国产一级理仑片日本| 蜜臀av性久久久久蜜臀aⅴ麻豆| 国产四虎精品8848hh| 麻豆人妻少妇精品无码专区| 国产成人亚洲综合色影视| 韩国成人在线视频| 国产成年无码久久久免费| 色五五月五月开| 国产精品另类激情久久久免费| 18美女扒开尿口无遮挡| 国产精品单位女同事在线| 天堂资源中文在线|