7b62c2f8b4
## H5 底部导航修复 (Bug #10) - 精简 App.vue,移除重复 tabbar,仅保留全局样式 - uni-page 设置 height: calc(100% - 50px) + overflow-y: auto - 内容区域精确停在底部导航上方,独立滚动不再叠加 - 恢复 custom-tab-bar 组件 ## 项目进度文档 - PROGRESS.md 更新至 10 个 Bug 修复 - 新增 H5 底部导航修复记录 - 新增历史变更条目
216 lines
4.6 KiB
JavaScript
216 lines
4.6 KiB
JavaScript
import { ref } from 'vue'
|
|
import { pushApi } from './api.js'
|
|
|
|
let pushClientId = ''
|
|
let isInitialized = ref(false)
|
|
|
|
export const pushService = {
|
|
/**
|
|
* 初始化推送服务
|
|
*/
|
|
init() {
|
|
return new Promise((resolve, reject) => {
|
|
// #ifdef APP-PLUS
|
|
const plus = window.plus
|
|
plus.push.init({}, {
|
|
cover: true,
|
|
sound: 'system'
|
|
}, () => {
|
|
console.log('Push init success')
|
|
this.getClientId()
|
|
resolve(true)
|
|
}, (err) => {
|
|
console.error('Push init failed:', err)
|
|
reject(err)
|
|
})
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
this.registerWechatDevice()
|
|
resolve(true)
|
|
// #endif
|
|
|
|
// #ifdef H5
|
|
this.registerWebDevice()
|
|
resolve(true)
|
|
// #endif
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 获取客户端推送ID
|
|
*/
|
|
getClientId() {
|
|
// #ifdef APP-PLUS
|
|
const push = window.plus.push
|
|
push.getClientInfo((info) => {
|
|
pushClientId = info.clientid
|
|
console.log('Push ClientID:', pushClientId)
|
|
this.registerDevice(pushClientId)
|
|
}, (err) => {
|
|
console.error('Get client ID failed:', err)
|
|
})
|
|
// #endif
|
|
},
|
|
|
|
/**
|
|
* 注册设备到服务器
|
|
*/
|
|
async registerDevice(clientId) {
|
|
if (!clientId) return
|
|
|
|
try {
|
|
const platform = uni.getSystemInfoSync().platform || 'web'
|
|
const deviceInfo = uni.getSystemInfoSync()
|
|
|
|
await pushApi.register(clientId, platform, '', deviceInfo)
|
|
console.log('Device registered successfully')
|
|
} catch (err) {
|
|
console.error('Register device failed:', err)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 监听接收推送消息
|
|
*/
|
|
onMessage(callback) {
|
|
// #ifdef APP-PLUS
|
|
const push = window.plus.push
|
|
push.addEventListener('receive', (msg) => {
|
|
console.log('Push received:', msg)
|
|
if (msg.payload) {
|
|
let payload
|
|
try {
|
|
payload = JSON.parse(msg.payload)
|
|
} catch (e) {
|
|
payload = { content: msg.payload }
|
|
}
|
|
callback({
|
|
title: msg.title || '外贸小助手',
|
|
content: msg.content,
|
|
payload,
|
|
timestamp: Date.now()
|
|
})
|
|
}
|
|
})
|
|
// #endif
|
|
},
|
|
|
|
/**
|
|
* 监听点击推送消息
|
|
*/
|
|
onClick(callback) {
|
|
// #ifdef APP-PLUS
|
|
const push = window.plus.push
|
|
push.addEventListener('click', (msg) => {
|
|
console.log('Push clicked:', msg)
|
|
if (msg.payload) {
|
|
let payload
|
|
try {
|
|
payload = JSON.parse(msg.payload)
|
|
} catch (e) {
|
|
payload = { content: msg.payload }
|
|
}
|
|
callback(payload)
|
|
}
|
|
})
|
|
// #endif
|
|
},
|
|
|
|
/**
|
|
* 创建本地推送通知
|
|
*/
|
|
createLocalNotification(options) {
|
|
return new Promise((resolve, reject) => {
|
|
// #ifdef APP-PLUS
|
|
const push = window.plus.push
|
|
const msg = {
|
|
title: options.title || '外贸小助手',
|
|
content: options.content,
|
|
payload: options.payload ? JSON.stringify(options.payload) : '',
|
|
delay: options.delay || 0,
|
|
icon: 'static/icons/logo.png'
|
|
}
|
|
|
|
push.createMessage(msg, (res) => {
|
|
console.log('Local notification created:', res)
|
|
resolve(res)
|
|
}, (err) => {
|
|
console.error('Create notification failed:', err)
|
|
reject(err)
|
|
})
|
|
// #endif
|
|
|
|
// #ifndef APP-PLUS
|
|
resolve(false)
|
|
// #endif
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 微信小程序设备注册
|
|
*/
|
|
registerWechatDevice() {
|
|
// #ifdef MP-WEIXIN
|
|
try {
|
|
const systemInfo = uni.getSystemInfoSync()
|
|
const clientId = `${systemInfo.platform}_${Date.now()}`
|
|
this.registerDevice(clientId)
|
|
} catch (err) {
|
|
console.error('Wechat device register failed:', err)
|
|
}
|
|
// #endif
|
|
},
|
|
|
|
/**
|
|
* H5 设备注册
|
|
*/
|
|
registerWebDevice() {
|
|
// #ifdef H5
|
|
try {
|
|
const clientId = `web_${Date.now()}`
|
|
this.registerDevice(clientId)
|
|
} catch (err) {
|
|
console.error('Web device register failed:', err)
|
|
}
|
|
// #endif
|
|
},
|
|
|
|
/**
|
|
* 清除所有推送消息
|
|
*/
|
|
clearNotifications() {
|
|
// #ifdef APP-PLUS
|
|
const push = window.plus.push
|
|
push.clear()
|
|
// #endif
|
|
},
|
|
|
|
/**
|
|
* 获取未读消息数量
|
|
*/
|
|
getBadgeCount() {
|
|
// #ifdef APP-PLUS
|
|
const main = window.plus.android.runtimeMainActivity()
|
|
const count = plus.android.invoke(main, 'getIntent', 'getIntExtra', '/badge', 0)
|
|
return count
|
|
// #endif
|
|
return 0
|
|
},
|
|
|
|
/**
|
|
* 设置角标
|
|
*/
|
|
setBadge(count) {
|
|
// #ifdef APP-PLUS
|
|
if (uni.setStorageSync) {
|
|
// 小程序设置角标
|
|
// #ifdef MP-WEIXIN
|
|
uni.setStorageSync('badgeCount', count)
|
|
// #endif
|
|
}
|
|
// #endif
|
|
}
|
|
}
|
|
|
|
export default pushService |