NodeAuthController.php 1.7 KB

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