Commit 321361c9 authored by xiandafu's avatar xiandafu
Browse files

init

parent 2971e3f1
package com.ibeetl.admin.core.util.beetl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.sql.core.engine.SQLParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.entity.CoreRoleFunction;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.rbac.DataAccess;
import com.ibeetl.admin.core.rbac.DataAccessFactory;
import com.ibeetl.admin.core.rbac.DataAccessResullt;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.FunctionLocal;
/**
* 数据权限拼sql,配合DataAccessFactory
* @author lijiazhi
*
*/
@Component
public class DataAccessFunction implements Function {
Log log = LogFactory.getLog(DataAccessFunction.class);
@Autowired
CorePlatformService platFormService;
@Autowired
DataAccessFactory dataAccessFactory;
private static Map defaultTargets = new HashMap();
static{
//数据库默认的跟组织和用户相关字段
defaultTargets.put("org", "org_id");
defaultTargets.put("user", "user_id");
}
private static final String SQL_MY_DATA = "user_id=? ";
private static final String SQL_MY_ORG_DATA = "org_id=? ";
public Object call(Object[] paras, Context ctx){
//项目初期,总是返回1==1,避免数据权限带来的麻烦
CoreUser user = platFormService.getCurrentUser();
//{"org":"org_id","user","user_id"}
Map targets = this.defaultTargets;
//用户调用conroller 结果"user.view"
String functionCode = FunctionLocal.get();
if(paras.length==1){
Object o = paras[0];
if(o instanceof String){
functionCode = (String)o;
}else if(o instanceof Map){
targets = (Map)paras[1];
}
}else if(paras.length==2){
functionCode = (String)paras[0];
targets = (Map)paras[1];
}
if(platFormService.isSupperAdmin(user)){
return " 1=1 /* admin */ ";
}
Long currentOrgId = platFormService.getCurrentOrgId();
List<CoreRoleFunction> roleFuns = platFormService.getRoleFunction(user.getId(),currentOrgId,functionCode);
if(roleFuns.isEmpty()){
//如果没有配置数据权限,是1=1,因此为角色指定功能的时候,需要设定数据权限,否则查询到所有数据
return "1=1 /*empty data access */ ";
}
List<Object> list = (List<Object>)ctx.getGlobal("_paras");
StringBuilder sb = new StringBuilder(" ");
//数据权限范围划定
boolean hasAppend = false;
for(int i=0;i<roleFuns.size();i++){
CoreRoleFunction fun = roleFuns.get(i);
Integer accessType = fun.getDataAccessType();
if(accessType==null){
continue;
}
if(hasAppend){
sb.append(" or ");
}
hasAppend = true;
DataAccess data = dataAccessFactory.getDataAccess(accessType);
DataAccessResullt ret = data.getOrg(user.getId(), currentOrgId);
switch(ret.getStatus()){
case NoneOrg:{
sb.append(targets.get("org")+" in (-1) ");
}
case AllOrg:{
//sql 不包含组织机构过滤信息
continue ;
}
case OnlyUser:{
List<Long> ids = ret.getUserIds();
sb.append(targets.get("user"));
if(ids.size()==0){
sb.append("=-1/*指定用户,但没有候选用户*/");
continue;
}
if(ids.size()==1){
sb.append(" =? ");
list.add(new SQLParameter(ids.get(0)));
continue;
}
sb.append(" in (");
for(int z=0;z<ids.size();z++){
sb.append(" ? ");
list.add(new SQLParameter(ids.get(z)));
if(z!=ids.size()-1){
sb.append(",");
}
}
sb.append(") ");
}
case OnlyOrg:{
List<Long> ids = ret.getOrgIds();
sb.append(targets.get("org"));
if(ids.size()==0){
sb.append("=-1/*指定机构,但没有候选机构*/");
continue;
}
if(ids.size()==1){
sb.append(" =? ");
list.add(new SQLParameter(ids.get(0)));
continue;
}
sb.append(" in (");
for(int z=0;z<ids.size();z++){
sb.append("?");
list.add(new SQLParameter(ids.get(z)));
if(z!=ids.size()-1){
sb.append(",");
}
}
sb.append(") ");
}
default:{
log.warn("错误的"+ret.getStatus().toString());
throw new UnsupportedOperationException(ret.getStatus().toString());
}
}
}
return sb.toString();
}
private String appendAlias(String alias,String sql){
if(alias==null){
return sql;
}
return " " +alias+"."+sql;
}
private void buildSqlIn(List<Long> list,List<Object> paras,Map targets,StringBuilder sql){
if(list.isEmpty()){
//如果没有满足条件的部门
sql.append(targets.get("org")+" in (-1)");
}
sql.append(targets.get("org")+" in (");
for(int i=0;i<list.size();i++){
sql.append("?");
if(i!=list.size()-1){
sql.append(",");
}
paras.add(new SQLParameter(list.get(i)));
}
sql.append(")");
}
}
package com.ibeetl.admin.core.util.beetl;
import java.util.List;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.service.CoreDictService;
@Component
public class DictQueryByValueFunction implements Function {
@Autowired
CoreDictService dictService;
@Override
public List<CoreDict> call(Object[] paras, Context arg1) {
String value =(String)paras[0];
CoreDict dict = dictService.findCoreDict(value);
if(dict==null) {
throw new RuntimeException("未能发现数据字典 "+value);
}
List<CoreDict> list = dictService.findAllByType(dict.getType());
return list;
}
}
package com.ibeetl.admin.core.util.beetl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.service.CoreDictService;
@Component
public class DictQueryFunction implements Function {
@Autowired
CoreDictService dictService;
@Override
public List<CoreDict> call(Object[] paras, Context arg1) {
String type =(String)paras[0];
return dictService.findAllByType(type);
}
}
package com.ibeetl.admin.core.util.beetl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.service.CoreDictService;
@Component
public class DictUpQueryFunction implements Function {
@Autowired
CoreDictService dictService;
@Override
public List<CoreDict> call(Object[] paras, Context arg1) {
String type =(String)paras[0];
String value =(String)paras[1];
if(StringUtils.isEmpty(value)) {
return dictService.findDefalutLevel(type);
}else {
return dictService.findLevelByValue(value);
}
}
}
package com.ibeetl.admin.core.util.beetl;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.rbac.tree.FunctionItem;
import com.ibeetl.admin.core.rbac.tree.OrgItem;
import com.ibeetl.admin.core.service.CorePlatformService;
import java.util.List;
/**
* 通过functionId获取functionName,从缓存中获取
*/
@Component
public class FunFunction implements Function {
@Autowired
CorePlatformService platFormService;
public Object call(Object[] paras, Context ctx) {
FunctionItem tree = platFormService.buildFunction();
FunctionItem item = tree.findChild((Long)paras[0]);
return item.getName();
}
}
package com.ibeetl.admin.core.util.beetl;
import java.util.Calendar;
import java.util.Date;
import org.beetl.core.Context;
import org.beetl.core.Function;
public class NextDayFunction implements Function {
@Override
public Object call(Object[] paras, Context ctx) {
Date date = (Date)paras[0];
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DAY_OF_YEAR, 1);// 今天+1天
return c.getTime();
}
}
package com.ibeetl.admin.core.util.beetl;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.rbac.tree.OrgItem;
import com.ibeetl.admin.core.service.CorePlatformService;
import java.util.List;
/**
* 通过orgId获取orgName,从缓存中获取
* @author xiandafu
*
*/
@Component
public class OrgFunction implements Function {
@Autowired
CorePlatformService platFormService;
public Object call(Object[] paras, Context ctx) {
OrgItem tree = platFormService.buildOrg();
OrgItem item = tree.findChild((Long)paras[0]);
return item.getName();
}
}
package com.ibeetl.admin.core.util.beetl;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.rbac.tree.OrgItem;
import com.ibeetl.admin.core.service.CoreRoleService;
/**
* 获取系统的所有角色列表
* @author xiandafu
*
*/
@Component
public class RoleFunction implements Function {
@Autowired
CoreRoleService coreRoleService;
public Object call(Object[] paras, Context ctx) {
String type = null;
if(paras.length!=0) {
type = (String)paras[0];
}
return coreRoleService.getAllRoles(type);
}
}
package com.ibeetl.admin.core.util.beetl;
import java.util.List;
import java.util.Map;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.ext.simulate.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ibeetl.admin.core.annotation.Query;
import com.ibeetl.admin.core.util.AnnotationUtil;
/**
* 描述: 通过解析注解,获取表达查询条件信息
*
* @author : lijiazhi
*/
@Component
public class SearchCondtionFunction implements Function {
/**
*
* @param paras 查询条件类名
* @param ctx
* @return
*/
@Override
public Object call(Object[] paras, Context ctx) {
String className = (String) paras[0];
try {
List<Map<String, Object>> list = AnnotationUtil.getInstance().getAnnotations(Query.class, Class.forName(className));
return list;
} catch (ClassNotFoundException e ) {
e.printStackTrace();
return null;
}
}
}
package com.ibeetl.admin.core.util.beetl;
import java.util.List;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ibeetl.admin.core.rbac.tree.FunctionItem;
import com.ibeetl.admin.core.rbac.tree.OrgItem;
import com.ibeetl.admin.core.service.CorePlatformService;
/**
* 手工构造一个功能树,书上包含模块,功能,按钮
* @author Administrator
*
*/
@Component
public class SysFunctionTreeFunction implements Function {
@Autowired
CorePlatformService platFormService;
public Object call(Object[] paras, Context ctx) {
FunctionItem tree = platFormService.buildFunction();
StringBuilder sb = new StringBuilder(256).append("[");
FunctionItem root = tree.getChildren().get(0);
build(sb,root);
sb.append("]");
return sb.toString();
}
private void build(StringBuilder sb,FunctionItem item){
sb.append("{name:'").append(item.getData().getName()).append("',code:'").append(item.getData().getCode());
sb.append("',id:").append(item.getData().getId());
if (item.getData().getParentId() == 0){
sb.append(",open:true");
}
List<FunctionItem> list = item.getChildren();
int size = list.size();
if(size==0){
sb.append("}").append("\n");
return ;
}
sb.append(",\n children:[");
for(int i=0;i<size;i++){
FunctionItem child = list.get(i);
build(sb,child);
if(!isLast(i,size)){
sb.append(",\n");
}
}
sb.append("]}").append("\n");
}
private boolean isLast(int index,int size){
return index==(size-1);
}
}
package com.ibeetl.admin.core.util.enums;
/**
* @author : xiandafu
*/
public class CoreDictType {
public static final String ORG_TYPE="org_type";
public static final String USER_STATE="user_state";
public static final String DEL_FLAG="del_flag";
public static final String ROLE_TYPE="role_type";
public static final String MENU_TYPE="menu_type";
public static final String FUNCTION_TYPE="function_type";
}
package com.ibeetl.admin.core.util.enums;
import org.beetl.sql.core.annotatoin.EnumMapping;
/**
* 描述:数据是否被逻辑删除
* @author : xiandafu
*/
@EnumMapping("value")
public enum DelFlagEnum {
NORMAL(0), DELETED(1);
private int value;
DelFlagEnum(int value) {
this.value = value;
}
public static DelFlagEnum getEnum(int value) {
for (DelFlagEnum type : DelFlagEnum.values()) {
if (type.value == value) {
return type;
}
}
return null;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
}
package com.ibeetl.admin.core.util.enums;
import org.beetl.sql.core.annotatoin.EnumMapping;
/**
* 描述:数据是否有效
*
* @author : lijiazhi
*/
@EnumMapping("value")
public enum GeneralStateEnum {
/**
* 启用
*/
ENABLE("S1"),
/**
* 禁用
*/
DISABLE("S0");
private String value;
GeneralStateEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static GeneralStateEnum getEnum(String value) {
for (GeneralStateEnum stateEnum : GeneralStateEnum.values()) {
if (stateEnum.value == value) {
return stateEnum;
}
}
return null;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
}
package com.ibeetl.admin.core.util.enums;
import org.beetl.sql.core.annotatoin.EnumMapping;
/**
* 描述: 工作流角色
*
* @author : Administrator
*/
@EnumMapping("value")
public enum RoleTypeEnum {
/**
* 操作角色
*/
ACCESS("R0"),
/**
* 工作流角色
*/
WORKFLOW("R1");
private String value;
RoleTypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static RoleTypeEnum getEnum(String value) {
for (RoleTypeEnum stateEnum : RoleTypeEnum.values()) {
if (stateEnum.value == value) {
return stateEnum;
}
}
return null;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
}
package com.ibeetl.admin.core.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.ibeetl.admin.core.gen.AutoGen;
import com.ibeetl.admin.core.gen.HtmlGen;
import com.ibeetl.admin.core.gen.JSGen;
import com.ibeetl.admin.core.gen.JavaCodeGen;
import com.ibeetl.admin.core.gen.MavenProjectTarget;
import com.ibeetl.admin.core.gen.MdGen;
import com.ibeetl.admin.core.gen.WebTarget;
import com.ibeetl.admin.core.gen.model.Attribute;
import com.ibeetl.admin.core.gen.model.Entity;
import com.ibeetl.admin.core.service.CoreCodeGenService;
import com.ibeetl.admin.core.util.PlatformException;
@Controller
public class CoreCodeGenController {
private final Log log = LogFactory.getLog(this.getClass());
private static final String MODEL = "/core/codeGen";
@Autowired
CoreCodeGenService codeGenService;
@GetMapping(MODEL + "/index.do")
public ModelAndView index() {
ModelAndView view = new ModelAndView("/core/codeGen/index.html");
return view;
}
@GetMapping(MODEL + "/tableDetail.do")
public ModelAndView tableDetail(String table) {
ModelAndView view = new ModelAndView("/core/codeGen/edit.html");
Entity entity = codeGenService.getEntityInfo(table);
view.addObject("entity", entity);
return view;
}
@PostMapping(MODEL + "/table.json")
@ResponseBody
public JsonResult<List<Entity>> getTable() {
List<Entity> list = codeGenService.getAllEntityInfo();
return JsonResult.success(list);
}
@PostMapping(MODEL + "/tableDetail.json")
@ResponseBody
public JsonResult<Entity> getInfo(String table) {
Entity entity = codeGenService.getEntityInfo(table);
if (entity == null) {
JsonResult.failMessage("表不存在");
}
return JsonResult.success(entity);
}
@PostMapping(MODEL + "/gen.json")
@ResponseBody
public JsonResult gen(EntityInfo data) {
Entity info = data.getEntity();
String urlBase = data.getUrlBase();
String basePackage = data.getBasePackage();
Entity entity = codeGenService.getEntityInfo(info.getTableName());
entity.setCode(info.getCode());
entity.setDisplayName(info.getDisplayName());
entity.setSystem(info.getSystem());
for (int i = 0; i < entity.getList().size(); i++) {
entity.getList().get(i).setDisplayName(info.getList().get(i).getDisplayName());
entity.getList().get(i).setShowInQuery(info.getList().get(i).isShowInQuery());
}
if (StringUtils.isEmpty(entity.getCode()) || StringUtils.isEmpty(entity.getSystem())) {
return JsonResult.failMessage("code,system不能为空");
}
MavenProjectTarget target = new MavenProjectTarget(entity, basePackage);
target.setUrlBase(urlBase);
JSGen jsGen = new JSGen();
jsGen.make(target, entity);
HtmlGen htmlGen = new HtmlGen();
htmlGen.make(target, entity);
JavaCodeGen javaGen = new JavaCodeGen(basePackage, entity);
javaGen.make(target, entity);
MdGen mdGen = new MdGen();
mdGen.make(target, entity);
return JsonResult.success();
}
@PostMapping(MODEL + "/html.json")
@ResponseBody
public JsonResult<Map<String, String>> html(EntityInfo data) {
Entity entity = getEntitiyInfo(data);
String urlBase = data.getUrlBase();
String basePackage = data.getBasePackage();
WebTarget webTarget = new WebTarget(entity, basePackage);
webTarget.setUrlBase(urlBase);
HtmlGen htmlGen = new HtmlGen();
htmlGen.make(webTarget, entity);
Map<String, String> content = new HashMap<String, String>();
for (Entry<Object, String> entry : webTarget.map.entrySet()) {
AutoGen gen = (AutoGen) entry.getKey();
String code = entry.getValue();
content.put(gen.getName(), code);
}
return JsonResult.success(content);
}
@PostMapping(MODEL + "/js.json")
@ResponseBody
public JsonResult<Map<String, String>> js(EntityInfo data) {
Entity entity = getEntitiyInfo(data);
String urlBase = data.getUrlBase();
String basePackage = data.getBasePackage();
WebTarget webTarget = new WebTarget(entity, basePackage);
webTarget.setUrlBase(urlBase);
JSGen jsGen = new JSGen();
jsGen.make(webTarget, entity);
Map<String, String> content = new HashMap<String, String>();
for (Entry<Object, String> entry : webTarget.map.entrySet()) {
AutoGen gen = (AutoGen) entry.getKey();
String code = entry.getValue();
content.put(gen.getName(), code);
}
return JsonResult.success(content);
}
@PostMapping(MODEL + "/java.json")
@ResponseBody
public JsonResult<Map<String, String>> javaCode(EntityInfo data) {
Entity entity = getEntitiyInfo(data);
String urlBase = data.getUrlBase();
String basePackage = data.getBasePackage();
WebTarget webTarget = new WebTarget(entity, basePackage);
webTarget.setUrlBase(urlBase);
JavaCodeGen javaGen = new JavaCodeGen(basePackage,entity);
javaGen.make(webTarget, entity);
Map<String, String> content = new HashMap<String, String>();
for (Entry<Object, String> entry : webTarget.map.entrySet()) {
AutoGen gen = (AutoGen) entry.getKey();
String code = entry.getValue();
content.put(gen.getName(), code);
}
return JsonResult.success(content);
}
@PostMapping(MODEL + "/sql.json")
@ResponseBody
public JsonResult<Map<String, String>> sql(EntityInfo data) {
Entity entity = getEntitiyInfo(data);
String urlBase = data.getUrlBase();
String basePackage = data.getBasePackage();
WebTarget webTarget = new WebTarget(entity, basePackage);
webTarget.setUrlBase(urlBase);
MdGen javaGen = new MdGen();
javaGen.make(webTarget, entity);
Map<String, String> content = new HashMap<String, String>();
for (Entry<Object, String> entry : webTarget.map.entrySet()) {
AutoGen gen = (AutoGen) entry.getKey();
String code = entry.getValue();
content.put(gen.getName(), code);
}
return JsonResult.success(content);
}
private Entity getEntitiyInfo(EntityInfo data) {
Entity info = data.getEntity();
String urlBase = data.getUrlBase();
String basePackage = data.getBasePackage();
Entity entity = codeGenService.getEntityInfo(info.getTableName());
entity.setCode(info.getCode());
entity.setDisplayName(info.getDisplayName());
entity.setSystem(info.getSystem());
for (int i = 0; i < entity.getList().size(); i++) {
Attribute attr = entity.getList().get(i);
attr.setDisplayName(info.getList().get(i).getDisplayName());
attr.setShowInQuery(info.getList().get(i).isShowInQuery());
if(attr.getName().equals(data.getNameAttr())) {
entity.setNameAttribute(attr);
}
}
if (StringUtils.isEmpty(entity.getCode()) || StringUtils.isEmpty(entity.getSystem())) {
throw new PlatformException("code,system不能为空");
}
return entity;
}
@GetMapping(MODEL + "/{table}/test.json")
@ResponseBody
public void test(@PathVariable String table) {
Entity entity = new Entity();
entity.setCode("blog");
entity.setName("CmsBlog");
entity.setDisplayName("博客");
entity.setTableName("CMS_BLOG");
entity.setSystem("console");
Attribute idAttr = new Attribute();
idAttr.setColName("id");
idAttr.setJavaType("Long");
idAttr.setName("id");
idAttr.setDisplayName("编号");
idAttr.setId(true);
entity.setIdAttribute(idAttr);
Attribute nameAttr = new Attribute();
nameAttr.setColName("title");
nameAttr.setJavaType("String");
nameAttr.setName("title");
nameAttr.setDisplayName("标题");
nameAttr.setShowInQuery(true);
Attribute contentAttr = new Attribute();
contentAttr.setColName("content");
contentAttr.setJavaType("String");
contentAttr.setName("content");
contentAttr.setDisplayName("内容");
contentAttr.setShowInQuery(true);
Attribute createTimeAttr = new Attribute();
createTimeAttr.setColName("create_time");
createTimeAttr.setJavaType("Date");
createTimeAttr.setName("createTime");
createTimeAttr.setDisplayName("创建日期");
createTimeAttr.setShowInQuery(true);
Attribute createUserIdAttr = new Attribute();
createUserIdAttr.setColName("create_user_id");
createUserIdAttr.setJavaType("Long");
createUserIdAttr.setName("createUserId");
createUserIdAttr.setDisplayName("创建人");
createUserIdAttr.setShowInQuery(true);
Attribute typeAttr = new Attribute();
typeAttr.setColName("type");
typeAttr.setJavaType("String");
typeAttr.setName("type");
typeAttr.setDisplayName("博客类型");
typeAttr.setShowInQuery(true);
entity.getList().add(idAttr);
entity.getList().add(nameAttr);
entity.getList().add(contentAttr);
entity.getList().add(createTimeAttr);
entity.getList().add(createUserIdAttr);
entity.getList().add(typeAttr);
entity.setNameAttribute(nameAttr);
entity.setComment("这是一个测试模型");
// ConsoleTarget target = new ConsoleTarget();
String basePackage = "com.ibeetl.admin.console";
MavenProjectTarget target = new MavenProjectTarget(entity, basePackage);
target.setUrlBase("admin");
JSGen jsGen = new JSGen();
jsGen.make(target, entity);
HtmlGen htmlGen = new HtmlGen();
htmlGen.make(target, entity);
JavaCodeGen javaGen = new JavaCodeGen(basePackage, entity);
javaGen.make(target, entity);
MdGen mdGen = new MdGen();
mdGen.make(target, entity);
}
}
class EntityInfo {
Entity entity;
String urlBase;
String basePackage;
String nameAttr;
public Entity getEntity() {
return entity;
}
public void setEntity(Entity entity) {
this.entity = entity;
}
public String getUrlBase() {
return urlBase;
}
public void setUrlBase(String urlBase) {
this.urlBase = urlBase;
}
public String getBasePackage() {
return basePackage;
}
public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}
public String getNameAttr() {
return nameAttr;
}
public void setNameAttr(String nameAttr) {
this.nameAttr = nameAttr;
}
}
package com.ibeetl.admin.core.web;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.service.CoreDictService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.DictUtil;
@Controller
@SuppressWarnings("unchecked")
public class CoreDictController {
private final Log log = LogFactory.getLog(this.getClass());
private static final String MODEL = "/core/dict";
@Autowired
CorePlatformService platformService;
@Autowired
CoreDictService dictService;
@Autowired
DictUtil dictUtil;
/**
* 查看字典类型对应的列表
* @param type
* @return
*/
@RequestMapping(MODEL + "/view.json")
@ResponseBody
public JsonResult<List<CoreDict>> view(String type) {
List<CoreDict> list = dictService.findAllByType(type);
return JsonResult.success(list);
}
/**
* 查看字典值的子字典
* @param value
* @return
*/
@RequestMapping(MODEL + "/viewChildren.json")
@ResponseBody
public JsonResult<List<CoreDict>> viewChild(String value) {
List<CoreDict> list = dictService.findChildrenByValue(value);
return JsonResult.success(list);
}
/**
* 查看字典值的所有后代字典
* @param value
* @return
*/
@RequestMapping(MODEL + "/batchViewChildren.json")
@ResponseBody
public JsonResult<List<List<CoreDict>>> batchViewChildren(String value) {
List<List<CoreDict>> list = dictService.batchFindChidren(value);
return JsonResult.success(list);
}
/**
* 批量获取字典数据
* @param types
* @return
*/
@RequestMapping(MODEL + "/batchView.json")
@ResponseBody
public JsonResult<Map<String, List<CoreDict>>> batchView(String types) {
String[] strs = types.split(",");
//按照顺序返回
Map<String, List<CoreDict>> map = new LinkedHashMap<String, List<CoreDict>>();
for (int i = 0; i < strs.length; i++) {
List<CoreDict> list = dictService.findAllByType(strs[i]);
map.put(strs[i], list);
}
return JsonResult.success(map);
}
}
package com.ibeetl.admin.core.web;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.ResponseBody;
import com.ibeetl.admin.core.conf.PasswordConfig.PasswordEncryptService;
import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.rbac.UserLoginInfo;
import com.ibeetl.admin.core.rbac.tree.FunctionItem;
import com.ibeetl.admin.core.rbac.tree.MenuItem;
import com.ibeetl.admin.core.rbac.tree.OrgItem;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.CoreUserService;
import com.ibeetl.admin.core.util.DictUtil;
import com.ibeetl.admin.core.util.HttpRequestLocal;
@Controller
@SuppressWarnings("unchecked")
public class CoreUserController {
private final Log log = LogFactory.getLog(this.getClass());
private static final String MODEL = "/core/user";
@Autowired
CorePlatformService platformService;
@Autowired
CoreUserService userService;
@Autowired
HttpRequestLocal httpRequestLocal;
@Autowired
DictUtil dictUtil;
@Autowired
PasswordEncryptService passwordEncryptService ;
@PostMapping(MODEL + "/login.json")
@ResponseBody
public JsonResult<UserLoginInfo> login(String code, String password) {
UserLoginInfo info = userService.login(code, password);
if (info == null) {
return JsonResult.failMessage("用户名密码错");
}
CoreUser user = info.getUser();
CoreOrg currentOrg = info.getOrgs().get(0);
for (CoreOrg org : info.getOrgs()) {
if (org.getId() == user.getOrgId()) {
currentOrg = org;
break;
}
}
info.setCurrentOrg(currentOrg);
// 记录登录信息到session
this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs());
return JsonResult.success(info);
}
/**
* 用户所在部门
*
* @return
*/
@PostMapping(MODEL + "/myOrgs.json")
@ResponseBody
public JsonResult<List<CoreOrg>> myOrgs() {
List<CoreOrg> orgs = (List<CoreOrg>) httpRequestLocal.getSessionValue(CorePlatformService.ACCESS_USER_ORGS);
return JsonResult.success(orgs);
}
/**
* 切换部门
*
* @param code
* @param orgId
* @return
*/
@PostMapping(MODEL + "/setOrg.json")
@ResponseBody
public JsonResult login(Long orgId) {
CoreUser user = platformService.getCurrentUser();
// 检查是否存在orgId
List<CoreOrg> orgs = platformService.getCurrentOrgs();
CoreOrg currentOrg = null;
for (CoreOrg org : orgs) {
if (orgId == org.getId()) {
currentOrg = org;
break;
}
}
if (currentOrg == null) {
// 非法切换
return JsonResult.failMessage("切换到不存在的部门");
}
httpRequestLocal.setSessionValue(CorePlatformService.ACCESS_CURRENT_ORG, currentOrg);
return JsonResult.success();
}
@PostMapping(MODEL + "/changePassword.json")
@ResponseBody
public JsonResult chnagePassword(String password, String newPassword) {
CoreUser temp = platformService.getCurrentUser();
CoreUser realUser = userService.getUserById(temp.getId());
String pwd = passwordEncryptService.password(password);
if (realUser.getPassword().equals(pwd)) {
realUser.setPassword(passwordEncryptService.password(newPassword));
userService.update(realUser);
return JsonResult.success();
} else {
return JsonResult.failMessage("密码错误");
}
}
/**
* 用户能查看的菜单
*
* @return
*/
@PostMapping(MODEL + "/menu/menuTree.json")
@ResponseBody
public JsonResult<List<MenuNodeView>> menus() {
CoreUser currentUser = platformService.getCurrentUser();
Long orgId = platformService.getCurrentOrgId();
MenuItem item = platformService.getMenuItem(currentUser.getId(), orgId);
List<MenuNodeView> view = this.build(item);
return JsonResult.success(view);
}
/**
* 获取系统
*
* @return
*/
@PostMapping(MODEL + "/menu/system.json")
@ResponseBody
public JsonResult<List<SystemMenuView>> getSystem() {
CoreUser currentUser = platformService.getCurrentUser();
Long orgId = platformService.getCurrentOrgId();
MenuItem menuItem = platformService.getMenuItem(currentUser.getId(), orgId);
List<MenuItem> list = menuItem.getChildren();
List<SystemMenuView> systems = new ArrayList<SystemMenuView>();
for (MenuItem item : list) {
systems.add(new SystemMenuView(item.getId(), item.getData().getCode(), item.getData().getName()));
}
return JsonResult.success(systems);
}
/**
* 获取系统对应的菜单树
*
* @return
*/
@PostMapping(MODEL + "/menu/systemMenu.json")
@ResponseBody
public JsonResult<List<MenuNodeView>> getMenuBySystem(long systemId) {
CoreUser currentUser = platformService.getCurrentUser();
Long orgId = platformService.getCurrentOrgId();
MenuItem menuItem = platformService.getMenuItem(currentUser.getId(), orgId);
MenuItem item = menuItem.findChild(systemId);
List<MenuNodeView> view = this.build(item);
return JsonResult.success(view);
}
/**
* 用户所在公司的组织机构树
*
* @return
*/
@PostMapping(MODEL + "/org.json")
@ResponseBody
public JsonResult<OrgItem> getUserCompany() {
OrgItem orgItem = platformService.getUserOrgTree();
return JsonResult.success(orgItem);
}
/**
* 获取系统的菜单树
*
* @return
*/
@GetMapping(MODEL + "/menu/tree.json")
@ResponseBody
public JsonResult<List<MenuNodeView>> getMenuTree() {
MenuItem menuItem = platformService.buildMenu();
List<MenuNodeView> view = this.build(menuItem);
return JsonResult.success(view);
}
/**
* 获取功能点树
* @return
*/
@PostMapping(MODEL + "/function/tree.json")
@ResponseBody
public JsonResult<List<FunctionNodeView> > getFunctionTree() {
FunctionItem root = this.platformService.buildFunction();
List<FunctionNodeView> tree = buildFunctionTree(root);
return JsonResult.success(tree);
}
private List<MenuNodeView> build(MenuItem node) {
List<MenuItem> list = node.getChildren();
if (list.size() == 0) {
return Collections.EMPTY_LIST;
}
List<MenuNodeView> views = new ArrayList<MenuNodeView>(list.size());
for (MenuItem item : list) {
MenuNodeView view = new MenuNodeView();
view.setCode(item.getData().getCode());
view.setName(item.getData().getName());
view.setIcon(item.getData().getIcon());
view.setId(item.getData().getId());
view.setPath((String) item.getData().get("accessUrl"));
List<MenuNodeView> children = this.build(item);
view.setChildren(children);
views.add(view);
}
return views;
}
private List<FunctionNodeView> buildFunctionTree(FunctionItem node){
List<FunctionItem> list = node.getChildren();
if(list.size()==0){
return Collections.EMPTY_LIST;
}
List<FunctionNodeView> views = new ArrayList<FunctionNodeView>(list.size());
for(FunctionItem item :list){
FunctionNodeView view = new FunctionNodeView();
view.setCode(item.getData().getCode());
view.setName(item.getData().getName());
view.setId(item.getData().getId());
List<FunctionNodeView> children = this.buildFunctionTree(item);
view.setChildren(children);
views.add(view);
}
return views;
}
class SystemMenuView {
String code;
Long id;
String name;
String icon;
public SystemMenuView(Long id, String code, String name) {
this.id = id;
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
class MenuNodeView {
String name;
String code;
Long id;
String path;
String icon;
List<MenuNodeView> children = new ArrayList<MenuNodeView>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<MenuNodeView> getChildren() {
return children;
}
public void setChildren(List<MenuNodeView> children) {
this.children = children;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
class FunctionNodeView{
String name;
String code;
Long id;
String icon;
List<FunctionNodeView> children=new ArrayList<FunctionNodeView>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<FunctionNodeView> getChildren() {
return children;
}
public void setChildren(List<FunctionNodeView> children) {
this.children = children;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
}
package com.ibeetl.admin.core.web;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.rbac.UserLoginInfo;
import com.ibeetl.admin.core.rbac.tree.MenuItem;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.CoreUserService;
import com.ibeetl.admin.core.util.HttpRequestLocal;
import com.ibeetl.admin.core.util.PlatformException;
@Controller
public class IndexController {
@Autowired
CorePlatformService platformService;
@Autowired
CoreUserService userService;
@Autowired
HttpRequestLocal httpRequestLocal;
@RequestMapping("/")
public ModelAndView login() {
ModelAndView view = new ModelAndView("/login.html");
return view;
}
@PostMapping("/login.do")
public ModelAndView login(String code, String password) {
UserLoginInfo info = userService.login(code, password);
if (info == null) {
throw new PlatformException("用户名密码错");
}
CoreUser user = info.getUser();
CoreOrg currentOrg = info.getOrgs().get(0);
for (CoreOrg org : info.getOrgs()) {
if (org.getId() == user.getOrgId()) {
currentOrg = org;
break;
}
}
info.setCurrentOrg(currentOrg);
// 记录登录信息到session
this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs());
ModelAndView view = new ModelAndView("redirect:/index.do");
return view;
}
@RequestMapping("/index.do")
public ModelAndView index() {
ModelAndView view = new ModelAndView("/index.html");
CoreUser currentUser = platformService.getCurrentUser();
Long orgId = platformService.getCurrentOrgId();
MenuItem menuItem = platformService.getMenuItem(currentUser.getId(), orgId);
view.addObject("menus", menuItem);
return view;
}
@RequestMapping("/logout.do")
public ModelAndView logout(HttpServletRequest request) {
HttpSession session = request.getSession();
Enumeration eum = session.getAttributeNames();
while(eum.hasMoreElements()) {
String key = (String)eum.nextElement();
session.removeAttribute(key);
}
ModelAndView view = new ModelAndView("redirect:/");
return view;
}
@RequestMapping("/changeOrg.do")
public ModelAndView changeOrg(HttpServletRequest request,Long orgId) {
platformService.changeOrg(orgId);
ModelAndView view = new ModelAndView("redirect:/index.do");
return view;
}
}
package com.ibeetl.admin.core.web;
/**
* 描述: json格式数据返回对象,使用CustomJsonResultSerializer 来序列化
* @author : lijiazhi
*/
public class JsonResult<T> {
private String code;
private String msg;
private T data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "code=" + code + " message=" + msg + " data=" + data;
}
public static <T> JsonResult<T> fail() {
JsonResult<T> ret = new JsonResult<T>();
ret.setCode(JsonReturnCode.FAIL.getCode());
ret.setMsg(JsonReturnCode.FAIL.getDesc());
return ret;
}
public static <T> JsonResult<T> fail(T data) {
JsonResult<T> ret = JsonResult.fail();
ret.setData(data);
return ret;
}
public static <T> JsonResult<T> failMessage(String msg) {
JsonResult<T> ret = JsonResult.fail();
ret.setMsg(msg);
return ret;
}
public static <T> JsonResult<T> successMessage(String msg) {
JsonResult<T> ret = JsonResult.success();
ret.setMsg(msg);
return ret;
}
public static <T> JsonResult<T> success() {
JsonResult<T> ret = new JsonResult<T>();
ret.setCode(JsonReturnCode.SUCCESS.getCode());
ret.setMsg(JsonReturnCode.SUCCESS.getDesc());
return ret;
}
public static <T> JsonResult<T> success(T data) {
JsonResult<T> ret = JsonResult.success();
ret.setData(data);
return ret;
}
public static <T> JsonResult<T> http404(T data) {
JsonResult<T> ret = new JsonResult<T>();
ret.setCode(JsonReturnCode.NOT_FOUND.getCode());
ret.setMsg(JsonReturnCode.NOT_FOUND.getDesc());
ret.setData(data);
return ret;
}
public static <T> JsonResult<T> http403(T data) {
JsonResult<T> ret = new JsonResult<T>();
ret.setCode(JsonReturnCode.ACCESS_ERROR.getCode());
ret.setMsg(JsonReturnCode.ACCESS_ERROR.getDesc());
ret.setData(data);
return ret;
}
}
package com.ibeetl.admin.core.web;
/**
* 描述: json格式数据返回码
*<ul>
* <li>100 : 用户未登录 </li>
* <li>200 : 成功 </li>
* <li>300 : 失败 </li>
* </ul>
* @author : Administrator
*/
public enum JsonReturnCode {
NOT_LOGIN("401","未登录"),
SUCCESS ("200","成功"),
FAIL ("500","内部失败"),
ACCESS_ERROR ("403","禁止访问"),
NOT_FOUND ("404","页面未发现");
private String code;
private String desc;
JsonReturnCode(String code, String desc) {
this.code = code;
this.desc = desc;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment