ThirdPartyCredentialController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Module\ThirdParty\AdminControllers;
  3. use UCore\DcatAdmin\AdminController;
  4. use App\Module\ThirdParty\Models\ThirdPartyCredential;
  5. use App\Module\ThirdParty\Models\ThirdPartyService;
  6. use App\Module\ThirdParty\Repositorys\ThirdPartyCredentialRepository;
  7. use App\Module\ThirdParty\Enums\AUTH_TYPE;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. /**
  13. * 第三方服务认证凭证管理控制器
  14. *
  15. * 路由: /admin/thirdparty/credentials
  16. */
  17. #[Resource('thirdparty/credentials', names: 'dcat.admin.thirdparty.credentials')]
  18. class ThirdPartyCredentialController extends AdminController
  19. {
  20. /**
  21. * 页面标题
  22. *
  23. * @var string
  24. */
  25. protected $title = '认证凭证管理';
  26. /**
  27. * 数据仓库
  28. *
  29. * @return string
  30. */
  31. protected function repository()
  32. {
  33. return ThirdPartyCredentialRepository::class;
  34. }
  35. /**
  36. * 列表页面
  37. *
  38. * @return Grid
  39. */
  40. protected function grid(): Grid
  41. {
  42. return Grid::make(new ThirdPartyCredentialRepository(['service']), function (Grid $grid) {
  43. // 基础设置
  44. $grid->column('id', 'ID')->sortable();
  45. // 关联服务
  46. $grid->column('service.name', '所属服务')->sortable();
  47. $grid->column('service.code', '服务代码');
  48. $grid->column('name', '凭证名称')->sortable();
  49. // 认证类型
  50. $grid->column('type', '认证类型')->display(function ($type) {
  51. $authType = AUTH_TYPE::tryFrom($type);
  52. if ($authType) {
  53. $label = $authType->getLabel();
  54. $level = $authType->getSecurityLevel();
  55. $color = $authType->getSecurityLevelColor();
  56. return "<span class='badge badge-{$color}' title='安全级别: {$level}'>{$label}</span>";
  57. }
  58. return $type;
  59. });
  60. // 环境
  61. $grid->column('environment', '环境')->display(function ($env) {
  62. $colors = [
  63. 'production' => 'danger',
  64. 'staging' => 'warning',
  65. 'development' => 'info',
  66. 'testing' => 'secondary',
  67. ];
  68. $color = $colors[$env] ?? 'secondary';
  69. $labels = [
  70. 'production' => '生产',
  71. 'staging' => '预发布',
  72. 'development' => '开发',
  73. 'testing' => '测试',
  74. ];
  75. $label = $labels[$env] ?? $env;
  76. return "<span class='badge badge-{$color}'>{$label}</span>";
  77. });
  78. // 状态
  79. $grid->column('is_active', '状态')->display(function ($active) {
  80. return $active ?
  81. '<span class="badge badge-success"><i class="fa fa-check"></i> 激活</span>' :
  82. '<span class="badge badge-secondary"><i class="fa fa-times"></i> 未激活</span>';
  83. });
  84. // 过期时间
  85. $grid->column('expires_at', '过期时间')->display(function ($time) {
  86. if (!$time) {
  87. return '<span class="badge badge-success">永不过期</span>';
  88. }
  89. $expireTime = \Carbon\Carbon::parse($time);
  90. if ($expireTime->isPast()) {
  91. return '<span class="badge badge-danger">已过期</span>';
  92. } elseif ($expireTime->diffInDays() <= 7) {
  93. return '<span class="badge badge-warning">即将过期</span>';
  94. } else {
  95. return $expireTime->format('Y-m-d H:i:s');
  96. }
  97. });
  98. // 使用统计
  99. $grid->column('usage_count', '使用次数')->sortable();
  100. $grid->column('last_used_at', '最后使用')->display(function ($time) {
  101. return $time ? \Carbon\Carbon::parse($time)->diffForHumans() : '未使用';
  102. });
  103. // 创建时间
  104. $grid->column('created_at', '创建时间')->sortable();
  105. // 过滤器
  106. $grid->filter(function (Grid\Filter $filter) {
  107. $filter->equal('service_id', '所属服务')->select(
  108. ThirdPartyService::pluck('name', 'id')->toArray()
  109. );
  110. $filter->equal('type', '认证类型')->select(AUTH_TYPE::getOptions());
  111. $filter->equal('environment', '环境')->select([
  112. 'production' => '生产',
  113. 'staging' => '预发布',
  114. 'development' => '开发',
  115. 'testing' => '测试',
  116. ]);
  117. $filter->equal('is_active', '状态')->select([
  118. 1 => '激活',
  119. 0 => '未激活',
  120. ]);
  121. $filter->like('name', '凭证名称');
  122. });
  123. // 批量操作
  124. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  125. // TODO: 创建批量操作类
  126. // $batch->add('批量激活', new \App\Module\ThirdParty\AdminActions\BatchActivateCredentialAction());
  127. // $batch->add('批量停用', new \App\Module\ThirdParty\AdminActions\BatchDeactivateCredentialAction());
  128. });
  129. // 工具栏
  130. $grid->tools(function (Grid\Tools $tools) {
  131. $tools->append('<a href="/admin/thirdparty/credentials/expired" class="btn btn-sm btn-warning"><i class="fa fa-exclamation-triangle"></i> 过期凭证</a>');
  132. $tools->append('<a href="/admin/thirdparty/credentials/test" class="btn btn-sm btn-info"><i class="fa fa-flask"></i> 测试凭证</a>');
  133. });
  134. // 行操作
  135. $grid->actions(function (Grid\Displayers\Actions $actions) {
  136. if ($actions->row->is_active) {
  137. $actions->append('<a href="/admin/thirdparty/credentials/' . $actions->getKey() . '/test" class="btn btn-xs btn-warning"><i class="fa fa-flask"></i> 测试</a>');
  138. }
  139. $actions->append('<a href="/admin/thirdparty/credentials/' . $actions->getKey() . '/usage" class="btn btn-xs btn-info"><i class="fa fa-chart-line"></i> 使用统计</a>');
  140. });
  141. // 默认排序
  142. $grid->model()->orderBy('service_id')->orderBy('environment')->orderBy('name');
  143. });
  144. }
  145. /**
  146. * 详情页面
  147. *
  148. * @return Show
  149. */
  150. protected function detail($id): Show
  151. {
  152. return Show::make($id, new ThirdPartyCredentialRepository(['service']), function (Show $show) {
  153. $show->field('id', 'ID');
  154. $show->field('service.name', '所属服务');
  155. $show->field('service.code', '服务代码');
  156. $show->field('name', '凭证名称');
  157. $show->field('type', '认证类型')->as(function ($type) {
  158. $authType = AUTH_TYPE::tryFrom($type);
  159. return $authType ? $authType->getLabel() : $type;
  160. });
  161. $show->field('environment', '环境')->as(function ($env) {
  162. $labels = [
  163. 'production' => '生产环境',
  164. 'staging' => '预发布环境',
  165. 'development' => '开发环境',
  166. 'testing' => '测试环境',
  167. ];
  168. return $labels[$env] ?? $env;
  169. });
  170. $show->field('is_active', '状态')->as(function ($active) {
  171. return $active ? '激活' : '未激活';
  172. });
  173. $show->field('expires_at', '过期时间')->as(function ($time) {
  174. if (!$time) {
  175. return '永不过期';
  176. }
  177. $expireTime = \Carbon\Carbon::parse($time);
  178. $status = $expireTime->isPast() ? ' (已过期)' :
  179. ($expireTime->diffInDays() <= 7 ? ' (即将过期)' : '');
  180. return $expireTime->format('Y-m-d H:i:s') . $status;
  181. });
  182. $show->field('usage_count', '使用次数');
  183. $show->field('last_used_at', '最后使用时间');
  184. $show->field('created_at', '创建时间');
  185. $show->field('updated_at', '更新时间');
  186. // 凭证信息(脱敏显示)
  187. $show->field('credentials', '凭证信息')->json();
  188. });
  189. }
  190. /**
  191. * 表单页面
  192. *
  193. * @return Form
  194. */
  195. protected function form(): Form
  196. {
  197. return Form::make(new ThirdPartyCredentialRepository(), function (Form $form) {
  198. $form->display('id', 'ID');
  199. $form->select('service_id', '所属服务')
  200. ->options(ThirdPartyService::pluck('name', 'id')->toArray())
  201. ->required()
  202. ->help('选择要配置凭证的第三方服务');
  203. $form->text('name', '凭证名称')->required()->help('凭证的显示名称');
  204. $form->select('type', '认证类型')
  205. ->options(AUTH_TYPE::getOptions())
  206. ->required()
  207. ->help('选择认证方式');
  208. $form->select('environment', '环境')
  209. ->options([
  210. 'production' => '生产环境',
  211. 'staging' => '预发布环境',
  212. 'development' => '开发环境',
  213. 'testing' => '测试环境',
  214. ])
  215. ->default('production')
  216. ->required();
  217. $form->switch('is_active', '是否激活')->default(1);
  218. $form->datetime('expires_at', '过期时间')->help('留空表示永不过期');
  219. // 凭证信息(根据认证类型动态显示)
  220. $form->keyValue('credentials', '凭证信息')
  221. ->required()
  222. ->help('根据认证类型填写相应的凭证信息,如API Key、Secret等');
  223. $form->display('usage_count', '使用次数');
  224. $form->display('last_used_at', '最后使用时间');
  225. $form->display('created_at', '创建时间');
  226. $form->display('updated_at', '更新时间');
  227. });
  228. }
  229. }