MrShi
11 小时以前 00a7a61df86db969f2ba61c508d02ba4709ce3d4
app/App.vue
@@ -1,12 +1,16 @@
<script>
   import { mapState } from 'vuex'
   import md5 from 'js-md5';
   import { getLocationWithNotice } from '@/utils/utils'
   export default {
      data() {
         return {
            locationTimer: null,
            jpushModule: null,
            tts: null
            tts: null,
            ttsPlatform: '',
            ttsVoice: null,
            hasGrantedLocationPermission: false
         }
      },
      computed: {
@@ -15,7 +19,6 @@
      watch: {
         token(newVal) {
            if (newVal) {
               this.checkAndStartLocationPolling()
               this.bindJPushAlias()
            } else {
               this.stopLocationPolling()
@@ -24,9 +27,10 @@
         }
      },
      onLaunch: function() {
         uni.$on('locationPermissionGranted', this.handleLocationPermissionGranted)
         uni.$on('loginSuccessStartLocationPolling', this.handleLoginSuccessStartLocationPolling)
         this.initTTS()
         this.initJPush()
         this.checkAndStartLocationPolling()
         this.checkAppUpdate()
      },
      onShow: function() {
@@ -38,37 +42,75 @@
         // this.stopLocationPolling()
      },
      onUnload() {
         uni.$off('locationPermissionGranted', this.handleLocationPermissionGranted)
         uni.$off('loginSuccessStartLocationPolling', this.handleLoginSuccessStartLocationPolling)
         if (this.tts) {
            this.tts.stop()
            this.tts.shutdown()
            try {
               if (this.ttsPlatform === 'android') {
                  this.tts.stop()
                  this.tts.shutdown()
               } else if (this.ttsPlatform === 'ios') {
                  this.tts.stopSpeakingAtBoundary(0)
               }
            } catch (e) {
               console.log('释放语音播报失败', e)
            }
         }
         this.stopLocationPolling()
      },
      methods: {
         handleLoginSuccessStartLocationPolling() {
            if (this.locationTimer) return
            if (this.hasGrantedLocationPermission) {
               console.log('登录成功后开始位置轮询上报')
               this.startLocationPolling()
               return
            }
            this.checkAndStartLocationPolling()
         },
         handleLocationPermissionGranted() {
            this.hasGrantedLocationPermission = true
            if (!this.token || this.locationTimer) return
            console.log('定位授权成功,开始位置轮询上报')
            this.startLocationPolling()
         },
         initTTS() {
            if (uni.getSystemInfoSync().platform !== 'android') {
               console.log('仅支持安卓')
            const platform = uni.getSystemInfoSync().platform
            this.ttsPlatform = platform
            if (platform === 'android') {
               try {
                  const TextToSpeech = plus.android.importClass('android.speech.tts.TextToSpeech')
                  const Locale = plus.android.importClass('java.util.Locale')
                  this.tts = new TextToSpeech(plus.android.runtimeMainActivity(), {
                     onInit: (status) => {
                        if (status == 0) {
                           this.tts.setLanguage(Locale.CHINA)
                           console.log('安卓语音初始化成功')
                        }
                     }
                  })
               } catch (e) {
                  console.log('安卓语音初始化失败', e)
               }
               return
            }
            try {
               // 导入安卓原生类
               const TextToSpeech = plus.android.importClass('android.speech.tts.TextToSpeech')
               const Locale = plus.android.importClass('java.util.Locale')
               // 创建TTS
               this.tts = new TextToSpeech(plus.android.runtimeMainActivity(), {
                  onInit: (status) => {
                     if (status == 0) {
                        // 设置中文
                        this.tts.setLanguage(Locale.CHINA)
                        console.log('语音初始化成功')
                     }
                  }
               })
            } catch (e) {
               console.log('初始化失败', e)
            if (platform === 'ios') {
               try {
                  const AVSpeechSynthesizer = plus.ios.importClass('AVSpeechSynthesizer')
                  const AVSpeechSynthesisVoice = plus.ios.importClass('AVSpeechSynthesisVoice')
                  this.tts = new AVSpeechSynthesizer()
                  this.ttsVoice = AVSpeechSynthesisVoice.voiceWithLanguage('zh-CN')
                  console.log('iOS语音初始化成功')
               } catch (e) {
                  console.log('iOS语音初始化失败', e)
               }
               return
            }
            console.log('当前平台暂不支持语音播报')
         },
         speak(text) {
            if (!this.tts) {
@@ -80,8 +122,25 @@
            }
            try {
               // 安卓原生播报(QUEUE_FLUSH = 立即播报,打断上一条)
               this.tts.speak(text, 0, null)
               if (this.ttsPlatform === 'android') {
                  this.tts.speak(text, 0, null)
                  return
               }
               if (this.ttsPlatform === 'ios') {
                  const content = String(text || '').trim()
                  if (!content) return
                  const AVSpeechUtterance = plus.ios.importClass('AVSpeechUtterance')
                  const utterance = AVSpeechUtterance.speechUtteranceWithString(content)
                  if (this.ttsVoice) {
                     utterance.setVoice(this.ttsVoice)
                  }
                  utterance.setRate(0.5)
                  utterance.setVolume(1)
                  utterance.setPitchMultiplier(1)
                  this.tts.stopSpeakingAtBoundary(0)
                  this.tts.speakUtterance(utterance)
               }
            } catch (err) {
               console.log('播报失败', err)
            }
@@ -219,20 +278,20 @@
         checkAndStartLocationPolling() {
            if (!this.token) return
            uni.getLocation({
            getLocationWithNotice({
               type: 'gcj02',
               success: (res) => {
                  console.log('获取定位权限成功,开始定时更新位置')
                  this.startLocationPolling()
               },
               fail: (err) => {
                  console.log('获取定位权限失败:', err.errMsg)
                  uni.showToast({
                     title: '需要定位权限才能更新位置',
                     icon: 'none'
                  })
                  // console.log('获取定位权限失败:', err.errMsg)
                  // uni.showToast({
                  //    title: '需要定位权限才能更新位置',
                  //    icon: 'none'
                  // })
               }
            })
            }).catch(() => {})
         },
         startLocationPolling() {
@@ -253,7 +312,7 @@
         updateLocation() {
            if (!this.token) return
            var that = this;
            uni.getLocation({
            getLocationWithNotice({
               type: 'gcj02',
               success: (res) => {
                  console.log('定时更新位置:', res.latitude, res.longitude)
@@ -274,7 +333,7 @@
               fail: (err) => {
                  console.log('获取位置失败:', err.errMsg)
               }
            })
            }).catch(() => {})
         },
         checkAppUpdate() {
@@ -331,4 +390,4 @@
   uni-mp-share {
      display: none !important;
   }
</style>
</style>