SystemController.php 6.2 KB

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