123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use app\service\UserService;
- use app\validate\EditUserInfo;
- use app\validate\RePassword;
- use app\validate\UserLogin;
- use think\annotation\route\Validate;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- use think\response\Json;
- use think\annotation\Route;
- use think\annotation\route\Middleware;
- use hg\apidoc\annotation as Apidoc;
- use thans\jwt\facade\JWTAuth;
- /**
- * @Apidoc\Title("用户")
- * @Apidoc\Group("index")
- * @Apidoc\Sort(1)
- */
- class Index extends BaseController
- {
- protected $middleware = [
- 'jwt' => ['except' => ['login','apidoc'] ],
- ];
- use ResponseJson;
- /**
- * @Apidoc\Title("基础的接口演示")
- * @Apidoc\Tag("基础,示例")
- * @Apidoc\Author("ihavoc")
- * @Apidoc\Method ("GET")
- * @Route("/")
- * @Middleware({})
- */
- public function index()
- {
- return $this->JsonSucess(["info"=>"这是后端接口"]);
- }
- public function hello($name = 'ThinkPHP6')
- {
- return 'hello,' . $name;
- }
- /**
- * @Apidoc\Title("登录接口")
- * @Apidoc\Tag("用户登录")
- * @Apidoc\Author("ihavoc")
- * @Apidoc\Method ("POST")
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @throws DbException
- * @Route("login",method="POST")
- * @Middleware({})
- */
- public function login():Json{
- $loginInfo = $this->request->post();
- try {
- validate(UserLogin::class)->check($loginInfo);
- }catch (ValidateException $e){
- return $this->JsonError($e->getError(),0);
- }
- $userInfo=UserService::selectLoginInfo($loginInfo);
- if ($userInfo){
- $token = JWTAuth::builder(['xzqdm' => $userInfo["xzqdm"],'id'=>$userInfo["id"]]);//参数为用户认证的信息,请自行添加
- return $this->JsonSucess(["token"=>$token,"userInfo"=>$userInfo],1001);
- }else{
- return $this->JsonError(1002);
- }
- }
- /**
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @throws DbException
- * @Apidoc\Title("根据token获取用户信息")
- * @Apidoc\Tag("用户信息")
- * @Apidoc\Method ("POST")
- * @Apidoc\Author("ihavoc")
- * @Route("/getUserInfo",method="POST")
- * @Middleware({"jwt"})
- */
- public function getUserInfo(): Json
- {
- return $this->JsonSucess(UserService::getUserInfoById(getUserId()));
- }
- /**
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @throws DbException
- * @Apidoc\Title("修改密码")
- * @Apidoc\Tag("用户信息")
- * @Apidoc\Method ("POST")
- * @Apidoc\Author("ihavoc")
- * @Route("/editPassword",method="POST")
- * @Middleware({"jwt"})
- */
- public function editPassword():Json{
- $rePassword = $this->request->post();
- try {
- validate(RePassword::class)->check($rePassword);
- }catch (ValidateException $e){
- return $this->JsonError($e->getError());
- }
- return $this->JsonSucess(UserService::editPasswordById(getUserId(),$rePassword["password"]));
- }/**
- * @throws ModelNotFoundException
- * @throws DataNotFoundException
- * @throws DbException
- * @Apidoc\Title("修改用户信息")
- * @Apidoc\Tag("用户信息")
- * @Apidoc\Method ("POST")
- * @Apidoc\Author("ihavoc")
- * @Route("/editUserInfo",method="POST")
- * @Middleware({"jwt"})
- */
- public function editUserInfoById():Json{
- $userInfo = $this->request->post();
- try {
- validate(EditUserInfo::class)->check($userInfo);
- }catch (ValidateException $e){
- return $this->JsonError($e->getError());
- }
- return $this->JsonSucess(UserService::editUserInfoById(getUserId(),$userInfo));
- }
- }
|