das接口新增

This commit is contained in:
chenhaojie 2024-06-26 14:54:29 +08:00
parent 760c86b649
commit f9507acb06
13 changed files with 372 additions and 42 deletions

View File

@ -22,7 +22,7 @@ public class FilterConfig {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//注册过滤器
registrationBean.setFilter(new DecryptingOncePerRequestFilter(aesProperties.getKey()));
registrationBean.addUrlPatterns("/api/*"); // 设置过滤器应用的URL模式
registrationBean.addUrlPatterns("/api/at"); // 设置过滤器应用的URL模式
registrationBean.setOrder(2); // 设置过滤器的顺序
return registrationBean;
}

View File

@ -6,6 +6,7 @@ import com.das.common.utils.PageQuery;
import com.das.modules.auth.domain.dto.DeleteDto;
import com.das.modules.auth.domain.dto.SysUserDto;
import com.das.modules.auth.domain.dto.SysUserQueryDto;
import com.das.modules.auth.domain.vo.SysUserVo;
import com.das.modules.auth.entity.SysUser;
import com.das.modules.auth.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
@ -56,7 +57,7 @@ public class SysUserController {
}
@PostMapping("/query")
public PageDataInfo<SysUser> queryUserList(@RequestBody SysUserQueryDto sysUserQueryDto, PageQuery pageQuery) {
public PageDataInfo<SysUserVo> queryUserList(@RequestBody SysUserQueryDto sysUserQueryDto, PageQuery pageQuery) {
return sysUserService.queryUserList(sysUserQueryDto, pageQuery);
}
}

View File

@ -4,33 +4,48 @@ import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Data
public class SysUserDto implements Serializable {
/** 角色ID */
private Long id ;
/** 账号 */
private String account ;
/** 密码 */
private String password ;
/** 职员名称 */
private String userName ;
/** 职员邮箱 */
private String email ;
/** 手机号 */
private String phone ;
/** 所属机构id */
private Long orgId ;
/** 最后登录时间 */
private Date lastLogin ;
/** 乐观锁 */
private Integer revision ;
/** 创建人 */
private String createdBy ;
/** 创建时间 */
private Date createdTime ;
/** 更新人 */
private String updatedBy ;
/** 更新时间 */
private Date updatedTime ;
}
/**
* 角色ID
*/
private Long id;
/**
* 账号
*/
private String account;
/**
* 密码
*/
private String password;
/**
* 职员名称
*/
private String userName;
/**
* 职员邮箱
*/
private String email;
/**
* 手机号
*/
private String phone;
/**
* 所属机构id
*/
private Long orgId;
/**
* 最后登录时间
*/
private Date lastLogin;
/**
* 乐观锁
*/
private Integer revision;
/**
* 角色列表
*/
private List<Long> roleList;
}

View File

@ -0,0 +1,38 @@
package com.das.modules.auth.domain.vo;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* 账号信息
*
* @author guchengwei
*/
@Data
public class SysUserRoleVo implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 账号ID
*/
private Long id;
/**
* 角色编码
*/
private String roleCode;
/**
* 角色名称
*/
private String roleName;
/**
* 版本号
*/
private Integer revision;
}

View File

@ -4,6 +4,7 @@ import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
/**
* 账号信息
@ -29,6 +30,12 @@ public class SysUserVo implements Serializable {
*/
private String userName;
/**
* 乐观锁
*/
private Integer revision;
/**
* 手机号
*/
@ -45,5 +52,10 @@ public class SysUserVo implements Serializable {
*/
private Long orgId;
/**
* 角色列表
*/
private List<SysUserRoleVo> roleList;
}

View File

@ -0,0 +1,57 @@
package com.das.modules.auth.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import com.das.common.constant.BaseEntity;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* (sys_r_role_authority)实体类
*
* @author chenhaojie
* @since 2024-06-25
*/
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_r_user_role")
public class SysUserRole extends BaseEntity implements Serializable {
/**
* 主键
*/
@TableId("id")
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
/**
* 用户id
*/
@TableField("user_id")
@JsonSerialize(using = ToStringSerializer.class)
private Long userId;
/**
* 角色id
*/
@TableField("role_id")
@JsonSerialize(using = ToStringSerializer.class)
private Long roleId;
/**
* 乐观锁
*/
@Version
@TableField("revision")
private Integer revision;
}

