| | |
| | | <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: { |
| | |
| | | watch: { |
| | | token(newVal) { |
| | | if (newVal) { |
| | | this.checkAndStartLocationPolling() |
| | | this.bindJPushAlias() |
| | | } else { |
| | | this.stopLocationPolling() |
| | |
| | | } |
| | | }, |
| | | onLaunch: function() { |
| | | uni.$on('locationPermissionGranted', this.handleLocationPermissionGranted) |
| | | uni.$on('loginSuccessStartLocationPolling', this.handleLoginSuccessStartLocationPolling) |
| | | this.initTTS() |
| | | this.initJPush() |
| | | this.checkAndStartLocationPolling() |
| | | this.checkAppUpdate() |
| | | }, |
| | | onShow: function() { |
| | |
| | | // 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) { |
| | |
| | | } |
| | | |
| | | 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) |
| | | } |
| | |
| | | |
| | | 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() { |
| | |
| | | updateLocation() { |
| | | if (!this.token) return |
| | | var that = this; |
| | | uni.getLocation({ |
| | | getLocationWithNotice({ |
| | | type: 'gcj02', |
| | | success: (res) => { |
| | | console.log('定时更新位置:', res.latitude, res.longitude) |
| | |
| | | fail: (err) => { |
| | | console.log('获取位置失败:', err.errMsg) |
| | | } |
| | | }) |
| | | }).catch(() => {}) |
| | | }, |
| | | |
| | | checkAppUpdate() { |
| | |
| | | uni-mp-share { |
| | | display: none !important; |
| | | } |
| | | </style> |
| | | </style> |