SystemController.php 6.9 KB

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