SettingServiceProvider.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\BearyChat\BearyChatChannel;
  15. use NotificationChannels\Telegram\TelegramChannel;
  16. class SettingServiceProvider extends ServiceProvider
  17. {
  18. public function boot()
  19. {
  20. $toApp = collect([
  21. 2 => ['geetest.id', 'geetest.key'],
  22. 3 => ['NoCaptcha.secret', 'NoCaptcha.sitekey'],
  23. 4 => ['HCaptcha.secret', 'HCaptcha.sitekey'],
  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. 'password_reset_notification',
  33. 'payment_confirm_notification',
  34. 'payment_received_notification',
  35. 'ticket_closed_notification',
  36. 'ticket_created_notification',
  37. 'ticket_replied_notification',
  38. ];
  39. $payments = ['is_AliPay', 'is_QQPay', 'is_WeChatPay', 'is_otherPay'];
  40. if (! Cache::has('settings')) {
  41. Cache::forever('settings', Config::whereNotNull('value')->get());
  42. }
  43. $settings = Cache::get('settings');
  44. $modified = $settings
  45. ->whereNotIn('name', $notifications) // 设置一般系统选项
  46. ->pluck('value', 'name')
  47. ->merge($settings->whereIn('name', $notifications)->pluck('value', 'name')->map(function ($item) {
  48. return self::setChannel(json_decode($item, true) ?? (is_array($item) ? $item : [$item])); // 设置通知相关选项
  49. }))
  50. ->merge(collect(['is_onlinePay' => $settings->whereIn('name', $payments)->pluck('value')->filter()->isNotEmpty()])) // 设置在线支付开关
  51. ->sortKeys()
  52. ->toArray();
  53. config(['settings' => $modified]); // 设置系统参数
  54. if (config('settings.is_captcha') > 1) {
  55. config([$toApp[config('settings.is_captcha')][0] => config('settings.captcha_secret')]);
  56. config([$toApp[config('settings.is_captcha')][1] => config('settings.captcha_key')]);
  57. }
  58. collect([
  59. 'website_name' => 'app.name',
  60. 'website_url' => 'app.url',
  61. ])->each(function ($item, $key) {
  62. config([$item => config('settings.'.$key)]); // 设置APP有关的选项
  63. });
  64. }
  65. private static function setChannel(array $channels)
  66. {
  67. foreach (
  68. [
  69. 'telegram' => TelegramChannel::class,
  70. 'beary' => BearyChatChannel::class,
  71. 'bark' => BarkChannel::class,
  72. 'pushPlus' => PushPlusChannel::class,
  73. 'serverChan' => ServerChanChannel::class,
  74. 'tgChat' => TgChatChannel::class,
  75. 'weChat' => WeChatChannel::class,
  76. 'iYuu' => iYuuChannel::class,
  77. 'pushDear' => PushDeerChannel::class,
  78. 'dingTalk' => DingTalkChannel::class,
  79. ] as $key => $channel
  80. ) {
  81. $index = array_search($key, $channels, true);
  82. if ($index !== false) {
  83. $channels[$index] = $channel;
  84. }
  85. }
  86. return $channels;
  87. }
  88. }