35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
'use strict';
|
||
|
||
const uniPush = uniCloud.getPushManager({
|
||
appId: process.env.UNI_PUSH_APPID || "__UNI__9462CA7"
|
||
});
|
||
|
||
exports.main = async (event, context) => {
|
||
// 兼容 HTTP 触发:参数在 event.body(string/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' };
|
||
}
|
||
}; |