ThirdPartyServiceController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. namespace App\Module\ThirdParty\AdminControllers;
  3. use UCore\DcatAdmin\AdminController;
  4. use App\Module\ThirdParty\Models\ThirdPartyService;
  5. use App\Module\ThirdParty\Repositorys\ThirdPartyServiceRepository;
  6. use App\Module\ThirdParty\Enums\SERVICE_TYPE;
  7. use App\Module\ThirdParty\Enums\AUTH_TYPE;
  8. use App\Module\ThirdParty\Enums\SERVICE_STATUS;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Spatie\RouteAttributes\Attributes\Resource;
  13. /**
  14. * 第三方服务管理控制器
  15. *
  16. * 路由: /admin/thirdparty/services
  17. */
  18. #[Resource('thirdparty/services', names: 'dcat.admin.thirdparty.services')]
  19. class ThirdPartyServiceController extends AdminController
  20. {
  21. /**
  22. * 页面标题
  23. *
  24. * @var string
  25. */
  26. protected $title = '第三方服务管理';
  27. /**
  28. * 数据仓库
  29. *
  30. * @return string
  31. */
  32. protected function repository()
  33. {
  34. return ThirdPartyServiceRepository::class;
  35. }
  36. /**
  37. * 列表页面
  38. *
  39. * @return Grid
  40. */
  41. protected function grid(): Grid
  42. {
  43. return Grid::make(new ThirdPartyServiceRepository(), function (Grid $grid) {
  44. // 基础设置
  45. $grid->column('id', 'ID')->sortable();
  46. $grid->column('name', '服务名称')->sortable();
  47. $grid->column('code', '服务代码')->sortable();
  48. // 服务类型
  49. $grid->column('type', '服务类型')->display(function ($type) {
  50. $serviceType = SERVICE_TYPE::tryFrom($type);
  51. if ($serviceType) {
  52. $label = $serviceType->getLabel();
  53. $color = $serviceType->getColor();
  54. return "<span class='badge badge-{$color}'>{$label}</span>";
  55. }
  56. return $type;
  57. });
  58. // 服务提供商
  59. $grid->column('provider', '服务提供商')->sortable();
  60. // 认证类型
  61. $grid->column('auth_type', '认证类型')->display(function ($authType) {
  62. $auth = AUTH_TYPE::tryFrom($authType);
  63. if ($auth) {
  64. $label = $auth->getLabel();
  65. $level = $auth->getSecurityLevel();
  66. $color = $auth->getSecurityLevelColor();
  67. return "<span class='badge badge-{$color}' title='安全级别: {$level}'>{$label}</span>";
  68. }
  69. return $authType;
  70. });
  71. // 服务状态
  72. $grid->column('status', '状态')->display(function ($status) {
  73. $serviceStatus = SERVICE_STATUS::tryFrom($status);
  74. if ($serviceStatus) {
  75. $label = $serviceStatus->getLabel();
  76. $color = $serviceStatus->getColor();
  77. $icon = $serviceStatus->getIcon();
  78. return "<span class='badge badge-{$color}'><i class='{$icon}'></i> {$label}</span>";
  79. }
  80. return $status;
  81. });
  82. // 优先级
  83. $grid->column('priority', '优先级')->sortable();
  84. // 健康状态
  85. $grid->column('health_status', '健康状态')->display(function ($healthStatus) {
  86. $colors = [
  87. 'HEALTHY' => 'success',
  88. 'WARNING' => 'warning',
  89. 'ERROR' => 'danger',
  90. 'UNKNOWN' => 'secondary',
  91. ];
  92. $color = $colors[$healthStatus] ?? 'secondary';
  93. return "<span class='badge badge-{$color}'>{$healthStatus}</span>";
  94. });
  95. // 最后健康检查时间
  96. $grid->column('last_health_check', '最后检查')->display(function ($time) {
  97. return $time ? $time : '未检查';
  98. });
  99. // 创建时间
  100. $grid->column('created_at', '创建时间')->sortable();
  101. // 过滤器
  102. $grid->filter(function (Grid\Filter $filter) {
  103. $filter->equal('type', '服务类型')->select(SERVICE_TYPE::getOptions());
  104. $filter->equal('status', '状态')->select(SERVICE_STATUS::getOptions());
  105. $filter->equal('auth_type', '认证类型')->select(AUTH_TYPE::getOptions());
  106. $filter->like('name', '服务名称');
  107. $filter->like('code', '服务代码');
  108. $filter->like('provider', '服务提供商');
  109. });
  110. // 批量操作
  111. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  112. // TODO: 创建批量操作类
  113. // $batch->add('批量激活', new \App\Module\ThirdParty\AdminActions\BatchActivateServiceAction());
  114. // $batch->add('批量停用', new \App\Module\ThirdParty\AdminActions\BatchDeactivateServiceAction());
  115. });
  116. // 工具栏
  117. $grid->tools(function (Grid\Tools $tools) {
  118. $tools->append('<a href="/admin/thirdparty/services/health-check" class="btn btn-sm btn-success"><i class="fa fa-heartbeat"></i> 健康检查</a>');
  119. $tools->append('<a href="/admin/thirdparty/services/stats" class="btn btn-sm btn-info"><i class="fa fa-chart-bar"></i> 统计报告</a>');
  120. });
  121. // 行操作
  122. $grid->actions(function (Grid\Displayers\Actions $actions) {
  123. $actions->append('<a href="/admin/thirdparty/credentials?service_id=' . $actions->getKey() . '" class="btn btn-xs btn-primary"><i class="fa fa-key"></i> 凭证</a>');
  124. $actions->append('<a href="/admin/thirdparty/logs?service_id=' . $actions->getKey() . '" class="btn btn-xs btn-info"><i class="fa fa-list"></i> 日志</a>');
  125. if ($actions->row->status === SERVICE_STATUS::ACTIVE->value) {
  126. $actions->append('<a href="/admin/thirdparty/services/' . $actions->getKey() . '/test" class="btn btn-xs btn-warning"><i class="fa fa-flask"></i> 测试</a>');
  127. }
  128. });
  129. // 默认排序
  130. $grid->model()->orderBy('priority')->orderBy('name');
  131. });
  132. }
  133. /**
  134. * 详情页面
  135. *
  136. * @return Show
  137. */
  138. protected function detail($id): Show
  139. {
  140. return Show::make($id, new ThirdPartyServiceRepository(), function (Show $show) {
  141. $show->field('id', 'ID');
  142. $show->field('name', '服务名称');
  143. $show->field('code', '服务代码');
  144. $show->field('type', '服务类型')->as(function ($type) {
  145. $serviceType = SERVICE_TYPE::tryFrom($type);
  146. return $serviceType ? $serviceType->getLabel() : $type;
  147. });
  148. $show->field('provider', '服务提供商');
  149. $show->field('description', '服务描述');
  150. $show->field('base_url', '基础URL');
  151. $show->field('version', 'API版本');
  152. $show->field('auth_type', '认证类型')->as(function ($authType) {
  153. $auth = AUTH_TYPE::tryFrom($authType);
  154. return $auth ? $auth->getLabel() : $authType;
  155. });
  156. $show->field('status', '状态')->as(function ($status) {
  157. $serviceStatus = SERVICE_STATUS::tryFrom($status);
  158. return $serviceStatus ? $serviceStatus->getLabel() : $status;
  159. });
  160. $show->field('priority', '优先级');
  161. $show->field('timeout', '超时时间')->as(function ($timeout) {
  162. return $timeout . ' 秒';
  163. });
  164. $show->field('retry_times', '重试次数');
  165. $show->field('retry_delay', '重试延迟')->as(function ($delay) {
  166. return $delay . ' 毫秒';
  167. });
  168. $show->field('config', '服务配置')->json();
  169. $show->field('headers', '默认请求头')->json();
  170. $show->field('params', '默认参数')->json();
  171. $show->field('webhook_url', 'Webhook URL');
  172. $show->field('health_check_url', '健康检查URL');
  173. $show->field('health_check_interval', '健康检查间隔')->as(function ($interval) {
  174. return $interval . ' 秒';
  175. });
  176. $show->field('health_status', '健康状态');
  177. $show->field('last_health_check', '最后健康检查时间');
  178. $show->field('created_at', '创建时间');
  179. $show->field('updated_at', '更新时间');
  180. // 关联信息
  181. $show->relation('credentials', '认证凭证', function ($model) {
  182. $grid = new Grid(new \App\Module\ThirdParty\Models\ThirdPartyCredential());
  183. $grid->model()->where('service_id', $model->id);
  184. $grid->column('name', '凭证名称');
  185. $grid->column('type', '认证类型');
  186. $grid->column('environment', '环境');
  187. $grid->column('is_active', '状态')->display(function ($active) {
  188. return $active ? '<span class="badge badge-success">激活</span>' : '<span class="badge badge-secondary">未激活</span>';
  189. });
  190. $grid->column('expires_at', '过期时间');
  191. $grid->disableCreateButton();
  192. $grid->disableActions();
  193. return $grid;
  194. });
  195. });
  196. }
  197. /**
  198. * 表单页面
  199. *
  200. * @return Form
  201. */
  202. protected function form(): Form
  203. {
  204. return Form::make(new ThirdPartyServiceRepository(), function (Form $form) {
  205. $form->display('id', 'ID');
  206. $form->text('name', '服务名称')->required()->help('第三方服务的显示名称');
  207. $form->text('code', '服务代码')->help('唯一标识符,留空自动生成');
  208. $form->select('type', '服务类型')->options(SERVICE_TYPE::getOptions())->required();
  209. $form->text('provider', '服务提供商')->required();
  210. $form->textarea('description', '服务描述');
  211. $form->url('base_url', '基础URL')->help('第三方服务的API基础地址');
  212. $form->text('version', 'API版本')->default('v1');
  213. $form->select('auth_type', '认证类型')->options(AUTH_TYPE::getOptions())->required();
  214. $form->select('status', '状态')->options(SERVICE_STATUS::getOptions())->default(SERVICE_STATUS::INACTIVE->value);
  215. $form->number('priority', '优先级')->default(0)->help('数字越小优先级越高');
  216. $form->number('timeout', '超时时间(秒)')->default(30)->min(1)->max(300);
  217. $form->number('retry_times', '重试次数')->default(3)->min(0)->max(10);
  218. $form->number('retry_delay', '重试延迟(毫秒)')->default(1000)->min(100)->max(60000);
  219. $form->keyValue('config', '服务配置')
  220. ->help('服务特定配置,键值对形式输入')
  221. ->default([]);
  222. $form->keyValue('headers', '默认请求头')
  223. ->help('默认HTTP请求头,键值对形式输入')
  224. ->default([]);
  225. $form->keyValue('params', '默认参数')
  226. ->help('默认请求参数,键值对形式输入')
  227. ->default([]);
  228. $form->url('webhook_url', 'Webhook URL')->help('接收第三方服务回调的地址');
  229. $form->text('webhook_secret', 'Webhook密钥')->help('用于验证Webhook请求的密钥');
  230. $form->url('health_check_url', '健康检查URL')->help('用于检查服务健康状态的URL');
  231. $form->number('health_check_interval', '健康检查间隔(秒)')->default(300)->min(60)->max(86400);
  232. $form->display('created_at', '创建时间');
  233. $form->display('updated_at', '更新时间');
  234. // 保存前处理
  235. $form->saving(function (Form $form) {
  236. // 处理keyValue字段,转换为JSON存储
  237. $keyValueFields = ['config', 'headers', 'params'];
  238. foreach ($keyValueFields as $field) {
  239. if (isset($form->$field)) {
  240. if (is_array($form->$field) && !empty($form->$field)) {
  241. // 过滤空值
  242. $filtered = array_filter($form->$field, function($value, $key) {
  243. return !empty($key) && $value !== null && $value !== '';
  244. }, ARRAY_FILTER_USE_BOTH);
  245. // 转换为JSON存储
  246. $form->$field = !empty($filtered) ? json_encode($filtered, JSON_UNESCAPED_UNICODE) : null;
  247. } else {
  248. $form->$field = null;
  249. }
  250. }
  251. }
  252. });
  253. });
  254. }
  255. /**
  256. * 综合报告页面
  257. *
  258. * @return \Illuminate\Contracts\View\View
  259. */
  260. public function overview()
  261. {
  262. $title = '第三方服务综合报告';
  263. // 获取基础统计数据
  264. $stats = $this->getOverviewStats();
  265. return view('thirdparty::reports.overview', compact('title', 'stats'));
  266. }
  267. /**
  268. * 测试页面
  269. *
  270. * @return \Illuminate\Contracts\View\View
  271. */
  272. public function test()
  273. {
  274. $title = 'ThirdParty 模块测试页面';
  275. return view('thirdparty::reports.test', compact('title'));
  276. }
  277. /**
  278. * 健康检查页面
  279. *
  280. * @return \Illuminate\Contracts\View\View
  281. */
  282. public function healthCheck()
  283. {
  284. $title = '服务健康检查';
  285. // 获取所有服务的健康状态
  286. $services = ThirdPartyService::with(['credentials', 'quotas'])
  287. ->orderBy('priority')
  288. ->get();
  289. return view('thirdparty::services.health-check', compact('title', 'services'));
  290. }
  291. /**
  292. * 统计报告页面
  293. *
  294. * @return \Illuminate\Contracts\View\View
  295. */
  296. public function stats()
  297. {
  298. $title = '服务统计报告';
  299. // 获取统计数据
  300. $stats = $this->getServiceStats();
  301. return view('thirdparty::services.stats', compact('title', 'stats'));
  302. }
  303. /**
  304. * 健康报告
  305. *
  306. * @return \Illuminate\Contracts\View\View
  307. */
  308. public function healthReport()
  309. {
  310. $title = '服务健康报告';
  311. // 获取健康状态统计
  312. $healthStats = $this->getHealthStats();
  313. return view('thirdparty::reports.health', compact('title', 'healthStats'));
  314. }
  315. /**
  316. * 使用统计报告
  317. *
  318. * @return \Illuminate\Contracts\View\View
  319. */
  320. public function usageReport()
  321. {
  322. $title = '服务使用统计报告';
  323. // 获取使用统计数据
  324. $usageStats = $this->getUsageStats();
  325. return view('thirdparty::reports.usage', compact('title', 'usageStats'));
  326. }
  327. /**
  328. * 获取综合报告统计数据
  329. *
  330. * @return array
  331. */
  332. protected function getOverviewStats(): array
  333. {
  334. $totalServices = ThirdPartyService::count();
  335. $activeServices = ThirdPartyService::where('status', SERVICE_STATUS::ACTIVE->value)->count();
  336. $healthyServices = ThirdPartyService::where('health_status', 'HEALTHY')->count();
  337. $totalCredentials = \App\Module\ThirdParty\Models\ThirdPartyCredential::count();
  338. $activeCredentials = \App\Module\ThirdParty\Models\ThirdPartyCredential::where('is_active', true)->count();
  339. $totalLogs = \App\Module\ThirdParty\Models\ThirdPartyLog::count();
  340. $todayLogs = \App\Module\ThirdParty\Models\ThirdPartyLog::whereDate('created_at', today())->count();
  341. $errorLogs = \App\Module\ThirdParty\Models\ThirdPartyLog::where('level', 'ERROR')->count();
  342. return [
  343. 'services' => [
  344. 'total' => $totalServices,
  345. 'active' => $activeServices,
  346. 'healthy' => $healthyServices,
  347. 'inactive' => $totalServices - $activeServices,
  348. 'unhealthy' => $totalServices - $healthyServices,
  349. ],
  350. 'credentials' => [
  351. 'total' => $totalCredentials,
  352. 'active' => $activeCredentials,
  353. 'inactive' => $totalCredentials - $activeCredentials,
  354. ],
  355. 'logs' => [
  356. 'total' => $totalLogs,
  357. 'today' => $todayLogs,
  358. 'errors' => $errorLogs,
  359. ],
  360. ];
  361. }
  362. /**
  363. * 获取服务统计数据
  364. *
  365. * @return array
  366. */
  367. protected function getServiceStats(): array
  368. {
  369. // 按服务类型统计
  370. $typeStats = ThirdPartyService::selectRaw('type, count(*) as count')
  371. ->groupBy('type')
  372. ->get()
  373. ->mapWithKeys(function ($item) {
  374. $serviceType = SERVICE_TYPE::tryFrom($item->type);
  375. $label = $serviceType ? $serviceType->getLabel() : $item->type;
  376. return [$label => $item->count];
  377. });
  378. // 按服务提供商统计
  379. $providerStats = ThirdPartyService::selectRaw('provider, count(*) as count')
  380. ->groupBy('provider')
  381. ->orderByDesc('count')
  382. ->limit(10)
  383. ->get()
  384. ->pluck('count', 'provider');
  385. // 按状态统计
  386. $statusStats = ThirdPartyService::selectRaw('status, count(*) as count')
  387. ->groupBy('status')
  388. ->get()
  389. ->mapWithKeys(function ($item) {
  390. $status = SERVICE_STATUS::tryFrom($item->status);
  391. $label = $status ? $status->getLabel() : $item->status;
  392. return [$label => $item->count];
  393. });
  394. return [
  395. 'by_type' => $typeStats,
  396. 'by_provider' => $providerStats,
  397. 'by_status' => $statusStats,
  398. ];
  399. }
  400. /**
  401. * 获取健康状态统计
  402. *
  403. * @return array
  404. */
  405. protected function getHealthStats(): array
  406. {
  407. $healthStats = ThirdPartyService::selectRaw('health_status, count(*) as count')
  408. ->groupBy('health_status')
  409. ->get()
  410. ->pluck('count', 'health_status');
  411. $recentChecks = ThirdPartyService::whereNotNull('last_health_check')
  412. ->where('last_health_check', '>=', now()->subHours(24))
  413. ->count();
  414. return [
  415. 'status_distribution' => $healthStats,
  416. 'recent_checks' => $recentChecks,
  417. 'total_services' => ThirdPartyService::count(),
  418. ];
  419. }
  420. /**
  421. * 获取使用统计数据
  422. *
  423. * @return array
  424. */
  425. protected function getUsageStats(): array
  426. {
  427. // 最近7天的API调用统计
  428. $dailyStats = \App\Module\ThirdParty\Models\ThirdPartyLog::selectRaw('DATE(created_at) as date, count(*) as count')
  429. ->where('created_at', '>=', now()->subDays(7))
  430. ->groupBy('date')
  431. ->orderBy('date')
  432. ->get()
  433. ->pluck('count', 'date');
  434. // 按服务统计调用次数
  435. $serviceStats = \App\Module\ThirdParty\Models\ThirdPartyLog::join('kku_thirdparty_services', 'kku_thirdparty_logs.service_id', '=', 'kku_thirdparty_services.id')
  436. ->selectRaw('kku_thirdparty_services.name, count(*) as count')
  437. ->groupBy('kku_thirdparty_services.id', 'kku_thirdparty_services.name')
  438. ->orderByDesc('count')
  439. ->limit(10)
  440. ->get()
  441. ->pluck('count', 'name');
  442. return [
  443. 'daily_calls' => $dailyStats,
  444. 'service_calls' => $serviceStats,
  445. ];
  446. }
  447. }