支持批量key推送,更新文档说明

This commit is contained in:
Easy 2022-02-19 22:50:47 +08:00
parent 9f669714b2
commit d1f1940bd2
3 changed files with 54 additions and 39 deletions

View File

@ -376,7 +376,7 @@ API_BASE=http://127.0.0.1:8800
|参数|说明|备注|
|-|-|-|
|pushkey|PushKey|
|pushkey|PushKey|多个key用`,`隔开在线版最多10个自架版默认最多100个|
|text|推送消息内容|
|desp|消息内容第二部分,选填|
|type|格式,选填|文本=textmarkdown图片=image默认为markdown|

View File

@ -65,3 +65,4 @@ WECHAT_APPID=
WECHAT_APPSECRET=
MAX_PUSH_EVERY_USER_PER_MINUTE=60
MAX_PUSH_KEY_PER_TIME=100

View File

@ -60,54 +60,68 @@ class PushDeerMessageController extends Controller
$validated['type'] = 'markdown';
}
// if (strtolower($validated['type'])=='image') {
// $validated['text'] = '[图片]';
// }
$key = PushDeerKey::where('key', $validated['pushkey'])->get()->first();
$result = [];
if ($key) {
$readkey = Str::random(32);
$the_message = [];
$the_message['uid'] = $key->uid;
$the_message['text'] = $validated['text'];
$the_message['desp'] = $validated['desp'];
$the_message['type'] = $validated['type'];
$the_message['readkey'] = $readkey;
$the_message['pushkey_name'] = $key->name;
$pd_message = Message::create($the_message);
$keys = explode(",", $validated['pushkey']);
// 去掉重复的key
$keys = array_unique($keys);
$sent = false;
// 如果配置MQTT服务
// if (strtolower(env('MQTT_ON')) == 'true') {
if (env('MQTT_ON') > 0) {
// 给 mqtt/send 转发消息
$result[] = make_post('http://mqtt/send', [
'key' => env('MQTT_API_KEY'),
'content' => $validated['text'],
'type' => $validated['type'] == 'image' ? 'bg_url' : 'text',
'topic' => $validated['pushkey'],
], 3);
}
// 限制key的数量
$keys = array_slice($keys, 0, intval(env('MAX_PUSH_KEY_PER_TIME')));
if ($devices = PushDeerDevice::where('uid', $key->uid)->get()) {
foreach ($devices as $device) {
if ($device) {
$func_name = $device['type'].'_send';
if (function_exists($func_name)) {
$result[] = $func_name($device->is_clip, $device->device_id, $validated['text'], '', env('APP_DEBUG'));
foreach ($keys as $thekey) {
$key = PushDeerKey::where('key', $thekey)->get()->first();
if ($key) {
$readkey = Str::random(32);
$the_message = [];
$the_message['uid'] = $key->uid;
$the_message['text'] = $validated['text'];
$the_message['desp'] = $validated['desp'];
$the_message['type'] = $validated['type'];
$the_message['readkey'] = $readkey;
$the_message['pushkey_name'] = $key->name;
$pd_message = Message::create($the_message);
// 因为通知是悬浮显示所以将URL改为文字提示
// 如果需要访问原始内容,使用 $the_message['text']
if (strtolower($validated['type'])=='image') {
$validated['text'] = '[图片]';
}
$sent = false;
// 如果配置MQTT服务
// if (strtolower(env('MQTT_ON')) == 'true') {
if (env('MQTT_ON') > 0) {
// 给 mqtt/send 转发消息
$result[] = make_post('http://mqtt/send', [
'key' => env('MQTT_API_KEY'),
'content' => $the_message['text'],
'type' => $validated['type'] == 'image' ? 'bg_url' : 'text',
'topic' => $thekey,
], 3);
}
if ($devices = PushDeerDevice::where('uid', $key->uid)->get()) {
foreach ($devices as $device) {
if ($device) {
$func_name = $device['type'].'_send';
if (function_exists($func_name)) {
$result[] = $func_name($device->is_clip, $device->device_id, $validated['text'], '', env('APP_DEBUG'));
}
}
}
}
} else {
if (!$sent) {
return send_error('没有可用的设备,请先注册', ErrorCode('ARGS'));
} else {
if (!$sent) {
return send_error('没有可用的设备,请先注册', ErrorCode('ARGS'));
}
}
}
}
return http_result(['result'=>$result]);
}