ThirdPartyServiceProvider.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Module\ThirdParty\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Route;
  5. /**
  6. * ThirdParty模块服务提供者
  7. *
  8. * 专注于第三方服务管理功能,提供统一的第三方服务接入和管理
  9. */
  10. class ThirdPartyServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * 注册服务
  14. *
  15. * @return void
  16. */
  17. public function register()
  18. {
  19. // 注册配置文件
  20. $this->mergeConfigFrom(
  21. __DIR__ . '/../Config/thirdparty.php',
  22. 'thirdparty'
  23. );
  24. // 注册服务
  25. $this->registerServices();
  26. }
  27. /**
  28. * 启动服务
  29. *
  30. * @return void
  31. */
  32. public function boot()
  33. {
  34. // 注册路由
  35. $this->registerRoutes();
  36. // 注册视图
  37. $this->registerViews();
  38. // 注册命令
  39. $this->registerCommands();
  40. // 注册事件监听器
  41. $this->registerEventListeners();
  42. // 发布资源
  43. $this->publishResources();
  44. }
  45. /**
  46. * 注册服务
  47. *
  48. * @return void
  49. */
  50. protected function registerServices()
  51. {
  52. // 注册第三方服务核心服务
  53. $this->app->singleton('thirdparty.service', function () {
  54. return new \App\Module\ThirdParty\Services\ThirdPartyService();
  55. });
  56. // 注册凭证管理服务
  57. $this->app->singleton('thirdparty.credential', function () {
  58. return new \App\Module\ThirdParty\Services\CredentialService();
  59. });
  60. // 注册监控服务
  61. $this->app->singleton('thirdparty.monitor', function () {
  62. return new \App\Module\ThirdParty\Services\MonitorService();
  63. });
  64. // 注册日志服务
  65. $this->app->singleton('thirdparty.log', function () {
  66. return new \App\Module\ThirdParty\Services\LogService();
  67. });
  68. // 注册配额管理服务
  69. $this->app->singleton('thirdparty.quota', function () {
  70. return new \App\Module\ThirdParty\Services\QuotaService();
  71. });
  72. // 注册Webhook分发服务
  73. $this->app->singleton('thirdparty.webhook', function () {
  74. return new \App\Module\ThirdParty\Services\WebhookDispatchService();
  75. });
  76. // 注册验证器
  77. $this->app->singleton(\App\Module\ThirdParty\Validators\ServiceValidator::class);
  78. $this->app->singleton(\App\Module\ThirdParty\Validators\CredentialValidator::class);
  79. }
  80. /**
  81. * 注册路由
  82. *
  83. * @return void
  84. */
  85. protected function registerRoutes()
  86. {
  87. // 后台管理路由现在通过路由注解自动注册
  88. // 参考 config/route-attributes.php 配置
  89. // 注册Webhook路由
  90. if (file_exists($webhookRoutes = __DIR__ . '/../Routes/webhook.php')) {
  91. Route::middleware(['api'])
  92. ->prefix('thirdParty/webhook')
  93. ->name('thirdparty.webhook.')
  94. ->group($webhookRoutes);
  95. }
  96. // 注册API路由(如果需要)
  97. if (file_exists($apiRoutes = __DIR__ . '/../Routes/api.php')) {
  98. Route::middleware(['api'])
  99. ->prefix('api/thirdparty')
  100. ->group($apiRoutes);
  101. }
  102. }
  103. /**
  104. * 注册视图 - 已移除
  105. * ThirdParty模块使用dcat admin的Grid/Show/Form构建页面,不使用自定义视图
  106. *
  107. * @return void
  108. */
  109. protected function registerViews()
  110. {
  111. // dcat admin模块不应该使用自定义视图
  112. // 所有页面通过Grid/Show/Form构建
  113. }
  114. /**
  115. * 注册命令
  116. *
  117. * @return void
  118. */
  119. protected function registerCommands()
  120. {
  121. if ($this->app->runningInConsole()) {
  122. $this->commands([
  123. \App\Module\ThirdParty\Commands\InsertThirdPartyAdminMenu::class,
  124. \App\Module\ThirdParty\Commands\HealthCheckCommand::class,
  125. \App\Module\ThirdParty\Commands\QuotaResetCommand::class,
  126. \App\Module\ThirdParty\Commands\CleanupLogsCommand::class,
  127. \App\Module\ThirdParty\Commands\SyncServicesCommand::class,
  128. \App\Module\ThirdParty\Commands\TestServiceCommand::class,
  129. \App\Module\ThirdParty\Commands\TestBaseArchitectureCommand::class,
  130. \App\Module\ThirdParty\Commands\WebhookMappingCommand::class,
  131. \App\Module\ThirdParty\Commands\TestUrsRequestCommand::class,
  132. \App\Module\ThirdParty\Commands\TestUrsLoginCommand::class,
  133. ]);
  134. }
  135. }
  136. /**
  137. * 注册事件监听器
  138. *
  139. * @return void
  140. */
  141. protected function registerEventListeners()
  142. {
  143. // 注册事件监听器
  144. $events = [
  145. // API调用事件监听器
  146. \App\Module\ThirdParty\Events\ApiCallEvent::class => [
  147. \App\Module\ThirdParty\Listeners\ApiCallLogListener::class,
  148. \App\Module\ThirdParty\Listeners\QuotaManagementListener::class,
  149. ],
  150. // 服务状态变更事件监听器
  151. \App\Module\ThirdParty\Events\ServiceStatusChangedEvent::class => [
  152. \App\Module\ThirdParty\Listeners\ServiceStatusMonitorListener::class,
  153. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleServiceStatusChanged',
  154. ],
  155. // 配额告警事件监听器
  156. \App\Module\ThirdParty\Events\QuotaAlertEvent::class => [
  157. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleQuotaAlert',
  158. ],
  159. // 监控告警事件监听器
  160. \App\Module\ThirdParty\Events\MonitorAlertEvent::class => [
  161. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleMonitorAlert',
  162. ],
  163. ];
  164. foreach ($events as $event => $listeners) {
  165. foreach ($listeners as $listener) {
  166. \Illuminate\Support\Facades\Event::listen($event, $listener);
  167. }
  168. }
  169. }
  170. /**
  171. * 发布资源
  172. *
  173. * @return void
  174. */
  175. protected function publishResources()
  176. {
  177. if ($this->app->runningInConsole()) {
  178. // 发布配置文件
  179. $this->publishes([
  180. __DIR__ . '/../Config/thirdparty.php' => config_path('thirdparty.php'),
  181. ], 'thirdparty-config');
  182. // 发布数据库迁移文件
  183. $this->publishes([
  184. __DIR__ . '/../Databases/GenerateSql/thirdparty_tables.sql' => database_path('sql/thirdparty_tables.sql'),
  185. ], 'thirdparty-migrations');
  186. // 发布视图文件 - 已移除
  187. // ThirdParty模块使用dcat admin构建页面,不需要发布视图文件
  188. // 发布前端资源(如果有)
  189. if (is_dir(__DIR__ . '/../Resources/assets')) {
  190. $this->publishes([
  191. __DIR__ . '/../Resources/assets' => public_path('vendor/thirdparty'),
  192. ], 'thirdparty-assets');
  193. }
  194. }
  195. }
  196. /**
  197. * 获取提供的服务
  198. *
  199. * @return array
  200. */
  201. public function provides()
  202. {
  203. return [
  204. 'thirdparty.service',
  205. 'thirdparty.credential',
  206. 'thirdparty.monitor',
  207. 'thirdparty.log',
  208. 'thirdparty.quota',
  209. 'thirdparty.webhook',
  210. ];
  211. }
  212. }