ThirdPartyServiceController.php 19 KB

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