1.add autoLogin 续期接口

main
Gary 1 year ago
parent 85225111c1
commit 4f968fe3d3

@ -5,4 +5,10 @@ public class Constants {
public static final String REDIS_KEY_IMAGE_CHECK_CODE="redis_key_image_check_code_";
public static final String REDIS_KEY_MOBILE_CHECK_CODE="redis_key_mobile_check_code_";
public static final String TOKEN_ROLE="roles";
public static final String TOKEN_ROLE_APP_USER="user";
public static final String TOKEN_ROLE_ADMIN_USER="admin";
public static final String TOKEN_ROLE_TOURIST="tourist";
}

@ -27,6 +27,7 @@ import com.luoo.user.util.NickNameUtil;
import api.PageResult;
import api.Result;
import api.StatusCode;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import util.IdWorker;
@ -106,7 +107,7 @@ public class UserController {
}
User user = userService.loginOrRegister(mobile);
BeanUtils.copyProperties(user, userRespDTO);
String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user");
String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER);
userRespDTO.setToken(token);
return Result.success(userRespDTO);
}finally {
@ -120,8 +121,9 @@ public class UserController {
public Result<UserRespDTO> touristLogin() {
UserRespDTO userRespDTO=new UserRespDTO();
userRespDTO.setId(String.valueOf(idWorker.nextId()));
String nickName="游客-"+NickNameUtil.getRandomNickName();
userRespDTO.setNickname("游客-"+NickNameUtil.getRandomNickName());
String token = jwtUtil.createJWT(userRespDTO.getId(),"tourist","tourist");
String token = jwtUtil.createJWT(userRespDTO.getId(),nickName,Constants.TOKEN_ROLE_TOURIST);
userRespDTO.setToken(token);
return Result.success(userRespDTO);
}
@ -140,17 +142,12 @@ public class UserController {
/**
* token
*/
/*
* @ApiOperation(value = "5.token 续期默认token 有效期7天")
*
* @PostMapping("/autoLogin")
*
* @GlobalInterceptor public Result<String>
* autoLogin(@PathVariable @VerifyParam(required=true,max=32) String deviceId,
*
* @PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String
* mobile) { userService.sendSms(deviceId,mobile); return Result.success(); }
*/
@ApiOperation(value = "5.token 续期有效期7天")
@PostMapping("/autoLogin/{token}")
@GlobalInterceptor
public Result<String> autoLogin(@PathVariable @VerifyParam(required = true) String token) {
return Result.success(userService.autoLogin(token));
}
/**
*

@ -31,6 +31,9 @@ import com.luoo.user.enumerate.Gender;
import com.luoo.user.pojo.User;
import com.luoo.user.util.NickNameUtil;
import api.Result;
import api.StatusCode;
import io.jsonwebtoken.Claims;
import util.JwtUtil;
/**
@ -282,4 +285,20 @@ public class UserService {
User user = userDao.findByMobile(mobile);
return user;
}
public String autoLogin(String token) {
try {
Claims claims = jwtUtil.parseJWT(token);
String userId = claims.getId();
if(Constants.TOKEN_ROLE_APP_USER.equals((String) claims.get(Constants.TOKEN_ROLE))){
User user = userDao.findById(userId).orElse(null);
user.setLastdate(new Date());
userDao.save(user);
}
return jwtUtil.createJWT(userId, claims.getSubject(), Constants.TOKEN_ROLE_APP_USER);
} catch (Exception e) {
return null;
}
}
}

Loading…
Cancel
Save