Browse Source

增加saber代码生成

test
smallchill 6 years ago
parent
commit
dc500ca0ef
  1. 10
      blade-common/src/main/java/org/springblade/common/constant/CommonConstant.java
  2. 3
      blade-ops/blade-develop/src/main/java/org/springblade/develop/controller/CodeController.java
  3. 108
      blade-ops/blade-develop/src/main/java/org/springblade/develop/support/BladeGenerator.java
  4. 16
      blade-ops/blade-develop/src/main/java/org/springblade/develop/support/CodeGenerator.java
  5. 50
      blade-ops/blade-develop/src/main/resources/templates/saber/api.js.vm
  6. 174
      blade-ops/blade-develop/src/main/resources/templates/saber/crud.vue.vm

10
blade-common/src/main/java/org/springblade/common/constant/CommonConstant.java

@ -27,6 +27,16 @@ public interface CommonConstant {
*/
String SENTINEL_PROD_ADDR = "192.168.186.129:8858";
/**
* sword 系统名
*/
String SWORD_NAME = "sword";
/**
* saber 系统名
*/
String SABER_NAME = "saber";
/**
* 顶级父节点id
*/

3
blade-ops/blade-develop/src/main/java/org/springblade/develop/controller/CodeController.java

@ -97,10 +97,11 @@ public class CodeController extends BladeController {
*/
@PostMapping("/gen-code")
@ApiOperation(value = "代码生成", notes = "传入ids", position = 8)
public R genCode(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
public R genCode(@ApiParam(value = "主键集合", required = true) @RequestParam String ids, @RequestParam(defaultValue = "sword") String system) {
Collection<Code> codes = codeService.listByIds(Func.toIntList(ids));
codes.forEach(code -> {
BladeGenerator generator = new BladeGenerator();
generator.setSystemName(system);
generator.setServiceName(code.getServiceName());
generator.setPackageName(code.getPackageName());
generator.setPackageDir(code.getApiPath());

108
blade-ops/blade-develop/src/main/java/org/springblade/develop/support/BladeGenerator.java

@ -28,6 +28,7 @@ import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.core.io.ClassPathResource;
@ -46,6 +47,10 @@ import java.util.*;
@Data
@Slf4j
public class BladeGenerator {
/**
* 代码所在系统
*/
private String systemName = CommonConstant.SWORD_NAME;
/**
* 代码所在服务名
*/
@ -195,48 +200,63 @@ public class BladeGenerator {
}
});
if (Func.isNotBlank(packageWebDir)) {
focList.add(new FileOutConfig("/templates/sword/action.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/actions" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/model.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/models" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/service.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/services" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/list.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/add.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Add.js";
}
});
focList.add(new FileOutConfig("/templates/sword/edit.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Edit.js";
}
});
focList.add(new FileOutConfig("/templates/sword/view.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "View.js";
}
});
if (Func.equals(systemName, CommonConstant.SWORD_NAME)) {
focList.add(new FileOutConfig("/templates/sword/action.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/actions" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/model.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/models" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/service.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/services" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/list.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/add.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Add.js";
}
});
focList.add(new FileOutConfig("/templates/sword/edit.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Edit.js";
}
});
focList.add(new FileOutConfig("/templates/sword/view.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.firstCharToUpper(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "View.js";
}
});
} else if (Func.equals(systemName, CommonConstant.SABER_NAME)) {
focList.add(new FileOutConfig("/templates/saber/api.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/api" + "/" + servicePackage.toLowerCase() + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/saber/crud.vue.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/views" + "/" + servicePackage.toLowerCase() + "/" + tableInfo.getEntityName().toLowerCase() + ".vue";
}
});
}
}
cfg.setFileOutConfigList(focList);
return cfg;
@ -265,7 +285,7 @@ public class BladeGenerator {
* @return outputDir
*/
public String getOutputDir() {
return Func.isBlank(packageDir) ? System.getProperty("user.dir") : packageDir + "/src/main/java";
return (Func.isBlank(packageDir) ? System.getProperty("user.dir") : packageDir) + "/src/main/java";
}
/**
@ -274,7 +294,7 @@ public class BladeGenerator {
* @return outputDir
*/
public String getOutputWebDir() {
return Func.isBlank(packageWebDir) ? System.getProperty("user.dir") : packageWebDir + "/src";
return (Func.isBlank(packageWebDir) ? System.getProperty("user.dir") : packageWebDir) + "/src";
}
/**

16
blade-ops/blade-develop/src/main/java/org/springblade/develop/support/CodeGenerator.java

@ -29,9 +29,13 @@ public class CodeGenerator {
*/
public static String PACKAGE_NAME = "org.springblade.demo";
/**
* 代码生成的地址
* 前端代码生成所属系统
*/
public static String PACKAGE_DIR = "/blade-ops/blade-codegen/src/main/java";
public static String SYSTEM_NAME = "saber";
/**
* 前端代码生成地址
*/
public static String PACKAGE_WEB_DIR = "/Users/chill/Workspaces/dev/Saber";
/**
* 需要去掉的表前缀
*/
@ -39,7 +43,7 @@ public class CodeGenerator {
/**
* 需要生成的表名(两者只能取其一)
*/
public static String[] INCLUDE_TABLES = {"blade_blog"};
public static String[] INCLUDE_TABLES = {"blade_notice"};
/**
* 需要排除的表名(两者只能取其一)
*/
@ -53,13 +57,15 @@ public class CodeGenerator {
*/
public static String[] SUPER_ENTITY_COLUNMS = {"id", "create_time", "create_user", "update_time", "update_user", "status", "is_deleted"};
/**
* RUN THIS
*/
public static void generate() {
public static void main(String[] args) {
BladeGenerator generator = new BladeGenerator();
generator.setSystemName(SYSTEM_NAME);
generator.setPackageName(PACKAGE_NAME);
generator.setPackageDir(PACKAGE_DIR);
generator.setPackageWebDir(PACKAGE_WEB_DIR);
generator.setTablePrefix(TABLE_PREFIX);
generator.setIncludeTables(INCLUDE_TABLES);
generator.setExcludeTables(EXCLUDE_TABLES);

50
blade-ops/blade-develop/src/main/resources/templates/saber/api.js.vm

@ -0,0 +1,50 @@
import request from '@/router/axios';
export const getList = (current, size, params) => {
return request({
url: '/api/$!{cfg.serviceName}/$!{table.entityPath}/list',
method: 'get',
params: {
...params,
current,
size,
}
})
}
export const getDetail = (id) => {
return request({
url: '/api/$!{cfg.serviceName}/$!{table.entityPath}/detail',
method: 'get',
params: {
id
}
})
}
export const remove = (ids) => {
return request({
url: '/api/$!{cfg.serviceName}/$!{table.entityPath}/remove',
method: 'post',
params: {
ids,
}
})
}
export const add = (row) => {
return request({
url: '/api/$!{cfg.serviceName}/$!{table.entityPath}/submit',
method: 'post',
data: row
})
}
export const update = (row) => {
return request({
url: '/api/$!{cfg.serviceName}/$!{table.entityPath}/submit',
method: 'post',
data: row
})
}

174
blade-ops/blade-develop/src/main/resources/templates/saber/crud.vue.vm

@ -0,0 +1,174 @@
<template>
<basic-container>
<avue-crud :option="option"
:data="data"
:page="page"
@row-del="rowDel"
v-model="form"
:permission="permissionList"
@row-update="rowUpdate"
@row-save="rowSave"
:before-open="beforeOpen"
@search-change="searchChange"
@search-reset="searchReset"
@selection-change="selectionChange"
@on-load="onLoad">
<template slot="menuLeft">
<el-button type="danger"
size="small"
icon="el-icon-delete"
plain
v-if="permission.$!{table.entityPath}_delete"
@click="handleDelete">删 除
</el-button>
</template>
</avue-crud>
</basic-container>
</template>
<script>
import {getList, getDetail, add, update, remove} from "@/api/$!{servicePackage}/$!{table.entityPath}";
import {mapGetters} from "vuex";
export default {
data() {
return {
form: {},
page: {
pageSize: 10,
currentPage: 1,
total: 0
},
selectionList: [],
option: {
tip: false,
border: true,
index: true,
viewBtn: true,
selection: true,
column: [
#foreach($field in $!{table.fields})
{
label: "$!{field.comment}",
prop: "$!{field.propertyName}",
rules: [{
required: true,
message: "请输入$!{field.comment}",
trigger: "blur"
}]
},
#end
]
},
data: []
};
},
computed: {
...mapGetters(["permission"]),
permissionList() {
return {
addBtn: this.permission.$!{table.entityPath}_add,
viewBtn: this.permission.$!{table.entityPath}_view,
delBtn: this.permission.$!{table.entityPath}_delete,
editBtn: this.permission.$!{table.entityPath}_edit
};
},
ids() {
let ids = [];
this.selectionList.forEach(ele => {
ids.push(ele.id);
});
return ids.join(",");
}
},
methods: {
rowSave(row, loading) {
add(row).then(() => {
loading();
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
});
},
rowUpdate(row, index, loading) {
update(row).then(() => {
loading();
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
});
},
rowDel(row) {
this.$confirm("确定将选择数据删除?", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
})
.then(() => {
return remove(row.id);
})
.then(() => {
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
});
},
searchReset() {
this.onLoad(this.page);
},
searchChange(params) {
this.onLoad(this.page, params);
},
selectionChange(list) {
this.selectionList = list;
},
handleDelete() {
if (this.selectionList.length === 0) {
this.$message.warning("请选择至少一条数据");
return;
}
this.$confirm("确定将选择数据删除?", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
})
.then(() => {
return remove(this.ids);
})
.then(() => {
this.onLoad(this.page);
this.$message({
type: "success",
message: "操作成功!"
});
this.$refs.crud.toggleSelection();
});
},
beforeOpen(done, type) {
if (["edit", "view"].includes(type)) {
getDetail(this.form.id).then(res => {
this.form = res.data.data;
});
}
done();
},
onLoad(page, params = {}) {
getList(page.currentPage, page.pageSize, params).then(res => {
const data = res.data.data;
this.page.total = data.total;
this.data = data.records;
});
}
}
};
</script>
<style>
</style>
Loading…
Cancel
Save