View File

@ -32,5 +32,7 @@ public interface SysRoleMapper extends BaseMapper<SysRole> {
SysRole selectByCode(@Param("roleCode") String roleCode);
long existRoleByRoleId(@Param("id")Long id);
long existRoleByName(@Param("name") String name);
}

View File

@ -2,6 +2,7 @@ package com.das.modules.auth.mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.das.modules.auth.domain.dto.SysUserQueryDto;
import com.das.modules.auth.domain.vo.SysUserVo;
import com.das.modules.auth.entity.SysUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
@ -14,5 +15,15 @@ import org.apache.ibatis.annotations.Param;
* @author chenhaojie
*/
public interface SysUserMapper extends BaseMapper<SysUser> {
IPage<SysUser> queryUserList(IPage<SysUser> page, @Param("sysUser") SysUserQueryDto sysUserQueryDto);
/**
* 判断用户是否存在
*
* @param account 用户账号
* @return 0 - 不存在 1 - 存在
*/
long existUserByAccount(@Param("account") String account);
IPage<SysUserVo> queryUserList(IPage<SysUserVo> page, @Param("sysUser") SysUserQueryDto sysUserQueryDto);
long existUserByUserName(@Param("userName") String userName);
}

View File

@ -0,0 +1,18 @@
package com.das.modules.auth.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.das.modules.auth.entity.SysRoleAuthority;
import com.das.modules.auth.entity.SysUserRole;
/**
* <p>
* 用户信息 Mapper 接口
* </p>
*
* @author chenhaojie
*/
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {
}

View File

@ -5,14 +5,15 @@ import com.das.common.utils.PageQuery;
import com.das.modules.auth.domain.dto.DeleteDto;
import com.das.modules.auth.domain.dto.SysUserDto;
import com.das.modules.auth.domain.dto.SysUserQueryDto;
import com.das.modules.auth.domain.vo.SysUserVo;
import com.das.modules.auth.entity.SysUser;
public interface SysUserService {
SysUser createUser(SysUserDto sysUserDto);
SysUserDto createUser(SysUserDto sysUserDto);
void updateUser(SysUserDto sysUserDto);
SysUserDto updateUser(SysUserDto sysUserDto);
void deleteUser(DeleteDto deleteDto);
PageDataInfo<SysUser> queryUserList(SysUserQueryDto sysUserQueryDto, PageQuery pageQuery);
PageDataInfo<SysUserVo> queryUserList(SysUserQueryDto sysUserQueryDto, PageQuery pageQuery);
}

View File

