feat: note

This commit is contained in:
hashbro
2026-09-08 04:53:35 +08:00
parent 9626fcc598
commit c1d24ab815
19 changed files with 870 additions and 58 deletions
+14 -16
View File
@@ -683,26 +683,24 @@ class DeviceController extends Controller
private function paginateNotes(Device $device, string $field, string $order, int $limit, int $page)
{
$sortable = ['id', 'created_at', 'updated_at'];
if (! in_array($field, $sortable, true)) {
$field = 'id';
$note = $device->notes()->orderByDesc('id')->first();
$items = $note ? $note->items() : [];
if (strtolower($order) === 'asc') {
$items = array_reverse($items);
}
$paginator = $device->notes()->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
$data = collect($paginator->items())->map(function (Note $note) {
$content = $note->content;
if (is_array($content)) {
$content = json_encode($content, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
$total = count($items);
$slice = array_slice($items, ($page - 1) * $limit, $limit);
$reportedAt = optional($note?->updated_at ?? $note?->created_at)->format('Y-m-d H:i:s');
$data = collect($slice)->values()->map(function (array $item, int $i) use ($page, $limit, $reportedAt) {
return [
'id' => $note->id,
'content' => $content ?: '',
'created_at' => optional($note->created_at)->format('Y-m-d H:i:s'),
'updated_at' => optional($note->updated_at)->format('Y-m-d H:i:s'),
'id' => (($page - 1) * $limit) + $i + 1,
'title' => $item['title'] ?? '',
'body' => $item['body'] ?? '',
'created_at' => $reportedAt ?: '',
];
})->values();
});
return $this->layuiPage($paginator->total(), $data);
return $this->layuiPage($total, $data);
}
private function paginateEvents(Device $device, string $field, string $order, int $limit, int $page)
@@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use App\Models\Note;
use App\Models\User;
use App\Support\AgentScope;
use App\Support\NoteContent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
@@ -44,16 +45,16 @@ class NoteController extends Controller
$portal = $this->portal();
$data = collect($paginator->items())->map(function ($row) use ($portal) {
$content = $row->content;
if (is_array($content)) {
$content = json_encode($content, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
$items = $row instanceof Note ? $row->items() : NoteContent::normalizeStored($row->content);
$titles = NoteContent::titles($items, 2);
return [
'id' => $row->id,
'device_key' => $row->device_key ?: '',
'channel_id' => $row->device_channel_id ?: '',
'content' => $content ?: '',
'item_count' => count($items),
'preview' => implode(' · ', $titles),
'items' => $items,
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
'updated_at' => optional($row->updated_at)->format('Y-m-d H:i:s'),
'detail_url' => route($portal.'.devices.show', ['device' => $row->device_id, 'tab' => 'notes']),