SystemController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Channels\BarkChannel;
  4. use App\Channels\ServerChanChannel;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Requests\Admin\SystemRequest;
  7. use App\Models\Config;
  8. use App\Notifications\Custom;
  9. use App\Services\TelegramService;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Notification;
  14. use NotificationChannels\Telegram\TelegramChannel;
  15. use Response;
  16. class SystemController extends Controller
  17. {
  18. // 系统设置
  19. public function index()
  20. {
  21. return view('admin.config.system', array_merge(['payments' => $this->getPayment(), 'captcha' => $this->getCaptcha()], Config::pluck('value', 'name')->toArray()));
  22. }
  23. private function getPayment() // 获取已经完成配置的支付渠道
  24. {
  25. if (sysConfig('f2fpay_app_id') && sysConfig('f2fpay_private_key') && sysConfig('f2fpay_public_key')) {
  26. $payment[] = 'f2fpay';
  27. }
  28. if (sysConfig('codepay_url') && sysConfig('codepay_id') && sysConfig('codepay_key')) {
  29. $payment[] = 'codepay';
  30. }
  31. if (sysConfig('epay_url') && sysConfig('epay_mch_id') && sysConfig('epay_key')) {
  32. $payment[] = 'epay';
  33. }
  34. if (sysConfig('payjs_mch_id') && sysConfig('payjs_key')) {
  35. $payment[] = 'payjs';
  36. }
  37. if (sysConfig('bitpay_secret')) {
  38. $payment[] = 'bitpayx';
  39. }
  40. if (sysConfig('paypal_username') && sysConfig('paypal_password') && sysConfig('paypal_secret')) {
  41. $payment[] = 'paypal';
  42. }
  43. if (sysConfig('stripe_public_key') && sysConfig('stripe_secret_key')) {
  44. $payment[] = 'stripe';
  45. }
  46. if (sysConfig('paybeaver_app_id') && sysConfig('paybeaver_app_secret')) {
  47. $payment[] = 'paybeaver';
  48. }
  49. if (sysConfig('theadpay_mchid') && sysConfig('theadpay_key')) {
  50. $payment[] = 'theadpay';
  51. }
  52. return $payment ?? [];
  53. }
  54. private function getCaptcha()
  55. {
  56. return sysConfig('captcha_secret') && sysConfig('captcha_key');
  57. }
  58. public function setExtend(Request $request): RedirectResponse // 设置系统扩展信息,例如客服、统计代码
  59. {
  60. if ($request->hasFile('website_home_logo')) {
  61. $validator = validator()->make($request->all(), ['website_home_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  62. if ($validator->fails()) {
  63. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  64. }
  65. $file = $request->file('website_home_logo');
  66. $ret = $file->move('uploads/logo', $file->getClientOriginalName());
  67. if ($ret && Config::find('website_home_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  68. return redirect()->route('admin.system.index', '#other')->with('successMsg', '更新成功');
  69. }
  70. }
  71. // 站内LOGO
  72. if ($request->hasFile('website_logo')) {
  73. $validator = validator()->make($request->all(), ['website_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  74. if ($validator->fails()) {
  75. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  76. }
  77. $file = $request->file('website_logo');
  78. $ret = $file->move('uploads/logo', $file->getClientOriginalName());
  79. if ($ret && Config::findOrFail('website_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  80. return redirect()->route('admin.system.index', '#other')->with('successMsg', '更新成功');
  81. }
  82. }
  83. return redirect()->route('admin.system.index', '#other')->withErrors('更新失败');
  84. }
  85. public function setConfig(SystemRequest $request): JsonResponse // 设置某个配置项
  86. {
  87. $name = $request->input('name');
  88. $value = $request->input('value');
  89. if (empty($value)) { // 关闭 或 空值 自动设NULL,减少系统设置存储
  90. $value = null;
  91. }
  92. // 支付设置判断
  93. if ($value !== null && in_array($name, ['is_AliPay', 'is_QQPay', 'is_WeChatPay', 'is_otherPay'], true) && ! in_array($value, $this->getPayment(), true)) {
  94. return Response::json(['status' => 'fail', 'message' => '请先完善该支付渠道的必要参数!']);
  95. }
  96. if ($value > 1 && $name === 'is_captcha' && ! $this->getCaptcha()) {
  97. return Response::json(['status' => 'fail', 'message' => '请先完善验证码的必要参数!']);
  98. }
  99. // 演示环境禁止修改特定配置项
  100. if (config('app.demo')) {
  101. $denyConfig = [
  102. 'website_url',
  103. 'is_captcha',
  104. 'min_rand_traffic',
  105. 'max_rand_traffic',
  106. 'push_bear_send_key',
  107. 'push_bear_qrcode',
  108. 'forbid_mode',
  109. 'website_security_code',
  110. ];
  111. if (in_array($name, $denyConfig, true)) {
  112. return Response::json(['status' => 'fail', 'message' => '演示环境禁止修改该配置']);
  113. }
  114. }
  115. // 如果是返利比例,则需要除100
  116. if ($name === 'referral_percent') {
  117. $value /= 100;
  118. }
  119. // 设置TG机器人
  120. if ($name === 'telegram_token' && $value) {
  121. $telegramService = new TelegramService($value);
  122. $telegramService->getMe();
  123. $telegramService->setWebhook(rtrim(sysConfig('website_url'), '/').'/api/telegram/webhook?access_token='.md5($value));
  124. }
  125. // 更新配置
  126. if (Config::findOrFail($name)->update(['value' => $value])) {
  127. return Response::json(['status' => 'success', 'message' => trans('common.update_action', ['action' => trans('common.success')])]);
  128. }
  129. return Response::json(['status' => 'fail', 'message' => trans('common.update_action', ['action' => trans('common.failed')])]);
  130. }
  131. public function sendTestNotification(): JsonResponse // 推送通知测试
  132. {
  133. switch (request('channel')) {
  134. case 'serverChan':
  135. Notification::sendNow(ServerChanChannel::class, new Custom('这是测试的标题', 'ProxyPanel测试内容'));
  136. break;
  137. case 'bark':
  138. Notification::sendNow(BarkChannel::class, new Custom('这是测试的标题', 'ProxyPanel测试内容'));
  139. break;
  140. case 'telegram':
  141. Notification::sendNow(TelegramChannel::class, new Custom('这是测试的标题', 'ProxyPanel测试内容'));
  142. break;
  143. default:
  144. return Response::json(['status' => 'fail', 'message' => '未知渠道']);
  145. }
  146. return Response::json(['status' => 'success', 'message' => '发送成功,请查看手机是否收到推送消息']);
  147. }
  148. }