/**
* 统一的方法进入
* @param _function_no
* @param _params
* @return JSON数据
*/
public Object ExcuteServices(String _function_no, NutMap _params) {
log.info("function_no : " + _function_no + " params:" + _params.toString());
//获取这个功能号的配置
List sysfunctions = dao.query(SysFunction.class, Cnd.where("functionid", "=", _function_no));
if(sysfunctions == null || sysfunctions.isEmpty()) {
return GetSystemExcepitonResponse(String.format("找不到功能编号为%s的方法!", _function_no));
}
SysFunction sysfunction = sysfunctions.get(0);
if(Strings.isEmpty(sysfunction.getFunctionId()) || Strings.isEmpty(sysfunction.getServiceClass()) || Strings.isEmpty(sysfunction.getServiceMethod())) {
return GetSystemExcepitonResponse(String.format("功能编号%s的配置错误!", _function_no));
}
//获得命名空间
//String _nameSpace = Strings.endsWithChar(sysfunction.getServiceClass(), '.') ? sysfunction.getServiceClass() : sysfunction.getServiceClass() + ".";
//获得服务名称
String services_name = sysfunction.getServiceClass();
//通过反射判断这个类是否存在
//反射之中的所有泛型都定义为?,返回值都是Object
Class<?> services_class;
try {
services_class = Class.forName(services_name);
}
catch (ClassNotFoundException e) {
return GetSystemExcepitonResponse(String.format("%s服务类没有找到!", services_name));
}
//判断这个类是否在IOC容器中存在
Object obj;
try {
obj = Mvcs.getIoc().get(services_class);
} catch (IocException e) {
return GetSystemExcepitonResponse(String.format("%s服务类在IOC容器中没有找到,请注意类是否为IOC管理!", services_name));
}
//通过反射调用方法
Mirror mirror = Mirror.me(obj.getClass());
//获得所有方法--判断是否存在
boolean hasMethod = false;
Method[] methods = mirror.getMethods();
for (Method method : methods) {
String methodName = method.getName();
if(methodName.toUpperCase().equals(sysfunction.getServiceMethod().toUpperCase())) {
hasMethod = true;
break;
}
}
if(!hasMethod) {
return GetSystemExcepitonResponse(String.format("%s服务类的{1}方法没有找到!", services_name, sysfunction.getServiceMethod()));
}
//判断权限除了登录方法 -- 先取SESSION,如SESSION中没有,就为WebServices传入
if(!_function_no.equals(Globals.LOGIN_FUNCTION_ID) && (sysfunction.getIsMustBind() == null || !"1".equals(sysfunction.getIsMustBind()))) {
SysUser user = (SysUser)Mvcs.getHttpSession().getAttribute(Globals.SESSION_USER_CODE);
if(user != null) {
//获得角色编号
List userRoleList = new ArrayList();
userRoleList = dao.query(UserRole.class, Cnd.where("userId","=",user.getUserId()));
if(userRoleList.size() == 0){
return GetResponse(Globals.Code.BUSINESS_EXCEPTION_CODE, "当前用户没有使用此功能的权限!");
}
// String roleId = user.getRoleId();
// if(roleId == null || roleId.equals("")) {
// return GetResponse(Globals.Code.BUSINESS_EXCEPTION_CODE, "当前用户没有使用此功能的权限!");
// }
//判断是不是管理员
boolean isAdmin = false;
for(int i=0; i<userRoleList.size(); i++){
UserRole ur = userRoleList.get(i);
Role role = dao.fetch(Role.class, Cnd.where("rolecode","=",ur.getRoleCode()));
String roleType = role.getRoleType();
if("0".equals(roleType)){
//有系统管理员的角色
isAdmin = true;
break;
}
}
if(!isAdmin){
//没有管理员权限
//查询数据库是否有权限
boolean isExist = false;
for(int i=0; i<userRoleList.size(); i++){
UserRole ur = userRoleList.get(i);
RoleFunction roleFunction = dao.fetch(RoleFunction.class, Cnd.where("rolecode","=",ur.getRoleCode()).and("functionid","=",_function_no));
if(roleFunction != null){
isExist = true;
break;
}
}
if(!isExist){
return GetResponse(Globals.Code.BUSINESS_EXCEPTION_CODE, "当前用户没有使用此功能的权限!");
}
}
// Role role = dao.fetch(Role.class, roleId);
// if(!role.getIsAdmin().equals("1")) {
// //查询数据库是否有权限
// RoleFunction roleFunction = dao.fetchx(RoleFunction.class, roleId, _function_no);
// if(roleFunction == null) {
// return GetResponse(Globals.Code.BUSINESS_EXCEPTION_CODE, "当前用户没有使用此功能的权限!");
// }
// }
} else {
//判断是否为Webservices进入
if(_params.containsKey(Globals.WEBSERVICES_ROLE)) {
RoleFunction roleFunction = dao.fetchx(RoleFunction.class, _params.get(Globals.WEBSERVICES_ROLE), _function_no);
if(roleFunction == null) {
return GetResponse(Globals.Code.BUSINESS_EXCEPTION_CODE, "当前用户没有使用此功能的权限!");
}
} else {
return GetResponse(Globals.Code.BUSINESS_EXCEPTION_CODE, "当前用户没有使用此功能的权限!");
}
}
}
//TODO 检查参数配置
try {
if(sysfunction.getNoCodeResponse() != null && "1".equals(sysfunction.getNoCodeResponse())){
//返回没有code的json形式
return mirror.invoke(obj, sysfunction.getServiceMethod(), _params);
}else{
return this.GetOkResponse(mirror.invoke(obj, sysfunction.getServiceMethod(), _params));
}
}
catch (Exception ex) {
if (ex.getCause() instanceof BusinessException) {
BusinessException businessException = (BusinessException)ex.getCause();
return this.GetBusinessExceptionResponse(businessException.getMessage());
} else if(ex.getCause() instanceof SystemException) {
SystemException systemException = (SystemException)ex.getCause();
return this.GetSystemExcepitonResponse(systemException.getMessage());
} else {
return this.GetSystemExcepitonResponse(ex.getMessage());
}
}
}