SystemController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Components\PushNotification;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Requests\Admin\SystemRequest;
  6. use App\Models\Config;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\RedirectResponse;
  9. use Response;
  10. class SystemController extends Controller
  11. {
  12. // 系统设置
  13. public function index()
  14. {
  15. return view('admin.config.system', array_merge(['payments' => $this->getPayment(), 'captcha' => $this->getCaptcha()], Config::pluck('value', 'name')->toArray()));
  16. }
  17. private function getPayment()
  18. {
  19. if (sysConfig('f2fpay_app_id') && sysConfig('f2fpay_private_key') && sysConfig('f2fpay_public_key')) {
  20. $payment[] = 'f2fpay';
  21. }
  22. if (sysConfig('codepay_url') && sysConfig('codepay_id') && sysConfig('codepay_key')) {
  23. $payment[] = 'codepay';
  24. }
  25. if (sysConfig('epay_url') && sysConfig('epay_mch_id') && sysConfig('epay_key')) {
  26. $payment[] = 'epay';
  27. }
  28. if (sysConfig('payjs_mch_id') && sysConfig('payjs_key')) {
  29. $payment[] = 'payjs';
  30. }
  31. if (sysConfig('bitpay_secret')) {
  32. $payment[] = 'bitpayx';
  33. }
  34. if (sysConfig('paypal_username') && sysConfig('paypal_password') && sysConfig('paypal_secret')) {
  35. $payment[] = 'paypal';
  36. }
  37. if (sysConfig('stripe_public_key') && sysConfig('stripe_secret_key')) {
  38. $payment[] = 'stripe';
  39. }
  40. if (sysConfig('paybeaver_app_id') && sysConfig('paybeaver_app_secret')) {
  41. $payment[] = 'paybeaver';
  42. }
  43. return $payment ?? [];
  44. }
  45. private function getCaptcha()
  46. {
  47. if (sysConfig('geetest_id') && sysConfig('geetest_key')) {
  48. $captcha[] = '2';
  49. }
  50. if (sysConfig('google_captcha_secret') && sysConfig('google_captcha_sitekey')) {
  51. $captcha[] = '3';
  52. }
  53. if (sysConfig('hcaptcha_secret') && sysConfig('hcaptcha_sitekey')) {
  54. $captcha[] = '4';
  55. }
  56. return $captcha ?? [];
  57. }
  58. // 设置系统扩展信息,例如客服、统计代码
  59. public function setExtend(SystemRequest $request): RedirectResponse
  60. {
  61. if ($request->hasFile('website_home_logo')) {
  62. $validator = validator()->make($request->all(), ['website_home_logo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048']);
  63. if ($validator->fails()) {
  64. return redirect()->route('admin.system.index', '#other')->withErrors($validator->errors());
  65. }
  66. $file = $request->file('website_home_logo');
  67. $ret = $file->move('uploads/logo', $file->getClientOriginalName());
  68. if ($ret && Config::find('website_home_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  69. return redirect()->route('admin.system.index', '#other')->with('successMsg', '更新成功');
  70. }
  71. }
  72. // 站内LOGO
  73. if ($request->hasFile('website_logo')) {
  74. $validator = validator()->make($request->all(), ['website_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_logo');
  79. $ret = $file->move('uploads/logo', $file->getClientOriginalName());
  80. if ($ret && Config::findOrFail('website_logo')->update(['value' => 'uploads/logo/'.$file->getClientOriginalName()])) {
  81. return redirect()->route('admin.system.index', '#other')->with('successMsg', '更新成功');
  82. }
  83. }
  84. return redirect()->route('admin.system.index', '#other')->withErrors('更新失败');
  85. }
  86. // 设置某个配置项
  87. public function setConfig(SystemRequest $request): JsonResponse
  88. {
  89. $name = $request->input('name');
  90. $value = $request->input('value');
  91. // 如果开启用户邮件重置密码,则先设置网站名称和网址
  92. if ($value !== '0' && in_array($name, ['is_reset_password', 'is_activate_account', 'expire_warning', 'traffic_warning'], true)) {
  93. if (! Config::find('website_url')->value) {
  94. return Response::json(['status' => 'fail', 'message' => '设置失败:启用该配置需要先设置【网站名称】']);
  95. }
  96. if (! Config::find('website_url')->value) {
  97. return Response::json(['status' => 'fail', 'message' => '设置失败:启用该配置需要先设置【网站地址】']);
  98. }
  99. }
  100. // 支付设置判断
  101. if ($value !== null && in_array($name, ['is_AliPay', 'is_QQPay', 'is_WeChatPay', 'is_otherPay'], true)) {
  102. switch ($value) {
  103. case 'f2fpay':
  104. if (! sysConfig('f2fpay_app_id') || ! sysConfig('f2fpay_private_key') || ! sysConfig('f2fpay_public_key')) {
  105. return Response::json(['status' => 'fail', 'message' => '请先设置【支付宝F2F】必要参数']);
  106. }
  107. break;
  108. case 'codepay':
  109. if (! sysConfig('codepay_url') || ! sysConfig('codepay_id') || ! sysConfig('codepay_key')) {
  110. return Response::json(['status' => 'fail', 'message' => '请先设置【码支付】必要参数']);
  111. }
  112. break;
  113. case 'epay':
  114. if (! sysConfig('epay_url') || ! sysConfig('epay_mch_id') || ! sysConfig('epay_key')) {
  115. return Response::json(['status' => 'fail', 'message' => '请先设置【易支付】必要参数']);
  116. }
  117. break;
  118. case 'payjs':
  119. if (! sysConfig('payjs_mch_id') || ! sysConfig('payjs_key')) {
  120. return Response::json(['status' => 'fail', 'message' => '请先设置【PayJs】必要参数']);
  121. }
  122. break;
  123. case 'bitpayx':
  124. if (! sysConfig('bitpay_secret')) {
  125. return Response::json(['status' => 'fail', 'message' => '请先设置【麻瓜宝】必要参数']);
  126. }
  127. break;
  128. case 'paypal':
  129. if (! sysConfig('paypal_username') || ! sysConfig('paypal_password') || ! sysConfig('paypal_secret')) {
  130. return Response::json(['status' => 'fail', 'message' => '请先设置【PayPal】必要参数']);
  131. }
  132. break;
  133. case 'stripe':
  134. if (! sysConfig('stripe_public_key') || ! sysConfig('stripe_secret_key')) {
  135. return Response::json(['status' => 'fail', 'message' => '请先设置【Stripe】必要参数']);
  136. }
  137. break;
  138. case 'paybeaver':
  139. if (! sysConfig('paybeaver_app_id') || ! sysConfig('paybeaver_app_secret')) {
  140. return Response::json(['status' => 'fail', 'message' => '请先设置【PayBeaver】必要参数']);
  141. }
  142. break;
  143. default:
  144. return Response::json(['status' => 'fail', 'message' => '未知支付渠道']);
  145. }
  146. }
  147. // 演示环境禁止修改特定配置项
  148. if (config('app.demo')) {
  149. $denyConfig = [
  150. 'website_url',
  151. 'is_captcha',
  152. 'min_rand_traffic',
  153. 'max_rand_traffic',
  154. 'push_bear_send_key',
  155. 'push_bear_qrcode',
  156. 'forbid_mode',
  157. 'website_security_code',
  158. ];
  159. if (in_array($name, $denyConfig, true)) {
  160. return Response::json(['status' => 'fail', 'message' => '演示环境禁止修改该配置']);
  161. }
  162. }
  163. // 如果是返利比例,则需要除100
  164. if ($name === 'referral_percent') {
  165. $value = (int) $value / 100;
  166. }
  167. // 更新配置
  168. if (Config::findOrFail($name)->update(['value' => $value])) {
  169. return Response::json(['status' => 'success', 'message' => trans('common.update_action', ['action' => trans('common.success')])]);
  170. }
  171. return Response::json(['status' => 'fail', 'message' => trans('common.update_action', ['action' => trans('common.failed')])]);
  172. }
  173. // 推送通知测试
  174. public function sendTestNotification(): JsonResponse
  175. {
  176. if (sysConfig('is_notification')) {
  177. $result = PushNotification::send('这是测试的标题', 'ProxyPanel测试内容');
  178. if ($result === false) {
  179. return Response::json(['status' => 'fail', 'message' => '发送失败,请重新尝试!']);
  180. }
  181. switch (sysConfig('is_notification')) {
  182. case 'serverChan':
  183. if (! $result['errno']) {
  184. return Response::json(['status' => 'success', 'message' => '发送成功,请查看手机是否收到推送消息']);
  185. }
  186. return Response::json(['status' => 'fail', 'message' => $result ? $result['errmsg'] : '未知']);
  187. case 'bark':
  188. if ($result['code'] === 200) {
  189. return Response::json(['status' => 'success', 'message' => '发送成功,请查看手机是否收到推送消息']);
  190. }
  191. return Response::json(['status' => 'fail', 'message' => $result['message']]);
  192. default:
  193. }
  194. }
  195. return Response::json(['status' => 'fail', 'message' => '请先选择【日志通知】渠道']);
  196. }
  197. }