SettingServiceProvider.php 3.3 KB

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