Files
2026-03-09 17:27:56 +08:00

35 lines
1.1 KiB
JavaScript
Raw Permalink 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: process.env.UNI_PUSH_APPID || "__UNI__9462CA7"
});
exports.main = async (event, context) => {
// 兼容 HTTP 触发:参数在 event.bodystring/json
let input = event || {};
if (input && typeof input.body === 'string') {
try { input = JSON.parse(input.body); } catch (e) { input = {}; }
} else if (input && input.body && typeof input.body === 'object') {
input = input.body;
}
const { token, push_clientid, title = '', content = '', payload = {} } = input;
const SECRET = process.env.PUSH_TOKEN;
if (SECRET && token !== SECRET) return { errCode: 401, errMsg: 'unauthorized' };
if (!push_clientid) return { errCode: 400, errMsg: 'push_clientid required' };
try {
const res = await uniPush.sendMessage({
push_clientid,
force_notification: true,
title,
content,
settings: { ttl: 86400000 },
payload
});
return { errCode: 0, errMsg: 'success', data: res };
} catch (e) {
return { errCode: 500, errMsg: e?.message || 'push error' };
}
};