Auth.php 1.3 KB

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