Login.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\controller\adminapi\admin;
  3. use app\extra\basic\Base;
  4. use app\middleware\AuthMiddleware;
  5. use app\model\system\SystemUser;
  6. use LinFly\Annotation\Route\Controller;
  7. use LinFly\Annotation\Route\Middleware;
  8. use LinFly\Annotation\Route\Route;
  9. use Shopwwi\WebmanAuth\Auth;
  10. use support\Request;
  11. use support\Response;
  12. use think\facade\Db;
  13. use Tinywan\Captcha\Captcha;
  14. #[Controller(prefix: "/adminapi")]
  15. class Login extends Base
  16. {
  17. /**
  18. * 登陆
  19. * @param Request $request
  20. * @return Response
  21. */
  22. #[Route(path: "login",methods: "post")]
  23. public function setLogin(Request $request): Response
  24. {
  25. try {
  26. $param = $this->_valid([
  27. "username.require" => trans("empty.user"),
  28. "password.require" => trans("empty.passwd"),
  29. "code.require" => trans("empty.code"),
  30. "key.require" => trans("empty.data"),
  31. ],"post");
  32. if (!is_array($param)) return error($param);
  33. // if (Captcha::check($param['code'],$param['key']) === false) return errorTrans("error.captcha");
  34. $map = ["is_deleted" => 0,"username" => $param['username']];
  35. [$state,$msg,$user] = $this->checkLogin($map,2,$param);
  36. if (!$state) return error($msg);
  37. return successTrans("success.login",get_object_vars((new Auth)->guard("admin")->login($user)));
  38. } catch (\Throwable $throwable) {
  39. return error($throwable->getMessage());
  40. }
  41. }
  42. /**
  43. * 登录验证处理
  44. * @param array $map
  45. * @param int $type
  46. * @param array $param
  47. * @return array
  48. */
  49. protected function checkLogin(array $map = [],int $type = 1,array $param = []): array
  50. {
  51. $user = (new SystemUser)->where($map)->findOrEmpty();
  52. if ($user->isEmpty()) return [0,trans("error.user-empty"),[]];
  53. if ($user['status'] <> 1) return [0,trans("error.user-status"),[]];
  54. if ($type == 2) {
  55. // if (md5($param['password'].$user['salt']) <> $user['password']) return [0,trans("error.passwd"),[]];
  56. }
  57. $user->login_at = getDateFull();
  58. $user->login_ip = request()->getRealIp();
  59. $user->login_num = Db::raw("login_num+1");
  60. $user->save();
  61. return [1,'success',$user->toArray()];
  62. }
  63. /**
  64. * @return Response
  65. */
  66. #[Route(path: "profile",methods: "get"),Middleware(AuthMiddleware::class)]
  67. public function getLoginUser(): Response
  68. {
  69. try {
  70. $userData = (new Auth)->guard("admin")->user()->toArray();
  71. if (isset($userData['password'])) unset($userData['password']);
  72. return successTrans("success.data",[
  73. "username" => $userData['username'],
  74. "truename" => $userData['truename'],
  75. "agent_id" => $userData['agent_id'],
  76. "super" => $userData['is_super'],
  77. "type" => $userData['type']
  78. ]);
  79. } catch (\Throwable $exception){
  80. return error($exception->getMessage());
  81. }
  82. }
  83. }