SystemController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Channels\BarkChannel;
  4. use App\Channels\PushPlusChannel;
  5. use App\Channels\ServerChanChannel;
  6. use App\Channels\TgChatChannel;
  7. use App\Channels\WeChatChannel;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Requests\Admin\SystemRequest;
  10. use App\Models\Config;
  11. use App\Notifications\Custom;
  12. use App\Services\TelegramService;
  13. use Auth;
  14. use Illuminate\Http\JsonResponse;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Notification;
  18. use NotificationChannels\Telegram\TelegramChannel;
  19. use Response;
  20. class SystemController extends Controller
  21. {
  22. // 系统设置
  23. public function index()
  24. {
  25. return view('admin.config.system', array_merge(['payments' => $this->getPayment(), 'captcha' => $this->getCaptcha()], Config::pluck('value', 'name')->toArray()));
  26. }
  27. private function getPayment() // 获取已经完成配置的支付渠道
  28. {
  29. if (sysConfig('f2fpay_app_id') && sysConfig('f2fpay_private_key') && sysConfig('f2fpay_public_key')) {
  30. $payment[] = 'f2fpay';
  31. }
  32. if (sysConfig('codepay_url') && sysConfig('codepay_id') && sysConfig('codepay_key')) {
  33. $payment[] = 'codepay';
  34. }
  35. if (sysConfig('epay_url') && sysConfig('epay_mch_id') && sysConfig('epay_key')) {
  36. $payment[] = 'epay';
  37. }
  38. if (sysConfig('payjs_mch_id') && sysConfig('payjs_key')) {
  39. $payment[] = 'payjs';
  40. }
  41. if (sysConfig('bitpay_secret')) {
  42. $payment[] = 'bitpayx';
  43. }
  44. if (sysConfig('paypal_username') && sysConfig('paypal_password') && sysConfig('paypal_secret')) {
  45. $payment[] = 'paypal';
  46. }
  47. if (sysConfig('stripe_public_key') && sysConfig('stripe_secret_key')) {
  48. $payment[] = 'stripe';
  49. }
  50. if (sysConfig('paybeaver_app_id') && sysConfig('paybeaver_app_secret')) {
  51. $payment[] = 'paybeaver';
  52. }
  53. if (sysConfig('theadpay_mchid') && sysConfig('theadpay_key')) {
  54. $payment[] = 'theadpay';
  55. }
  56. return $payment ?? [];
  57. }
  58. private function getCaptcha()
  59. {
  60. return sysConfig('captcha_secret') && sysConfig('captcha_key');
  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. if (empty($value)) { // 关闭 或 空值 自动设NULL,减少系统设置存储
  94. $value = null;
  95. }
  96. // 支付设置判断
  97. if ($value !== null && in_array($name, ['is_AliPay', 'is_QQPay', 'is_WeChatPay', 'is_otherPay'], true) && ! in_array($value, $this->getPayment(), true)) {
  98. return Response::json(['status' => 'fail', 'message' => '请先完善该支付渠道的必要参数!']);
  99. }
  100. if ($value > 1 && $name === 'is_captcha' && ! $this->getCaptcha()) {
  101. return Response::json(['status' => 'fail', 'message' => '请先完善验证码的必要参数!']);
  102. }
  103. // 演示环境禁止修改特定配置项
  104. if (config('app.demo')) {
  105. $denyConfig = [
  106. 'website_url',
  107. 'is_captcha',
  108. 'min_rand_traffic',
  109. 'max_rand_traffic',
  110. 'push_bear_send_key',
  111. 'push_bear_qrcode',
  112. 'forbid_mode',
  113. 'website_security_code',
  114. ];
  115. if (in_array($name, $denyConfig, true)) {
  116. return Response::json(['status' => 'fail', 'message' => '演示环境禁止修改该配置']);
  117. }
  118. }
  119. // 如果是返利比例,则需要除100
  120. if ($name === 'referral_percent') {
  121. $value /= 100;
  122. }
  123. // 设置TG机器人
  124. if ($name === 'telegram_token' && $value) {
  125. $telegramService = new TelegramService($value);
  126. $telegramService->getMe();
  127. $telegramService->setWebhook(rtrim(sysConfig('website_url'), '/').'/api/telegram/webhook?access_token='.md5($value));
  128. }
  129. // 更新配置
  130. if (Config::findOrFail($name)->update(['value' => $value])) {
  131. return Response::json(['status' => 'success', 'message' => trans('common.update_action', ['action' => trans('common.success')])]);
  132. }
  133. return Response::json(['status' => 'fail', 'message' => trans('common.update_action', ['action' => trans('common.failed')])]);
  134. }
  135. public function sendTestNotification(): JsonResponse // 推送通知测试
  136. {
  137. $data = ['这是测试的标题', 'ProxyPanel测试内容'];
  138. switch (request('channel')) {
  139. case 'serverChan':
  140. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [ServerChanChannel::class]);
  141. break;
  142. case 'bark':
  143. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [BarkChannel::class]);
  144. break;
  145. case 'telegram':
  146. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [TelegramChannel::class]);
  147. break;
  148. case 'weChat':
  149. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [WeChatChannel::class]);
  150. break;
  151. case 'tgChat':
  152. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [TgChatChannel::class]);
  153. break;
  154. case 'pushPlus':
  155. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [PushPlusChannel::class]);
  156. break;
  157. default:
  158. return Response::json(['status' => 'fail', 'message' => '未知渠道']);
  159. }
  160. return Response::json(['status' => 'success', 'message' => '发送成功,请查看手机是否收到推送消息']);
  161. }
  162. }