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'
})
return Promise.reject(new Error(msg))
}else if (code == 199){
14 }else if (code == 199){
Message({
message: "查询结果为空!",
type: 'info'

View File

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

View File

@ -186,6 +186,7 @@ import { listModule, getModule, delModule, addModule, updateModule, exportModule
import { getModulePreview } from '@/api/tool/interTable'
import hljs from "highlight.js/lib/highlight";
import "highlight.js/styles/github-gist.css";
import {downLoadZip} from "@/utils/zipdownload";
hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql"));
export default {
@ -247,7 +248,6 @@ export default {
methods: {
/** 预览代码 */
handlePreview(id){
console.log(id)
getModulePreview(id).then(res=>{
this.preview.data = res.data;
this.preview.open = true;
@ -255,11 +255,14 @@ export default {
this.preview.activeName = keys[0]
})
},
/** 生成代码操作 */
handleGenTable(row) {
downLoadZip("/generator/intertable/moudlegen/" + row.id, "hchyun");
},
/** 高亮显示 */
highlightedCode(code, key) {
const vmName = key;
var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length);
console.log(language)
const result = hljs.highlight(language, code || "", true);
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);
/**
* 生成代码(工作台)
* @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.ServerResult;
import com.hchyun.generator.dao.ApiclassDao;
import com.hchyun.generator.dao.GenTableDao;
import com.hchyun.generator.dao.ModuleDao;
import com.hchyun.generator.dto.InterTableDto;
import com.hchyun.generator.entity.Apiclass;
@ -50,6 +51,8 @@ public class InterTableServiceImpl implements InterTableService {
private ApiclassDao apiclassDao;
@Autowired
private ModuleDao moduleDao;
@Autowired
private GenTableDao genTableDao;
/**
* 查询接口信息
@ -257,6 +260,7 @@ public class InterTableServiceImpl implements InterTableService {
}
@Override
@Transactional
public ServerResult<byte[]> generatorCodeClass(Long cid) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@ -268,45 +272,84 @@ public class InterTableServiceImpl implements InterTableService {
StringWriter sw = new StringWriter();
Template template = Velocity.getTemplate(templates.get("java"), Constants.UTF8);
template.merge(context, sw);
zip = genCode(getJavaClassName(apiclass.getcName()),zip,sw);
if (zip == null){
return new ServerResult<byte[]>(false,"代码生成失败!");
zip = genCode(getJavaClassName(apiclass.getcName()), zip, sw);
if (zip == null) {
return new ServerResult<byte[]>(false, "代码生成失败!");
}
sw = new StringWriter();
template = Velocity.getTemplate(templates.get("sql"), Constants.UTF8);
template.merge(context, sw);
zip = genCode(getSqlName(apiclass.getcName()),zip,sw);
if (zip == null){
return new ServerResult<byte[]>(false,"代码生成失败!");
zip = genCode(getSqlName(apiclass.getcName()), zip, sw);
if (zip == null) {
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);
return new ServerResult<byte[]>(true,outputStream.toByteArray());
return new ServerResult<byte[]>(true, outputStream.toByteArray());
}catch (RuntimeException e){
} catch (RuntimeException e) {
logger.error(e.getMessage());
return new ServerResult<byte[]>(false,"代码生成失败!");
return new ServerResult<byte[]>(false, "代码生成失败!");
}
}
@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 {
zip.putNextEntry(new ZipEntry(className));
IOUtils.write(sw.toString(),zip,Constants.UTF8);
IOUtils.write(sw.toString(), zip, Constants.UTF8);
IOUtils.closeQuietly(sw);
zip.flush();
zip.closeEntry();
return zip;
}catch (IOException e){
} catch (IOException e) {
logger.error(e.getMessage());
return null;
}
}
public String getJavaClassName(String cName) {
return InterTableUtils.getUpperCase(cName) + "Controller.java";
}