SyncServicesCommand.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace App\Module\ThirdParty\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\ThirdParty\Models\ThirdPartyService;
  5. use App\Module\ThirdParty\Enums\SERVICE_TYPE;
  6. use App\Module\ThirdParty\Enums\AUTH_TYPE;
  7. /**
  8. * 第三方服务同步命令
  9. */
  10. class SyncServicesCommand extends Command
  11. {
  12. /**
  13. * 命令签名
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'thirdparty:sync-services
  18. {--config= : 配置文件路径}
  19. {--dry-run : 只显示将要同步的服务,不实际执行}
  20. {--force : 强制覆盖现有服务}';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '从配置文件同步第三方服务';
  27. /**
  28. * 执行命令
  29. *
  30. * @return int
  31. */
  32. public function handle(): int
  33. {
  34. $this->info('开始同步第三方服务...');
  35. try {
  36. $services = $this->loadServicesConfig();
  37. if (empty($services)) {
  38. $this->warn('没有找到需要同步的服务配置');
  39. return Command::SUCCESS;
  40. }
  41. $this->displayServicesToSync($services);
  42. if ($this->option('dry-run')) {
  43. $this->info('这是预览模式,没有实际同步服务');
  44. return Command::SUCCESS;
  45. }
  46. if (!$this->option('force') && !$this->confirm('确认要同步这些服务吗?')) {
  47. $this->info('操作已取消');
  48. return Command::SUCCESS;
  49. }
  50. $results = $this->syncServices($services);
  51. $this->displayResults($results);
  52. return Command::SUCCESS;
  53. } catch (\Exception $e) {
  54. $this->error("同步失败: {$e->getMessage()}");
  55. return Command::FAILURE;
  56. }
  57. }
  58. /**
  59. * 加载服务配置
  60. *
  61. * @return array
  62. */
  63. protected function loadServicesConfig(): array
  64. {
  65. $configPath = $this->option('config');
  66. if ($configPath) {
  67. if (!file_exists($configPath)) {
  68. throw new \Exception("配置文件不存在: {$configPath}");
  69. }
  70. $config = json_decode(file_get_contents($configPath), true);
  71. if (json_last_error() !== JSON_ERROR_NONE) {
  72. throw new \Exception("配置文件格式错误: " . json_last_error_msg());
  73. }
  74. return $config['services'] ?? [];
  75. }
  76. // 使用默认配置
  77. return $this->getDefaultServicesConfig();
  78. }
  79. /**
  80. * 获取默认服务配置
  81. *
  82. * @return array
  83. */
  84. protected function getDefaultServicesConfig(): array
  85. {
  86. return [
  87. [
  88. 'name' => '阿里云短信服务',
  89. 'code' => 'aliyun_sms',
  90. 'type' => 'SMS',
  91. 'provider' => 'ALIYUN',
  92. 'description' => '阿里云短信发送服务',
  93. 'base_url' => 'https://dysmsapi.aliyuncs.com',
  94. 'auth_type' => 'API_KEY',
  95. 'status' => 'INACTIVE',
  96. ],
  97. [
  98. 'name' => '腾讯云短信服务',
  99. 'code' => 'tencent_sms',
  100. 'type' => 'SMS',
  101. 'provider' => 'TENCENT',
  102. 'description' => '腾讯云短信发送服务',
  103. 'base_url' => 'https://sms.tencentcloudapi.com',
  104. 'auth_type' => 'SIGNATURE',
  105. 'status' => 'INACTIVE',
  106. ],
  107. [
  108. 'name' => '支付宝支付',
  109. 'code' => 'alipay',
  110. 'type' => 'PAYMENT',
  111. 'provider' => 'ALIPAY',
  112. 'description' => '支付宝开放平台支付服务',
  113. 'base_url' => 'https://openapi.alipay.com/gateway.do',
  114. 'auth_type' => 'SIGNATURE',
  115. 'status' => 'INACTIVE',
  116. ],
  117. [
  118. 'name' => '微信支付',
  119. 'code' => 'wechat_pay',
  120. 'type' => 'PAYMENT',
  121. 'provider' => 'WECHAT',
  122. 'description' => '微信支付API服务',
  123. 'base_url' => 'https://api.mch.weixin.qq.com',
  124. 'auth_type' => 'SIGNATURE',
  125. 'status' => 'INACTIVE',
  126. ],
  127. ];
  128. }
  129. /**
  130. * 显示将要同步的服务
  131. *
  132. * @param array $services
  133. * @return void
  134. */
  135. protected function displayServicesToSync(array $services): void
  136. {
  137. $this->info("\n将要同步的服务:");
  138. $this->line(str_repeat('-', 80));
  139. $headers = ['名称', '代码', '类型', '提供商', '状态'];
  140. $rows = [];
  141. foreach ($services as $service) {
  142. $rows[] = [
  143. $service['name'],
  144. $service['code'],
  145. $service['type'],
  146. $service['provider'],
  147. $service['status'] ?? 'INACTIVE',
  148. ];
  149. }
  150. $this->table($headers, $rows);
  151. }
  152. /**
  153. * 同步服务
  154. *
  155. * @param array $services
  156. * @return array
  157. */
  158. protected function syncServices(array $services): array
  159. {
  160. $results = [
  161. 'created' => [],
  162. 'updated' => [],
  163. 'skipped' => [],
  164. 'failed' => [],
  165. ];
  166. $progressBar = $this->output->createProgressBar(count($services));
  167. $progressBar->start();
  168. foreach ($services as $serviceConfig) {
  169. try {
  170. $result = $this->syncSingleService($serviceConfig);
  171. $results[$result['action']][] = $result;
  172. $this->line("\n{$result['action']}: {$serviceConfig['name']}");
  173. } catch (\Exception $e) {
  174. $results['failed'][] = [
  175. 'service' => $serviceConfig,
  176. 'error' => $e->getMessage(),
  177. ];
  178. $this->line("\n<fg=red>失败: {$serviceConfig['name']}: {$e->getMessage()}</fg>");
  179. }
  180. $progressBar->advance();
  181. }
  182. $progressBar->finish();
  183. $this->line('');
  184. return $results;
  185. }
  186. /**
  187. * 同步单个服务
  188. *
  189. * @param array $serviceConfig
  190. * @return array
  191. */
  192. protected function syncSingleService(array $serviceConfig): array
  193. {
  194. // 验证配置
  195. $this->validateServiceConfig($serviceConfig);
  196. $existingService = ThirdPartyService::where('code', $serviceConfig['code'])->first();
  197. if ($existingService) {
  198. if ($this->option('force')) {
  199. // 更新现有服务
  200. $existingService->update($serviceConfig);
  201. return [
  202. 'action' => 'updated',
  203. 'service' => $existingService,
  204. ];
  205. } else {
  206. // 跳过现有服务
  207. return [
  208. 'action' => 'skipped',
  209. 'service' => $existingService,
  210. 'reason' => '服务已存在',
  211. ];
  212. }
  213. } else {
  214. // 创建新服务
  215. $service = ThirdPartyService::create($serviceConfig);
  216. return [
  217. 'action' => 'created',
  218. 'service' => $service,
  219. ];
  220. }
  221. }
  222. /**
  223. * 验证服务配置
  224. *
  225. * @param array $config
  226. * @return void
  227. * @throws \Exception
  228. */
  229. protected function validateServiceConfig(array $config): void
  230. {
  231. $required = ['name', 'code', 'type', 'provider', 'auth_type'];
  232. foreach ($required as $field) {
  233. if (empty($config[$field])) {
  234. throw new \Exception("服务配置缺少必填字段: {$field}");
  235. }
  236. }
  237. // 验证服务类型
  238. if (!SERVICE_TYPE::tryFrom($config['type'])) {
  239. throw new \Exception("无效的服务类型: {$config['type']}");
  240. }
  241. // 验证认证类型
  242. if (!AUTH_TYPE::tryFrom($config['auth_type'])) {
  243. throw new \Exception("无效的认证类型: {$config['auth_type']}");
  244. }
  245. // 验证URL格式
  246. if (isset($config['base_url']) && !filter_var($config['base_url'], FILTER_VALIDATE_URL)) {
  247. throw new \Exception("无效的基础URL: {$config['base_url']}");
  248. }
  249. }
  250. /**
  251. * 显示同步结果
  252. *
  253. * @param array $results
  254. * @return void
  255. */
  256. protected function displayResults(array $results): void
  257. {
  258. $this->line(str_repeat('-', 80));
  259. $this->info('同步结果:');
  260. $created = count($results['created']);
  261. $updated = count($results['updated']);
  262. $skipped = count($results['skipped']);
  263. $failed = count($results['failed']);
  264. $this->line("<fg=green>创建: {$created}</fg>");
  265. $this->line("<fg=blue>更新: {$updated}</fg>");
  266. $this->line("<fg=yellow>跳过: {$skipped}</fg>");
  267. $this->line("<fg=red>失败: {$failed}</fg>");
  268. // 显示失败的详情
  269. if ($failed > 0) {
  270. $this->line('');
  271. $this->error('失败的服务:');
  272. foreach ($results['failed'] as $failure) {
  273. $this->line("- {$failure['service']['name']}: {$failure['error']}");
  274. }
  275. }
  276. // 显示跳过的详情
  277. if ($skipped > 0 && !$this->option('force')) {
  278. $this->line('');
  279. $this->warn('跳过的服务 (使用 --force 强制更新):');
  280. foreach ($results['skipped'] as $skipped) {
  281. $this->line("- {$skipped['service']['name']}: {$skipped['reason']}");
  282. }
  283. }
  284. }
  285. /**
  286. * 生成示例配置文件
  287. *
  288. * @return void
  289. */
  290. protected function generateExampleConfig(): void
  291. {
  292. $config = [
  293. 'services' => $this->getDefaultServicesConfig()
  294. ];
  295. $configPath = storage_path('app/thirdparty_services_example.json');
  296. file_put_contents($configPath, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  297. $this->info("示例配置文件已生成: {$configPath}");
  298. }
  299. }