SettingServiceProvider.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Providers;
  3. use App\Channels\BarkChannel;
  4. use App\Channels\ServerChanChannel;
  5. use App\Models\Config;
  6. use Illuminate\Support\ServiceProvider;
  7. use NotificationChannels\BearyChat\BearyChatChannel;
  8. use NotificationChannels\Telegram\TelegramChannel;
  9. class SettingServiceProvider extends ServiceProvider
  10. {
  11. /**
  12. * Bootstrap services.
  13. *
  14. * @return void
  15. */
  16. public function boot()
  17. {
  18. $toApp = collect([
  19. 'geetest_id' => 'geetest.id',
  20. 'geetest_key' => 'geetest.key',
  21. 'google_captcha_secret' => 'NoCaptcha.secret',
  22. 'google_captcha_sitekey' => 'NoCaptcha.sitekey',
  23. 'hcaptcha_secret' => 'HCaptcha.secret',
  24. 'hcaptcha_sitekey' => 'HCaptcha.sitekey',
  25. ]);
  26. $notifications = collect([
  27. 'account_expire_notification',
  28. 'data_anomaly_notification',
  29. 'data_exhaust_notification',
  30. 'node_blocked_notification',
  31. 'node_daily_notification',
  32. 'node_offline_notification',
  33. 'password_reset_notification',
  34. 'payment_received_notification',
  35. 'ticket_closed_notification',
  36. 'ticket_created_notification',
  37. 'ticket_replied_notification',
  38. ]);
  39. $settings = Config::all();
  40. $modified = $settings
  41. ->whereNotIn('name', $toApp->keys()->merge($notifications)) // 设置一般系统选项
  42. ->pluck('value', 'name')
  43. ->merge($settings->whereIn('name', $notifications)->pluck('value', 'name')->map(function ($item) {
  44. return self::setChannel(json_decode($item, true) ?? []);
  45. })) // 设置通知相关选项
  46. ->merge(collect(['is_onlinePay' => $settings->whereIn('name', ['is_AliPay', 'is_QQPay', 'is_WeChatPay', 'is_otherPay'])->pluck('value')->filter()->isNotEmpty(),
  47. ])) // 设置在线支付开关
  48. ->sortKeys();
  49. config(['settings' => $modified]); // 设置系统参数
  50. $settings->whereIn('name', $toApp->keys())->pluck('value', 'name')->each(function ($item, $key) use ($toApp) {
  51. config([$toApp[$key] => $item]); // 设置PHP软件包相关配置
  52. });
  53. collect([
  54. 'website_name' => 'app.name',
  55. 'website_url' => 'app.url',
  56. ])->each(function ($item, $key) {
  57. config([$item => config('settings.'.$key)]); // 设置APP有关的选项
  58. });
  59. }
  60. private static function setChannel(array $channels)
  61. {
  62. $options = [
  63. 'telegram' => TelegramChannel::class,
  64. 'beary' => BearyChatChannel::class,
  65. 'bark' => BarkChannel::class,
  66. 'serverChan' => ServerChanChannel::class,
  67. ];
  68. foreach ($options as $option => $str) {
  69. if (($key = array_search($option, $channels, true)) !== false) {
  70. $channels[$key] = $str;
  71. }
  72. }
  73. return $channels;
  74. }
  75. }