NodeAuthController.php 2.4 KB

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