1
0

SystemController.php 10.0 KB

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