2024-04-08 15:44:24 +08:00
|
|
|
package com.hongliang.videotask.controller;
|
|
|
|
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
|
import com.hongliang.videotask.bean.LoginBean;
|
|
|
|
|
import com.hongliang.videotask.bean.LoginResultBean;
|
2024-04-11 09:24:05 +08:00
|
|
|
import com.hongliang.videotask.bean.UserBean;
|
2024-04-08 15:44:24 +08:00
|
|
|
import com.hongliang.videotask.common.Response;
|
|
|
|
|
import com.hongliang.videotask.common.ResponseCode;
|
|
|
|
|
import com.hongliang.videotask.service.impl.LoginServiceImpl;
|
2024-04-11 09:24:05 +08:00
|
|
|
import com.hongliang.videotask.service.impl.UserServiceImpl;
|
2024-04-08 15:44:24 +08:00
|
|
|
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;
|
2024-04-11 09:24:05 +08:00
|
|
|
@Resource
|
|
|
|
|
private UserServiceImpl userService;
|
2024-04-08 15:44:24 +08:00
|
|
|
|
|
|
|
|
@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(){
|
2024-04-11 09:24:05 +08:00
|
|
|
StpUtil.logout();
|
2024-04-08 15:44:24 +08:00
|
|
|
return ResponseEntity.ok(new Response(ResponseCode.OK,"成功退出",null));
|
|
|
|
|
}
|
2024-04-11 09:24:05 +08:00
|
|
|
|
|
|
|
|
@GetMapping("getuserbyid")
|
|
|
|
|
public ResponseEntity<?> GetUserByID(@RequestParam("id") int id){
|
|
|
|
|
return ResponseEntity.ok(new Response(ResponseCode.OK,"成功",userService.GetUserByID(id)));
|
|
|
|
|
}
|
2024-04-08 15:44:24 +08:00
|
|
|
}
|