| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace App\Module\OpenAPI\AdminControllers;
- use App\Module\OpenAPI\Models\OpenApiKey;
- use App\Module\OpenAPI\Models\OpenApiApp;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Form;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * OpenAPI密钥管理控制器
- *
- * @AdminController(
- * title="API密钥管理",
- * permission="openapi.keys"
- * )
- */
- #[Resource('openapi-keys')]
- class KeyController extends AdminController
- {
- /**
- * 配置数据表格
- *
- * @return Grid
- */
- protected function grid(): Grid
- {
- return Grid::make(new OpenApiKey(), function (Grid $grid) {
- // 基础配置
- $grid->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([
- 'USER_READ' => '用户读取',
- 'USER_WRITE' => '用户写入',
- 'GAME_READ' => '游戏读取',
- 'GAME_WRITE' => '游戏写入',
- 'ITEM_READ' => '物品读取',
- 'ITEM_WRITE' => '物品写入',
- 'FUND_READ' => '资金读取',
- 'TRADE_READ' => '交易读取',
- 'TRADE_WRITE' => '交易写入',
- ]);
- // 过期设置
- $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));
- }
- });
- });
- }
- }
|