This commit is contained in:
20932067@zju.edu.cn 2021-01-28 23:57:15 +08:00
parent 3967854cd2
commit 51386bff28
6 changed files with 98 additions and 18 deletions

View File

@ -48,7 +48,7 @@ service.interceptors.response.use(res => {
type: 'error' type: 'error'
}) })
return Promise.reject(new Error(msg)) return Promise.reject(new Error(msg))
}else if (code == 199){ 14 }else if (code == 199){
Message({ Message({
message: "查询结果为空!", message: "查询结果为空!",
type: 'info' type: 'info'

View File

@ -1,5 +1,6 @@
import axios from 'axios' import axios from 'axios'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import {Message} from "element-ui";
const mimeMap = { const mimeMap = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
@ -37,4 +38,5 @@ export function resolveBlob(res, mimeType) {
document.body.appendChild(aLink) document.body.appendChild(aLink)
aLink.click() aLink.click()
document.body.appendChild(aLink) document.body.appendChild(aLink)
} }

View File

@ -186,6 +186,7 @@ import { listModule, getModule, delModule, addModule, updateModule, exportModule
import { getModulePreview } from '@/api/tool/interTable' import { getModulePreview } from '@/api/tool/interTable'
import hljs from "highlight.js/lib/highlight"; import hljs from "highlight.js/lib/highlight";
import "highlight.js/styles/github-gist.css"; import "highlight.js/styles/github-gist.css";
import {downLoadZip} from "@/utils/zipdownload";
hljs.registerLanguage("java", require("highlight.js/lib/languages/java")); hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql")); hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql"));
export default { export default {
@ -247,7 +248,6 @@ export default {
methods: { methods: {
/** 预览代码 */ /** 预览代码 */
handlePreview(id){ handlePreview(id){
console.log(id)
getModulePreview(id).then(res=>{ getModulePreview(id).then(res=>{
this.preview.data = res.data; this.preview.data = res.data;
this.preview.open = true; this.preview.open = true;
@ -255,11 +255,14 @@ export default {
this.preview.activeName = keys[0] this.preview.activeName = keys[0]
}) })
}, },
/** 生成代码操作 */
handleGenTable(row) {
downLoadZip("/generator/intertable/moudlegen/" + row.id, "hchyun");
},
/** 高亮显示 */ /** 高亮显示 */
highlightedCode(code, key) { highlightedCode(code, key) {
const vmName = key; const vmName = key;
var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length); var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length);
console.log(language)
const result = hljs.highlight(language, code || "", true); const result = hljs.highlight(language, code || "", true);
return result.value || ' '; return result.value || ' ';
}, },

View File

@ -144,6 +144,33 @@ public class InterTableController extends HcyBaseController {
} }
} }
/**
* 生成代码(工作台)
* @param id
* @param response
* @return
*/
@ApiOperation("生成代码(工作台)")
@PreAuthorize("@ss.hasPermi('generator:intertable:moudlegen')")
@Log(title = "生成代码(工作台)", businessType = BusinessType.GENCODE)
@GetMapping("/moudlegen/{id}")
public AjaxResult moudleGenerator(@PathVariable("id") Long id, HttpServletResponse response){
try {
ServerResult<byte[]> serverResult = interTableService.generatorCodeMoudle(id);
if (serverResult.isStart()) {
genCode(response, serverResult.getData());
return AjaxResult.success();
} else {
return AjaxResult.error(serverResult.getMsg());
}
}catch (RuntimeException | IOException e){
logger.error(e.getMessage());
return AjaxResult.error(ReturnConstants.SYS_ERROR);
}
}
/** /**
* 修改接口信息 * 修改接口信息
*/ */

View File

@ -85,6 +85,11 @@ public interface InterTableService
*/ */
ServerResult<byte[]> generatorCodeClass(Long cid); ServerResult<byte[]> generatorCodeClass(Long cid);
/**
* 生成代码(工作台)
* @param mid
* @return
*/
ServerResult<byte[]> generatorCodeMoudle(Long mid);
} }

View File

@ -15,6 +15,7 @@ import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.utils.SecurityUtils; import com.hchyun.common.utils.SecurityUtils;
import com.hchyun.common.utils.ServerResult; import com.hchyun.common.utils.ServerResult;
import com.hchyun.generator.dao.ApiclassDao; import com.hchyun.generator.dao.ApiclassDao;
import com.hchyun.generator.dao.GenTableDao;
import com.hchyun.generator.dao.ModuleDao; import com.hchyun.generator.dao.ModuleDao;
import com.hchyun.generator.dto.InterTableDto; import com.hchyun.generator.dto.InterTableDto;
import com.hchyun.generator.entity.Apiclass; import com.hchyun.generator.entity.Apiclass;
@ -50,6 +51,8 @@ public class InterTableServiceImpl implements InterTableService {
private ApiclassDao apiclassDao; private ApiclassDao apiclassDao;
@Autowired @Autowired
private ModuleDao moduleDao; private ModuleDao moduleDao;
@Autowired
private GenTableDao genTableDao;
/** /**
* 查询接口信息 * 查询接口信息
@ -257,6 +260,7 @@ public class InterTableServiceImpl implements InterTableService {
} }
@Override @Override
@Transactional
public ServerResult<byte[]> generatorCodeClass(Long cid) { public ServerResult<byte[]> generatorCodeClass(Long cid) {
try { try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@ -280,6 +284,10 @@ public class InterTableServiceImpl implements InterTableService {
if (zip == null) { if (zip == null) {
return new ServerResult<byte[]>(false, "代码生成失败!"); return new ServerResult<byte[]>(false, "代码生成失败!");
} }
int start = genTableDao.insertMenu(sw.toString());
if (!(start > 0)) {
return new ServerResult<byte[]>(false, ReturnConstants.DB_EX);
}
IOUtils.closeQuietly(zip); IOUtils.closeQuietly(zip);
return new ServerResult<byte[]>(true, outputStream.toByteArray()); return new ServerResult<byte[]>(true, outputStream.toByteArray());
@ -289,6 +297,43 @@ public class InterTableServiceImpl implements InterTableService {
} }
} }
@Override
@Transactional
public ServerResult<byte[]> generatorCodeMoudle(Long mid) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
List<InterTable> interTableList = interTableDao.selectInterTableModule(mid);
if (interTableList.size() == 0) {
return new ServerResult<byte[]>(false, "该模块下接口为空,请先创建接口!");
}
VelocityInitializer.initVelocity();
List<VelocityContext> contextList = InterTableUtils.prepareMoudleContext(interTableList);
Map<String, String> templates = InterTableUtils.getTemplateList(2);
StringWriter sqlsw = new StringWriter();
Template sqltemplate = Velocity.getTemplate(templates.get("sql"), Constants.UTF8);
for (VelocityContext context : contextList) {
StringWriter sw = new StringWriter();
Template template = Velocity.getTemplate(templates.get("java"), Constants.UTF8);
template.merge(context, sw);
sqltemplate.merge(context, sqlsw);
zip = genCode(getJavaClassName((String) context.get("ClassName")), zip, sw);
}
zip = genCode(getSqlName("module"), zip, sqlsw);
if (zip != null) {
int start = genTableDao.insertMenu(sqlsw.toString());
if (!(start > 0)) {
return new ServerResult<byte[]>(false, ReturnConstants.DB_EX);
}
}
IOUtils.closeQuietly(zip);
return new ServerResult<byte[]>(true, outputStream.toByteArray());
} catch (RuntimeException e) {
logger.error(e.getMessage());
return new ServerResult<byte[]>(false, "代码生成失败!");
}
}
public ZipOutputStream genCode(String className, ZipOutputStream zip, StringWriter sw) { public ZipOutputStream genCode(String className, ZipOutputStream zip, StringWriter sw) {
try { try {
@ -305,8 +350,6 @@ public class InterTableServiceImpl implements InterTableService {
} }
public String getJavaClassName(String cName) { public String getJavaClassName(String cName) {
return InterTableUtils.getUpperCase(cName) + "Controller.java"; return InterTableUtils.getUpperCase(cName) + "Controller.java";
} }