Browse Source

优化新增用户代码,与oicd联调成功

tags/v1.0.0^2
wanjing 1 year ago
parent
commit
f143f1581f
6 changed files with 101 additions and 26 deletions
  1. +1
    -1
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/UserController.java
  2. +17
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/enums/UserTypeEnum.java
  3. +11
    -2
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/enums/code/user/AddUserCodeEnum.java
  4. +20
    -13
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/third/oidc/AddOidcUserService.java
  5. +8
    -6
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/user/IUserService.java
  6. +44
    -4
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/user/add/AddUserService.java

+ 1
- 1
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/UserController.java View File

*/ */
@PostMapping("/add") @PostMapping("/add")
public JsonResult add(@RequestBody User entity) { public JsonResult add(@RequestBody User entity) {
return userService.edit(entity);
return userService.add(entity);
} }


/** /**

+ 17
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/enums/UserTypeEnum.java View File



import lombok.Getter; import lombok.Getter;


import java.util.HashMap;
import java.util.Map;

/** /**
* 用户类型 * 用户类型
* *
@Getter @Getter
private String description; private String description;


private static Map<Integer, UserTypeEnum> data = new HashMap<>();

static {
for (UserTypeEnum type : UserTypeEnum.values()) {
data.put(type.code, type);
}
}

public static UserTypeEnum parse(int code) {
if (data.containsKey(code)) {
return data.get(code);
}
return null;
}
} }

+ 11
- 2
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/enums/code/user/AddUserCodeEnum.java View File

public enum AddUserCodeEnum { public enum AddUserCodeEnum {


ADD_IS_FAILED(1100100, "添加失败"), ADD_IS_FAILED(1100100, "添加失败"),
PHONE_NOT_IMATCH(1100101, "手机号不匹配"),
USERNAME_ALREADY_EXISTS(1100102, "系统中已经存在该用户名");
CODE_IS_NULL(1100101, "编号不能为空"),
USERNAME_IS_NULL(1100102, "账号不能为空"),
REALNAME_IS_NULL(1100103, "姓名不能为空"),
ROLE_ID_IS_NULL(1100104, "角色不能为空"),
DEPT_ID_IS_NULL(1100105, "部门不能为空"),
CLIENT_ID_IS_NULL(1100106, "ClientID不能为空"),
TYPE_ID_IS_NULL(1100107, "用户类型不能为空"),
TYPE_ID_IS_NOT_EXISTS(1100108, "用户类型不存在"),
PHONE_NOT_IMATCH(1100109, "手机号不匹配"),
USERNAME_ALREADY_EXISTS(1100110, "系统中已经存在该用户名"),
ROLE_IS_NOT_EXISTS(1100111, "该角色不存在");


/** /**
* 错误码 * 错误码

+ 20
- 13
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/third/oidc/AddOidcUserService.java View File

package com.tuoheng.admin.service.third.oidc; package com.tuoheng.admin.service.third.oidc;


import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.admin.constant.OidcUrlConstant; import com.tuoheng.admin.constant.OidcUrlConstant;
import com.tuoheng.admin.dto.ClientRoleDto; import com.tuoheng.admin.dto.ClientRoleDto;
import com.tuoheng.admin.entity.Tenant; import com.tuoheng.admin.entity.Tenant;
HttpHeaders resultRequestHeader = new HttpHeaders(); HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + SecurityUserUtils.token()); resultRequestHeader.add("Authorization", "Bearer " + SecurityUserUtils.token());
HttpEntity httpEntity = new HttpEntity(request, resultRequestHeader); HttpEntity httpEntity = new HttpEntity(request, resultRequestHeader);

String url = CommonConfig.oidcUrl + OidcUrlConstant.USER_CREATE; String url = CommonConfig.oidcUrl + OidcUrlConstant.USER_CREATE;
ResponseEntity<JsonResult> response; ResponseEntity<JsonResult> response;
try { try {
} else { } else {
oidcCreateUserRequest.setPassword("123456"); oidcCreateUserRequest.setPassword("123456");
} }
// 普通用户新增
oidcCreateUserRequest.setTenantFlag(0);
oidcCreateUserRequest.setTenantName(tenant.getUsername());
if (ObjectUtil.isNull(tenant)) {
// 普通用户新增
oidcCreateUserRequest.setTenantFlag(0);
oidcCreateUserRequest.setTenantName("");
} else {
oidcCreateUserRequest.setTenantFlag(1);
oidcCreateUserRequest.setTenantName(tenant.getName());
}
List<ClientRoleDto> clientRoleDtoList = new ArrayList<>(); List<ClientRoleDto> clientRoleDtoList = new ArrayList<>();
ClientRoleDto clientRoleDto; ClientRoleDto clientRoleDto;
String[] clientIdArr = user.getClientId().split(",");
for (String clientIdStr : clientIdArr) {
int clientId = Integer.valueOf(clientIdStr);
ClientEnum clientEnum = ClientEnum.parse(clientId);
if (null != clientEnum) {
clientRoleDto = new ClientRoleDto();
clientRoleDto.setClientId(clientEnum.getDescription());
clientRoleDto.setRoleId(user.getRoleId());
clientRoleDtoList.add(clientRoleDto);
if (StringUtils.isNotEmpty(user.getClientId())) {
String[] clientIdArr = user.getClientId().split(",");
for (String clientIdStr : clientIdArr) {
int clientId = Integer.valueOf(clientIdStr);
ClientEnum clientEnum = ClientEnum.parse(clientId);
if (null != clientEnum) {
clientRoleDto = new ClientRoleDto();
clientRoleDto.setClientId(clientEnum.getDescription());
clientRoleDto.setRoleId(user.getRoleId());
clientRoleDtoList.add(clientRoleDto);
}
} }
} }
oidcCreateUserRequest.setClientRoleDtoList(clientRoleDtoList); oidcCreateUserRequest.setClientRoleDtoList(clientRoleDtoList);

+ 8
- 6
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/user/IUserService.java View File

/** /**
* 添加用户 * 添加用户
* *
* @param entity 实体对象
* @param user 实体对象
* @return * @return
*/ */
JsonResult add(@RequestBody User entity);
JsonResult add(@RequestBody User user);


