栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

尚硅谷智慧校园 —— 6、管理员功能实现及修改密码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

尚硅谷智慧校园 —— 6、管理员功能实现及修改密码

目录

1、查询管理员信息(分页带条件)

1.1、在 serivce 及其实现类添加方法

1.2、在 controller 添加方法 

2、添加或修改管理员信息

2.1、在 controller 添加方法

3、删除和批量删除管理员信息

 3.1、在 controller 中添加方法


1、查询管理员信息(分页带条件)

Request URL: http://localhost:8080/sms/adminController/getAllAdmin/{pageNo}/{pageSize}?adminName=%E4%B8%89
Request Method: GET

1.1、在 serivce 及其实现类添加方法

AdminService

    
    IPage getAdminByOpr(Page page, String adminName);

 AdminServiceImpl

    
    @Override
    public IPage getAdminByOpr(Page page, String adminName) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 若adminName的条件不为空,则添加模糊查询的条件
        if(!StringUtils.isEmpty(adminName)){
            queryWrapper.like("name", adminName);
        }
        queryWrapper.orderByDesc("id");
        Page adminPage = baseMapper.selectPage(page, queryWrapper);
        return adminPage;
    }

1.2、在 controller 添加方法 

AdminController

@RestController
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private AdminService adminService;

    
    @ApiOperation("查询管理员信息(分页带条件)")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @ApiParam("分页查询的页码数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("分页查询每页的数据量") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("管理员名字,用于模糊查询") String adminName
    ){
        Page page = new Page<>(pageNo, pageSize);
        IPage iPage = adminService.getAdminByOpr(page, adminName);
        return Result.ok(iPage);
    }

}

2、添加或修改管理员信息

Request URL: http://localhost:8080/sms/adminController/saveOrUpdateAdmin
Request Method: POST 

添加管理员时没有 id 属性,修改管理员会发送 id 

2.1、在 controller 添加方法
    
    @ApiOperation("添加或修改管理员信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(
            @ApiParam("添加或修改的管理员信息") @RequestBody Admin admin
    ){
        Integer id = admin.getId();
        if(null == id || id == 0){
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }

3、删除和批量删除管理员信息

Request URL: http://localhost:8080/sms/adminController/deleteAdmin
Request Method: DELETE

发送要删除的管理员 id 的 JSON 数组 

 3.1、在 controller 中添加方法
    
    @ApiOperation("删除或批量删除管理员信息")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(
            @ApiParam("要删除的管理员的id的JSON数组") @RequestBody List ids
    ){
        adminService.removeByIds(ids);
        return Result.ok();
    }
4、修改密码

Request URL: http://localhost:8080/sms/system/updatePwd/{oldPwd}/{newPwd}
Request Method: POST

在请求头包含 token,蕴含用户信息

4.1、在 controller 添加方法

SystemController

    
    @ApiOperation("修改用户密码")
    @PostMapping("/updatePwd/{oldPwd}/{newPwd}")
    public Result updatePwd(
            @ApiParam("蕴含用户信息的token口令") @RequestHeader("token") String token,
            @ApiParam("原密码") @PathVariable("oldPwd") String olePwd,
            @ApiParam("新密码") @PathVariable("newPwd") String newPwd
    ){
        // 判断token是否在有效期内
        boolean expiration = JwtHelper.isExpiration(token);
        if(expiration){
            return Result.fail().message("token失效,请重新登录");
        }

        // 获取用户id和用户类型
        Long userId = JwtHelper.getUserId(token);
        Integer userType = JwtHelper.getUserType(token);

        olePwd = MD5.encrypt(olePwd);
        newPwd = MD5.encrypt(newPwd);

        switch (userType){
            case 1:
                QueryWrapper adminQueryWrapper = new QueryWrapper<>();
                adminQueryWrapper.eq("id", userId.intValue())
                        .eq("password", olePwd);
                Admin admin = adminService.getOne(adminQueryWrapper);
                if(admin != null){
                    // 找到该用户,修改密码
                    admin.setPassword(newPwd);
                    adminService.saveOrUpdate(admin);
                }else{
                    // 没有找到该用户,返回错误信息
                    return Result.fail().message("原密码错误");
                }
                break;

            case 2:
                QueryWrapper studentQueryWrapper = new QueryWrapper<>();
                studentQueryWrapper.eq("id", userId.intValue())
                        .eq("password", olePwd);
                Student student = studentService.getOne(studentQueryWrapper);
                if(student != null){
                    // 找到该用户,修改密码
                    student.setPassword(newPwd);
                    studentService.saveOrUpdate(student);
                }else{
                    // 没有找到该用户,返回错误信息
                    return Result.fail().message("原密码错误");
                }
                break;

            case 3:
                QueryWrapper teacherQueryWrapper = new QueryWrapper<>();
                teacherQueryWrapper.eq("id", userId.intValue())
                        .eq("password", olePwd);
                Teacher teacher = teacherService.getOne(teacherQueryWrapper);
                if(teacher != null){
                    // 找到该用户,修改密码
                    teacher.setPassword(newPwd);
                    teacherService.saveOrUpdate(teacher);
                }else{
                    // 没有找到该用户,返回错误信息
                    return Result.fail().message("原密码错误");
                }
                break;
        }
        return  Result.ok();
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号