day01/bwie-auth/src/main/java/auth/controller/AuthController.java

87 lines
3.2 KiB
Java

package auth.controller;
import com.alibaba.fastjson.JSONObject;
import com.bwie.auth.service.AuthService;
import com.bwie.auth.utils.ApiIdempotent;
import com.bwie.common.domain.SysUser;
import com.bwie.common.domain.request.UserRequest;
import com.bwie.common.domain.response.UserResponse;
import com.bwie.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author : ZhangBingYan
* @className : AuthController
* @description : 描述说明该类的功能
* @createTime : 2023/7/28 9:31
* @updateUser : ZhangBingYan
* @updateTime : 2023/7/28 9:31
* @updateRemark : 描述说明本次修改内容
*/
@RestController
@Log4j2
public class AuthController {
@Autowired
private AuthService authService;
@Autowired
private HttpServletRequest request;
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* 账号登录 + 拦截器
* */
@PostMapping("login")
@ApiIdempotent
public Result<UserResponse> login(@RequestBody UserRequest userRequest){
log.info("功能名称:账号登录,请求URI:{},请求方法:{},请求参数:{}",
request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(userRequest));
// String token = UUID.randomUUID().toString();
// redisTemplate.opsForValue().set(token, "登录成功", 1, TimeUnit.HOURS);
Result<UserResponse> result = authService.login(userRequest);
log.info("功能名称:账号登录,请求URI:{},请求方法:{},响应结果:{}",
request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(result));
return result;
// if (result.isSuccess()) {
// return Result.success(token); // 返回生成的Token
// } else {
// return Result.error(result.getCode(), result.getMsg());
// }
}
/**
* 响应消息
* */
@PostMapping("info")
public Result<SysUser> info(){
log.info("功能名称:响应消息,请求URI:{},请求方法:{}",
request.getRequestURI(),request.getMethod());
SysUser sysUser=authService.info();
Result<SysUser> result=Result.success(sysUser);
log.info("功能名称:响应消息,请求URI:{},请求方法:{},响应结果:{}",
request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(result));
return result;
}
/**
* 注销
* */
@GetMapping("logout")
public Result logout(){
log.info("功能名称:注销,请求URI:{},请求方法:{}",
request.getRequestURI(),request.getMethod());
authService.logout();
Result result=Result.success();
log.info("功能名称:注销,请求URI:{},请求方法:{},响应结果:{}",
request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(result));
return result;
}
}