/** /**
* 编辑用户 * 编辑用户
* *
* @param entity 实体对象
* @param user 实体对象
* @return * @return
*/ */
JsonResult edit(User entity);
JsonResult edit(User user);


/** /**
* 删除用户 * 删除用户
/** /**
* 设置状态 * 设置状态
* *
* @param entity 实体对象
* @param id
* @param status
*
* @return * @return
*/ */
JsonResult updeateStatus(String id, Integer status); JsonResult updeateStatus(String id, Integer status);
/** /**
* 重置密码 * 重置密码
* *
* @param request 参数
* @param id 参数
* @return * @return
*/ */
JsonResult resetPwd(String id); JsonResult resetPwd(String id);

+ 44
- 4
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/user/add/AddUserService.java View File

package com.tuoheng.admin.service.user.add; package com.tuoheng.admin.service.user.add;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.entity.Tenant; import com.tuoheng.admin.entity.Tenant;
import com.tuoheng.admin.entity.User; import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.UserTypeEnum;
import com.tuoheng.admin.enums.code.user.AddUserCodeEnum; import com.tuoheng.admin.enums.code.user.AddUserCodeEnum;
import com.tuoheng.admin.mapper.RoleMapper;
import com.tuoheng.admin.mapper.TenantMapper; import com.tuoheng.admin.mapper.TenantMapper;
import com.tuoheng.admin.mapper.UserMapper; import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.service.third.oidc.OidcService; import com.tuoheng.admin.service.third.oidc.OidcService;
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;


@Autowired
private RoleMapper roleMapper;

@Autowired @Autowired
private TenantMapper tenantMapper; private TenantMapper tenantMapper;


if (rowCount <= 0) { if (rowCount <= 0) {
return JsonResult.error(AddUserCodeEnum.ADD_IS_FAILED.getCode(), AddUserCodeEnum.ADD_IS_FAILED.getMsg()); return JsonResult.error(AddUserCodeEnum.ADD_IS_FAILED.getCode(), AddUserCodeEnum.ADD_IS_FAILED.getMsg());
} }

