fix: keystore

This commit is contained in:
hashbro
2026-09-20 06:15:26 +08:00
parent 2625707aed
commit 2a41a29310
29 changed files with 1972 additions and 66 deletions
@@ -679,10 +679,27 @@ class DeviceController extends Controller
if (! in_array($field, $sortable, true)) {
$field = 'id';
}
$cols = WalletKeystore::listColumns();
$paginator = $device->keystores()->select($cols)->orderBy($field, $order)->paginate($limit, $cols, 'page', $page);
// Two-step query to avoid MySQL "Out of sort memory" (HY001):
// LENGTH(raw_json) forces MySQL to read large blobs during sort.
// Step 1: get paginated IDs ordered by the sort field (no blob access).
// Step 2: fetch light columns (no LENGTH(raw_json)) for those IDs only.
$idQuery = $device->keystores()->orderBy($field, $order);
$total = $idQuery->toBase()->getCountForPagination();
$page = max(1, $page);
$ids = $idQuery->toBase()->forPage($page, $limit)->pluck('wallet_keystores.id')->all();
$rows = count($ids) > 0
? WalletKeystore::query()
->whereIn('id', $ids)
->select(WalletKeystore::listColumnsLight())
->get()
->sortBy(fn (WalletKeystore $row) => array_search($row->id, $ids))
->values()
: collect();
$portal = $this->portal();
$data = collect($paginator->items())->map(function (WalletKeystore $row) use ($portal) {
$data = $rows->map(function (WalletKeystore $row) use ($portal) {
$stats = $row->listStats();
return [
@@ -692,13 +709,15 @@ class DeviceController extends Controller
'kind' => $stats['kind'],
'item_count' => $stats['item_count'],
'summary' => $stats['summary'],
'has_web3_keystore' => (bool) ($stats['has_web3_keystore'] ?? false),
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
'items_url' => route($portal.'.keystores.items', $row->id),
'detail_api_url' => route($portal.'.keystores.detail', $row->id),
'decrypt_url' => route($portal.'.keystores.decrypt', $row->id),
];
})->values();
return $this->layuiPage($paginator->total(), $data);
return $this->layuiPage($total, $data);
}
private function paginateApps(Device $device, string $field, string $order, int $limit, int $page)
@@ -730,7 +749,8 @@ class DeviceController extends Controller
private function paginateNotes(Device $device, string $field, string $order, int $limit, int $page)
{
$note = $device->notes()->orderByDesc('id')->first();
$noteId = $device->notes()->orderByDesc('id')->value('id');
$note = $noteId !== null ? Note::query()->find($noteId) : null;
$items = $note ? $note->items() : [];
if (strtolower($order) === 'asc') {
$items = array_reverse($items);