SystemController.php 9.5 KB

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