源码获取:博客首页 "资源" 里下载!

考试流程:

  1. 用户前台注册成为学生

  2. 管理员后台添加老师,系统将该用户角色上升为老师

  3. 老师登录,添加考试,添加题目,发布考试

  4. 考生登录前台参加考试,交卷

  5. 老师后台批改试卷,查看成绩

  6. 考试查看成绩

练习流程:

  1. 考生登录前台参加练习,练习完自动判分,记录错题

  2. 考生查看成绩,查看错题

角色控制层:



/**
 * 角色控制层
 */
@RestController
@RequestMapping("/v1/authorities")
public class AuthorityController {

    private static Logger logger = LoggerFactory.getLogger(AuthorityController.class);

    @Autowired
    AuthorityService authorityService;

    @ApiOperation(value = "获取权限列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<Authority> getAuthorityList() {
        return authorityService.getAuthorityList();
    }

    @ApiOperation(value = "获取权限树列表", notes = "")
    @RequestMapping(value = "/tree/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<DtoAuthority> getAuthorityTreeList(@PathVariable String id) {
        return authorityService.getAuthorityTreeList(id);
    }

    @ApiOperation(value = "新增权限", notes = "新增权限")
    @ApiImplicitParam(name = "authority", value = "权限实体authority", required = true, dataType = "Authority")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> postAuthority(@RequestBody Authority authority) {
        authorityService.saveAuthority(authority);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @ApiOperation(value = "获取权限信息", notes = "根据权限id获取权限详细信息")
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public Authority getAuthority(@PathVariable String id) {
        return authorityService.getAuthority(id);
    }

    @ApiOperation(value = "更新权限信息", notes = "根据权限id更新权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "authority", value = "权限实体", required = true, dataType = "Authority")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> putAuthority(@PathVariable String id, @RequestBody Authority authority) {
        authorityService.updateAuthority(authority);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "删除权限", notes = "根据权限id删除用户")
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> deleteAuthority(@PathVariable String id) {
        authorityService.deleteAuthority(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}

联系人控制层:

/**
 * 联系人控制层
 */
@RestController
@RequestMapping("/v1/contacts")
public class ContactController {

    private static Logger logger = LoggerFactory.getLogger(ContactController.class);

    @Autowired
    ContactService contactService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public PageInfo<Grade> getContactList(@RequestParam(required = false) Integer pageIndex,
                                        @RequestParam(required = false) Integer pageSize,
                                        @RequestParam(required = false) Integer limit,
                                        @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<Contact> contacts = contactService.getContactList();
        PageInfo pageInfo = new PageInfo(contacts);
        return pageInfo;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public Contact getContact(@PathVariable Long id) {
        return contactService.getContactById(id);
    }

    @RequestMapping(value = "/user/{username}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public Contact getContact(@PathVariable String username) {
        return contactService.getContactByUsername(username);
    }

    @RequestMapping(value = "/users/{username}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<Contact> getContactList(@PathVariable String username) {
        return contactService.getContactListByUsername(username);
    }

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public int getContactCount(@RequestParam String status) {
        return contactService.getContactCountByStatus(status);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<?> postContact(@RequestBody Contact contact) {
        contactService.saveContact(contact);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> putContact(@RequestBody Contact contact) {
        contactService.updateContact(contact);
        return new ResponseEntity(HttpStatus.OK);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> deleteContact(@PathVariable Long id) {
        Contact contact = new Contact();
        contact.setId(id);
        contactService.deleteContact(contact);
        return new ResponseEntity(HttpStatus.OK);
    }
}

用户控制层:

/**
 * 用户控制层
 */
@RestController
@RequestMapping(value = "/v1/users")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Value("${my.localFilepath}")
    private String localFilepath;

    @Value("${my.fileServer}")
    private String fileServer;

    @Autowired
    UserService userService;

    @ApiOperation(value = "获取用户列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public PageInfo<MapperUser> getUserList(@RequestParam(required = false) Integer pageIndex,
                                            @RequestParam(required = false) Integer pageSize,
                                            @RequestParam(required = false) Integer limit,
                                            @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<MapperUser> mapperUsers = userService.getUserList();
        PageInfo pageInfo = new PageInfo(mapperUsers);
        return pageInfo;
    }

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "MapperUser")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> postUser(@RequestBody MapperUser user) {
        userService.saveUser(user);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/id", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public MapperUser getUserById(@RequestParam String id) {
        return userService.getUserById(id);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户name获取用户详细信息")
    @ApiImplicitParam(name = "name", value = "用户name", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/name", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<MapperUser> getUserFuzzyByName(@RequestParam String name) {
        //模糊查询
        return userService.getUserFuzzy(name);
    }

    @ApiOperation(value = "更新用户信息", notes = "根据用户id更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "MapperUser")
    })
    @RequestMapping(value = "/id", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> putUser(@RequestBody MapperUser user) {
        userService.updateUser(user);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "删除用户", notes = "根据用户id删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/id", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> deleteUser(@RequestParam String id) {
        userService.deleteUser(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户名获取用户详细信息")
    @RequestMapping(value = "/me", method = RequestMethod.GET)
    public MapperUser getUser(Principal principal) {
        MapperUser user = null;
        if(principal != null) {
            user = userService.getUserByName(principal.getName());
        }
        return user;
    }

    @ApiOperation(value = "注册", notes = "用户注册")
    @ApiImplicitParam(name = "dtoUser", value = "用户实体", required = true, dataType = "DtoUser")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity<?> registry(@RequestBody DtoUser dtoUser) {
        BCryptPasswordEncoder bc=new BCryptPasswordEncoder(4);//将密码加密
        dtoUser.setPassword(bc.encode(dtoUser.getPassword()));
        userService.registry(dtoUser);
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 注册时验证用户名是否存在
     * true:用户名已存在
     * false:用户名不存在,可以使用此用户名注册
     * @param username
     * @return
     */
    @ApiOperation(value = "注册时验证用户名是否存在", notes = "注册时验证用户名是否存在")
    @RequestMapping(value = "/register/name", method = RequestMethod.GET)
    public boolean getUserByName(@RequestParam String username) {
        if(userService.getUserByName(username) == null) {
            return true;
        }else {
            return false;
        }
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ApiImplicitParam(name = "dtoUser", value = "用户", required = true, dataType = "DtoUser")
    @RequestMapping(value = "/password", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public ResponseEntity<?> changePassword(@RequestBody DtoUser dtoUser, Principal principal) {
        String username = dtoUser.getUsername();
        if(username == null) {
            username = principal.getName();
        }
        MapperUser user = userService.getUserByName(username);
        if(user == null) {
            logger.error("修改密码->用户名不存在!");
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        BCryptPasswordEncoder bc=new BCryptPasswordEncoder(4);
        //判断旧密码是否匹配
        if(bc.matches(dtoUser.getOldPwd(),user.getPassword())) {
            //更新密码
            user.setPassword(bc.encode(dtoUser.getNewPwd()));
            userService.updateUser(user);
        }else {
            return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(HttpStatus.OK);
    }

    @RequestMapping(value = "/avatar", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public ResponseEntity<?>  uploadImg(HttpServletRequest request, Principal principal) {
        //获取当前用户信息
        MapperUser user = null;
        if(principal != null) {
            user = userService.getUserByName(principal.getName());
        }
        //解析器解析request的上下文
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver(request.getSession().getServletContext());
        //先判断request中是否包涵multipart类型的数据,
        if(multipartResolver.isMultipart(request)){
            //再将request中的数据转化成multipart类型的数据
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            Iterator iter = multiRequest.getFileNames();
            while(iter.hasNext()){
                //这里的name为fileItem的alias属性值,相当于form表单中name
                String name=(String)iter.next();
                //根据name值拿取文件
                MultipartFile file = multiRequest.getFile(name);
                if(file != null){
                    String[] names = file.getOriginalFilename().split("\\.");
                    String fileName = user.getUsername() + "." + names[1];
                    File localFile = new File(localFilepath + fileName);
                    if(!localFile.getParentFile().exists()) {
                        //如果目标文件所在的目录不存在,则创建父目录
                        localFile.getParentFile().mkdirs();
                    }
                    //写文件到本地
                    try {
                        file.transferTo(localFile);
                        //更新用户信息
                        user.setAvatar(fileServer + fileName);
                        userService.updateUser(user);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return new ResponseEntity<Object>(HttpStatus.EXPECTATION_FAILED);
                    }
                }
            }
        }else {
            return new ResponseEntity<Object>(HttpStatus.EXPECTATION_FAILED);
        }
        return new ResponseEntity<Object>(HttpStatus.OK);
    }
}

 源码获取:博客首页 "资源" 里下载!

Logo

前往低代码交流专区

更多推荐