2021-12-23 00:19:55 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use App\Models\PushDeerUser;
|
|
|
|
|
use App\Models\PushDeerKey;
|
|
|
|
|
use App\Models\PushDeerDevice;
|
|
|
|
|
use App\Models\PushDeerMessage as Message;
|
|
|
|
|
use App\Models\PushDeerMessage;
|
|
|
|
|
|
|
|
|
|
class PushDeerMessageController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function list(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$validated = $request->validate(
|
|
|
|
|
[
|
|
|
|
|
'limit' => 'integer|nullable',
|
2022-01-12 19:44:19 +08:00
|
|
|
|
'since_id' => 'integer|nullable',
|
2021-12-23 00:19:55 +08:00
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$limit = !isset($validated['limit']) ? 10 : intval($validated['limit']);
|
|
|
|
|
|
|
|
|
|
if ($limit > 100) {
|
|
|
|
|
$limit = 100;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 19:44:19 +08:00
|
|
|
|
if (isset($validated['since_id']) && intval($validated['since_id']) > 0) {
|
|
|
|
|
$pd_sql = Message::where('uid', $_SESSION['uid'])->where('id', '>', intval($validated['since_id']));
|
|
|
|
|
} else {
|
|
|
|
|
$pd_sql = Message::where('uid', $_SESSION['uid']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pd_messages = $pd_sql->orderBy('id', 'DESC')->offset(0)->limit($limit)->get(['id', 'uid', 'text', 'desp', 'type','pushkey_name','created_at']);
|
|
|
|
|
|
|
|
|
|
|
2021-12-23 00:19:55 +08:00
|
|
|
|
|
|
|
|
|
return http_result(['messages' => $pd_messages]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
public function push(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$validated = $request->validate(
|
|
|
|
|
[
|
|
|
|
|
'pushkey' => 'string|required',
|
|
|
|
|
'text' => 'string|required',
|
|
|
|
|
'desp' => 'string|nullable',
|
|
|
|
|
'type' => 'string|nullable',
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!isset($validated['desp'])) {
|
|
|
|
|
$validated['desp'] = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($validated['type'])) {
|
|
|
|
|
$validated['type'] = 'markdown';
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 22:50:47 +08:00
|
|
|
|
$result = [];
|
2022-01-31 16:49:03 +08:00
|
|
|
|
|
2022-02-19 22:50:47 +08:00
|
|
|
|
$keys = explode(",", $validated['pushkey']);
|
|
|
|
|
// 去掉重复的key
|
|
|
|
|
$keys = array_unique($keys);
|
2021-12-23 00:19:55 +08:00
|
|
|
|
|
2022-02-19 22:50:47 +08:00
|
|
|
|
// 限制key的数量
|
|
|
|
|
$keys = array_slice($keys, 0, intval(env('MAX_PUSH_KEY_PER_TIME')));
|
2021-12-23 00:19:55 +08:00
|
|
|
|
|
2022-02-19 22:50:47 +08:00
|
|
|
|
foreach ($keys as $thekey) {
|
|
|
|
|
$key = PushDeerKey::where('key', $thekey)->get()->first();
|
2022-06-27 00:33:35 +08:00
|
|
|
|
$user = PushDeerUser::where('id', $key->uid)->get()->first();
|
|
|
|
|
if ($user->level < 1) {
|
|
|
|
|
return send_error('此账号已被停用', ErrorCode('ARGS'));
|
|
|
|
|
}
|
2022-02-19 22:50:47 +08:00
|
|
|
|
|
|
|
|
|
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'] = '[图片]';
|
|
|
|
|
}
|
2022-02-16 17:47:54 +08:00
|
|
|
|
|
2022-02-19 22:50:47 +08:00
|
|
|
|
$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'],
|
2022-03-30 18:19:46 +08:00
|
|
|
|
'payload' => json_encode($the_message),
|
2022-02-19 22:50:47 +08:00
|
|
|
|
'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'));
|
|
|
|
|
}
|
2022-01-29 09:34:45 +08:00
|
|
|
|
}
|
2022-01-21 14:39:27 +08:00
|
|
|
|
}
|
2022-02-19 22:50:47 +08:00
|
|
|
|
} else {
|
|
|
|
|
if (!$sent) {
|
|
|
|
|
return send_error('没有可用的设备,请先注册', ErrorCode('ARGS'));
|
|
|
|
|
}
|
2022-02-16 17:47:54 +08:00
|
|
|
|
}
|
2021-12-23 00:19:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 22:50:47 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-23 16:06:22 +08:00
|
|
|
|
return http_result(['result'=>$result]);
|
2021-12-23 00:19:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function remove(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$validated = $request->validate(
|
|
|
|
|
[
|
2022-01-24 19:11:00 +08:00
|
|
|
|
'id' => 'integer|required',
|
2021-12-23 00:19:55 +08:00
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2021-12-27 18:02:48 +08:00
|
|
|
|
if ($pd_message = PushDeerMessage::where('id', $validated['id'])->get(['id', 'uid', 'text', 'desp', 'type','created_at'])->first()) {
|
|
|
|
|
if ($pd_message->uid == $_SESSION['uid']) {
|
|
|
|
|
$pd_message->delete();
|
|
|
|
|
return http_result(['message'=>'done']);
|
|
|
|
|
}
|
2021-12-23 00:19:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 18:02:48 +08:00
|
|
|
|
return send_error('消息不存在或已删除', ErrorCode('ARGS'));
|
2021-12-23 00:19:55 +08:00
|
|
|
|
}
|
2022-03-31 00:20:59 +08:00
|
|
|
|
|
|
|
|
|
public function clean(Request $request)
|
|
|
|
|
{
|
|
|
|
|
PushDeerMessage::where('uid', $_SESSION['uid'])->delete();
|
|
|
|
|
return http_result(['message'=>'done']);
|
|
|
|
|
}
|
2021-12-23 00:19:55 +08:00
|
|
|
|
}
|