KeyController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Module\OpenAPI\AdminControllers;
  3. use App\Module\OpenAPI\Models\OpenApiKey;
  4. use App\Module\OpenAPI\Models\OpenApiApp;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Form;
  8. use UCore\DcatAdmin\AdminController;
  9. use Spatie\RouteAttributes\Attributes\Resource;
  10. /**
  11. * OpenAPI密钥管理控制器
  12. *
  13. * @AdminController(
  14. * title="API密钥管理",
  15. * permission="openapi.keys"
  16. * )
  17. */
  18. #[Resource('openapi-keys')]
  19. class KeyController extends AdminController
  20. {
  21. /**
  22. * 配置数据表格
  23. *
  24. * @return Grid
  25. */
  26. protected function grid(): Grid
  27. {
  28. return Grid::make(new OpenApiKey(), function (Grid $grid) {
  29. // 基础配置
  30. $grid->model()->with('app')->orderBy('created_at', 'desc');
  31. // 字段配置
  32. $grid->column('id', 'ID')->sortable();
  33. $grid->column('app_id', '应用');
  34. $grid->column('key_id', '密钥ID')->copyable();
  35. $grid->column('name', '密钥名称');
  36. $grid->column('status', '状态')->using([
  37. 'ACTIVE' => '激活',
  38. 'INACTIVE' => '停用',
  39. 'EXPIRED' => '已过期',
  40. 'REVOKED' => '已撤销',
  41. ])->label([
  42. 'ACTIVE' => 'success',
  43. 'INACTIVE' => 'warning',
  44. 'EXPIRED' => 'danger',
  45. 'REVOKED' => 'secondary',
  46. ]);
  47. $grid->column('scopes', '权限范围')->display(function ($scopes) {
  48. if (empty($scopes)) return '无';
  49. return implode(', ', array_slice($scopes, 0, 3)) . (count($scopes) > 3 ? '...' : '');
  50. });
  51. $grid->column('last_used_at', '最后使用')->sortable();
  52. $grid->column('expires_at', '过期时间')->sortable();
  53. $grid->column('created_at', '创建时间')->sortable();
  54. // 筛选器
  55. $grid->filter(function (Grid\Filter $filter) {
  56. $filter->equal('app_id', '应用ID');
  57. $filter->like('name', '密钥名称');
  58. $filter->equal('status', '状态')->select([
  59. 'ACTIVE' => '激活',
  60. 'INACTIVE' => '停用',
  61. 'EXPIRED' => '已过期',
  62. 'REVOKED' => '已撤销',
  63. ]);
  64. $filter->between('created_at', '创建时间')->datetime();
  65. $filter->between('expires_at', '过期时间')->datetime();
  66. });
  67. // 行操作
  68. $grid->actions(function (Grid\Displayers\Actions $actions) {
  69. // 可以在这里添加自定义操作
  70. });
  71. });
  72. }
  73. /**
  74. * 配置详情页面
  75. *
  76. * @return Show
  77. */
  78. protected function detail(): Show
  79. {
  80. return Show::make(OpenApiKey::class, function (Show $show) {
  81. // 基础信息
  82. $show->field('id', 'ID');
  83. $show->field('app_id', '应用ID');
  84. $show->field('key_id', '密钥ID');
  85. $show->field('name', '密钥名称');
  86. $show->field('description', '密钥描述');
  87. $show->field('status', '状态')->using([
  88. 'ACTIVE' => '激活',
  89. 'INACTIVE' => '停用',
  90. 'EXPIRED' => '已过期',
  91. 'REVOKED' => '已撤销',
  92. ]);
  93. // 权限信息
  94. $show->divider('权限信息');
  95. $show->field('scopes', '权限范围')->as(function ($scopes) {
  96. return $scopes ? implode(', ', $scopes) : '无';
  97. });
  98. // 使用信息
  99. $show->divider('使用信息');
  100. $show->field('last_used_at', '最后使用时间');
  101. $show->field('expires_at', '过期时间');
  102. // 时间信息
  103. $show->divider('时间信息');
  104. $show->field('created_at', '创建时间');
  105. $show->field('updated_at', '更新时间');
  106. // 工具栏配置
  107. });
  108. }
  109. /**
  110. * 配置表单
  111. *
  112. * @return Form
  113. */
  114. protected function form(): Form
  115. {
  116. return Form::make(new OpenApiKey(), function (Form $form) {
  117. // 基础信息
  118. $form->select('app_id', '应用')
  119. ->options(OpenApiApp::pluck('name', 'app_id'))
  120. ->required();
  121. $form->text('name', '密钥名称')->required();
  122. $form->textarea('description', '密钥描述');
  123. $form->select('status', '状态')
  124. ->options([
  125. 'ACTIVE' => '激活',
  126. 'INACTIVE' => '停用',
  127. ])
  128. ->default('ACTIVE')
  129. ->required();
  130. // 权限配置
  131. $form->divider('权限配置');
  132. $form->checkbox('scopes', '权限范围')
  133. ->options([
  134. 'USER_READ' => '用户读取',
  135. 'USER_WRITE' => '用户写入',
  136. 'GAME_READ' => '游戏读取',
  137. 'GAME_WRITE' => '游戏写入',
  138. 'ITEM_READ' => '物品读取',
  139. 'ITEM_WRITE' => '物品写入',
  140. 'FUND_READ' => '资金读取',
  141. 'TRADE_READ' => '交易读取',
  142. 'TRADE_WRITE' => '交易写入',
  143. ]);
  144. // 过期设置
  145. $form->divider('过期设置');
  146. $form->datetime('expires_at', '过期时间')->help('留空表示永不过期');
  147. // 工具栏配置
  148. // 保存前处理
  149. $form->saving(function (Form $form) {
  150. if ($form->isCreating()) {
  151. // 生成密钥ID和Secret
  152. $form->key_id = 'ak_' . \Illuminate\Support\Str::random(32);
  153. $form->key_secret = hash('sha256', 'sk_' . \Illuminate\Support\Str::random(64));
  154. }
  155. });
  156. });
  157. }
  158. }