LoginController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Http\ApiControllers;
  3. use Illuminate\Foundation\Auth\User;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Hash;
  8. class LoginController
  9. {
  10. /**
  11. * @api {post} /login 登录接口
  12. * @apiName login
  13. * @apiGroup User
  14. *
  15. * @apiParam {string} username 用户名
  16. * @apiParam {string} password 用户密码,需要base64加密
  17. *
  18. * @apiSuccess {string} message 响应描述
  19. * @apiSuccess {int} status_code HTTP响应码
  20. * @apiSuccess {string} data.token 认证token
  21. * @apiSuccess {String} data.menu 菜单JSON
  22. *
  23. * @apiSuccessExample {json} Success-Response:
  24. * HTTP/1.1 200 OK
  25. * {
  26. * "message": "请求成功",
  27. * "status_code": "200"
  28. * "data":{
  29. * "toke":"token",
  30. * "menu":"{}",
  31. * }
  32. * }
  33. */
  34. public function login(Request $request):JsonResponse
  35. {
  36. $userName = $request->get('username','');
  37. $password = $request->get('password','');
  38. $user = User::query()->where("name",$userName)->first();
  39. $response = [
  40. 'message' => '请求成功',
  41. 'status_code' => 200,
  42. ];
  43. //验证用户登录
  44. if (!$user || !Hash::check(base64_decode($password),$user->password)){
  45. $response["message"] = "用户名或密码错误";
  46. $response["status_code"] = 410;
  47. return response()->json($response);
  48. }
  49. //获取公私钥
  50. try {
  51. $privateKey = file_get_contents(base_path().'/private.pem');
  52. }catch (\Exception $e){
  53. $response["status_code"] = 410;
  54. if (strpos($e->getMessage(),"No such file or directory")!==false)$response["message"] = "服务器异常,资源丢失";
  55. else $response["message"] = "访问某些资源失败";
  56. return response()->json($response);
  57. }
  58. try {
  59. $response["data"] = ["token"=>app("UserService")->getJWTToken($user,$privateKey),
  60. "menu"=>$this->getMenu($user),"info"=>["id"=>$user->id,"name"=>$user->name]];
  61. app("UserService")->setOrRefreshCache($user);
  62. return response()->json($response);
  63. }catch (\Exception $e){
  64. $response["status_code"] = 409;
  65. $response["message"] = "资源异常,无法反馈";
  66. return response()->json($response);
  67. }
  68. }
  69. private function getMenu($user)
  70. {
  71. Auth::setUser($user);
  72. $authorities = app("AuthorityService")->getUserAndroidAuthority();
  73. $result = [];
  74. foreach (app("AuthorityService")->format($authorities) as $authority){
  75. foreach ($authority["child"] as $authorityOne){
  76. $item = [];
  77. foreach ($authorityOne["child"] as $authorityTwo){
  78. $item[] = $authorityTwo["name"];
  79. }
  80. $result[$authorityOne["name"]] = $item;
  81. }
  82. }
  83. return json_encode($result,JSON_UNESCAPED_UNICODE);
  84. }
  85. }