<script>
|
import { mapState } from 'vuex'
|
import md5 from 'js-md5';
|
export default {
|
data() {
|
return {
|
locationTimer: null,
|
jpushModule: null,
|
tts: null
|
}
|
},
|
computed: {
|
...mapState(['userInfo', 'token'])
|
},
|
watch: {
|
token(newVal) {
|
if (newVal) {
|
this.checkAndStartLocationPolling()
|
this.bindJPushAlias()
|
} else {
|
this.stopLocationPolling()
|
this.deleteJPushAlias()
|
}
|
}
|
},
|
onLaunch: function() {
|
this.initTTS()
|
this.initJPush()
|
this.checkAndStartLocationPolling()
|
this.checkAppUpdate()
|
},
|
onShow: function() {
|
console.log('App Show')
|
// this.checkAndStartLocationPolling()
|
},
|
onHide: function() {
|
console.log('App Hide')
|
// this.stopLocationPolling()
|
},
|
onUnload() {
|
if (this.tts) {
|
this.tts.stop()
|
this.tts.shutdown()
|
}
|
this.stopLocationPolling()
|
},
|
methods: {
|
initTTS() {
|
if (uni.getSystemInfoSync().platform !== 'android') {
|
console.log('仅支持安卓')
|
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)
|
}
|
},
|
speak(text) {
|
if (!this.tts) {
|
uni.showToast({
|
title: '语音未准备好',
|
icon: 'none'
|
})
|
return
|
}
|
|
try {
|
// 安卓原生播报(QUEUE_FLUSH = 立即播报,打断上一条)
|
this.tts.speak(text, 0, null)
|
} 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('极光推送initJPushService调用成功')
|
} 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模块未初始化,无法获取RegistrationID')
|
return
|
}
|
console.log('开始获取JPush 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
|
}
|
// 优先从store获取,如果为空则从本地存储获取
|
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
|
uni.getLocation({
|
type: 'gcj02',
|
success: (res) => {
|
console.log('获取定位权限成功,开始定时更新位置')
|
this.startLocationPolling()
|
},
|
fail: (err) => {
|
console.log('获取定位权限失败:', err.errMsg)
|
uni.showToast({
|
title: '需要定位权限才能更新位置',
|
icon: 'none'
|
})
|
}
|
})
|
},
|
|
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;
|
uni.getLocation({
|
type: 'gcj02',
|
success: (res) => {
|
console.log('定时更新位置:', res.latitude, 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)
|
}
|
})
|
},
|
|
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>
|
page {
|
background-color: #f6f8fc;
|
}
|
uni-mp-share {
|
display: none !important;
|
}
|
</style>
|