package com.matrix.system.common.actions; import com.matrix.core.anotations.RemoveRequestToken; import com.matrix.core.anotations.SaveRequestToken; import com.matrix.core.constance.MatrixConstance; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.pojo.PaginationVO; import com.matrix.core.tools.LogUtil; import com.matrix.core.tools.StringUtils; import com.matrix.core.tools.WebUtil; import com.matrix.core.web.BaseAction; import com.matrix.system.common.authority.DefaultAuthorityManager; import com.matrix.system.common.bean.SysFnBtnRel; import com.matrix.system.common.bean.SysFunction; import com.matrix.system.common.bean.SysRole; import com.matrix.system.common.bean.SysUsers; import com.matrix.system.common.constance.AppVocabularyCode; import com.matrix.system.common.init.LocalCache; import com.matrix.system.common.service.SysFunctionService; import com.matrix.system.common.service.SysRoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static com.matrix.system.common.constance.AppConstance.SAFEPATH; /** * 功能管理 * * @author jiangyouyaoaa * */ @Controller @RequestMapping(value = "admin/sysRole") public class SysRoleAction extends BaseAction { @Autowired private SysRoleService sysRoleService; @Autowired private SysFunctionService sysFunctionService; public static final String BEV = "SYSROLE_BEV"; /** * 列表显示 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date Dec 10, 2017 * @param sysRole * @param pageVo * @return */ @RequestMapping(value = "/showList") public @ResponseBody AjaxResult showList(SysRole sysRole, PaginationVO pageVo) { // 默认按创建时间排序 if(StringUtils.isBlank(pageVo.getSort())) { pageVo.setSort("createTime"); pageVo.setOrder("desc"); } return showList(sysRoleService, sysRole, pageVo); } /** * 列表显示当前登录人公司的列表 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date Dec 10, 2017 * @return */ @RequestMapping(value = SAFEPATH + "/showCompanyRole") public @ResponseBody AjaxResult showCompanyRole(SysRole sysRole, PaginationVO pageVo) { // 默认按创建时间排序 if(StringUtils.isBlank(pageVo.getSort())) { pageVo.setSort("createTime"); pageVo.setOrder("desc"); } SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); sysRole.setCompanyId(user.getCompanyId()); return new AjaxResult(AjaxResult.STATUS_SUCCESS, sysRoleService.findInPage(sysRole, pageVo), sysRoleService.findTotal(sysRole)); } /** * 列表显示当前登录人门店的角色列表 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date Dec 10, 2017 * @return */ @RequestMapping(value = "/showShopRole") public @ResponseBody AjaxResult showShopRole(SysRole sysRole, PaginationVO pageVo) { // 默认按创建时间排序 if(StringUtils.isBlank(pageVo.getSort())) { pageVo.setSort("createTime"); pageVo.setOrder("desc"); } SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); sysRole.setCompanyId(user.getCompanyId()); sysRole.setShopAble(SysRole.SHOP_ABLE); return new AjaxResult(AjaxResult.STATUS_SUCCESS, sysRoleService.findInPage(sysRole, pageVo), sysRoleService.findTotal(sysRole)); } /** * 新增权限 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date Dec 10, 2017 * @param sysRole * @return */ @RemoveRequestToken @PostMapping(value = SAFEPATH + "/addRole") public @ResponseBody AjaxResult addRole(SysRole sysRole) { // 按钮 Map> btnMap = new HashMap>(); String btnKeys = sysRole.getBtnKeys(); List btnKeyList = new ArrayList(); SysFnBtnRel sysFnBtnRel = null; if (StringUtils.isNotBlank(btnKeys)) { btnKeyList = StringUtils.strToCollToString(btnKeys, ","); // 构建菜单ID为键 按钮集合为值的map for (String btnKey : btnKeyList) { // fnId-btnValue String[] fnBtnArr = btnKey.split("-"); String btnValue = fnBtnArr[1]; String fnId = fnBtnArr[0]; if (btnMap.containsKey(fnId)) { sysFnBtnRel = new SysFnBtnRel(); sysFnBtnRel.setBtnValue(btnValue); List relList = btnMap.get(fnId); relList.add(sysFnBtnRel); btnMap.put(fnId, relList); } else { List relList = new ArrayList(); sysFnBtnRel = new SysFnBtnRel(); sysFnBtnRel.setBtnValue(btnValue); relList.add(sysFnBtnRel); btnMap.put(fnId, relList); } } } // 构建菜单 List sysFunctionList = new ArrayList(); String menuKeys = sysRole.getMenuKeys(); SysFunction sysFunction = null; if (StringUtils.isNotBlank(menuKeys)) { List ids = StringUtils.strToCollToString(menuKeys, ","); for (String fnId : ids) { sysFunction = new SysFunction(); sysFunction.setFnId(Long.parseLong(fnId)); // 将按钮关系加入 List relList = btnMap.get(fnId); sysFunction.setSysFnBtnRel(relList); sysFunctionList.add(sysFunction); } sysRole.setFnList(sysFunctionList); } return add(sysRoleService, sysRole, AppVocabularyCode.ROLE); } /** * 修改权限 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date Dec 10, 2017 * @param sysRole * @return */ @RemoveRequestToken @PostMapping(value = SAFEPATH + "/modifyRole") public @ResponseBody AjaxResult modifyRole(SysRole sysRole) { // 按钮 Map> btnMap = new HashMap>(); String btnKeys = sysRole.getBtnKeys(); List btnKeyList = new ArrayList(); SysFnBtnRel sysFnBtnRel = null; if (StringUtils.isNotBlank(btnKeys)) { btnKeyList = StringUtils.strToCollToString(btnKeys, ","); // 构建菜单ID为键 按钮集合为值的map for (String btnKey : btnKeyList) { // fnId-btnValue String[] fnBtnArr = btnKey.split("-"); String btnValue = fnBtnArr[1]; String fnId = fnBtnArr[0]; if (btnMap.containsKey(fnId)) { sysFnBtnRel = new SysFnBtnRel(); sysFnBtnRel.setBtnValue(btnValue); List relList = btnMap.get(fnId); relList.add(sysFnBtnRel); btnMap.put(fnId, relList); } else { List relList = new ArrayList(); sysFnBtnRel = new SysFnBtnRel(); sysFnBtnRel.setBtnValue(btnValue); relList.add(sysFnBtnRel); btnMap.put(fnId, relList); } } } // 构建菜单 List sysFunctionList = new ArrayList(); String menuKeys = sysRole.getMenuKeys(); SysFunction sysFunction = null; if (StringUtils.isNotBlank(menuKeys)) { List ids = StringUtils.strToCollToString(menuKeys, ","); for (String fnId : ids) { sysFunction = new SysFunction(); sysFunction.setFnId(Long.parseLong(fnId)); // 将按钮关系加入 List relList = btnMap.get(fnId); sysFunction.setSysFnBtnRel(relList); sysFunctionList.add(sysFunction); } sysRole.setFnList(sysFunctionList); } AjaxResult result = modify(sysRoleService, WebUtil.getSessionAttribute(BEV), sysRole, AppVocabularyCode.ROLE); WebUtil.removeSessionAttribute(BEV); LocalCache.batchRemove(DefaultAuthorityManager.USER_POWER_REDISKEY+"*"); return result; } /** * 进入修改界面 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date Dec 10, 2017 * @param id * @return */ @SaveRequestToken @GetMapping(value = SAFEPATH + "/editForm") public ModelAndView editForm(String id) { ModelAndView mv = new ModelAndView("admin/sys/sysRole-form"); SysRole sysRole = new SysRole(); mv.addObject("obj", sysRole); List functions = null; if (id != null) { sysRole = sysRoleService.findById(id); // 获取所有的功能 functions = sysFunctionService.findRoleFuntion(id); mv.addObject("obj", sysRole); // WebUtil.getRequest().setAttribute("obj", sysRole); WebUtil.setSessionAttribute(BEV, sysRole); } else { functions = sysFunctionService.findRoleFuntion(null); } LogUtil.debug("查询出来的权限信息={}", functions); mv.addObject("functions", functions); return mv; } /** * 删除 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date Dec 10, 2017 * @param keys * @return */ @RequestMapping(value = SAFEPATH + "/del") public @ResponseBody AjaxResult del(String keys) { return remove(sysRoleService, keys); } }