SystemController.php 10 KB

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