Auth.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace app\agent\controller;
  3. use app\common\controller\AgentController;
  4. use app\http\IResponse;
  5. class Auth extends AgentController
  6. {
  7. /**
  8. * 后台登录
  9. *
  10. * @author 许祖兴 < zuxing.xu@lettered.cn>
  11. * @date 2020/3/16 11:57
  12. *
  13. * @return \think\response\Json
  14. * @throws \Lettered\Support\Exceptions\FailedException
  15. */
  16. public function login()
  17. {
  18. // 接收数据
  19. $param = $this->request->param();
  20. // 内置验证
  21. $valid = $this->validate($param, [
  22. 'username|用户名' => 'require',
  23. 'password|密码' => 'require',
  24. // 'captcha|验证码' => 'require|captcha:manage',
  25. ]);
  26. // 错误
  27. if (true !== $valid){
  28. return IResponse::failure($valid);
  29. }
  30. // attempt
  31. $token = $this->auth->attempt($param);
  32. return $token ? IResponse::success([
  33. 'token' => 'Bearer ' . $token,
  34. ], '登录成功') : IResponse::failure('登录失败');
  35. }
  36. }