@RestController
@RequestMapping("/api/oauth")
public class LoginController {
@Autowired
private UserService userService;
@Autowired
private TokenEndpoint tokenEndpoint;
@Autowired
private LoginService loginService;
@Autowired
private UserProvider userProvider;
@Autowired
private ConfigValueUtil configValueUtil;
@Autowired
private RedisUtil redisUtil;
@Autowired
private ExpertInfoService expertInfoService;
@Autowired
private RoleService roleService;
@ApiOperation("登陆(切换登录模式需请清空loginForm中的值)")
@PostMapping("/Login")
public ActionResult<LoginVO> login(Principal principal, @RequestParam Map<String, String> parameters, @RequestBody LoginForm loginForm) throws LoginException {
TenantContextHolder.clear();
UserInfo userInfo = new UserInfo();
String phone = loginForm.getPhone();
String phoneCode = loginForm.getPhoneCode();
String timestampkey = loginForm.getTimestamp();
if(StringUtil.isNotEmpty(phone)){
List<UserEntity> userEntityList = userService.list(new QueryWrapper<UserEntity>().lambda().eq(UserEntity::getMobilePhone,phone));
if(CollectionUtils.isNotEmpty(userEntityList)){
String phoneCode1 = String.valueOf(redisUtil.getString(phone));
if("null".equals(phoneCode1)){
throw new LoginException("验证码已过期!");
}
if(!(phoneCode1.equals(phoneCode))){
throw new LoginException("验证码输入错误!");
}
if(StringUtil.isNotEmpty(loginForm.getAccount())){
userEntityList = userEntityList.stream().filter(t->loginForm.getAccount().equals(t.getAccount())).collect(Collectors.toList());
}
if(userEntityList.size() > 1){
List<UserLoginForm> userLoginFormList = JsonUtil.getJsonToList(userEntityList,UserLoginForm.class);
LoginVO loginVO = new LoginVO();
loginVO.setUserLogFormList(userLoginFormList);
return ActionResult.success(loginVO);
}
UserEntity userEntity = userEntityList.get(0);
loginForm.setAccount(userEntity.getAccount());
loginForm.setPassword(userEntity.getPassword());
redisUtil.remove(phone);
}
}else{
String code = loginForm.getCode();
String timestamp = String.valueOf(redisUtil.getString(timestampkey));
if("null".equals(timestamp)){
throw new LoginException("验证码已过期!");
}
if(!(code).equalsIgnoreCase(timestamp)){
throw new LoginException("验证码错误!");
}
}
loginService.isExistUser(loginForm.getAccount().trim(), loginForm.getPassword().trim());
List<UserEntity> userEntityList = userService.getUserEntitys(StringUtil.isNotEmpty(
loginForm.getPhonePassword())?loginForm.getPhonePassword():loginForm.getAccount());
UserEntity entity = new UserEntity();
if(userEntityList.size() > 1){
for (UserEntity item : userEntityList) {
if(item.getPassword().equals(Md5Util.getStringMd5(loginForm.getPassword() + item.getSecretkey().toLowerCase()))){
if(StringUtil.isNotEmpty(loginForm.getPhonePassword())){
entity = userEntityList.stream().filter(t->loginForm.getAccount().equals(t.getAccount())).collect(Collectors.toList()).get(0);
loginForm.setAccount(entity.getAccount());
loginForm.setPassword(entity.getPassword());
}else{
List<UserLoginForm> userLoginFormList = JsonUtil.getJsonToList(userEntityList,UserLoginForm.class);
LoginVO loginVO = new LoginVO();
loginVO.setUserLogFormList(userLoginFormList);
return ActionResult.success(loginVO);
}
}
}
if(StringUtil.isEmpty(loginForm.getPhonePassword())){
throw new LoginException("账号密码错误");
}
}
if(StringUtil.isEmpty(loginForm.getPhonePassword())){
entity = userEntityList.get(0);
}
userInfo = loginService.userInfo(userInfo, entity);
// if(StringUtil.isNotEmpty(loginForm.getRoleId())){
// String[] roles = new String[1];
// roles[0] = loginForm.getRoleId();
// userInfo.setRoleIds(roles);
// }
//
// List<RoleLoginVo> roleLoginVoList = new ArrayList<>();
//
// if(ArrayUtils.isNotEmpty(userInfo.getRoleIds())){
// if(userInfo.getRoleIds().length > 1){
// for (String roleId : userInfo.getRoleIds()) {
// RoleLoginVo roleLoginVo = JsonUtil.getJsonToBean(roleService.getById(roleId),RoleLoginVo.class);
// roleLoginVoList.add(roleLoginVo);
// }
// }
// }
// if(CollectionUtil.isNotEmpty(roleLoginVoList)){
// LoginVO loginVO = new LoginVO();
// loginVO.setRoleList(roleLoginVoList);
// return ActionResult.success(loginVO);
// }
userInfo.setMybatisTenantId(entity.getTenantId());
ExpertInfoEntity expertInfoEntity = expertInfoService.getOne(
new QueryWrapper<ExpertInfoEntity>().lambda().eq(ExpertInfoEntity::getUserId,entity.getId()));
if(null != expertInfoEntity){
userInfo.setExpertId(expertInfoEntity.getId());
}
//写入会话
userProvider.add(userInfo);
//验证账号密码
Map<String, String> map = new HashMap<>(16);
map.put("account",loginForm.getAccount());
map.put("password",loginForm.getPassword());
map.putAll(parameters);
map.put("username", loginForm.getAccount());
OAuth2AccessToken oAuth2AccessToken;
try {
oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, map).getBody();
} catch (HttpRequestMethodNotSupportedException e) {
throw new LoginException("账号密码错误");
}
TenantContextHolder.setTenant(entity.getTenantId());
//登陆日志记录在JwtTokenEnhancer类中
//获取主题
LoginVO loginVO = new LoginVO();
loginVO.setToken(oAuth2AccessToken.getTokenType() + " " + oAuth2AccessToken.getValue());
loginVO.setTheme(entity.getTheme() == null ? "classic" : entity.getTheme());
return ActionResult.success(loginVO);
}
}
|