系统功能结构图
具体实现
4.1 系统登录界面实现
打开系统,首先进入的是登陆界面,在此业主和管理人员可以进行登录,进入到系统中,实现该功能的关键步骤如下:
(1)创建一个网站页面,命名为login。使用layUI设置登录用的窗口和系统提示弹窗
(2)使用代码实现对窗口内的账号监听,从数据库中匹配相应的账户,判断账户类型,之后进入系统。登陆界面如图4-1所示。
图4-1登陆界面
4.2 系统主页前端实现
在系统用户或者系统管理员登陆成功后,会进入到不同的主页当中。主页是该系统的框架页,由上,中,左三部分组成。系统主页界面如图4-2所示。
图4-2系统主页界面
4.4 搜索功能实现
当点击首页左侧菜单栏的“社区管理”目录下的 “社区列表”链接时,会加载“社区 列表页面”到右侧内容显示位置进行社区列表数据展示。社区列表中显示的是当前可管理的社区信息,管理者可以通过翻页进行对所需要查询的社区进行查找,也可以选择时间范围和填写社区名称,或者是搜索关键字,来查看社区的信息。为了方便使用者查询,我们对页面显示数据进行了人性化的修改,使用者可以自主选择页面显示的数据数量,默认为每页显示2条数据。搜索功能实现如图4-3所示。
图4-3搜索功能实现图
关键代码如下:
@Override public Page<Community> search(Map searchMap) { //通用mapper 多条件搜索,标准写法 //第一步 初始化分页条件 int pageNum = 1; int pageSize = 2; Example example = new Example(Community.class); //指定查询的表tbcommunity if(searchMap != null){ Example.Criteria criteria = example.createCriteria();//创建查询条件 //时间区间 if (StringUtil.isNotEmpty((String) searchMap.get("startTime"))){ criteria.andGreaterThanOrEqualTo("createTime",searchMap.get("startTime"));//里面写的属性名,映射关系CreatTime和creat_time映射 } if (StringUtil.isNotEmpty((String) searchMap.get("updateTime"))){ criteria.andLessThanOrEqualTo("updateTime",searchMap.get("updateTime")); } //名称模糊搜索 if (StringUtil.isNotEmpty((String) searchMap.get("name"))){ criteria.andLike("name", "%"+(String) searchMap.get("name")+"%");//模糊搜索有语法规范,前后加上百分号} if((Integer) searchMap.get("pageNum") !=null){ pageNum = (Integer) searchMap.get("pageNum");} if((Integer) searchMap.get("pageSize") !=null){ pageSize = (Integer) searchMap.get("pageSize");} } PageHelper.startPage(pageNum,pageSize);//分页插件 Page<Community> communities = (Page<Community>) communityMapper.selectByExample(example);//跟去查询条件进行查询。默认没有分页,然后转换为page对象 return communities;}
4.5 管理员功能实现
考虑到功能模块过多,以管理员经常使用的社区功能模块进行展示。
4.5.1 新增社区信息
在社区列表页面中点击(添加)按钮,会弹出增加信息的弹出框,管理员可以在弹出框中进行社区信息的添加。添加社区信息功能如图4-4所示。
图4-4添加社区信息图
关键代码如下:
@PostMapping("/add") public Result add(@RequestBody Community community){ Boolean add = communityService.add(community);//调用service中的public boolean add Return new Result(true,StatusCode.OK, MessageConstant.COMMUNITY_ADD_SUCCESS); } @Override public Boolean add(Community community) { int row = communityMapper.insert(community); if(row>0){ return true; }else{ return false;}} /** \* 通用接口,直接继承Mapper 在里面指定泛型(所要操作的实体类)就好 */ @Repository public interface CommunityMapper extends Mapper <Community>{ }
4.5.2 删除社区信息
在社区列表页面中,每一行社区信息末端,设置有操作列,管理员可在操作列中对社区试行删除操作,当管理员点击“删除”图标后,系统弹出弹框进行确认,以此来实现管理员对后台社区信息数据的删除。删除社区信息功能如图4-5所示。
图4-5删除社区信息图
关键代码如下:
@RequestMapping("/del")//请求的路径 public Result del(@RequestBody List<Integer> ids){ Boolean flag = communityService.del(ids); return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_DELETE_SUCCESS); } @Override public Boolean del(List<Integer> ids) { for (Integer id:ids){ communityMapper.deleteByPrimaryKey(id); } return true;}
4.5.3 更改社区信息
更改社区信息功能如图4-6所示。
图4-6更改社区信息图
关键代码如下:
@RequestMapping("/update") public Result update(@RequestBody Community community){ Boolean update = communityService.update(community); return new Result(true,StatusCode.OK, MessageConstant.COMMUNITY_UPDATE_SUCCESS); } //想要转递参数/community/updateStatus/0/1 嵌套路径方法 @RequestMapping("/updateStatus/{status}/{id}") public Result updateStatus(@PathVariable("status") String status, @PathVariable("id") Integer id){ Boolean updateStatus = communityService.updateStatus(status,id);//不需要返回值,返回布尔也是可以的 return new Result(true,StatusCode.OK, MessageConstant.COMMUNITY_UPDATE_STATUS_SUCCESS);//不需要调用数据 } @Override public Boolean update(Community community) { int row = communityMapper.updateByPrimaryKeySelective(community); if(row>0){ return true; } else { return false;} } @Override public Boolean updateStatus(String status, Integer id) { Community community = new Community();//封装了参数 因为需要community community.setId(id); }
4.5.4 查找社区信息
在社区页面顶部,我加入了对社区信息查询的功能,管理者可以根据创建时间查询社区信息,也可以根据社区信息中的关键字,有针对性地查询所需要的信息。查找社区信息这个功能可以极大的方便管理者对社区信息的了解。关键代码如下:
@Override public List<Community> findAll() { List<Community> communities = communityMapper.selectAll(); return communities; /** \* 传递的是个空值,让用controller调用 */ } @Override public Community findById(Integer id) {//小区信息的查询 return communityMapper.selectByPrimaryKey(id);//获取单个对象 } @RequestMapping("/findById") public Result findById(Integer id){ Community community = communityService.findById(id);//定义一个方法并返回小区的对象 return new Result(true,StatusCode.OK, MessageConstant.COMMUNITY_FIND_BY_ID_SUCCESS,community);}
代码参考
@IgnoreAuth @PostMapping(value = "/login") public R login(String username, String password, String captcha, HttpServletRequest request) { UsersEntity user = userService.selectOne(new EntityWrapper<UsersEntity>().eq("username", username)); if(user==null || !user.getPassword().equals(password)) { return R.error("账号或密码不正确"); } String token = tokenService.generateToken(user.getId(),username, "users", user.getRole()); return R.ok().put("token", token); } @Override public String generateToken(Long userid,String username, String tableName, String role) { TokenEntity tokenEntity = this.selectOne(new EntityWrapper<TokenEntity>().eq("userid", userid).eq("role", role)); String token = CommonUtil.getRandomString(32); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.HOUR_OF_DAY, 1); if(tokenEntity!=null) { tokenEntity.setToken(token); tokenEntity.setExpiratedtime(cal.getTime()); this.updateById(tokenEntity); } else { this.insert(new TokenEntity(userid,username, tableName, role, token, cal.getTime())); } return token; } /** * 权限(Token)验证 */ @Component public class AuthorizationInterceptor implements HandlerInterceptor { public static final String LOGIN_TOKEN_KEY = "Token"; @Autowired private TokenService tokenService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //支持跨域请求 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with,request-source,Token, Origin,imgType, Content-Type, cache-control,postman-token,Cookie, Accept,authorization"); response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // 跨域时会首先发送一个OPTIONS请求,这里我们给OPTIONS请求直接返回正常状态 if (request.getMethod().equals(RequestMethod.OPTIONS.name())) { response.setStatus(HttpStatus.OK.value()); return false; } IgnoreAuth annotation; if (handler instanceof HandlerMethod) { annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class); } else { return true; } //从header中获取token String token = request.getHeader(LOGIN_TOKEN_KEY); /** * 不需要验证权限的方法直接放过 */ if(annotation!=null) { return true; } TokenEntity tokenEntity = null; if(StringUtils.isNotBlank(token)) { tokenEntity = tokenService.getTokenEntity(token); } if(tokenEntity != null) { request.getSession().setAttribute("userId", tokenEntity.getUserid()); request.getSession().setAttribute("role", tokenEntity.getRole()); request.getSession().setAttribute("tableName", tokenEntity.getTablename()); request.getSession().setAttribute("username", tokenEntity.getUsername()); return true; } PrintWriter writer = null; response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); try { writer = response.getWriter(); writer.print(JSONObject.toJSONString(R.error(401, "请先登录"))); } finally { if(writer != null){ writer.close(); } } // throw new EIException("请先登录", 401); return false; } }
论文参考







