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';
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = PushDeerKey::where('key', $validated['pushkey'])->get()->first();
|
|
|
|
|
|
|
|
$result = false;
|
|
|
|
|
|
|
|
if ($key) {
|
2022-01-09 17:39:12 +08:00
|
|
|
$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;
|
2022-01-09 18:03:01 +08:00
|
|
|
$the_message['pushkey_name'] = $key->name;
|
2022-01-09 17:39:12 +08:00
|
|
|
$pd_message = Message::create($the_message);
|
|
|
|
|
2021-12-23 00:19:55 +08:00
|
|
|
$devices = PushDeerDevice::where('uid', $key->uid)->get();
|
|
|
|
|
|
|
|
foreach ($devices as $device) {
|
|
|
|
if ($device) {
|
|
|
|
$result[] = ios_send($device->is_clip, $device->device_id, $validated['text']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
[
|
|
|
|
'id' => 'integer',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|