配置push云函数
This commit is contained in:
@@ -178,7 +178,7 @@
|
|||||||
}
|
}
|
||||||
uni.showLoading({ title: '登录中...', mask: true });
|
uni.showLoading({ title: '登录中...', mask: true });
|
||||||
const push_cid = uni.getStorageSync('push_cid')
|
const push_cid = uni.getStorageSync('push_cid')
|
||||||
// console.log('cid',push_cid)
|
// console.log('登录cid',push_cid)
|
||||||
let params = {
|
let params = {
|
||||||
username:phoneNumber.value,
|
username:phoneNumber.value,
|
||||||
password:password.value,
|
password:password.value,
|
||||||
|
|||||||
@@ -1,14 +1,144 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const uniPush = uniCloud.getPushManager({
|
|
||||||
appId: "__UNI__5F224F3"
|
|
||||||
})
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 推送云函数
|
||||||
|
* 支持单推(cid)和群推(cids)
|
||||||
|
*/
|
||||||
exports.main = async (event, context) => {
|
exports.main = async (event, context) => {
|
||||||
const res = await uniPush.sendMessage({
|
// HTTP 触发器接收的参数在 event.body 中(JSON 字符串),需要解析
|
||||||
push_clientid: event.cid,
|
let requestData;
|
||||||
title: "通知标题",
|
|
||||||
content: "通知内容",
|
if (event.body && typeof event.body === 'string') {
|
||||||
force_notification: true
|
try {
|
||||||
})
|
requestData = JSON.parse(event.body);
|
||||||
return res
|
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 || '推送失败,请稍后重试'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "push-send-butler",
|
"name": "push-send",
|
||||||
|
"cloudfunction-config": {
|
||||||
|
"path": "/push-send",
|
||||||
|
"timeout": 60
|
||||||
|
},
|
||||||
"dependencies": {},
|
"dependencies": {},
|
||||||
"extensions": {
|
"extensions": {
|
||||||
"uni-cloud-jql": {},
|
|
||||||
"uni-cloud-push": {}
|
"uni-cloud-push": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user