RouteController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Http\Controllers\Admin\Server;
  3. use App\Http\Requests\Admin\ServerShadowsocksSave;
  4. use App\Http\Requests\Admin\ServerShadowsocksUpdate;
  5. use App\Models\ServerRoute;
  6. use App\Models\ServerShadowsocks;
  7. use App\Services\ServerService;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Controller;
  10. class RouteController extends Controller
  11. {
  12. public function fetch(Request $request)
  13. {
  14. $routes = ServerRoute::get();
  15. // TODO: remove on 1.8.0
  16. foreach ($routes as $k => $route) {
  17. $array = json_decode($route->match, true);
  18. if (is_array($array)) $routes[$k]['match'] = $array;
  19. }
  20. // TODO: remove on 1.8.0
  21. return [
  22. 'data' => $routes
  23. ];
  24. }
  25. public function save(Request $request)
  26. {
  27. $params = $request->validate([
  28. 'remarks' => 'required',
  29. 'match' => 'required|array',
  30. 'action' => 'required|in:block,dns',
  31. 'action_value' => 'nullable'
  32. ], [
  33. 'remarks.required' => '备注不能为空',
  34. 'match.required' => '匹配值不能为空',
  35. 'action.required' => '动作类型不能为空',
  36. 'action.in' => '动作类型参数有误'
  37. ]);
  38. $params['match'] = array_filter($params['match']);
  39. // TODO: remove on 1.8.0
  40. $params['match'] = json_encode($params['match']);
  41. // TODO: remove on 1.8.0
  42. if ($request->input('id')) {
  43. try {
  44. $route = ServerRoute::find($request->input('id'));
  45. $route->update($params);
  46. return [
  47. 'data' => true
  48. ];
  49. } catch (\Exception $e) {
  50. abort(500, '保存失败');
  51. }
  52. }
  53. if (!ServerRoute::create($params)) abort(500, '创建失败');
  54. return [
  55. 'data' => true
  56. ];
  57. }
  58. public function drop(Request $request)
  59. {
  60. $route = ServerRoute::find($request->input('id'));
  61. if (!$route) abort(500, '路由不存在');
  62. if (!$route->delete()) abort(500, '删除失败');
  63. return [
  64. 'data' => true
  65. ];
  66. }
  67. }