SettingServiceProvider.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_confirm_notification',
  31. 'payment_received_notification',
  32. 'ticket_closed_notification',
  33. 'ticket_created_notification',
  34. 'ticket_replied_notification',
  35. ];
  36. $payments = ['is_AliPay', 'is_QQPay', 'is_WeChatPay', 'is_otherPay'];
  37. if (! Cache::has('settings')) {
  38. Cache::forever('settings', Config::whereNotNull('value')->get());
  39. }
  40. $settings = Cache::get('settings');
  41. $modified = $settings
  42. ->whereNotIn('name', $notifications) // 设置一般系统选项
  43. ->pluck('value', 'name')
  44. ->merge($settings->whereIn('name', $notifications)->pluck('value', 'name')->map(function ($item) {
  45. return self::setChannel(json_decode($item, true) ?? (is_array($item) ? $item : [$item])); // 设置通知相关选项
  46. }))
  47. ->merge(collect(['is_onlinePay' => $settings->whereIn('name', $payments)->pluck('value')->filter()->isNotEmpty()])) // 设置在线支付开关
  48. ->sortKeys()
  49. ->toArray();
  50. config(['settings' => $modified]); // 设置系统参数
  51. if (config('settings.is_captcha') > 1) {
  52. config([$toApp[config('settings.is_captcha')][0] => config('settings.captcha_secret')]);
  53. config([$toApp[config('settings.is_captcha')][1] => config('settings.captcha_key')]);
  54. }
  55. collect([
  56. 'website_name' => 'app.name',
  57. 'website_url' => 'app.url',
  58. ])->each(function ($item, $key) {
  59. config([$item => config('settings.'.$key)]); // 设置APP有关的选项
  60. });
  61. }
  62. private static function setChannel(array $channels)
  63. {
  64. foreach (
  65. [
  66. 'telegram' => TelegramChannel::class,
  67. 'beary' => BearyChatChannel::class,
  68. 'bark' => BarkChannel::class,
  69. 'pushPlus' => PushPlusChannel::class,
  70. 'serverChan' => ServerChanChannel::class,
  71. 'tgChat' => TgChatChannel::class,
  72. 'weChat' => WeChatChannel::class,
  73. ] as $key => $channel
  74. ) {
  75. $index = array_search($key, $channels, true);
  76. if ($index !== false) {
  77. $channels[$index] = $channel;
  78. }
  79. }
  80. return $channels;
  81. }
  82. }