return JsonResult.success(); return JsonResult.success();

} }

/** /**
* 检查参数 * 检查参数
* @param tenantId * @param tenantId
* @return * @return
*/ */
private JsonResult check(String tenantId, User user) { private JsonResult check(String tenantId, User user) {
if (StringUtils.isEmpty(user.getCode())) {
return JsonResult.error(AddUserCodeEnum.CODE_IS_NULL.getCode(), AddUserCodeEnum.CODE_IS_NULL.getMsg());
}
if (StringUtils.isEmpty(user.getUsername())) {
return JsonResult.error(AddUserCodeEnum.USERNAME_IS_NULL.getCode(), AddUserCodeEnum.USERNAME_IS_NULL.getMsg());
}
if (StringUtils.isEmpty(user.getRealname())) {
return JsonResult.error(AddUserCodeEnum.REALNAME_IS_NULL.getCode(), AddUserCodeEnum.REALNAME_IS_NULL.getMsg());
}
if (ObjectUtil.isNull(user.getRoleId()) || 0 == user.getRoleId()) {
return JsonResult.error(AddUserCodeEnum.ROLE_ID_IS_NULL.getCode(), AddUserCodeEnum.ROLE_ID_IS_NULL.getMsg());
}
if (StringUtils.isEmpty(user.getDeptId())) {
return JsonResult.error(AddUserCodeEnum.DEPT_ID_IS_NULL.getCode(), AddUserCodeEnum.DEPT_ID_IS_NULL.getMsg());
}

if (ObjectUtil.isNull(user.getType()) || 0 == user.getType()) {
return JsonResult.error(AddUserCodeEnum.TYPE_ID_IS_NULL.getCode(), AddUserCodeEnum.TYPE_ID_IS_NULL.getMsg());
} else {
UserTypeEnum userTypeEnum = UserTypeEnum.parse(user.getType());
if (null == userTypeEnum) {
return JsonResult.error(AddUserCodeEnum.TYPE_ID_IS_NOT_EXISTS.getCode(), AddUserCodeEnum.TYPE_ID_IS_NOT_EXISTS.getMsg());
}
}

if (StringUtils.isEmpty(user.getClientId())) {
return JsonResult.error(AddUserCodeEnum.CLIENT_ID_IS_NULL.getCode(), AddUserCodeEnum.CLIENT_ID_IS_NULL.getMsg());
}
if (StringUtils.isNotEmpty(user.getMobile())) { if (StringUtils.isNotEmpty(user.getMobile())) {
//校验手机号 //校验手机号
boolean flag = CommonUtils.isMobile(user.getMobile()); boolean flag = CommonUtils.isMobile(user.getMobile());
if (count > 0) { if (count > 0) {
return JsonResult.error(AddUserCodeEnum.USERNAME_ALREADY_EXISTS.getCode(), AddUserCodeEnum.USERNAME_ALREADY_EXISTS.getMsg()); return JsonResult.error(AddUserCodeEnum.USERNAME_ALREADY_EXISTS.getCode(), AddUserCodeEnum.USERNAME_ALREADY_EXISTS.getMsg());
} }
count = roleMapper.selectCount(new LambdaQueryWrapper<Role>()
.eq(Role::getTenantId, tenantId)
.eq(Role::getId, user.getRoleId())
.eq(Role::getMark, 1));
if (count <= 0) {
return JsonResult.error(AddUserCodeEnum.ROLE_IS_NOT_EXISTS.getCode(), AddUserCodeEnum.ROLE_IS_NOT_EXISTS.getMsg());
}
return JsonResult.success(); return JsonResult.success();
} }


} }


//id //id
user.setCreateUser(CurrentUserUtil.getUserId());
user.setTenantId(tenantId);
user.setCityName(""); user.setCityName("");
user.setTenantId(tenantId);
user.setCreateUser(CurrentUserUtil.getUserId()); user.setCreateUser(CurrentUserUtil.getUserId());
user.setCreateTime(DateUtils.now()); user.setCreateTime(DateUtils.now());
} }

Loading…
Cancel
Save