100 lines
3.2 KiB
PHP
100 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Concerns\PortalAware;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Photo;
|
|
use App\Models\User;
|
|
use App\Support\AgentScope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PhotoController extends Controller
|
|
{
|
|
use PortalAware;
|
|
|
|
public function index()
|
|
{
|
|
$agents = $this->isAgentPortal()
|
|
? collect()
|
|
: User::query()->orderBy('username')->get(['id', 'username']);
|
|
|
|
return view('admin.photos.index', [
|
|
'portal' => $this->portal(),
|
|
'agents' => $agents,
|
|
]);
|
|
}
|
|
|
|
public function data(Request $request)
|
|
{
|
|
$q = $this->baseQuery($request);
|
|
|
|
$sortable = ['id', 'size', 'x_hit', 'created_at'];
|
|
$field = (string) $request->query('field', 'id');
|
|
$order = strtolower((string) $request->query('order', 'desc')) === 'asc' ? 'asc' : 'desc';
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'id';
|
|
}
|
|
$q->orderBy('photos.'.$field, $order);
|
|
|
|
$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) {
|
|
return [
|
|
'id' => $row->id,
|
|
'device_key' => $row->device_key ?: '',
|
|
'channel_id' => $row->device_channel_id ?: '',
|
|
'url' => route($portal.'.devices.photo', [$row->device_id, $row->id]),
|
|
'size' => $row->size,
|
|
'x_hit' => $row->x_hit,
|
|
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
|
|
'detail_url' => route($portal.'.devices.show', ['device' => $row->device_id, 'tab' => 'photos']),
|
|
];
|
|
})->values();
|
|
|
|
return response()->json([
|
|
'code' => 0,
|
|
'msg' => '',
|
|
'count' => $paginator->total(),
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
private function baseQuery(Request $request): Builder
|
|
{
|
|
$q = Photo::query()
|
|
->join('devices', 'devices.id', '=', 'photos.device_id')
|
|
->select([
|
|
'photos.*',
|
|
'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 ((string) $request->query('sensitive', '') === '1') {
|
|
$q->where('photos.x_hit', '>', 0);
|
|
}
|
|
if (! $this->isAgentPortal()) {
|
|
AgentScope::applyAgentUserFilter(
|
|
$q,
|
|
AgentScope::parseAgentUserIdFilter($request->query('agent_user_id'))
|
|
);
|
|
}
|
|
|
|
return $q;
|
|
}
|
|
}
|