Index.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\service\UserService;
  5. use app\validate\EditUserInfo;
  6. use app\validate\RePassword;
  7. use app\validate\UserLogin;
  8. use think\annotation\route\Validate;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\exception\ValidateException;
  13. use think\response\Json;
  14. use think\annotation\Route;
  15. use think\annotation\route\Middleware;
  16. use hg\apidoc\annotation as Apidoc;
  17. use thans\jwt\facade\JWTAuth;
  18. /**
  19. * @Apidoc\Title("用户")
  20. * @Apidoc\Group("index")
  21. * @Apidoc\Sort(1)
  22. */
  23. class Index extends BaseController
  24. {
  25. protected $middleware = [
  26. 'jwt' => ['except' => ['login','apidoc'] ],
  27. ];
  28. use ResponseJson;
  29. /**
  30. * @Apidoc\Title("基础的接口演示")
  31. * @Apidoc\Tag("基础,示例")
  32. * @Apidoc\Author("ihavoc")
  33. * @Apidoc\Method ("GET")
  34. * @Route("/")
  35. * @Middleware({})
  36. */
  37. public function index()
  38. {
  39. return $this->JsonSucess(["info"=>"这是后端接口"]);
  40. }
  41. public function hello($name = 'ThinkPHP6')
  42. {
  43. return 'hello,' . $name;
  44. }
  45. /**
  46. * @Apidoc\Title("登录接口")
  47. * @Apidoc\Tag("用户登录")
  48. * @Apidoc\Author("ihavoc")
  49. * @Apidoc\Method ("POST")
  50. * @throws ModelNotFoundException
  51. * @throws DataNotFoundException
  52. * @throws DbException
  53. * @Route("login",method="POST")
  54. * @Middleware({})
  55. */
  56. public function login():Json{
  57. $loginInfo = $this->request->post();
  58. try {
  59. validate(UserLogin::class)->check($loginInfo);
  60. }catch (ValidateException $e){
  61. return $this->JsonError($e->getError(),0);
  62. }
  63. $userInfo=UserService::selectLoginInfo($loginInfo);
  64. if ($userInfo){
  65. $token = JWTAuth::builder(['xzqdm' => $userInfo["xzqdm"],'id'=>$userInfo["id"]]);//参数为用户认证的信息,请自行添加
  66. return $this->JsonSucess(["token"=>$token,"userInfo"=>$userInfo],1001);
  67. }else{
  68. return $this->JsonError(1002);
  69. }
  70. }
  71. /**
  72. * @throws ModelNotFoundException
  73. * @throws DataNotFoundException
  74. * @throws DbException
  75. * @Apidoc\Title("根据token获取用户信息")
  76. * @Apidoc\Tag("用户信息")
  77. * @Apidoc\Method ("POST")
  78. * @Apidoc\Author("ihavoc")
  79. * @Route("/getUserInfo",method="POST")
  80. * @Middleware({"jwt"})
  81. */
  82. public function getUserInfo(): Json
  83. {
  84. return $this->JsonSucess(UserService::getUserInfoById(getUserId()));
  85. }
  86. /**
  87. * @throws ModelNotFoundException
  88. * @throws DataNotFoundException
  89. * @throws DbException
  90. * @Apidoc\Title("修改密码")
  91. * @Apidoc\Tag("用户信息")
  92. * @Apidoc\Method ("POST")
  93. * @Apidoc\Author("ihavoc")
  94. * @Route("/editPassword",method="POST")
  95. * @Middleware({"jwt"})
  96. */
  97. public function editPassword():Json{
  98. $rePassword = $this->request->post();
  99. try {
  100. validate(RePassword::class)->check($rePassword);
  101. }catch (ValidateException $e){
  102. return $this->JsonError($e->getError());
  103. }
  104. return $this->JsonSucess(UserService::editPasswordById(getUserId(),$rePassword["password"]));
  105. }/**
  106. * @throws ModelNotFoundException
  107. * @throws DataNotFoundException
  108. * @throws DbException
  109. * @Apidoc\Title("修改用户信息")
  110. * @Apidoc\Tag("用户信息")
  111. * @Apidoc\Method ("POST")
  112. * @Apidoc\Author("ihavoc")
  113. * @Route("/editUserInfo",method="POST")
  114. * @Middleware({"jwt"})
  115. */
  116. public function editUserInfoById():Json{
  117. $userInfo = $this->request->post();
  118. try {
  119. validate(EditUserInfo::class)->check($userInfo);
  120. }catch (ValidateException $e){
  121. return $this->JsonError($e->getError());
  122. }
  123. return $this->JsonSucess(UserService::editUserInfoById(getUserId(),$userInfo));
  124. }
  125. }