NodeAuthController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Node;
  5. use App\Models\NodeAuth;
  6. use Exception;
  7. use Illuminate\Http\JsonResponse;
  8. use Log;
  9. use Response;
  10. use Str;
  11. class NodeAuthController extends Controller
  12. {
  13. // 节点授权列表
  14. public function index()
  15. {
  16. return view('admin.node.auth', ['authorizations' => NodeAuth::with('node:id,name,type,server,ip,ipv6')->has('node')->orderBy('node_id')->paginate()->appends(request('page'))]);
  17. }
  18. // 添加节点授权
  19. public function store(): JsonResponse
  20. {
  21. $nodes = Node::whereStatus(1)->doesntHave('auth')->orderBy('id')->get();
  22. if ($nodes->isEmpty()) {
  23. return Response::json(['status' => 'success', 'message' => trans('admin.node.auth.empty')]);
  24. }
  25. $nodes->each(function ($node) {
  26. $node->auth()->create(['key' => Str::random(), 'secret' => Str::random(8)]);
  27. });
  28. return Response::json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.generate')])]);
  29. }
  30. // 重置节点授权
  31. public function update(NodeAuth $auth): JsonResponse
  32. {
  33. if ($auth->update(['key' => Str::random(), 'secret' => Str::random(8)])) {
  34. return Response::json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.reset')])]);
  35. }
  36. return Response::json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.reset')])]);
  37. }
  38. // 删除节点授权
  39. public function destroy(NodeAuth $auth): JsonResponse
  40. {
  41. try {
  42. if ($auth->delete()) {
  43. return Response::json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  44. }
  45. } catch (Exception $e) {
  46. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('admin.menu.node.auth')]).': '.$e->getMessage());
  47. return Response::json(['status' => 'fail', 'message' => trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('admin.menu.node.auth')]).', '.$e->getMessage()]);
  48. }
  49. return Response::json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  50. }
  51. }