144 lines
3.8 KiB
JavaScript
144 lines
3.8 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 推送云函数
|
||
* 支持单推(cid)和群推(cids)
|
||
*/
|
||
exports.main = async (event, context) => {
|
||
// HTTP 触发器接收的参数在 event.body 中(JSON 字符串),需要解析
|
||
let requestData;
|
||
|
||
if (event.body && typeof event.body === 'string') {
|
||
try {
|
||
requestData = JSON.parse(event.body);
|
||
console.log('成功解析 event.body');
|
||
} catch (e) {
|
||
console.error('JSON 解析失败:', e.message);
|
||
return {
|
||
success: false,
|
||
error: {
|
||
code: 'ParseError',
|
||
message: '请求体 JSON 格式错误'
|
||
}
|
||
};
|
||
}
|
||
} else if (event.body && typeof event.body === 'object') {
|
||
// 某些情况下 body 可能已经是对象
|
||
requestData = event.body;
|
||
console.log('使用 event.body 对象');
|
||
} else {
|
||
// 兼容直接传递参数的情况(非 HTTP 触发器调用或测试环境)
|
||
requestData = event;
|
||
console.log('使用 event 对象(兼容模式)');
|
||
}
|
||
|
||
console.log('请求数据:', JSON.stringify(requestData));
|
||
|
||
// 从解析后的数据中解构参数
|
||
const { appId, cids, cid, title, content, payload, request_id } = requestData;
|
||
|
||
// 参数校验
|
||
if (!appId || typeof appId !== 'string') {
|
||
console.error('appId 无效:', appId, '类型:', typeof appId);
|
||
return {
|
||
success: false,
|
||
error: {
|
||
code: 'ParameterError',
|
||
message: '缺少必填参数:appId,且必须为字符串'
|
||
}
|
||
};
|
||
}
|
||
|
||
// 统一推送目标:cid 优先,其次 cids
|
||
const clientIds = cid ? [cid] : cids;
|
||
if (!clientIds || (Array.isArray(clientIds) && clientIds.length === 0)) {
|
||
console.error('clientIds 无效:', clientIds);
|
||
return {
|
||
success: false,
|
||
error: {
|
||
code: 'ParameterError',
|
||
message: '缺少必填参数:cids 或 cid'
|
||
}
|
||
};
|
||
}
|
||
|
||
if (!title || typeof title !== 'string') {
|
||
console.error('title 无效:', title);
|
||
return {
|
||
success: false,
|
||
error: {
|
||
code: 'ParameterError',
|
||
message: '缺少必填参数:title,且必须为字符串'
|
||
}
|
||
};
|
||
}
|
||
|
||
if (!content || typeof content !== 'string') {
|
||
console.error('content 无效');
|
||
return {
|
||
success: false,
|
||
error: {
|
||
code: 'ParameterError',
|
||
message: '缺少必填参数:content,且必须为字符串'
|
||
}
|
||
};
|
||
}
|
||
|
||
// 发送推送
|
||
try {
|
||
console.log('初始化推送管理器,appId:', appId);
|
||
const uniPush = uniCloud.getPushManager({ appId });
|
||
|
||
const sendMessageParams = {
|
||
push_clientid: Array.isArray(clientIds) ? clientIds : [clientIds],
|
||
title,
|
||
content,
|
||
force_notification: true // 离线厂商推送必填
|
||
};
|
||
|
||
// 自定义 payload(仅当为有效对象时添加)
|
||
if (
|
||
payload &&
|
||
typeof payload === 'object' &&
|
||
!Array.isArray(payload) &&
|
||
Object.keys(payload).length > 0
|
||
) {
|
||
sendMessageParams.payload = payload;
|
||
console.log('添加自定义 payload');
|
||
}
|
||
|
||
// 可选的 requestId
|
||
if (request_id) {
|
||
sendMessageParams.request_id = request_id;
|
||
console.log('添加 request_id:', request_id);
|
||
}
|
||
|
||
console.log('准备发送推送,参数:', JSON.stringify(sendMessageParams));
|
||
|
||
const res = await uniPush.sendMessage(sendMessageParams);
|
||
|
||
console.log('推送成功,结果:', JSON.stringify(res));
|
||
|
||
return {
|
||
success: true,
|
||
data: res
|
||
};
|
||
|
||
} catch (error) {
|
||
// 记录完整错误信息到云函数日志,便于排查
|
||
console.error('push-send 推送失败:', JSON.stringify({
|
||
message: error.message,
|
||
errMsg: error.errMsg,
|
||
code: error.code,
|
||
stack: error.stack
|
||
}));
|
||
|
||
return {
|
||
success: false,
|
||
error: {
|
||
code: error.code || 'PushError',
|
||
message: error.errMsg || error.message || '推送失败,请稍后重试'
|
||
}
|
||
};
|
||
}
|
||
}; |