NodeAuthController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 Response;
  9. use Str;
  10. class NodeAuthController extends Controller
  11. {
  12. // 节点授权列表
  13. public function index()
  14. {
  15. return view('admin.node.auth', ['authorizations' => NodeAuth::with('node')->orderBy('node_id')->paginate()->appends(request('page'))]);
  16. }
  17. // 添加节点授权
  18. public function store(): JsonResponse
  19. {
  20. $nodes = Node::whereStatus(1)->doesntHave('auth')->orderBy('id')->get();
  21. if ($nodes->isEmpty()) {
  22. return Response::json(['status' => 'success', 'message' => '没有需要生成授权的节点']);
  23. }
  24. $nodes->each(function ($node) {
  25. $node->auth()->create(['key' => Str::random(), 'secret' => Str::random(8)]);
  26. });
  27. return Response::json(['status' => 'success', 'message' => trans('common.generate_item', ['attribute' => trans('common.success')])]);
  28. }
  29. // 重置节点授权
  30. public function update(NodeAuth $auth): JsonResponse
  31. {
  32. if ($auth->update(['key' => Str::random(), 'secret' => Str::random(8)])) {
  33. return Response::json(['status' => 'success', 'message' => '操作成功']);
  34. }
  35. return Response::json(['status' => 'fail', 'message' => '操作失败']);
  36. }
  37. // 删除节点授权
  38. public function destroy(NodeAuth $auth): JsonResponse
  39. {
  40. try {
  41. $auth->delete();
  42. } catch (Exception $e) {
  43. return Response::json(['status' => 'fail', 'message' => '错误:'.var_export($e, true)]);
  44. }
  45. return Response::json(['status' => 'success', 'message' => '操作成功']);
  46. }
  47. }