model()->with('app')->orderBy('created_at', 'desc'); // 字段配置 $grid->column('id', 'ID')->sortable(); $grid->column('app_id', '应用'); $grid->column('key_id', '密钥ID')->copyable(); $grid->column('name', '密钥名称'); $grid->column('status', '状态')->using([ 'ACTIVE' => '激活', 'INACTIVE' => '停用', 'EXPIRED' => '已过期', 'REVOKED' => '已撤销', ])->label([ 'ACTIVE' => 'success', 'INACTIVE' => 'warning', 'EXPIRED' => 'danger', 'REVOKED' => 'secondary', ]); $grid->column('scopes', '权限范围')->display(function ($scopes) { if (empty($scopes)) return '无'; return implode(', ', array_slice($scopes, 0, 3)) . (count($scopes) > 3 ? '...' : ''); }); $grid->column('last_used_at', '最后使用')->sortable(); $grid->column('expires_at', '过期时间')->sortable(); $grid->column('created_at', '创建时间')->sortable(); // 筛选器 $grid->filter(function (Grid\Filter $filter) { $filter->equal('app_id', '应用ID'); $filter->like('name', '密钥名称'); $filter->equal('status', '状态')->select([ 'ACTIVE' => '激活', 'INACTIVE' => '停用', 'EXPIRED' => '已过期', 'REVOKED' => '已撤销', ]); $filter->between('created_at', '创建时间')->datetime(); $filter->between('expires_at', '过期时间')->datetime(); }); // 行操作 $grid->actions(function (Grid\Displayers\Actions $actions) { // 可以在这里添加自定义操作 }); }); } /** * 配置详情页面 * * @return Show */ protected function detail(): Show { return Show::make(OpenApiKey::class, function (Show $show) { // 基础信息 $show->field('id', 'ID'); $show->field('app_id', '应用ID'); $show->field('key_id', '密钥ID'); $show->field('name', '密钥名称'); $show->field('description', '密钥描述'); $show->field('status', '状态')->using([ 'ACTIVE' => '激活', 'INACTIVE' => '停用', 'EXPIRED' => '已过期', 'REVOKED' => '已撤销', ]); // 权限信息 $show->divider('权限信息'); $show->field('scopes', '权限范围')->as(function ($scopes) { return $scopes ? implode(', ', $scopes) : '无'; }); // 使用信息 $show->divider('使用信息'); $show->field('last_used_at', '最后使用时间'); $show->field('expires_at', '过期时间'); // 时间信息 $show->divider('时间信息'); $show->field('created_at', '创建时间'); $show->field('updated_at', '更新时间'); // 工具栏配置 }); } /** * 配置表单 * * @return Form */ protected function form(): Form { return Form::make(new OpenApiKey(), function (Form $form) { // 基础信息 $form->select('app_id', '应用') ->options(OpenApiApp::pluck('name', 'app_id')) ->required(); $form->text('name', '密钥名称')->required(); $form->textarea('description', '密钥描述'); $form->select('status', '状态') ->options([ 'ACTIVE' => '激活', 'INACTIVE' => '停用', ]) ->default('ACTIVE') ->required(); // 权限配置 $form->divider('权限配置'); $form->checkbox('scopes', '权限范围') ->options(\App\Module\OpenAPI\Enums\SCOPE_TYPE::getOptions()) ->help('选择API密钥可以访问的权限范围,请谨慎选择高风险权限'); // 过期设置 $form->divider('过期设置'); $form->datetime('expires_at', '过期时间')->help('留空表示永不过期'); // 工具栏配置 // 保存前处理 $form->saving(function (Form $form) { if ($form->isCreating()) { // 生成密钥ID和Secret $form->key_id = 'ak_' . \Illuminate\Support\Str::random(32); $form->key_secret = hash('sha256', 'sk_' . \Illuminate\Support\Str::random(64)); } }); }); } }