From eae03d88f6038e288b9efa6a7ef005d34c805b20 Mon Sep 17 00:00:00 2001 From: "wangiegie@gmail.com" Date: Sat, 16 Dec 2017 17:11:24 +0800 Subject: [PATCH] =?UTF-8?q?Controller=20=E5=B1=82=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pig-admin-service/pom.xml | 5 ++ .../pig/admin/controller/LogController.java | 20 ++----- .../pig/admin/controller/MenuController.java | 27 +++------- .../pig/admin/controller/RoleController.java | 31 ++++------- .../pig/admin/controller/UserController.java | 2 +- .../pig/admin/service/SysLogService.java | 8 +++ .../pig/admin/service/SysMenuService.java | 18 +++++++ .../pig/admin/service/SysRoleMenuService.java | 12 ++++- .../admin/service/impl/SysLogServiceImpl.java | 15 ++++++ .../service/impl/SysMenuServiceImpl.java | 28 ++++++++++ .../service/impl/SysRoleMenuServiceImpl.java | 22 +++++++- pig-common/pom.xml | 6 +++ .../github/pig/common/bean/ControllerAop.java | 32 ++++++++--- .../com/github/pig/common/util/Assert.java | 53 +++++++++++++++++++ .../github/pig/common/web/BaseController.java | 2 + 15 files changed, 214 insertions(+), 67 deletions(-) create mode 100644 pig-common/src/main/java/com/github/pig/common/util/Assert.java diff --git a/pig-admin-service/pom.xml b/pig-admin-service/pom.xml index c1095b85..545015e9 100644 --- a/pig-admin-service/pom.xml +++ b/pig-admin-service/pom.xml @@ -64,6 +64,11 @@ qiniu-java-sdk [7.2.0, 7.2.99] + + com.github.binarywang + weixin-java-pay + 2.9.0 + diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java index 6547e8d3..2badf745 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java @@ -5,14 +5,12 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.github.pig.admin.service.SysLogService; import com.github.pig.common.constant.CommonConstant; -import com.github.pig.common.entity.SysLog; import com.github.pig.common.util.Query; -import com.github.pig.common.util.UserUtils; +import com.github.pig.common.util.R; import com.github.pig.common.web.BaseController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.Date; import java.util.Map; /** @@ -29,12 +27,6 @@ public class LogController extends BaseController { @Autowired private SysLogService sysLogService; - @PostMapping - public Boolean log(@RequestBody SysLog sysLog) { - sysLog.setCreateBy(UserUtils.getUserName()); - return sysLogService.insert(sysLog); - } - /** * 分页查询日志信息 * @@ -44,7 +36,7 @@ public class LogController extends BaseController { @RequestMapping("/logPage") public Page logPage(@RequestParam Map params) { params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL); - return sysLogService.selectPage(new Query<>(params),new EntityWrapper<>()); + return sysLogService.selectPage(new Query<>(params), new EntityWrapper<>()); } /** @@ -54,11 +46,7 @@ public class LogController extends BaseController { * @return success/false */ @DeleteMapping("/{id}") - public Boolean delete(@PathVariable Integer id) { - SysLog sysLog = new SysLog(); - sysLog.setId(id); - sysLog.setDelFlag(CommonConstant.STATUS_DEL); - sysLog.setUpdateTime(new Date()); - return sysLogService.updateById(sysLog); + public R delete(@PathVariable Integer id) { + return new R<>(sysLogService.updateByLogId(id)); } } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java index d194e098..315257dd 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/MenuController.java @@ -6,6 +6,7 @@ import com.github.pig.admin.entity.SysMenu; import com.github.pig.admin.service.SysMenuService; import com.github.pig.admin.common.util.TreeUtil; import com.github.pig.common.constant.CommonConstant; +import com.github.pig.common.util.R; import com.github.pig.common.vo.MenuVo; import com.github.pig.common.web.BaseController; import org.springframework.beans.factory.annotation.Autowired; @@ -98,8 +99,8 @@ public class MenuController extends BaseController { * @return success/false */ @PostMapping - public Boolean menu(@RequestBody SysMenu sysMenu) { - return sysMenuService.insert(sysMenu); + public R menu(@RequestBody SysMenu sysMenu) { + return new R<>(sysMenuService.insert(sysMenu)); } /** @@ -110,27 +111,13 @@ public class MenuController extends BaseController { * TODO 级联删除下级节点 */ @DeleteMapping("/{id}") - @CacheEvict(value = "menu_details", allEntries = true) - public Boolean menuDel(@PathVariable Integer id) { - // 删除当前节点 - SysMenu condition1 = new SysMenu(); - condition1.setMenuId(id); - condition1.setDelFlag(CommonConstant.STATUS_DEL); - sysMenuService.updateById(condition1); - - // 删除父节点为当前节点的节点 - SysMenu conditon2 = new SysMenu(); - conditon2.setParentId(id); - SysMenu sysMenu = new SysMenu(); - sysMenu.setDelFlag(CommonConstant.STATUS_DEL); - sysMenuService.update(sysMenu, new EntityWrapper<>(conditon2)); - return Boolean.TRUE; + public R menuDel(@PathVariable Integer id) { + return new R<>(sysMenuService.deleteMenu(id,getRole().get(0))); } @PutMapping - @CacheEvict(value = "menu_details", allEntries = true) - public Boolean menuUpdate(@RequestBody SysMenu sysMenu) { - return sysMenuService.updateById(sysMenu); + public R menuUpdate(@RequestBody SysMenu sysMenu) { + return new R<>(sysMenuService.updateMenuById(sysMenu,getRole().get(0))); } private List getMenuTree(List menus, int root) { diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/RoleController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/RoleController.java index 31eb260d..369620ba 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/RoleController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/RoleController.java @@ -48,8 +48,8 @@ public class RoleController extends BaseController { * @return success、false */ @PostMapping - public R role(@RequestBody SysRole sysRole) { - return new R(sysRoleService.insert(sysRole)); + public R role(@RequestBody SysRole sysRole) { + return new R<>(sysRoleService.insert(sysRole)); } /** @@ -59,16 +59,15 @@ public class RoleController extends BaseController { * @return success/false */ @PutMapping - public Boolean roleUpdate(@RequestBody SysRole sysRole) { - sysRoleService.updateById(sysRole); - return Boolean.TRUE; + public R roleUpdate(@RequestBody SysRole sysRole) { + return new R<>(sysRoleService.updateById(sysRole)); } @DeleteMapping("/{id}") - public Boolean roleDel(@PathVariable Integer id) { + public R roleDel(@PathVariable Integer id) { SysRole sysRole = sysRoleService.selectById(id); sysRole.setDelFlag(CommonConstant.STATUS_DEL); - return sysRoleService.updateById(sysRole); + return new R<>(sysRoleService.updateById(sysRole)); } /** @@ -93,7 +92,7 @@ public class RoleController extends BaseController { @RequestMapping("/rolePage") public Page rolePage(@RequestParam Map params) { params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL); - return sysRoleService.selectPage(new Query<>(params),new EntityWrapper<>()); + return sysRoleService.selectPage(new Query<>(params), new EntityWrapper<>()); } /** @@ -104,19 +103,7 @@ public class RoleController extends BaseController { * @return success、false */ @PutMapping("/roleMenuUpd") - @CacheEvict(value = "menu_details", allEntries = true) - public Boolean roleMenuUpd(Integer roleId, @RequestParam("menuIds[]") Integer[] menuIds) { - SysRoleMenu condition = new SysRoleMenu(); - condition.setRoleId(roleId); - sysRoleMenuService.delete(new EntityWrapper<>(condition)); - - List roleMenuList = new ArrayList<>(); - for (Integer menuId : menuIds) { - SysRoleMenu roleMenu = new SysRoleMenu(); - roleMenu.setRoleId(roleId); - roleMenu.setMenuId(menuId); - roleMenuList.add(roleMenu); - } - return sysRoleMenuService.insertBatch(roleMenuList); + public R roleMenuUpd(Integer roleId, @RequestParam("menuIds[]") Integer[] menuIds) { + return new R<>(sysRoleMenuService.insertRoleMenus(getRole().get(0),roleId,menuIds)); } } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java index 8effaadb..1bae0a04 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java @@ -2,8 +2,8 @@ package com.github.pig.admin.controller; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; -import com.github.pig.common.bean.QiniuPropertiesConfig; import com.github.pig.admin.dto.UserDto; +import com.github.pig.common.bean.QiniuPropertiesConfig; import com.github.pig.admin.dto.UserInfo; import com.github.pig.admin.entity.SysUser; import com.github.pig.admin.entity.SysUserRole; diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysLogService.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysLogService.java index c333065c..a9dcc863 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysLogService.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysLogService.java @@ -2,6 +2,7 @@ package com.github.pig.admin.service; import com.baomidou.mybatisplus.service.IService; import com.github.pig.common.entity.SysLog; +import com.github.pig.common.util.R; /** *

@@ -13,4 +14,11 @@ import com.github.pig.common.entity.SysLog; */ public interface SysLogService extends IService { + /** + * 通过ID删除日志(逻辑删除) + * + * @param id 日志ID + * @return true/false + */ + Boolean updateByLogId(Integer id); } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java index 541b2ede..69869183 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysMenuService.java @@ -31,4 +31,22 @@ public interface SysMenuService extends IService { * @return 权限列表 */ String[] findPermission(String[] roles); + + /** + * 级联删除菜单 + * + * @param id 菜单ID + * @param role 角色 + * @return 成功、失败 + */ + Boolean deleteMenu(Integer id, String role); + + /** + * 更新菜单信息 + * + * @param sysMenu 菜单信息 + * @param role 角色 + * @return 成功、失败 + */ + Boolean updateMenuById(SysMenu sysMenu, String role); } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysRoleMenuService.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysRoleMenuService.java index b814763a..12fce7b1 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/SysRoleMenuService.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/SysRoleMenuService.java @@ -13,5 +13,15 @@ import com.github.pig.admin.entity.SysRoleMenu; * @since 2017-10-29 */ public interface SysRoleMenuService extends IService { - + + /** + * 更新角色菜单 + * + * + * @param role + * @param roleId 角色 + * @param menuIds 菜单列表 + * @return + */ + Boolean insertRoleMenus(String role, Integer roleId, Integer[] menuIds); } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysLogServiceImpl.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysLogServiceImpl.java index 12cc897a..5770317f 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysLogServiceImpl.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysLogServiceImpl.java @@ -3,9 +3,14 @@ package com.github.pig.admin.service.impl; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.github.pig.admin.mapper.SysLogMapper; import com.github.pig.admin.service.SysLogService; +import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.entity.SysLog; +import com.github.pig.common.util.Assert; +import com.github.pig.common.util.R; import org.springframework.stereotype.Service; +import java.util.Date; + /** *

* 日志表 服务实现类 @@ -17,4 +22,14 @@ import org.springframework.stereotype.Service; @Service public class SysLogServiceImpl extends ServiceImpl implements SysLogService { + @Override + public Boolean updateByLogId(Integer id) { + Assert.isNull(id, "日志ID为空"); + + SysLog sysLog = new SysLog(); + sysLog.setId(id); + sysLog.setDelFlag(CommonConstant.STATUS_DEL); + sysLog.setUpdateTime(new Date()); + return updateById(sysLog); + } } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java index bf188cbc..43d27f12 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysMenuServiceImpl.java @@ -1,12 +1,16 @@ package com.github.pig.admin.service.impl; +import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.github.pig.admin.entity.SysMenu; import com.github.pig.admin.mapper.SysMenuMapper; import com.github.pig.admin.service.SysMenuService; +import com.github.pig.common.constant.CommonConstant; +import com.github.pig.common.util.Assert; import com.github.pig.common.vo.MenuVo; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @@ -50,4 +54,28 @@ public class SysMenuServiceImpl extends ServiceImpl impl return permissions.toArray(new String[permissions.size()]); } + + @Override + @CacheEvict(value = "menu_details", key = "#role + '_menu'") + public Boolean deleteMenu(Integer id, String role) { + Assert.isNull(id, "菜单ID不能为空"); + // 删除当前节点 + SysMenu condition1 = new SysMenu(); + condition1.setMenuId(id); + condition1.setDelFlag(CommonConstant.STATUS_DEL); + this.updateById(condition1); + + // 删除父节点为当前节点的节点 + SysMenu conditon2 = new SysMenu(); + conditon2.setParentId(id); + SysMenu sysMenu = new SysMenu(); + sysMenu.setDelFlag(CommonConstant.STATUS_DEL); + return this.update(sysMenu, new EntityWrapper<>(conditon2)); + } + + @Override + @CacheEvict(value = "menu_details", key = "#role + '_menu'") + public Boolean updateMenuById(SysMenu sysMenu, String role) { + return this.updateById(sysMenu); + } } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysRoleMenuServiceImpl.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysRoleMenuServiceImpl.java index 04b77597..5a1809a0 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysRoleMenuServiceImpl.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/SysRoleMenuServiceImpl.java @@ -1,11 +1,16 @@ package com.github.pig.admin.service.impl; +import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.github.pig.admin.entity.SysRoleMenu; import com.github.pig.admin.mapper.SysRoleMenuMapper; import com.github.pig.admin.service.SysRoleMenuService; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + /** *

* 角色菜单表 服务实现类 @@ -16,5 +21,20 @@ import org.springframework.stereotype.Service; */ @Service public class SysRoleMenuServiceImpl extends ServiceImpl implements SysRoleMenuService { - + @Override + @CacheEvict(value = "menu_details", key = "#role + '_menu'") + public Boolean insertRoleMenus(String role, Integer roleId, Integer[] menuIds) { + SysRoleMenu condition = new SysRoleMenu(); + condition.setRoleId(roleId); + this.delete(new EntityWrapper<>(condition)); + + List roleMenuList = new ArrayList<>(); + for (Integer menuId : menuIds) { + SysRoleMenu roleMenu = new SysRoleMenu(); + roleMenu.setRoleId(roleId); + roleMenu.setMenuId(menuId); + roleMenuList.add(roleMenu); + } + return this.insertBatch(roleMenuList); + } } diff --git a/pig-common/pom.xml b/pig-common/pom.xml index f5071651..a2cdd452 100644 --- a/pig-common/pom.xml +++ b/pig-common/pom.xml @@ -67,6 +67,12 @@ org.springframework.boot spring-boot-starter-aop + + + org.hibernate + hibernate-validator + 5.4.1.Final + diff --git a/pig-common/src/main/java/com/github/pig/common/bean/ControllerAop.java b/pig-common/src/main/java/com/github/pig/common/bean/ControllerAop.java index c0806bb0..b17a234b 100644 --- a/pig-common/src/main/java/com/github/pig/common/bean/ControllerAop.java +++ b/pig-common/src/main/java/com/github/pig/common/bean/ControllerAop.java @@ -30,17 +30,37 @@ public class ControllerAop { private static final Logger logger = LoggerFactory.getLogger(ControllerAop.class); @Pointcut("execution(public com.github.pig.common.util.R *(..))") - public void pointCut() { + public void pointCutR() { } /** * 拦截器具体实现 * - * @param pjp 切点 + * @param pjp 切点 所有返回对象R * @return R 结果包装 */ - @Around("pointCut()") - public Object methodHandler(ProceedingJoinPoint pjp) { + @Around("pointCutR()") + public Object methodRHandler(ProceedingJoinPoint pjp) { + return methodHandler(pjp); + } + + + @Pointcut("execution(public com.baomidou.mybatisplus.plugins.Page *(..))") + public void pointCutPage() { + } + + /** + * 拦截器具体实现 + * + * @param pjp 切点 所有返回对象Page + * @return R 结果包装 + */ + @Around("pointCutPage()") + public Object methodPageHandler(ProceedingJoinPoint pjp) { + return methodHandler(pjp); + } + + private Object methodHandler(ProceedingJoinPoint pjp) { long startTime = System.currentTimeMillis(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); @@ -56,10 +76,10 @@ public class ControllerAop { logger.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName()); logger.info("ARGS : " + Arrays.toString(pjp.getArgs())); - R result; + Object result; try { - result = (R) pjp.proceed(); + result = pjp.proceed(); logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime)); } catch (Throwable e) { result = handlerException(pjp, e); diff --git a/pig-common/src/main/java/com/github/pig/common/util/Assert.java b/pig-common/src/main/java/com/github/pig/common/util/Assert.java new file mode 100644 index 00000000..78251789 --- /dev/null +++ b/pig-common/src/main/java/com/github/pig/common/util/Assert.java @@ -0,0 +1,53 @@ +package com.github.pig.common.util; + +import com.github.pig.common.util.exception.CheckException; +import org.apache.commons.lang.StringUtils; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import java.util.Set; + +/** + * 数据校验 + * @author chenshun + * @email sunlightcs@gmail.com + * @date 2017-03-23 15:50 + */ +public abstract class Assert { + private static Validator validator; + + static { + validator = Validation.buildDefaultValidatorFactory().getValidator(); + } + + /** + * 校验对象 + * @param object 待校验对象 + * @param groups 待校验的组 + * @throws CheckException 校验不通过,则报RRException异常 + */ + public static void validateEntity(Object object, Class... groups) + throws CheckException { + Set> constraintViolations = validator.validate(object, groups); + if (!constraintViolations.isEmpty()) { + StringBuilder msg = new StringBuilder(); + for(ConstraintViolation constraint: constraintViolations){ + msg.append(constraint.getMessage()).append("
"); + } + throw new CheckException(msg.toString()); + } + } + + public static void isBlank(String str, String message) { + if (StringUtils.isBlank(str)) { + throw new CheckException(message); + } + } + + public static void isNull(Object object, String message) { + if (object == null) { + throw new CheckException(message); + } + } +} diff --git a/pig-common/src/main/java/com/github/pig/common/web/BaseController.java b/pig-common/src/main/java/com/github/pig/common/web/BaseController.java index 34013eb8..3535fdce 100644 --- a/pig-common/src/main/java/com/github/pig/common/web/BaseController.java +++ b/pig-common/src/main/java/com/github/pig/common/web/BaseController.java @@ -32,4 +32,6 @@ public class BaseController { List roleNames = (List) claims.get("authorities"); return roleNames; } + + }