SystemController.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Channels\BarkChannel;
  4. use App\Channels\DingTalkChannel;
  5. use App\Channels\iYuuChannel;
  6. use App\Channels\PushDeerChannel;
  7. use App\Channels\PushPlusChannel;
  8. use App\Channels\ServerChanChannel;
  9. use App\Channels\TgChatChannel;
  10. use App\Channels\WeChatChannel;
  11. use App\Http\Controllers\Controller;
  12. use App\Http\Requests\Admin\SystemRequest;
  13. use App\Models\Config;
  14. use App\Notifications\Custom;
  15. use App\Services\TelegramService;
  16. use Auth;
  17. use Illuminate\Http\JsonResponse;
  18. use Illuminate\Http\RedirectResponse;
  19. use Illuminate\Http\Request;
  20. use Notification;
  21. use NotificationChannels\Telegram\TelegramChannel;
  22. use Response;
  23. class SystemController extends Controller
  24. {
  25. // 系统设置
  26. public function index()
  27. {
  28. return view('admin.config.system', array_merge(['payments' => $this->getPayment(), 'captcha' => $this->getCaptcha()], Config::pluck('value', 'name')->toArray()));
  29. }
  30. private function getPayment(): array
  31. { // 获取已经完成配置的支付渠道
  32. if (sysConfig('f2fpay_app_id') && sysConfig('f2fpay_private_key') && sysConfig('f2fpay_public_key')) {
  33. $payment[] = 'f2fpay';
  34. }
  35. if (sysConfig('codepay_url') && sysConfig('codepay_id') && sysConfig('codepay_key')) {
  36. $payment[] = 'codepay';
  37. }
  38. if (sysConfig('epay_url') && sysConfig('epay_mch_id') && sysConfig('epay_key')) {
  39. $payment[] = 'epay';
  40. }
  41. if (sysConfig('payjs_mch_id') && sysConfig('payjs_key')) {
  42. $payment[] = 'payjs';
  43. }
  44. if (sysConfig('bitpay_secret')) {
  45. $payment[] = 'bitpayx';
  46. }
  47. if (sysConfig('paypal_client_id') && sysConfig('paypal_client_secret') && sysConfig('paypal_app_id')) {
  48. $payment[] = 'paypal';
  49. }
  50. if (sysConfig('stripe_public_key') && sysConfig('stripe_secret_key')) {
  51. $payment[] = 'stripe';
  52. }
  53. if (sysConfig('paybeaver_app_id') && sysConfig('paybeaver_app_secret')) {
  54. $payment[] = 'paybeaver';
  55. }
  56. if (sysConfig('theadpay_mchid') && sysConfig('theadpay_key')) {
  57. $payment[] = 'theadpay';
  58. }
  59. return $payment ?? [];
  60. }
  61. private function getCaptcha(): bool
  62. {
  63. return sysConfig('captcha_secret') && sysConfig('captcha_key');
  64. }
  65. public function setExtend(Request $request): RedirectResponse // 设置涉及到上传的设置
  66. {
  67. if ($request->hasAny(['website_home_logo', 'website_home_logo'])) { // 首页LOGO
  68. if ($request->hasFile('website_home_logo')) {
  69. $validator = validator()->make($request->all(), ['website_home_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  70. if ($validator->fails()) {
  71. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  72. }
  73. $file = $request->file('website_home_logo');
  74. $file->move('uploads/logo', $file->getClientOriginalName());
  75. if (Config::find('website_home_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  76. return redirect()->route('admin.system.index', '#other')->with('successMsg', '更新成功');
  77. }
  78. }
  79. if ($request->hasFile('website_logo')) { // 站内LOGO
  80. $validator = validator()->make($request->all(), ['website_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  81. if ($validator->fails()) {
  82. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  83. }
  84. $file = $request->file('website_logo');
  85. $file->move('uploads/logo', $file->getClientOriginalName());
  86. if (Config::findOrFail('website_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  87. return redirect()->route('admin.system.index', '#other')->with('successMsg', '更新成功');
  88. }
  89. }
  90. return redirect()->route('admin.system.index', '#other')->withErrors('更新失败');
  91. }
  92. if ($request->hasAny(['alipay_qrcode', 'wechat_qrcode'])) {
  93. if ($request->hasFile('alipay_qrcode')) {
  94. $validator = validator()->make($request->all(), ['alipay_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  95. if ($validator->fails()) {
  96. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  97. }
  98. $file = $request->file('alipay_qrcode');
  99. $file->move('uploads/images', $file->getClientOriginalName());
  100. if (Config::find('alipay_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  101. return redirect()->route('admin.system.index', '#payment')->with('successMsg', '更新成功');
  102. }
  103. }
  104. if ($request->hasFile('wechat_qrcode')) { // 站内LOGO
  105. $validator = validator()->make($request->all(), ['wechat_qrcode' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  106. if ($validator->fails()) {
  107. return redirect()->route('admin.system.index', '#payment')->withErrors($validator->errors());
  108. }
  109. $file = $request->file('wechat_qrcode');
  110. $file->move('uploads/images', $file->getClientOriginalName());
  111. if (Config::findOrFail('wechat_qrcode')->update(['value' => 'uploads/images/'.$file->getClientOriginalName()])) {
  112. return redirect()->route('admin.system.index', '#payment')->with('successMsg', '更新成功');
  113. }
  114. }
  115. return redirect()->route('admin.system.index', '#payment')->withErrors('更新失败');
  116. }
  117. return redirect()->route('admin.system.index');
  118. }
  119. public function setConfig(SystemRequest $request): JsonResponse // 设置某个配置项
  120. {
  121. $name = $request->input('name');
  122. $value = $request->input('value');
  123. if (empty($value) || $value === 'NaN') { // 关闭 或 空值 自动设NULL,减少系统设置存储
  124. $value = null;
  125. }
  126. // 支付设置判断
  127. if ($value !== null && in_array($name, ['is_AliPay', 'is_QQPay', 'is_WeChatPay'], true) && ! in_array($value, $this->getPayment(), true)) {
  128. return Response::json(['status' => 'fail', 'message' => '请先完善该支付渠道的必要参数!']);
  129. }
  130. if ($value > 1 && $name === 'is_captcha' && ! $this->getCaptcha()) {
  131. return Response::json(['status' => 'fail', 'message' => '请先完善验证码的必要参数!']);
  132. }
  133. // 演示环境禁止修改特定配置项
  134. if (config('app.demo')) {
  135. $denyConfig = [
  136. 'website_url',
  137. 'is_captcha',
  138. 'min_rand_traffic',
  139. 'max_rand_traffic',
  140. 'forbid_mode',
  141. 'website_security_code',
  142. 'website_security_code',
  143. 'username_type',
  144. ];
  145. if (in_array($name, $denyConfig, true)) {
  146. return Response::json(['status' => 'fail', 'message' => '演示环境禁止修改该配置']);
  147. }
  148. }
  149. // 如果是返利比例,则需要除100
  150. if ($name === 'referral_percent') {
  151. $value /= 100;
  152. }
  153. // 设置TG机器人
  154. if ($name === 'telegram_token' && $value) {
  155. $telegramService = new TelegramService($value);
  156. $telegramService->getMe();
  157. $telegramService->setWebhook(rtrim(sysConfig('website_url'), '/').'/api/telegram/webhook?access_token='.md5($value));
  158. }
  159. // 更新配置
  160. if (Config::findOrFail($name)->update(['value' => $value])) {
  161. return Response::json(['status' => 'success', 'message' => trans('common.update_action', ['action' => trans('common.success')])]);
  162. }
  163. return Response::json(['status' => 'fail', 'message' => trans('common.update_action', ['action' => trans('common.failed')])]);
  164. }
  165. public function sendTestNotification(): JsonResponse // 推送通知测试
  166. {
  167. $data = ['这是测试的标题', 'ProxyPanel测试内容'];
  168. switch (request('channel')) {
  169. case 'serverChan':
  170. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [ServerChanChannel::class]);
  171. break;
  172. case 'bark':
  173. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [BarkChannel::class]);
  174. break;
  175. case 'telegram':
  176. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [TelegramChannel::class]);
  177. break;
  178. case 'weChat':
  179. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [WeChatChannel::class]);
  180. break;
  181. case 'tgChat':
  182. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [TgChatChannel::class]);
  183. break;
  184. case 'pushPlus':
  185. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [PushPlusChannel::class]);
  186. break;
  187. case 'iYuu':
  188. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [iYuuChannel::class]);
  189. break;
  190. case 'pushDeer':
  191. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [PushDeerChannel::class]);
  192. break;
  193. case 'dingTalk':
  194. Notification::sendNow(Auth::getUser(), new Custom($data[0], $data[1]), [DingTalkChannel::class]);
  195. break;
  196. default:
  197. return Response::json(['status' => 'fail', 'message' => '未知渠道']);
  198. }
  199. return Response::json(['status' => 'success', 'message' => '发送成功,请查看手机是否收到推送消息']);
  200. }
  201. }