模拟第三方信息存入数据库并进行消息推送

This commit is contained in:
not-like-juvenile
2026-03-09 17:27:56 +08:00
parent 436b7b251f
commit ee9fabd806
5 changed files with 413 additions and 29 deletions

View File

@@ -1,25 +1,25 @@
// 简单的使用示例
'use strict';
// 可配置的云函数 uni-push 示例:从 event 中读取参数并校验简单 token
const uniPush = uniCloud.getPushManager({ appId: process.env.UNI_PUSH_APPID || "__UNI__9462CA7" });
const uniPush = uniCloud.getPushManager({
appId: process.env.UNI_PUSH_APPID || "__UNI__9462CA7"
});
exports.main = async (event, context) => {
// event: { token, push_clientid, title, content, payload }
const { token, push_clientid, title = '', content = '', payload = {} } = event || {};
// 兼容 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;
// 简单鉴权,建议把真实 secret 存到云函数环境变量 PUSH_TOKEN
const SECRET = process.env.PUSH_TOKEN;
if (SECRET && token !== SECRET) {
console.warn('unauthorized token', { hasToken: !!token });
return { errCode: 401, errMsg: 'unauthorized' };
}
if (!push_clientid) {
return { errCode: 400, errMsg: 'push_clientid required' };
}
if (SECRET && token !== SECRET) return { errCode: 401, errMsg: 'unauthorized' };
if (!push_clientid) return { errCode: 400, errMsg: 'push_clientid required' };
try {
console.log('sending uni-push', { push_clientid, title, content, payload });
const res = await uniPush.sendMessage({
push_clientid,
force_notification: true,
@@ -28,10 +28,8 @@ exports.main = async (event, context) => {
settings: { ttl: 86400000 },
payload
});
console.log('uni-push response', res);
return { errCode: 0, errMsg: 'success', data: res };
} catch (e) {
console.error('uni-push send error', e && (e.stack || e.message || e));
return { errCode: 500, errMsg: e && e.message ? e.message : 'push error', detail: e };
return { errCode: 500, errMsg: e?.message || 'push error' };
}
};