diff --git a/hchyun-ui/src/utils/request.js b/hchyun-ui/src/utils/request.js index 77fe3d1..0a42a4f 100644 --- a/hchyun-ui/src/utils/request.js +++ b/hchyun-ui/src/utils/request.js @@ -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' diff --git a/hchyun-ui/src/utils/zipdownload.js b/hchyun-ui/src/utils/zipdownload.js index 363c45a..44a5bdc 100644 --- a/hchyun-ui/src/utils/zipdownload.js +++ b/hchyun-ui/src/utils/zipdownload.js @@ -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) + } diff --git a/hchyun-ui/src/views/tool/module/index.vue b/hchyun-ui/src/views/tool/module/index.vue index 97ba1e7..6f80b39 100644 --- a/hchyun-ui/src/views/tool/module/index.vue +++ b/hchyun-ui/src/views/tool/module/index.vue @@ -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 || ' '; }, diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/InterTableController.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/InterTableController.java index 607257d..0775fe7 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/InterTableController.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/controller/InterTableController.java @@ -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 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); + } + } + + + /** * 修改接口信息 */ diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/InterTableService.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/InterTableService.java index ee0622f..f64ef22 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/InterTableService.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/InterTableService.java @@ -85,6 +85,11 @@ public interface InterTableService */ ServerResult generatorCodeClass(Long cid); - + /** + * 生成代码(工作台) + * @param mid + * @return + */ + ServerResult generatorCodeMoudle(Long mid); } \ No newline at end of file diff --git a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/InterTableServiceImpl.java b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/InterTableServiceImpl.java index 3d8652a..c538159 100644 --- a/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/InterTableServiceImpl.java +++ b/hchyun/hchyun-generator/src/main/java/com/hchyun/generator/service/impl/InterTableServiceImpl.java @@ -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 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(false,"代码生成失败!"); + zip = genCode(getJavaClassName(apiclass.getcName()), zip, sw); + if (zip == null) { + return new ServerResult(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(false,"代码生成失败!"); + zip = genCode(getSqlName(apiclass.getcName()), zip, sw); + if (zip == null) { + return new ServerResult(false, "代码生成失败!"); + } + int start = genTableDao.insertMenu(sw.toString()); + if (!(start > 0)) { + return new ServerResult(false, ReturnConstants.DB_EX); } IOUtils.closeQuietly(zip); - return new ServerResult(true,outputStream.toByteArray()); + return new ServerResult(true, outputStream.toByteArray()); - }catch (RuntimeException e){ + } catch (RuntimeException e) { logger.error(e.getMessage()); - return new ServerResult(false,"代码生成失败!"); + return new ServerResult(false, "代码生成失败!"); + } + } + + @Override + @Transactional + public ServerResult generatorCodeMoudle(Long mid) { + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ZipOutputStream zip = new ZipOutputStream(outputStream); + List interTableList = interTableDao.selectInterTableModule(mid); + if (interTableList.size() == 0) { + return new ServerResult(false, "该模块下接口为空,请先创建接口!"); + } + VelocityInitializer.initVelocity(); + List contextList = InterTableUtils.prepareMoudleContext(interTableList); + Map 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(false, ReturnConstants.DB_EX); + } + } + IOUtils.closeQuietly(zip); + return new ServerResult(true, outputStream.toByteArray()); + } catch (RuntimeException e) { + logger.error(e.getMessage()); + return new ServerResult(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"; }