| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace App\Module\ThirdParty\AdminControllers;
- use UCore\DcatAdmin\AdminController;
- use App\Module\ThirdParty\Models\ThirdPartyCredential;
- use App\Module\ThirdParty\Models\ThirdPartyService;
- use App\Module\ThirdParty\Repositorys\ThirdPartyCredentialRepository;
- use App\Module\ThirdParty\Enums\AUTH_TYPE;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- /**
- * 第三方服务认证凭证管理控制器
- *
- * 路由: /admin/thirdparty/credentials
- */
- class ThirdPartyCredentialController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '认证凭证管理';
- /**
- * 数据仓库
- *
- * @return string
- */
- protected function repository()
- {
- return ThirdPartyCredentialRepository::class;
- }
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid(): Grid
- {
- return Grid::make(new ThirdPartyCredentialRepository(), function (Grid $grid) {
- // 基础设置
- $grid->column('id', 'ID')->sortable();
-
- // 关联服务
- $grid->column('service.name', '所属服务')->sortable();
- $grid->column('service.code', '服务代码');
-
- $grid->column('name', '凭证名称')->sortable();
-
- // 认证类型
- $grid->column('type', '认证类型')->display(function ($type) {
- $authType = AUTH_TYPE::tryFrom($type);
- if ($authType) {
- $label = $authType->getLabel();
- $level = $authType->getSecurityLevel();
- $color = $authType->getSecurityLevelColor();
- return "<span class='badge badge-{$color}' title='安全级别: {$level}'>{$label}</span>";
- }
- return $type;
- });
- // 环境
- $grid->column('environment', '环境')->display(function ($env) {
- $colors = [
- 'production' => 'danger',
- 'staging' => 'warning',
- 'development' => 'info',
- 'testing' => 'secondary',
- ];
- $color = $colors[$env] ?? 'secondary';
- $labels = [
- 'production' => '生产',
- 'staging' => '预发布',
- 'development' => '开发',
- 'testing' => '测试',
- ];
- $label = $labels[$env] ?? $env;
- return "<span class='badge badge-{$color}'>{$label}</span>";
- });
- // 状态
- $grid->column('is_active', '状态')->display(function ($active) {
- return $active ?
- '<span class="badge badge-success"><i class="fa fa-check"></i> 激活</span>' :
- '<span class="badge badge-secondary"><i class="fa fa-times"></i> 未激活</span>';
- });
- // 过期时间
- $grid->column('expires_at', '过期时间')->display(function ($time) {
- if (!$time) {
- return '<span class="badge badge-success">永不过期</span>';
- }
-
- $expireTime = \Carbon\Carbon::parse($time);
- if ($expireTime->isPast()) {
- return '<span class="badge badge-danger">已过期</span>';
- } elseif ($expireTime->diffInDays() <= 7) {
- return '<span class="badge badge-warning">即将过期</span>';
- } else {
- return $expireTime->format('Y-m-d H:i:s');
- }
- });
- // 使用统计
- $grid->column('usage_count', '使用次数')->sortable();
- $grid->column('last_used_at', '最后使用')->display(function ($time) {
- return $time ? \Carbon\Carbon::parse($time)->diffForHumans() : '未使用';
- });
- // 创建时间
- $grid->column('created_at', '创建时间')->sortable();
- // 过滤器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('service_id', '所属服务')->select(
- ThirdPartyService::pluck('name', 'id')->toArray()
- );
- $filter->equal('type', '认证类型')->select(AUTH_TYPE::getOptions());
- $filter->equal('environment', '环境')->select([
- 'production' => '生产',
- 'staging' => '预发布',
- 'development' => '开发',
- 'testing' => '测试',
- ]);
- $filter->equal('is_active', '状态')->select([
- 1 => '激活',
- 0 => '未激活',
- ]);
- $filter->like('name', '凭证名称');
- });
- // 批量操作
- $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
- // TODO: 创建批量操作类
- // $batch->add('批量激活', new \App\Module\ThirdParty\AdminActions\BatchActivateCredentialAction());
- // $batch->add('批量停用', new \App\Module\ThirdParty\AdminActions\BatchDeactivateCredentialAction());
- });
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a href="/admin/thirdparty/credentials/expired" class="btn btn-sm btn-warning"><i class="fa fa-exclamation-triangle"></i> 过期凭证</a>');
- $tools->append('<a href="/admin/thirdparty/credentials/test" class="btn btn-sm btn-info"><i class="fa fa-flask"></i> 测试凭证</a>');
- });
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- if ($actions->row->is_active) {
- $actions->append('<a href="/admin/thirdparty/credentials/' . $actions->getKey() . '/test" class="btn btn-xs btn-warning"><i class="fa fa-flask"></i> 测试</a>');
- }
-
- $actions->append('<a href="/admin/thirdparty/credentials/' . $actions->getKey() . '/usage" class="btn btn-xs btn-info"><i class="fa fa-chart-line"></i> 使用统计</a>');
- });
- // 默认排序
- $grid->model()->orderBy('service_id')->orderBy('environment')->orderBy('name');
- });
- }
- /**
- * 详情页面
- *
- * @return Show
- */
- protected function detail($id): Show
- {
- return Show::make($id, new ThirdPartyCredentialRepository(), function (Show $show) {
- $show->field('id', 'ID');
-
- $show->field('service.name', '所属服务');
- $show->field('service.code', '服务代码');
-
- $show->field('name', '凭证名称');
-
- $show->field('type', '认证类型')->as(function ($type) {
- $authType = AUTH_TYPE::tryFrom($type);
- return $authType ? $authType->getLabel() : $type;
- });
- $show->field('environment', '环境')->as(function ($env) {
- $labels = [
- 'production' => '生产环境',
- 'staging' => '预发布环境',
- 'development' => '开发环境',
- 'testing' => '测试环境',
- ];
- return $labels[$env] ?? $env;
- });
- $show->field('is_active', '状态')->as(function ($active) {
- return $active ? '激活' : '未激活';
- });
- $show->field('expires_at', '过期时间')->as(function ($time) {
- if (!$time) {
- return '永不过期';
- }
-
- $expireTime = \Carbon\Carbon::parse($time);
- $status = $expireTime->isPast() ? ' (已过期)' :
- ($expireTime->diffInDays() <= 7 ? ' (即将过期)' : '');
-
- return $expireTime->format('Y-m-d H:i:s') . $status;
- });
- $show->field('usage_count', '使用次数');
- $show->field('last_used_at', '最后使用时间');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 凭证信息(脱敏显示)
- $show->field('credentials', '凭证信息')->json();
- });
- }
- /**
- * 表单页面
- *
- * @return Form
- */
- protected function form(): Form
- {
- return Form::make(new ThirdPartyCredentialRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->select('service_id', '所属服务')
- ->options(ThirdPartyService::pluck('name', 'id')->toArray())
- ->required()
- ->help('选择要配置凭证的第三方服务');
- $form->text('name', '凭证名称')->required()->help('凭证的显示名称');
-
- $form->select('type', '认证类型')
- ->options(AUTH_TYPE::getOptions())
- ->required()
- ->help('选择认证方式');
- $form->select('environment', '环境')
- ->options([
- 'production' => '生产环境',
- 'staging' => '预发布环境',
- 'development' => '开发环境',
- 'testing' => '测试环境',
- ])
- ->default('production')
- ->required();
- $form->switch('is_active', '是否激活')->default(1);
- $form->datetime('expires_at', '过期时间')->help('留空表示永不过期');
- // 凭证信息(根据认证类型动态显示)
- $form->json('credentials', '凭证信息')
- ->required()
- ->help('根据认证类型填写相应的凭证信息,如API Key、Secret等');
- $form->display('usage_count', '使用次数');
- $form->display('last_used_at', '最后使用时间');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 保存前处理
- $form->saving(function (Form $form) {
- // 加密敏感信息
- if ($form->credentials) {
- $form->credentials = encrypt($form->credentials);
- }
- });
- });
- }
- }
|