SettingServiceProvider.php 3.0 KB

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