fix: query

This commit is contained in:
hashbro
2026-09-20 06:27:30 +08:00
parent 2a41a29310
commit 004b38f5f2
3 changed files with 89 additions and 1 deletions
@@ -254,6 +254,15 @@ class PluginSessionController extends Controller
);
}
$createdFrom = trim((string) $request->query('created_from', ''));
$createdTo = trim((string) $request->query('created_to', ''));
if ($createdFrom !== '' && preg_match('/^\d{4}-\d{2}-\d{2}/', $createdFrom)) {
$q->whereDate('plugin_sessions.created_at', '>=', substr($createdFrom, 0, 10));
}
if ($createdTo !== '' && preg_match('/^\d{4}-\d{2}-\d{2}/', $createdTo)) {
$q->whereDate('plugin_sessions.created_at', '<=', substr($createdTo, 0, 10));
}
return $q;
}
+16 -1
View File
@@ -24,6 +24,18 @@
<input type="text" name="account_id" class="layui-input" autocomplete="off">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">创建时间</label>
<div class="layui-input-block">
<div class="layui-input-inline" style="width: 150px;">
<input type="text" name="created_from" id="LAY-session-created-from" placeholder="开始日期" autocomplete="off" class="layui-input">
</div>
<div class="layui-form-mid">-</div>
<div class="layui-input-inline" style="width: 150px;">
<input type="text" name="created_to" id="LAY-session-created-to" placeholder="结束日期" autocomplete="off" class="layui-input">
</div>
</div>
</div>
<div class="layui-inline">
<button class="layui-btn" lay-submit lay-filter="LAY-session-search">搜索</button>
</div>
@@ -44,11 +56,14 @@
@push('scripts')
<script>
layui.use(['table', 'form', 'layer'], function () {
layui.use(['table', 'form', 'laydate', 'layer'], function () {
var table = layui.table, form = layui.form, layer = layui.layer;
var isWhatsApp = @json($isWhatsApp);
var dataUrl = @json($isWhatsApp ? route($portal.'.whatsapp.data') : route($portal.'.telegram.data'));
if (window.CorunaFilterOptions) CorunaFilterOptions.apply(form);
laydate.render({ elem: '#LAY-session-created-from', type: 'date' });
laydate.render({ elem: '#LAY-session-created-to', type: 'date' });
function esc(s) {
return String(s == null ? '' : s)
.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
+64
View File
@@ -7,6 +7,7 @@ use App\Models\Channel;
use App\Models\Device;
use App\Models\PluginSession;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
@@ -201,6 +202,69 @@ class PluginSessionPageTest extends TestCase
->assertJsonPath('code', 1);
}
#[Test]
public function list_and_export_respect_created_date_range(): void
{
Storage::fake('local');
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$device = Device::query()->create([
'device_id' => 'dev-range',
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
]);
$old = Carbon::now()->subDays(10)->startOfDay();
$mid = Carbon::now()->subDays(2)->startOfDay();
$new = Carbon::now()->startOfDay();
foreach ([['id' => 'old', 'at' => $old], ['id' => 'mid', 'at' => $mid], ['id' => 'new', 'at' => $new]] as $row) {
$session = PluginSession::query()->create([
'device_id' => $device->id,
'device_key' => $device->device_id,
'kind' => PluginSession::KIND_TELEGRAM,
'account_id' => $row['id'],
'payload' => ['user_id' => $row['id']],
]);
// created_at is guarded, so set it directly on the model and save.
$session->created_at = $row['at'];
$session->updated_at = $row['at'];
$session->save();
}
$ymd = static fn (Carbon $c) => $c->format('Y-m-d');
// from = mid day => only mid + new remain (old excluded).
$this->actingAs($admin, 'admin')
->getJson(route('admin.telegram.data', ['created_from' => $ymd($mid)]))
->assertOk()
->assertJsonPath('count', 2);
// to = mid day => only old + mid remain (new excluded).
$this->actingAs($admin, 'admin')
->getJson(route('admin.telegram.data', ['created_to' => $ymd($mid)]))
->assertOk()
->assertJsonPath('count', 2);
// both = exactly mid day => only mid remains.
$this->actingAs($admin, 'admin')
->getJson(route('admin.telegram.data', [
'created_from' => $ymd($mid),
'created_to' => $ymd($mid),
]))
->assertOk()
->assertJsonPath('count', 1)
->assertJsonPath('data.0.account_id', 'mid');
// Export honors the same range and reports the narrowed count.
$resp = $this->actingAs($admin, 'admin')
->get(route('admin.sessions.export', [
'kind' => '1',
'created_from' => $ymd($mid),
'created_to' => $ymd($mid),
]))
->assertOk();
$this->assertSame('1', $resp->headers->get('x-export-count'));
}
#[Test]
public function agent_export_respects_channel_scope(): void
{