Initial commit: TradeMate 外贸小助手 MVP
项目结构: - backend/ Python FastAPI 后端 - uni-app/ uni-app跨端前端 - docs/ 设计文档 - docker-compose.yml Docker编排 - nginx/scripts/systemd 运维配置 已完成功能: - 用户认证 (JWT) - 智能翻译 + 回复建议 - 营销素材生成 - 客户管理 + 沉默检测 - 报价单管理 - 产品库管理 - 汇率换算 - 推送通知 (uni-push) - WhatsApp Webhook框架 - Celery定时任务
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
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
|
||||
|
||||
// #ifndef APP-PLUS
|
||||
console.log('非App环境下跳过推送初始化')
|
||||
resolve(false)
|
||||
// #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 { request } = require('./api.js')
|
||||
await request('/push/register', 'POST', {
|
||||
client_id: clientId,
|
||||
platform: uni.getSystemInfoSync().platform,
|
||||
device_info: uni.getSystemInfoSync(),
|
||||
})
|
||||
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
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 清除所有推送消息
|
||||
*/
|
||||
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
|
||||
Reference in New Issue
Block a user