NodeAuthController.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\NodeAuth;
  5. use Exception;
  6. use Illuminate\Http\Request;
  7. class NodeAuthController extends Controller
  8. {
  9. // 节点授权列表
  10. public function index()
  11. {
  12. return view('admin.node.auth', ['authorizations' => NodeAuth::orderBy('node_id')->paginate()->appends(request('page'))]);
  13. }
  14. // 添加节点授权
  15. public function store(Request $request)
  16. {
  17. $nodeArray = Node::whereStatus(1)->orderBy('id')->pluck('id')->toArray();
  18. $authArray = NodeAuth::orderBy('node_id')->pluck('node_id')->toArray();
  19. $arrayDifferent = array_diff($nodeArray, $authArray);
  20. if (empty($arrayDifferent)) {
  21. return Response::json(['status' => 'success', 'message' => '没有需要生成授权的节点']);
  22. }
  23. foreach ($arrayDifferent as $nodeId) {
  24. $obj = new NodeAuth();
  25. $obj->node_id = $nodeId;
  26. $obj->key = Str::random();
  27. $obj->secret = Str::random(8);
  28. $obj->save();
  29. }
  30. return Response::json(['status' => 'success', 'message' => '生成成功']);
  31. }
  32. // 重置节点授权
  33. public function update(NodeAuth $auth)
  34. {
  35. if ($auth->update(['key' => Str::random(), 'secret' => Str::random(8)])) {
  36. return Response::json(['status' => 'success', 'message' => '操作成功']);
  37. }
  38. return Response::json(['status' => 'fail', 'message' => '操作失败']);
  39. }
  40. // 删除节点授权
  41. public function destroy(NodeAuth $auth)
  42. {
  43. try {
  44. $auth->delete();
  45. } catch (Exception $e) {
  46. return Response::json(['status' => 'fail', 'message' => '错误:'.var_export($e, true)]);
  47. }
  48. return Response::json(['status' => 'success', 'message' => '操作成功']);
  49. }
  50. }