User.php 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\controller;
  4. use app\api\validate\User as UserValidate;
  5. use app\api\service\ApiAuthService;
  6. use think\annotation\route\Post;
  7. use think\exception\ValidateException;
  8. class User extends Api
  9. {
  10. protected $noNeedLogin = ['login'];
  11. public function userinfo(ApiAuthService $apiAuth)
  12. {
  13. $user= $apiAuth->userinfo();
  14. return $this->jsonSuccess('登录成功', $user);
  15. }
  16. //登录
  17. public function login(ApiAuthService $apiAuth)
  18. {
  19. $data=$this->request->post();
  20. try {
  21. validate(UserValidate::class)->scene('edit')->check($data);
  22. $ret = $apiAuth->login($data['username'], $data['password']);
  23. return $this->jsonSuccess('登录成功', $ret);
  24. } catch (ValidateException $e) {
  25. return $this->jsonError($e->getError());
  26. }catch (\Exception $e) {
  27. return $this->jsonError($e->getMessage());
  28. }
  29. }
  30. }