User.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\auth\MysqlAdapter;
  4. use app\api\service\auth\ApiAuthService;
  5. use app\api\validate\User as UserValidate;
  6. use think\exception\ValidateException;
  7. use think\Cache;
  8. class User extends Base
  9. {
  10. protected $noNeedLogin = ['login'];
  11. public function userinfo()
  12. {
  13. $user = $this->userinfo;
  14. return $this->success('ok', $user);
  15. }
  16. public function login(ApiAuthService $authService)
  17. {
  18. $data = $this->request->post();
  19. try{
  20. validate(UserValidate::class)->scene('login')->check($data);
  21. $user = $authService->login($data['username'],$data['password']);
  22. }catch (ValidateException $e) {
  23. return $this->error($e->getError());
  24. }catch (\Exception $e){
  25. return $this->error($e->getMessage());
  26. }
  27. return $this->success('ok', $user);
  28. }
  29. //退出登录
  30. public function logout()
  31. {
  32. MysqlAdapter::logout($this->userinfo['id']);
  33. return $this->success('ok');
  34. }
  35. }