@ -1,7 +1,9 @@
package com.das.modules.auth.service.impl;
import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.das.common.exceptions.ServiceException;
import com.das.common.utils.BeanCopyUtils;
import com.das.common.utils.SequenceUtils;
import com.das.common.utils.PageDataInfo;
@ -9,21 +11,50 @@ import com.das.common.utils.PageQuery;
import com.das.modules.auth.domain.dto.DeleteDto;
import com.das.modules.auth.domain.dto.SysUserDto;
import com.das.modules.auth.domain.dto.SysUserQueryDto;
import com.das.modules.auth.domain.vo.SysUserVo;
import com.das.modules.auth.entity.SysRole;
import com.das.modules.auth.entity.SysRoleAuthority;
import com.das.modules.auth.entity.SysUser;
import com.das.modules.auth.entity.SysUserRole;
import com.das.modules.auth.mapper.SysRoleMapper;
import com.das.modules.auth.mapper.SysUserMapper;
import com.das.modules.auth.mapper.SysUserRoleMapper;
import com.das.modules.auth.service.SysUserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class SysUserServiceImpl implements SysUserService {
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
@Autowired
SysUserMapper sysUserMapper;
@Autowired
SysRoleMapper sysRoleMapper;
@Autowired
SysUserRoleMapper sysUserRoleMapper;
@Override
public SysUser createUser(SysUserDto sysUserDto) {
public SysUserDto createUser(SysUserDto sysUserDto) {
//根据账号account判断是否存在
long roleCount = sysUserMapper.existUserByAccount(sysUserDto.getAccount());
if (roleCount > 0) {
throw new ServiceException(String.format("账号 %s 已存在", sysUserDto.getAccount()));
}
SysUser sysUser = new SysUser();
BeanCopyUtils.copy(sysUserDto,sysUser);
//密码加密
@ -34,24 +65,129 @@ public class SysUserServiceImpl implements SysUserService {
sysUser.setUpdatedTime(new Date());
sysUser.setPassword(passwordEncode);
sysUserMapper.insert(sysUser);
return sysUser;
if (sysUserDto.getRoleList() != null && sysUserDto.getRoleList().size() > 0) {
//验证角色有效性
String errorRoles = checkErrorRole(sysUserDto.getRoleList());
if (StringUtils.hasText(errorRoles)) {
throw new ServiceException("角色不存在 " + errorRoles);
}
//解锁role与Account绑定
QueryWrapper<SysUserRole> accountRoleQueryWrapper = new QueryWrapper<>();
accountRoleQueryWrapper.eq("user_id",sysUser.getId());
sysUserRoleMapper.delete(accountRoleQueryWrapper);
//绑定角色
for (Long roleId : sysUserDto.getRoleList()) {
SysUserRole accountRole = new SysUserRole();
accountRole.setUserId(sysUser.getId());
accountRole.setRoleId(roleId);
accountRole.setRevision(1);
accountRole.setCreatedTime(new Date());
accountRole.setUpdatedTime(new Date());
sysUserRoleMapper.insert(accountRole);
}
}
// 消除密码
sysUserDto.setPassword(null);
return sysUserDto;
}
/**
* 验证角色是否存在
*/
private String checkErrorRole(List<Long> roles) {
if (roles != null && roles.size() > 0) {
List<Long> errorRole = new ArrayList<>();
for (Long roleId : roles) {
if (roleId == null) {
continue;
}
long roleCount = sysRoleMapper.existRoleByRoleId(roleId);
if (roleCount == 0) {
errorRole.add(roleId);
}
}
if (errorRole.size() > 0) {
try {
return JSON_MAPPER.writeValueAsString(errorRole);
} catch (JsonProcessingException e) {
return null;
}
}
}
return null;
}
@Override
public void updateUser(SysUserDto sysUserDto) {
SysUser sysUser = new SysUser();
BeanCopyUtils.copy(sysUserDto,sysUser);
sysUserMapper.updateById(sysUser);
public SysUserDto updateUser(SysUserDto sysUserDto) {
if (sysUserDto.getId() == null) {
throw new ServiceException("参数缺失");
}
SysUser sysUser = sysUserMapper.selectById(sysUserDto.getId());
if (sysUser == null) {
throw new ServiceException(String.format("账号 %d 不存在", sysUserDto.getId()));
}
if (StringUtils.hasText(sysUserDto.getUserName())) {
//若与数据库中账户名相同 则表示未对名称进行修改
if (!sysUser.getUserName().equals(sysUserDto.getUserName())) {
//判断账户名是否存在
long userCount = sysUserMapper.existUserByUserName(sysUserDto.getUserName());
if (userCount > 0) {
throw new ServiceException(String.format("账户名 %s 已存在", sysUserDto.getUserName()));
}
}
}
SysUser newSysUser = new SysUser();
BeanUtils.copyProperties(sysUserDto, newSysUser, "password");
sysUserMapper.updateById(newSysUser);
if (sysUserDto.getRoleList() != null) {
//验证角色有效性
String errorRoles = checkErrorRole(sysUserDto.getRoleList());
if (StringUtils.hasText(errorRoles)) {
throw new ServiceException("角色不存在 " + errorRoles);
}
//解锁role与Account绑定
QueryWrapper<SysUserRole> accountRoleQueryWrapper = new QueryWrapper<>();
accountRoleQueryWrapper.eq("user_id",newSysUser.getId());
sysUserRoleMapper.delete(accountRoleQueryWrapper);
//绑定角色
for (Long roleId : sysUserDto.getRoleList()) {
SysUserRole accountRole = new SysUserRole();
accountRole.setUserId(sysUser.getId());
accountRole.setRoleId(roleId);
accountRole.setRevision(1);
accountRole.setCreatedTime(new Date());
accountRole.setUpdatedTime(new Date());
sysUserRoleMapper.insert(accountRole);
}
}
return sysUserDto;
}
@Override
public void deleteUser(DeleteDto deleteDto) {
if (StringUtils.isEmpty(deleteDto.getId())) {
throw new ServiceException("参数缺失");
}
SysUser sysUser = sysUserMapper.selectById(deleteDto.getId());
if (sysUser == null) {
throw new ServiceException("账号不存在");
}
//解锁role与用户绑定
QueryWrapper<SysUserRole> accountRoleQueryWrapper = new QueryWrapper<>();
accountRoleQueryWrapper.eq("user_id", deleteDto.getId());
this.sysUserRoleMapper.delete(accountRoleQueryWrapper);
//删除角色
sysUserMapper.deleteById(deleteDto.getId());
}
@Override
public PageDataInfo<SysUser> queryUserList(SysUserQueryDto sysUserQueryDto, PageQuery pageQuery) {
IPage<SysUser> iPage = sysUserMapper.queryUserList(pageQuery.build(), sysUserQueryDto);
public PageDataInfo<SysUserVo> queryUserList(SysUserQueryDto sysUserQueryDto, PageQuery pageQuery) {
IPage<SysUserVo> iPage = sysUserMapper.queryUserList(pageQuery.build(), sysUserQueryDto);
return PageDataInfo.build(iPage.getRecords(), iPage.getTotal());
}
}

View File

@ -35,6 +35,10 @@
select count(1) from sys_role t where t.role_code=#{roleCode}
</select>
<select id="existRoleByRoleId" resultType="java.lang.Long">
select count(1) from sys_role t where t.id=#{id}
</select>
<select id="queryAuthorityById" resultType="com.das.modules.auth.domain.vo.SysRoleAuthVo">
select sa.id, sa.authority_code as authorityCode, sa.authority_name as authorityName from sys_authority sa left join sys_r_role_authority srra on sa.id = srra.authority_id
where srra.role_id = #{id}

View File

@ -18,7 +18,42 @@
<result property="updatedTime" column="updated_time" jdbcType="VARCHAR"/>
</resultMap>
<select id="queryUserList" resultMap="SysUserMap">
<resultMap type="com.das.modules.auth.domain.vo.SysUserVo" id="SysUserRoleMap">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="account" column="account" jdbcType="VARCHAR"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="email" column="email" jdbcType="VARCHAR"/>
<result property="phone" column="phone" jdbcType="VARCHAR"/>
<result property="orgId" column="org_id" jdbcType="VARCHAR"/>
<result property="lastLogin" column="last_login" jdbcType="VARCHAR"/>
<result property="revision" column="revision" jdbcType="VARCHAR"/>
<collection property="roleList" column="id" ofType="com.das.modules.auth.domain.vo.SysUserRoleVo" select="queryRoleByUserId"/>
</resultMap>
<resultMap type="com.das.modules.auth.domain.vo.SysUserRoleVo" id="UserRoleRelationMap">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="roleCode" column="role_code" jdbcType="VARCHAR"/>
<result property="roleName" column="role_name" jdbcType="VARCHAR"/>
<result property="revision" column="revision" jdbcType="VARCHAR"/>
</resultMap>
<select id="existUserByAccount" resultType="java.lang.Long">
select count(1) from sys_user t where t.account=#{account}
</select>
<select id="existUserByUserName" resultType="java.lang.Long">
select count(1) from sys_user t where t.user_name=#{userName}
</select>
<select id="queryRoleByUserId" resultMap="UserRoleRelationMap">
select sr.role_code,sr.role_name ,sr.id ,sr.revision from sys_role sr left join sys_r_user_role srur on sr.id = srur.role_id
where srur.user_id = #{id}
</select>
<select id="queryUserList" resultMap="SysUserRoleMap">
select * from sys_user
<where>
<if test="sysUser.userName != null and sysUser.userName != ''">