SettingServiceProvider.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Providers;
  3. use App\Channels\BarkChannel;
  4. use App\Channels\DingTalkChannel;
  5. use App\Channels\iYuuChannel;
  6. use App\Channels\PushDeerChannel;
  7. use App\Channels\PushPlusChannel;
  8. use App\Channels\ServerChanChannel;
  9. use App\Channels\TgChatChannel;
  10. use App\Channels\WeChatChannel;
  11. use App\Models\Config;
  12. use Cache;
  13. use Illuminate\Support\ServiceProvider;
  14. use NotificationChannels\Telegram\TelegramChannel;
  15. class SettingServiceProvider extends ServiceProvider
  16. {
  17. public function boot(): void
  18. {
  19. $toApp = collect([
  20. 2 => ['geetest.id', 'geetest.key'],
  21. 3 => ['NoCaptcha.secret', 'NoCaptcha.sitekey'],
  22. 4 => ['HCaptcha.secret', 'HCaptcha.sitekey'],
  23. 5 => ['turnstile.secret_key', 'turnstile.site_key'],
  24. ]);
  25. $notifications = [
  26. 'account_expire_notification',
  27. 'data_anomaly_notification',
  28. 'data_exhaust_notification',
  29. 'node_blocked_notification',
  30. 'node_daily_notification',
  31. 'node_offline_notification',
  32. 'node_renewal_notification',
  33. 'password_reset_notification',
  34. 'payment_confirm_notification',
  35. 'payment_received_notification',
  36. 'ticket_closed_notification',
  37. 'ticket_created_notification',
  38. 'ticket_replied_notification',
  39. ];
  40. $payments = ['is_AliPay', 'is_QQPay', 'is_WeChatPay', 'is_otherPay'];
  41. if (! Cache::has('settings')) {
  42. Cache::forever('settings', Config::whereNotNull('value')->get());
  43. }
  44. $settings = Cache::get('settings');
  45. $modified = $settings
  46. ->whereNotIn('name', $notifications) // 设置一般系统选项
  47. ->pluck('value', 'name')
  48. ->merge($settings->whereIn('name', $notifications)->pluck('value', 'name')->map(function ($item) {
  49. return self::setChannel(json_decode($item, true) ?? (is_array($item) ? $item : [$item])); // 设置通知相关选项
  50. }))
  51. ->merge(collect(['is_onlinePay' => $settings->whereIn('name', $payments)->pluck('value')->filter()->isNotEmpty()])) // 设置在线支付开关
  52. ->sortKeys()
  53. ->toArray();
  54. $modified['tasks_clean'] = json_decode($modified['tasks_clean'], true);
  55. $modified['tasks_close'] = json_decode($modified['tasks_close'], true);
  56. config(['settings' => $modified]); // 设置系统参数
  57. if (config('settings.is_captcha') > 1) {
  58. config([$toApp[config('settings.is_captcha')][0] => config('settings.captcha_secret')]);
  59. config([$toApp[config('settings.is_captcha')][1] => config('settings.captcha_key')]);
  60. }
  61. collect(['website_name' => 'app.name', 'website_url' => 'app.url'])->each(fn ($item, $key) => config([$item => config('settings.'.$key)]));
  62. }
  63. private static function setChannel(array $channels): array
  64. {
  65. foreach (
  66. [
  67. 'telegram' => TelegramChannel::class,
  68. 'bark' => BarkChannel::class,
  69. 'pushPlus' => PushPlusChannel::class,
  70. 'serverChan' => ServerChanChannel::class,
  71. 'tgChat' => TgChatChannel::class,
  72. 'weChat' => WeChatChannel::class,
  73. 'iYuu' => iYuuChannel::class,
  74. 'pushDear' => PushDeerChannel::class,
  75. 'dingTalk' => DingTalkChannel::class,
  76. ] as $key => $channel
  77. ) {
  78. $index = array_search($key, $channels, true);
  79. if ($index !== false) {
  80. $channels[$index] = $channel;
  81. }
  82. }
  83. return $channels;
  84. }
  85. }