| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\BaseController;
- use app\http\IResponse;
- use Lettered\Support\Auth as IAuth;
- use think\App;
- class Auth extends BaseController
- {
- protected $auth;
- /**
- * Auth constructor.
- * @param App $app
- * @param IAuth $auth
- */
- public function __construct(App $app, IAuth $auth)
- {
- parent::__construct($app);
- $this->auth = $auth;
- }
- /**
- * 后台登录
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 11:57
- *
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\FailedException
- */
- public function login()
- {
- // 接收数据
- $param = $this->request->param();
- // 内置验证
- $valid = $this->validate($param, [
- 'username|用户名' => 'require',
- 'password|密码' => 'require',
- // 'captcha|验证码' => 'require|captcha:manage',
- ]);
- // 错误
- if (true !== $valid){
- return IResponse::failure($valid);
- }
- // attempt
- $token = $this->auth->attempt($param);
- return $token ? IResponse::success([
- 'token' => 'Bearer ' . $token,
- ], '登录成功') : IResponse::failure('登录失败');
- }
- }
|