SettingServiceProvider.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. 'geetest_id' => 'geetest.id',
  16. 'geetest_key' => 'geetest.key',
  17. 'google_captcha_secret' => 'NoCaptcha.secret',
  18. 'google_captcha_sitekey' => 'NoCaptcha.sitekey',
  19. 'hcaptcha_secret' => 'HCaptcha.secret',
  20. 'hcaptcha_sitekey' => 'HCaptcha.sitekey',
  21. ]);
  22. $notifications = collect([
  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', $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', $payments)->pluck('value')->filter()->isNotEmpty()])) // 设置在线支付开关
  47. ->sortKeys()
  48. ->toArray();
  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. foreach (
  63. [
  64. 'telegram' => TelegramChannel::class,
  65. 'beary' => BearyChatChannel::class,
  66. 'bark' => BarkChannel::class,
  67. 'serverChan' => ServerChanChannel::class,
  68. ] as $key => $channel
  69. ) {
  70. if (($index = array_search($key, $channels, true)) && $index !== false) {
  71. $channels[$index] = $channel;
  72. }
  73. }
  74. return $channels;
  75. }
  76. }