Browse Source
Merge branch 'master' of http://47.92.195.73:8081/project_boditech_vidas_a8000_v3/a8k_app
tags/v0
Merge branch 'master' of http://47.92.195.73:8081/project_boditech_vidas_a8000_v3/a8k_app
tags/v0
8 changed files with 207 additions and 13 deletions
-
2pom.xml
-
18src/main/java/a8k/MyApplicationRunner.java
-
23src/main/java/a8k/controler/AppController.java
-
69src/main/java/a8k/controler/engineer/EngineerPageControler.java
-
20src/main/java/a8k/service/usermgr/AppUserMgrService.java
-
10src/main/java/a8k/utils/AppService.java
-
9src/main/java/a8k/utils/AppServiceAction.java
-
69src/main/java/a8k/utils/AppServiceManager.java
@ -0,0 +1,18 @@ |
|||||
|
package a8k; |
||||
|
import a8k.utils.AppServiceManager; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import org.springframework.boot.ApplicationArguments; |
||||
|
import org.springframework.boot.ApplicationRunner; |
||||
|
import org.springframework.core.annotation.Order; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
@Order(1) |
||||
|
@Component |
||||
|
public class MyApplicationRunner implements ApplicationRunner { |
||||
|
@Resource |
||||
|
private AppServiceManager appServiceManager; |
||||
|
|
||||
|
@Override |
||||
|
public void run(ApplicationArguments args) throws Exception { |
||||
|
this.appServiceManager.loadActions(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
package a8k.controler; |
||||
|
import a8k.appbase.appret.AppRet; |
||||
|
import a8k.utils.AppServiceManager; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
import java.util.Map; |
||||
|
@Controller |
||||
|
public class AppController { |
||||
|
@Resource |
||||
|
private AppServiceManager appServiceManager; |
||||
|
|
||||
|
@PostMapping("/api/app/execute-action") |
||||
|
@ResponseBody |
||||
|
public AppRet<Object> executeAction( @RequestBody Map<String, Object> params ) throws Exception { |
||||
|
String action = (String) params.get("name"); |
||||
|
Map<String, Object> actionParams = (Map<String, Object>) params.get("params"); |
||||
|
var result = this.appServiceManager.executeAction(action, actionParams); |
||||
|
return result instanceof AppRet ? (AppRet)result : AppRet.success(result); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
package a8k.utils; |
||||
|
import java.lang.annotation.ElementType; |
||||
|
import java.lang.annotation.Retention; |
||||
|
import java.lang.annotation.RetentionPolicy; |
||||
|
import java.lang.annotation.Target; |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
@Target({ElementType.TYPE}) |
||||
|
public @interface AppService { |
||||
|
String name(); |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package a8k.utils; |
||||
|
import java.lang.annotation.ElementType; |
||||
|
import java.lang.annotation.Retention; |
||||
|
import java.lang.annotation.RetentionPolicy; |
||||
|
import java.lang.annotation.Target; |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
@Target({ElementType.METHOD}) |
||||
|
public @interface AppServiceAction { |
||||
|
} |
@ -0,0 +1,69 @@ |
|||||
|
package a8k.utils; |
||||
|
import com.iflytop.uf.UfApplication; |
||||
|
import com.iflytop.uf.util.UfClassHelper; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import java.lang.reflect.Method; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
@Component |
||||
|
public class AppServiceManager { |
||||
|
public static final Logger LOG = LoggerFactory.getLogger(AppServiceManager.class); |
||||
|
|
||||
|
// actions |
||||
|
public Map<String, Method> actions; |
||||
|
// services |
||||
|
public Map<Method, Class<?>> actionServices; |
||||
|
|
||||
|
// load actions |
||||
|
public void loadActions() throws Exception { |
||||
|
this.actions = new HashMap<String, Method>(); |
||||
|
this.actionServices = new HashMap<Method, Class<?>>(); |
||||
|
var classList = UfClassHelper.getAllClassesInPackage("a8k"); |
||||
|
for (var clazz : classList) { |
||||
|
var serviceAnnotation = clazz.getAnnotation(AppService.class); |
||||
|
if (serviceAnnotation == null) { |
||||
|
continue ; |
||||
|
} |
||||
|
var methods = clazz.getMethods(); |
||||
|
for (var method : methods) { |
||||
|
if (!method.isAnnotationPresent(AppServiceAction.class)) { |
||||
|
continue ; |
||||
|
} |
||||
|
String actionKey = String.format("%s.%s", serviceAnnotation.name(), method.getName()); |
||||
|
if (this.actions.containsKey(actionKey)) { |
||||
|
throw new Exception(String.format("重复action '%s'", actionKey)); |
||||
|
} |
||||
|
this.actions.put(actionKey, method); |
||||
|
this.actionServices.put(method, clazz); |
||||
|
LOG.info("load app action : {}", actionKey); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// execute action |
||||
|
public Object executeAction(String action, Map<String, Object> params) throws Exception { |
||||
|
var method = actions.get(action); |
||||
|
if (method == null) { |
||||
|
throw new Exception("不存在"); |
||||
|
} |
||||
|
|
||||
|
var actionParamDefs = method.getParameters(); |
||||
|
Object[] args = new Object[actionParamDefs.length]; |
||||
|
for ( var i=0; i<actionParamDefs.length; i++ ) { |
||||
|
var name = actionParamDefs[i].getName(); |
||||
|
var value = params.get(name); |
||||
|
args[i] = value; |
||||
|
} |
||||
|
|
||||
|
var serviceClass = this.actionServices.get(method); |
||||
|
var service = UfApplication.getContext().getBean(serviceClass); |
||||
|
if ( method.getReturnType().equals(Void.TYPE)) { |
||||
|
method.invoke(service, args); |
||||
|
return null; |
||||
|
} else { |
||||
|
return method.invoke(service, args); |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue