From 3f9032e92fdd383bfefc87a0bec9b242e1223851 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期二, 09 六月 2026 17:08:44 +0800
Subject: [PATCH] 改bug
---
app/App.vue | 393 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 388 insertions(+), 5 deletions(-)
diff --git a/app/App.vue b/app/App.vue
index a3c7e0c..609b777 100644
--- a/app/App.vue
+++ b/app/App.vue
@@ -1,21 +1,404 @@
<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,
+ ttsPlatform: '',
+ ttsVoice: null,
+ hasGrantedLocationPermission: false
+ }
+ },
computed: {
- ...mapState(['userInfo'])
+ ...mapState(['userInfo', 'token'])
+ },
+ watch: {
+ token(newVal) {
+ if (newVal) {
+ this.bindJPushAlias()
+ } else {
+ this.stopLocationPolling()
+ this.deleteJPushAlias()
+ }
+ }
},
onLaunch: function() {
-
+ uni.$on('locationPermissionGranted', this.handleLocationPermissionGranted)
+ uni.$on('loginSuccessStartLocationPolling', this.handleLoginSuccessStartLocationPolling)
+ uni.$on('privacyAgreed', this.handlePrivacyAgreed)
+ // 妫�鏌ユ槸鍚﹀凡缁忓悓鎰忚繃闅愮鏀跨瓥锛屽鏋滄槸鍒欑洿鎺ュ垵濮嬪寲鏋佸厜鎺ㄩ��
+ console.log('login_privacy_policy_agreed', uni.getStorageSync('login_privacy_policy_agreed'))
+ if (uni.getStorageSync('login_privacy_policy_agreed')) {
+ // 鍒濆鍖栨瀬鍏夋帹閫�
+ this.initJPush()
+ // 鍒濆鍖栬闊虫挱鎶�
+ this.initTTS()
+ // 鑾峰彇鐗堟湰鍙�
+ this.checkAppUpdate()
+ }
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
+ },
+ onUnload() {
+ uni.$off('locationPermissionGranted', this.handleLocationPermissionGranted)
+ uni.$off('loginSuccessStartLocationPolling', this.handleLoginSuccessStartLocationPolling)
+ uni.$off('privacyAgreed', this.handlePrivacyAgreed)
+ if (this.tts) {
+ 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()
+ },
+ handlePrivacyAgreed() {
+ console.log('闅愮鏀跨瓥宸插悓鎰忥紝鍒濆鍖栨瀬鍏夋帹閫�')
+ this.initJPush()
+ },
+ handleLocationPermissionGranted() {
+ this.hasGrantedLocationPermission = true
+ if (!this.token || this.locationTimer) return
+ console.log('瀹氫綅鎺堟潈鎴愬姛锛屽紑濮嬩綅缃疆璇笂鎶�')
+ this.startLocationPolling()
+ },
+ initTTS() {
+ 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
+ }
+
+ 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) {
+ uni.showToast({
+ title: '璇煶鏈噯澶囧ソ',
+ icon: 'none'
+ })
+ return
+ }
+
+ try {
+ 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)
+ }
+ },
+ initJPush() {
+ console.log('寮�濮嬪垵濮嬪寲鏋佸厜鎺ㄩ��...')
+ // #ifdef APP-PLUS
+ let jpushModule = null
+ try {
+ jpushModule = uni.requireNativePlugin('JG-JPush')
+ } catch (e) {
+ console.error('鍔犺浇鏋佸厜鎺ㄩ�佹彃浠跺け璐�:', e)
+ return
+ }
+ if (!jpushModule) {
+ console.error('鏋佸厜鎺ㄩ�佹彃浠舵湭鎵惧埌')
+ return
+ }
+ this.jpushModule = jpushModule
+ console.log('鏋佸厜鎺ㄩ�佹彃浠跺姞杞芥垚鍔燂紝妯″潡:', typeof jpushModule)
+ console.log('妯″潡鏂规硶:', Object.keys(jpushModule))
+
+ if (typeof jpushModule.initJPushService === 'function') {
+ jpushModule.initJPushService()
+ console.log('鏋佸厜鎺ㄩ�乮nitJPushService璋冪敤鎴愬姛')
+ } else {
+ console.error('jpushModule.initJPushService 涓嶆槸鍑芥暟锛屽綋鍓嶆柟娉�:', typeof jpushModule.initJPushService)
+ return
+ }
+
+ if (typeof jpushModule.setLoggerEnable === 'function') {
+ jpushModule.setLoggerEnable(true)
+ }
+ console.log('鏋佸厜鎺ㄩ�佸垵濮嬪寲瀹屾垚锛岃缃洃鍚櫒...')
+ this.setupJPushListeners()
+ this.getRegistrationID()
+ // #endif
+ },
+
+ setupJPushListeners() {
+ var that = this
+ if (!this.jpushModule) return
+
+ // 鐩戝惉杩炴帴鐘舵��
+ this.jpushModule.addConnectEventListener((result) => {
+ console.log('鏋佸厜杩炴帴鐘舵��:', result.connectEnable)
+ })
+
+ // 鐩戝惉閫氱煡
+ this.jpushModule.addNotificationListener((result) => {
+ console.log('鏀跺埌閫氱煡:', JSON.stringify(result))
+ that.speak(result.content || '')
+ const notificationEventType = result.notificationEventType
+ // notificationOpened = 鐐瑰嚮閫氱煡
+ if (notificationEventType === 'notificationOpened') {
+ console.log('閫氱煡琚偣鍑�', result.extras)
+ const extras = result.extras ? result.extras : {}
+ if (extras.type === 'new_order' && extras.orderId) {
+ uni.navigateTo({
+ url: '/pages/order-detail/order-detail?id=' + extras.orderId
+ })
+ } else {
+ uni.switchTab({
+ url: '/pages/index/index'
+ })
+ }
+ }
+ })
+ },
+
+ getRegistrationID() {
+ if (!this.jpushModule) {
+ console.log('JPush妯″潡鏈垵濮嬪寲锛屾棤娉曡幏鍙朢egistrationID')
+ return
+ }
+ console.log('寮�濮嬭幏鍙朖Push RegistrationID...')
+ this.jpushModule.getRegistrationID((result) => {
+ console.log('JPush getRegistrationID result:', JSON.stringify(result))
+ if (result && result.registerID) {
+ console.log('JPush RegistrationID鑾峰彇鎴愬姛:', result.registerID)
+ uni.setStorageSync('jpush_registration_id', result.registerID)
+ } else {
+ console.log('JPush RegistrationID鑾峰彇澶辫触锛屽皾璇曢噸鏂拌幏鍙�')
+ setTimeout(() => {
+ this.getRegistrationID()
+ }, 2000)
+ }
+ })
+ },
+
+ bindJPushAlias() {
+ if (!this.token) return
+ if (!this.jpushModule) {
+ console.log('JPush鏈垵濮嬪寲锛屽欢杩熻缃埆鍚�')
+ setTimeout(() => {
+ this.bindJPushAlias()
+ }, 1000)
+ return
+ }
+ // 浼樺厛浠巗tore鑾峰彇锛屽鏋滀负绌哄垯浠庢湰鍦板瓨鍌ㄨ幏鍙�
+ let telephone = this.$store.state.userInfo?.telephone || ''
+ if (!telephone) {
+ telephone = uni.getStorageSync('userInfo')?.telephone || ''
+ }
+ if (!telephone) {
+ console.log('鐢ㄦ埛鎵嬫満鍙蜂负绌猴紝寤惰繜鑾峰彇...')
+ setTimeout(() => {
+ this.bindJPushAlias()
+ }, 2000)
+ return
+ }
+ const alias = md5(telephone)
+ console.log('璁剧疆鏋佸厜鍒悕:', alias)
+ this.jpushModule.setAlias({
+ alias: alias,
+ sequence: Date.now()
+ }, (result) => {
+ console.log('璁剧疆鍒悕缁撴灉:', JSON.stringify(result))
+ if (result && (result.code === 0 || result.errCode === 0)) {
+ console.log('璁剧疆鏋佸厜鍒悕鎴愬姛')
+ } else {
+ console.log('璁剧疆鏋佸厜鍒悕澶辫触')
+ }
+ })
+ },
+
+ deleteJPushAlias() {
+ if (!this.jpushModule) return
+ this.jpushModule.deleteAlias({
+ sequence: Date.now()
+ }, (result) => {
+ console.log('鍒犻櫎鏋佸厜鍒悕:', JSON.stringify(result))
+ })
+ },
+
+ checkAndStartLocationPolling() {
+ if (!this.token) return
+ getLocationWithNotice({
+ type: 'gcj02',
+ success: (res) => {
+ console.log('鑾峰彇瀹氫綅鏉冮檺鎴愬姛锛屽紑濮嬪畾鏃舵洿鏂颁綅缃�')
+ this.startLocationPolling()
+ },
+ fail: (err) => {
+ // console.log('鑾峰彇瀹氫綅鏉冮檺澶辫触:', err.errMsg)
+ // uni.showToast({
+ // title: '闇�瑕佸畾浣嶆潈闄愭墠鑳芥洿鏂颁綅缃�',
+ // icon: 'none'
+ // })
+ }
+ }).catch(() => {})
+ },
+
+ startLocationPolling() {
+ this.stopLocationPolling()
+ this.updateLocation()
+ this.locationTimer = setInterval(() => {
+ this.updateLocation()
+ }, 60000)
+ },
+
+ stopLocationPolling() {
+ if (this.locationTimer) {
+ clearInterval(this.locationTimer)
+ this.locationTimer = null
+ }
+ },
+
+ updateLocation() {
+ if (!this.token) return
+ var that = this;
+ getLocationWithNotice({
+ type: 'gcj02',
+ success: (res) => {
+ console.log('瀹氭椂鏇存柊浣嶇疆:', res.latitude, res.longitude)
+ that.$store.commit('setLocation', { latitude: res.latitude, longitude: res.longitude })
+ that.$u.api.updateLocation({
+ latitude: res.latitude,
+ longitude: res.longitude
+ }).then(res => {
+ if (res.code === 200) {
+ console.log('鏇存柊浣嶇疆鎴愬姛')
+ } else {
+ console.log('鏇存柊浣嶇疆澶辫触')
+ }
+ }).catch(err => {
+ console.log('鏇存柊浣嶇疆璇锋眰澶辫触:', err)
+ })
+ },
+ fail: (err) => {
+ console.log('鑾峰彇浣嶇疆澶辫触:', err.errMsg)
+ }
+ }).catch(() => {})
+ },
+
+ checkAppUpdate() {
+ plus.runtime.getProperty(plus.runtime.appid, (inf) => {
+ const currentVersion = inf.versionCode
+ this.$u.api.getApiVersion({ type: 0 }).then(res => {
+ if (res.code === 200 && res.data) {
+ const latestVersion = res.data.versionNum
+ if (latestVersion > currentVersion) {
+ if (res.data.isForce === 1) {
+ this.showForceUpdateDialog(res.data.fileUrl, res.data.versionNum)
+ } else {
+ this.showOptionalUpdateDialog(res.data.fileUrl, res.data.versionNum)
+ }
+ }
+ }
+ })
+ })
+ },
+
+ showForceUpdateDialog(fileUrl, version) {
+ uni.showModal({
+ title: '鐗堟湰鏇存柊',
+ content: `妫�娴嬪埌鏂扮増鏈�${version}锛岃鏇存柊鍚庣户缁娇鐢╜,
+ showCancel: false,
+ confirmText: '绔嬪嵆鏇存柊',
+ success: () => {
+ plus.runtime.openURL(fileUrl)
+ }
+ })
+ },
+
+ showOptionalUpdateDialog(fileUrl, version) {
+ uni.showModal({
+ title: '鐗堟湰鏇存柊',
+ content: `妫�娴嬪埌鏂扮増鏈�${version}锛屾槸鍚︽洿鏂帮紵`,
+ confirmText: '鏇存柊',
+ cancelText: '绋嶅悗',
+ success: (res) => {
+ if (res.confirm) {
+ plus.runtime.openURL(fileUrl)
+ }
+ }
+ })
+ }
}
}
</script>
-<style lang="scss">
- @import "uview-ui/index.scss";
-</style>
\ No newline at end of file
+<style>
+ page {
+ background-color: #f6f8fc;
+ }
+ uni-mp-share {
+ display: none !important;
+ }
+</style>
--
Gitblit v1.9.3