merchapp/java-mall-app-shop-admin/uniCloud-aliyun/cloudfunctions/push/index.js
2025-06-25 16:42:05 +08:00

160 lines
5.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const uniPush = uniCloud.getPushManager({
appId: "__UNI__95F809F"
});
const db = uniCloud.database();
const pushLogCollection = db.collection('push_logs'); // 假设存在 push_logs 集合用于记录推送日志
// 验证设备 ID 是否有效
const isValidDeviceId = (deviceId) => {
return typeof deviceId ==='string' && deviceId.trim()!== '';
};
exports.main = async (event) => {
try {
let obj = JSON.parse(event.body);
// 检查发送者和接收者信息
if (!obj.senderId ||!obj.receiverId) {
const errorMsg = '缺少发送者或接收者信息';
// 记录错误日志
await pushLogCollection.add({
senderId: obj.senderId,
receiverId: obj.receiverId,
title: obj.title,
content: obj.content,
status: 'failed',
errorCode: 400,
errorMsg: errorMsg,
timestamp: new Date(),
requestBody: obj
});
return {
code: 400,
msg: errorMsg
};
}
// 如果 category 不是对象,将其移除
if (typeof obj.category!== 'object') {
delete obj.category;
}
// 从数据库中获取接收者的设备标识
if (typeof obj.receiverId!== 'string' || obj.receiverId.trim() === '') {
const errorMsg = '接收者 ID 无效';
// 记录错误日志
await pushLogCollection.add({
senderId: obj.senderId,
receiverId: obj.receiverId,
title: obj.title,
content: obj.content,
status: 'failed',
errorCode: 400,
errorMsg: errorMsg,
timestamp: new Date(),
requestBody: obj
});
return {
code: 400,
msg: errorMsg
};
}
const receiverDevices = await db.collection('opendb-device')
.where({ userId: obj.receiverId })
.get();
console.log('接收者 ID:', obj.receiverId);
console.log('查询到的设备信息原始数据:', receiverDevices.data); // 打印原始数据
if (receiverDevices.data.length === 0) {
const errorMsg = `未找到接收者ID: ${obj.receiverId})的设备信息`;
// 记录错误日志
await pushLogCollection.add({
senderId: obj.senderId,
receiverId: obj.receiverId,
title: obj.title,
content: obj.content,
status: 'failed',
errorCode: 404,
errorMsg: errorMsg,
timestamp: new Date(),
requestBody: obj
});
return {
code: 404,
msg: errorMsg
};
}
const pushClientIds = "设备id";
// console.log(pushClientIds);
const validDeviceIds = ["设备id"];
validDeviceIds.push();
const invalidDeviceIds = [];
if (validDeviceIds.length === 0) {
const errorMsg = `接收者ID: ${obj.receiverId})的所有设备 ID 均无效,无效的 ID 包括: ${invalidDeviceIds.join(', ')}`;
// 记录错误日志
await pushLogCollection.add({
senderId: obj.senderId,
receiverId: obj.receiverId,
title: obj.title,
content: obj.content,
status: 'failed',
errorCode: 400,
errorMsg: errorMsg,
timestamp: new Date(),
requestBody: obj
});
return {
code: 400,
msg: errorMsg
};
}
const res = await uniPush.sendMessage({
"push_clientid": validDeviceIds,
"title": obj.title,
"content": obj.content,
"payload": obj.payload,
"force_notification": true,
"request_id": obj.request_id,
"options": obj.options
});
// 记录成功日志
await pushLogCollection.add({
senderId: obj.senderId,
receiverId: obj.receiverId,
title: obj.title,
content: obj.content,
status:'success',
pushResult: res,
timestamp: new Date(),
requestBody: obj
});
return {
code: 200,
msg: '消息推送成功',
data: res
};
} catch (error) {
console.error('消息推送出错:', error);
// 记录异常错误日志
await pushLogCollection.add({
senderId: event.senderId,
receiverId: event.receiverId,
title: event.title,
content: event.content,
status: 'failed',
errorCode: 500,
errorMsg: '消息推送失败',
errorDetail: error.message,
timestamp: new Date(),
requestBody: event.body
});
return {
code: 500,
msg: '消息推送失败',
error: error.message
};
}
};