package com.hongliang.videotask.controller; import cn.dev33.satoken.annotation.SaCheckLogin; import cn.dev33.satoken.stp.StpUtil; import com.hongliang.videotask.bean.LoginBean; import com.hongliang.videotask.bean.LoginResultBean; import com.hongliang.videotask.bean.UserBean; import com.hongliang.videotask.common.Response; import com.hongliang.videotask.common.ResponseCode; import com.hongliang.videotask.service.impl.LoginServiceImpl; import com.hongliang.videotask.service.impl.UserServiceImpl; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @RestController @RequestMapping("/user") public class UserControl { @Resource private LoginServiceImpl loginService; @Resource private UserServiceImpl userService; @GetMapping("hello") public ResponseEntity hello(){ return ResponseEntity.ok(new Response(200,"ok","hello world!")); } @PostMapping("login") public ResponseEntity login(@RequestBody LoginBean loginBean){ LoginResultBean loginResultBean= loginService.Login(loginBean.getUsername(),loginBean.getPassword()); if (loginResultBean==null){ return ResponseEntity.ok(new Response(ResponseCode.LOGIN_ERROR,"用户名或密码错误",null)); }else{ StpUtil.login(loginResultBean.getUserid()); //saToken登录 //获取token loginResultBean.setAccess_token(StpUtil.getTokenInfo().tokenValue); //获取过期时间 LocalDateTime now=LocalDateTime.now(); LocalDateTime newTime=now.plusSeconds(StpUtil.getTokenInfo().getTokenTimeout()); loginResultBean.setExpire_datetime(newTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); return ResponseEntity.ok(new Response(ResponseCode.OK,"登录成功",loginResultBean)); } } @GetMapping("islogin") public ResponseEntity isLogin(){ return ResponseEntity.ok(new Response(ResponseCode.OK,StpUtil.isLogin()?"已登录":"未登录",StpUtil.isLogin())); } @PostMapping("logout") public ResponseEntity logout(){ StpUtil.logout(); return ResponseEntity.ok(new Response(ResponseCode.OK,"成功退出",null)); } @SaCheckLogin @GetMapping("/user/{id}") public ResponseEntity GetUserByID(@PathVariable("id") int id){ return ResponseEntity.ok(new Response(ResponseCode.OK,"成功",userService.GetUserByID(id))); } }