SystemController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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->hasAny(['website_home_logo', 'website_home_logo'])) { // 首页LOGO
  65. if ($request->hasFile('website_home_logo')) {
  66. $validator = validator()->make($request->all(), ['website_home_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  67. if ($validator->fails()) {
  68. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  69. }
  70. $file = $request->file('website_home_logo');
  71. $ret = $file->move('uploads/logo', $file->getClientOriginalName());
  72. if ($ret && Config::find('website_home_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  73. return redirect()->route('admin.system.index', '#other')->with('successMsg', '更新成功');
  74. }
  75. }
  76. if ($request->hasFile('website_logo')) { // 站内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. if ($request->hasAny(['alipay_qrcode', 'wechat_qrcode'])) {
  90. if ($request->hasFile('alipay_qrcode')) {
  91. $validator = validator()->make($request->all(), ['alipay_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  92. if ($validator->fails()) {
  93. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  94. }
  95. $file = $request->file('alipay_qrcode');
  96. $ret = $file->move('uploads/images', $file->getClientOriginalName());
  97. if ($ret && Config::find('alipay_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  98. return redirect()->route('admin.system.index', '#payment')->with('successMsg', '更新成功');
  99. }
  100. }
  101. if ($request->hasFile('wechat_qrcode')) { // 站内LOGO
  102. $validator = validator()->make($request->all(), ['wechat_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  103. if ($validator->fails()) {
  104. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  105. }
  106. $file = $request->file('wechat_qrcode');
  107. $ret = $file->move('uploads/images', $file->getClientOriginalName());
  108. if ($ret && Config::findOrFail('wechat_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  109. return redirect()->route('admin.system.index', '#payment')->with('successMsg', '更新成功');
  110. }
  111. }
  112. return redirect()->route('admin.system.index', '#payment')->withErrors('更新失败');
  113. }
  114. return redirect()->route('admin.system.index');
  115. }
  116. public function setConfig(SystemRequest $request): JsonResponse // 设置某个配置项
  117. {
  118. $name = $request->input('name');
  119. $value = $request->input('value');
  120. if (empty($value) || $value === 'NaN') { // 关闭 或 空值 自动设NULL,减少系统设置存储
  121. $value = null;
  122. }
  123. // 支付设置判断
  124. if ($value !== null && in_array($name, ['is_AliPay', 'is_QQPay', 'is_WeChatPay'], true) && ! in_array($value, $this->getPayment(), true)) {
  125. return Response::json(['status' => 'fail', 'message' => '请先完善该支付渠道的必要参数!']);
  126. }
  127. if ($value > 1 && $name === 'is_captcha' && ! $this->getCaptcha()) {
  128. return Response::json(['status' => 'fail', 'message' => '请先完善验证码的必要参数!']);
  129. }
  130. // 演示环境禁止修改特定配置项
  131. if (config('app.demo')) {
  132. $denyConfig = [
  133. 'website_url',
  134. 'is_captcha',
  135. 'min_rand_traffic',
  136. 'max_rand_traffic',
  137. 'push_bear_send_key',
  138. 'push_bear_qrcode',
  139. 'forbid_mode',
  140. 'website_security_code',
  141. ];
  142. if (in_array($name, $denyConfig, true)) {
  143. return Response::json(['status' => 'fail', 'message' => '演示环境禁止修改该配置']);
  144. }
  145. }
  146. // 如果是返利比例,则需要除100
  147. if ($name === 'referral_percent') {
  148. $value /= 100;
  149. }
  150. // 设置TG机器人
  151. if ($name === 'telegram_token' && $value) {
  152. $telegramService = new TelegramService($value);
  153. $telegramService->getMe();
  154. $telegramService->setWebhook(rtrim(sysConfig('website_url'), '/').'/api/telegram/webhook?access_token='.md5($value));
  155. }
  156. // 更新配置
  157. if (Config::findOrFail($name)->update(['value' => $value])) {
  158. return Response::json(['status' => 'success', 'message' => trans('common.update_action', ['action' => trans('common.success')])]);
  159. }
  160. return Response::json(['status' => 'fail', 'message' => trans('common.update_action', ['action' => trans('common.failed')])]);
  161. }
  162. public function sendTestNotification(): JsonResponse // 推送通知测试
  163. {
  164. $data = ['这是测试的标题', 'ProxyPanel测试内容'];
  165. switch (request('channel')) {
  166. case 'serverChan':
  167. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [ServerChanChannel::class]);
  168. break;
  169. case 'bark':
  170. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [BarkChannel::class]);
  171. break;
  172. case 'telegram':
  173. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [TelegramChannel::class]);
  174. break;
  175. case 'weChat':
  176. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [WeChatChannel::class]);
  177. break;
  178. case 'tgChat':
  179. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [TgChatChannel::class]);
  180. break;
  181. case 'pushPlus':
  182. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [PushPlusChannel::class]);
  183. break;
  184. default:
  185. return Response::json(['status' => 'fail', 'message' => '未知渠道']);
  186. }
  187. return Response::json(['status' => 'success', 'message' => '发送成功,请查看手机是否收到推送消息']);
  188. }
  189. }