User.php 926 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace app\api\controller;
  3. use think\captcha\facade\Captcha;
  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. }