ThirdPartyCredentialController.php 10 KB

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