SpringBoot+Shiro+LayUI权限管理系统项目-7.实现用户管理

1.说明

只讲解关键部分,详细看源码,文章下方捐赠或QQ联系捐赠获取。

2.功能展示

包括用户增删改查和分配角色。
在这里插入图片描述
在这里插入图片描述

3.业务模型

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_user")
public class SysUser implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 用户密码
     */
    private String password;

    /**
     * 真实姓名
     */
    private String truename;


    /**
     * 邮箱
     */
    private String email;

    /**
     * 电话
     */
    private String phone;

    /**
     * 所属部门ID
     */
    private Integer deptId;

    /**
     * 头像地址
     */
    //private String avatar;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 创建人
     */
    private String createUser;

    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 修改人
     */
    private String updateUser;

    @TableField(exist=false)
    private String deptName;

    @TableField(exist=false)
    private SysRole role;

    @TableField(exist=false)
    private SysDept dept;


}

4.控制器

@Controller
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysDeptService deptService;

    @Autowired
    private SysRoleUserService roleUserService;

    @GetMapping("listUI")
    public String listUI() {
        return "user/list";
    }

    @GetMapping("toSelectDept")
    public String SelectDept() {
        return "user/selectDept";
    }

    @GetMapping("selectRole/{userId}")
    public String SelectRole(@PathVariable Integer userId, Model model) {
        // 根据用户ID查询其拥有的角色
        //List<Map<String,Object>> roles = userService.selectRolesByUserId(userId);
        SysUser entity = userService.getById(userId);
        model.addAttribute("user", entity);

        return "user/selectRole";
    }

    @PostMapping("list")
    @ResponseBody
    public Result<IPage<SysUser>> list(@RequestParam(value = "username", required = false) String username,
                                       @RequestParam(value = "deptId", required = false) Integer deptId,
                                       @RequestParam(defaultValue = "1") Integer pageNo,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        // 获取当前已登录用户部门ID
        /*Integer currentDeptId = getUserEntity().getDeptId();
        if(deptId==null){
            deptId = currentDeptId;
        }*/
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        Page<SysUser> page = new Page<>(pageNo,pageSize);
        IPage<SysUser> result = userService.selectUserList(page, username, deptId);
        // 设置总记录数
        result.setTotal(userService.count(queryWrapper));


        return ResultUtil.ok(result);
    }

    @GetMapping("checkAccount")
    @ResponseBody
    public boolean checkAccount(@RequestParam(required=true) String username) {
        List<SysUser> user = (List<SysUser>) userService.listByMap(ConvertUtil.toMap("username",(Object)username));
        if(!Validator.isNullOrEmpty(user)){
            return false;
        }
        return true;
    }

    @OperLog(operModule = "用户管理",operType = "修改",operDesc = "修改用户")
    @PostMapping("save")
    @ResponseBody
    public Result<String> add(@RequestBody SysUser user, Map<String,Object> map){
        if(user.getId()==null){
            // 检查用户是否存在
            if(!checkAccount(user.getUsername())){
                return ResultUtil.fail("用户名已存在!");
            }
            // 设置添加用户的密码和加密盐
            user.setPassword(MD5Util.md5Password(user.getPassword(),2));
            //user.setSalt(userEntity.getSalt());
            // 设置创建者姓名
            user.setCreateUser("");
            user.setCreateTime(new Date());

            // 保存用户
            boolean result = userService.save(user);
            if(!result)
            {
                return ResultUtil.fail("添加失败!");
            }

            // 添加角色和用户关系记录
            /*SysRoleUser roleUser = new SysRoleUser();
            roleUser.setRoleId(user.getRole().getId());
            roleUser.setUserId(user.getId());
            roleUserService.save(roleUser);*/
        }else{
            user.setUpdateTime(new Date());
            userService.saveOrUpdate(user);
        }
        return ResultUtil.ok();
    }

    @OperLog(operModule = "用户管理",operType = "删除",operDesc = "删除用户")
    @PostMapping("remove")
    @ResponseBody
    public Result<String> remove(@RequestParam Integer id) {
        // 1.删除用户与角色的关联记录
        Map<String,Object> param = new HashMap<>();
        param.put("user_id", id);
        roleUserService.removeByMap(param);
        // 2.删除用户
        if(!userService.removeById(id)){
            return ResultUtil.fail("删除失败!");
        }
        return ResultUtil.ok();
    }

    @GetMapping("info/{userId}")
    public String selectInfo(Map<String,Object> map,@PathVariable(required=true) Integer userId) {
        SysUser user = userService.getById(userId);
        map.put("user", user);
        return "user/info";
    }

    // 根据用户Id查询其拥有的角色
    @GetMapping("/getRoles/{userId}")
    @ResponseBody
    public List<SysRole> getRoles(@PathVariable Integer userId){
        List<SysRole> roles = userService.selectRolesByUserId(userId);

        return roles;
    }

    // 保存用户角色
    @OperLog(operModule = "用户管理",operType = "修改",operDesc = "分配角色")
    @PostMapping("saveRole")
    @ResponseBody
    public Result<String> saveRole(@RequestParam Integer userId, @RequestParam(value = "roleIds[]") Integer[] roleIds){
        userService.saveRole(userId, Arrays.asList(roleIds));
        return ResultUtil.ok();
    }

}

5.前端页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/static/plugin/layui/css/layui.css}" media="all">
</head>
<body>
<div class="searchTable">
    用户名:
    <div class="layui-inline">
        <input class="layui-input" name="s_username" id="s_username" autocomplete="off">
    </div>
    <button id="searchBtn" class="layui-btn" data-type="reload">搜索</button>
</div>

<table class="layui-hide" id="SysUser" lay-filter="SysUser"></table>

<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">

<!--编辑表单-->
<div class="layui-row" id="editForm" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px">
            <input type="text" id="id" name="id" hidden="hidden">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">所属部门</label>
                    <div class="layui-input-line" style="float: left;">
                        <input type="text" id="deptName" name="deptName" required lay-verify="required" autocomplete="off"
                               class="layui-input" disabled="disabled">
                    </div>
                        <button class="layui-btn layui-btn-sm" id="chooseDept" type="button" style="float: left;margin-left: 1.5em;">
                            <i class="layui-icon">&#xe615;</i>
                        </button>

                    <input type="hidden" id="deptId" name="deptId"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" id="username" name="username" required lay-verify="required" autocomplete="off"
                           placeholder="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" id="password" name="password" required lay-verify="required"
                           autocomplete="off"
                           placeholder="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" id="truename" name="truename" required lay-verify="required" autocomplete="off"
                           placeholder="" class="layui-input">
                </div>
            </div>
            <!--<div class="layui-form-item">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block">
                    <select id="roleId" name="roleId" lay-verify="required">
                        <option value=""></option>
                    </select>
                    <div class="layui-unselect layui-form-select">
                        <div class="layui-select-title">
                            <input type="text" placeholder="请选择" value="" readonly=""
                                   class="layui-input layui-unselect">
                            <i class="layui-edge"></i>
                        </div>
                    </div>
                </div>
            </div>-->
            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="confirm">确认</button>
                    <button type="button" class="layui-btn layui-btn-primary" id="back">关闭</button>
                </div>
            </div>
        </form>
    </div>
</div>

6 获取源码

捐赠任意金额,评论区留下邮箱发送 :)

在这里插入图片描述

posted @ 2021-04-19 23:00  一锤子技术员  阅读(21)  评论(0编辑  收藏  举报  来源