之前发的未做完整测试,现在已进行完整测试,并修改了一部分代码,下面是最终版
现在前后端开发已经成为主流,为了加快前后端效率。。。省略一万字。。。
上菜:
工具类:Api.java
/** * @author Catch * @date 2018-10-16 下午5:38 * @description 生成前端 Api 模块 */ public class Api { private static final String TARGET_DIR="/media/catch/software/projects/WebStormProjects/wenzuo-user/src/api"; public static void main(String[] args) { genApi(); } public static void genApi(){ String empty=""; Engine engine=Engine.create("api").setSourceFactory(new ClassPathSourceFactory()); Template template=engine.getTemplate("api_template.txt"); ApiRoutes apiRoutes=new ApiRoutes(); apiRoutes.config(); List<Routes.Route> routes=apiRoutes.getRouteItemList(); routes.forEach(route -> { String ctrlName=route.getControllerKey().replace("/api/admin/",empty).replace("/api/",empty); Class<? extends Controller> ctrlClazz=route.getControllerClass(); genControllerApi(template,ctrlName,ctrlClazz); }); } public static void genControllerApi(Template template,String ctrlName,Class<? extends Controller> ctrlClazz){ File file=FileUtil.touch(TARGET_DIR,ctrlName+".js"); List<Kv> apis=new ArrayList<>(); Method[] methods=ctrlClazz.getDeclaredMethods(); Kv api; for (Method method : methods) { api=Kv.create(); Des des=method.getDeclaredAnnotation(Des.class); if (des!=null){ api.set("des",des.value()); } Parameter[] parameters = method.getParameters(); List<Kv> params=new ArrayList<>(); for (Parameter parameter : parameters) { params.add( Kv.by("name",parameter.getName()) .set("type",parameter.getType().getSimpleName()) ); } api.set("params",params); api.set("method",ctrlName+"_"+method.getName()); api.set("router","/"+ctrlName+"/"+method.getName()); apis.add(api); } Kv kv=Kv.by("apis",apis); template.render(kv,file); } }
ApiRoutes.java
/** * @author Catch * @date 2018-09-17 下午3:45 * @description api 模块路由 */ public class ApiRoutes extends Routes { @Override public void config() { addInterceptor(new CORSInterceptor()); add("/api/common", CommonController.class); add("/api/admin/user", UserController.class); } }
Controller.java
/** * @author Catch * @date 2018-09-17 下午3:43 * @description 后台-用户模块 */ @Before(SiteAuthInterceptor.class) public class UserController extends Controller { @Inject private UserService service; @Clear(SiteAuthInterceptor.class) @Des("用户-注册") public void register(String host,String mobile,String smsCode,String password){ Ret ret=service.register(host, mobile, smsCode, password); renderJson(ret); } @Clear(SiteAuthInterceptor.class) @Des("用户-登录") public void login(String mobile, String password, boolean remember){ renderJson(service.login(mobile,password,remember)); } public void getInfo(){ User user=getAttr("user"); renderJson(service.getInfo(user)); } }
Des注解类
/** * @author Catch * @date 2018-10-16 下午5:57 * @description api描述注解 */ @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface Des { String value(); }
api_template.txt
import request from '@/assets/js/request' #for(api : apis) /** #if(api.des) * #(api.des) #end * @param data #for(param : api.params) * #(param.name): #(param.type) #end */ export function #(api.method)(data) { return request({ url: '#(api.router)', method: 'post', data }) } #end
生成效果:user.js 为例
import request from '@/assets/js/request' /** * 用户-登录 * @param data * mobile: String * password: String * remember: boolean */ export function user_login(data) { return request({ url: '/user/login', method: 'post', data }) } /** * 用户-注册 * @param data * host: String * mobile: String * smsCode: String * password: String */ export function user_register(data) { return request({ url: '/user/register', method: 'post', data }) } /** * @param data */ export function user_getInfo(data) { return request({ url: '/user/getInfo', method: 'post', data }) }