package com.doumee.api.web;
|
|
import com.doumee.config.Jwt.JwtProperties;
|
import com.doumee.config.Jwt.JwtTokenUtil;
|
import com.doumee.config.annotation.LoginRequired;
|
import com.doumee.core.annotation.pr.PreventRepeat;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.core.model.ApiResponse;
|
import com.doumee.dao.business.model.Member;
|
import com.doumee.dao.web.dto.LoginH5DTO;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.server.ServerWebExchange;
|
|
import javax.annotation.Resource;
|
|
/**
|
* JWT获取令牌和刷新令牌接口
|
*/
|
@RestController
|
@Api(tags ="web端用户相关接口")
|
@RequestMapping("/web/member")
|
public class WebMemberController {
|
@Resource
|
private JwtTokenUtil jwtTokenUtil;
|
@PreventRepeat(limit = 10, lockTime = 10000)
|
@ApiOperation("H5业务登录")
|
@PostMapping("/loginH5")
|
public ApiResponse<String> loginH5 (@Validated @RequestBody LoginH5DTO dto, ServerWebExchange serverWebExchange) {
|
try {
|
ServerHttpRequest request = serverWebExchange.getRequest();
|
Member user = new Member();
|
String token = jwtTokenUtil.generateToken(user);
|
return ApiResponse.success(token);
|
}catch (BusinessException e){
|
return ApiResponse.failed(e.getCode(),e.getMessage());
|
}catch (Exception e){
|
e.printStackTrace();
|
return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
|
}
|
}
|
/**
|
* 刷新JWT令牌,用旧的令牌换新的令牌
|
*/
|
@GetMapping("/getMemberInfo")
|
@ApiOperation("获取当前登陆用户")
|
@LoginRequired
|
public ApiResponse<Member> getMemberInfo(@RequestHeader(JwtTokenUtil.HEADER_KEY) String token){
|
try {
|
Member user =jwtTokenUtil.getUserInfoByToken(token);
|
jwtTokenUtil.refreshToken(token);//每次更新token有效期
|
if(user !=null){
|
return ApiResponse.success( user);
|
}
|
}catch (BusinessException e){
|
return ApiResponse.failed(e.getCode(),e.getMessage());
|
}catch (Exception e){
|
return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
|
}
|
return ApiResponse.failed( "登录已失效");
|
}
|
|
}
|