diff --git a/skyeye-common/src/main/java/com/skyeye/common/util/ToolUtil.java b/skyeye-common/src/main/java/com/skyeye/common/util/ToolUtil.java index bd21c892608e9db556355d107b0c3f31ffb1c203..d6f4cfdebf84c5c843ea8e9cfe297086a6c9b679 100644 --- a/skyeye-common/src/main/java/com/skyeye/common/util/ToolUtil.java +++ b/skyeye-common/src/main/java/com/skyeye/common/util/ToolUtil.java @@ -1,6 +1,8 @@ package com.skyeye.common.util; import java.io.File; +import java.io.FileOutputStream; +import java.io.RandomAccessFile; import java.security.MessageDigest; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -487,6 +489,32 @@ public class ToolUtil { else return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString(); } + + /** + * 写入内容到文件 + * @param content + * @param fileName + * @return + * @throws Exception + */ + public static boolean writeTxtFile(String content, File fileName) throws Exception { + RandomAccessFile mm = null; + boolean flag = false; + FileOutputStream o = null; + try { + o = new FileOutputStream(fileName); + o.write(content.getBytes("GBK")); + o.close(); + flag = true; + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (mm != null) { + mm.close(); + } + } + return flag; + } public static void main(String[] args) throws Exception { diff --git a/skyeye-dao/src/main/java/com/skyeye/common/dao/CommonDao.java b/skyeye-dao/src/main/java/com/skyeye/common/dao/CommonDao.java index d4a33dfd3924780bab4f1a9c199fc3f800790c5a..b75d6a84c7ed7d37006e832289cac7597bfbbd3f 100644 --- a/skyeye-dao/src/main/java/com/skyeye/common/dao/CommonDao.java +++ b/skyeye-dao/src/main/java/com/skyeye/common/dao/CommonDao.java @@ -1,5 +1,10 @@ package com.skyeye.common.dao; +import java.util.List; +import java.util.Map; + public interface CommonDao { + public int insertCodeModelHistory(List> inBeans) throws Exception; + } diff --git a/skyeye-service/src/main/java/com/skyeye/common/service/CommonService.java b/skyeye-service/src/main/java/com/skyeye/common/service/CommonService.java index 762e5f98b39453309549a41e71dab33f0972a784..887656fb32c2be65665aebe35b2918f44941ae61 100644 --- a/skyeye-service/src/main/java/com/skyeye/common/service/CommonService.java +++ b/skyeye-service/src/main/java/com/skyeye/common/service/CommonService.java @@ -9,4 +9,6 @@ public interface CommonService { public void uploadFileBase64(InputObject inputObject, OutputObject outputObject) throws Exception; + public void downloadFileByJsonData(InputObject inputObject, OutputObject outputObject) throws Exception; + } diff --git a/skyeye-service/src/main/java/com/skyeye/common/service/impl/CommonServiceImpl.java b/skyeye-service/src/main/java/com/skyeye/common/service/impl/CommonServiceImpl.java index dc595196e4001fca7dfdb897e5f0f6c0bf068915..a9d991bd540d0b843b851ae2a90fbd234b67592b 100644 --- a/skyeye-service/src/main/java/com/skyeye/common/service/impl/CommonServiceImpl.java +++ b/skyeye-service/src/main/java/com/skyeye/common/service/impl/CommonServiceImpl.java @@ -1,17 +1,30 @@ package com.skyeye.common.service.impl; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.URLEncoder; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import javax.servlet.http.HttpServletResponse; + import org.apache.commons.codec.binary.Base64; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; + import com.skyeye.common.constans.Constants; import com.skyeye.common.dao.CommonDao; import com.skyeye.common.object.InputObject; @@ -19,6 +32,9 @@ import com.skyeye.common.object.OutputObject; import com.skyeye.common.service.CommonService; import com.skyeye.common.util.ToolUtil; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + @Service public class CommonServiceImpl implements CommonService{ @@ -179,5 +195,72 @@ public class CommonServiceImpl implements CommonService{ outputObject.setreturnMessage("上传失败,数据不合法"); } } + + /** + * + * @Title: downloadFileByJsonData + * @Description: 代码生成器下载 + * @param @param inputObject + * @param @param outputObject + * @param @throws Exception 参数 + * @return void 返回类型 + * @throws + */ + @SuppressWarnings({ "static-access" }) + @Override + public void downloadFileByJsonData(InputObject inputObject, OutputObject outputObject) throws Exception { + Map map = inputObject.getParams(); + JSONArray array = JSONArray.fromObject(map.get("jsonData").toString()); + String tPath = inputObject.getRequest().getSession().getServletContext().getRealPath("/"); + String basePath = tPath.substring(0, inputObject.getRequest().getSession().getServletContext().getRealPath("/").indexOf(Constants.PROJECT_WEB)); + List> inBeans = new ArrayList<>(); + Map user = inputObject.getLogParams(); + String zipName = ToolUtil.getSurFaceId() + ".zip"; + String strZipPath = basePath + "/" + zipName; + ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath)); + byte[] buffer = new byte[1024]; + + for(int i = 0; i < array.size(); i++){ + JSONObject object = (JSONObject) array.get(i); + //加入压缩包 + ByteArrayInputStream stream = new ByteArrayInputStream(object.getString("content").getBytes()); + out.putNextEntry(new ZipEntry(object.getString("fileName") + "." + object.getString("modelType").toLowerCase())); + int len; + // 读入需要下载的文件的内容,打包到zip文件 + while ((len = stream.read(buffer)) > 0) { + out.write(buffer, 0, len); + } + out.closeEntry(); + Map bean = new HashMap<>(); + bean.put("id", ToolUtil.getSurFaceId()); + bean.put("tableName", object.getString("tableName")); + bean.put("groupId", object.getString("groupId")); + bean.put("modelId", object.getString("modelId")); + bean.put("content", object.getString("content")); + bean.put("createId", user.get("id")); + bean.put("filePath", zipName); + bean.put("createTime", ToolUtil.getTimeAndToString()); + inBeans.add(bean); + } + out.close(); + commonDao.insertCodeModelHistory(inBeans); + +// //下载 +// //获取输入流 +// InputStream bis = new BufferedInputStream(new FileInputStream(new File(strZipPath))); +// inputObject.getResponse().setHeader("REQUESTMATION", "DOWNLOAD"); +// // 转码,免得文件名中文乱码 +// String filename = URLEncoder.encode(zipName, "UTF-8"); +// // 设置文件下载头 +// inputObject.getResponse().addHeader("Content-Disposition", "attachment;filename=" + filename); +// // 1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 +// inputObject.getResponse().setContentType("multipart/form-data"); +// BufferedOutputStream out1 = new BufferedOutputStream(inputObject.getResponse().getOutputStream()); +// int len = 0; +// while ((len = bis.read()) != -1) { +// out1.write(len); +// out1.flush(); +// } + } } diff --git a/skyeye-web/src/main/java/com/skyeye/common/controller/CommonController.java b/skyeye-web/src/main/java/com/skyeye/common/controller/CommonController.java index 98d7076160d8089dca4da58b3cfdf0618f0b6467..c9a60007f8fe7d6dc8d1686dd744ac1e8d731d92 100644 --- a/skyeye-web/src/main/java/com/skyeye/common/controller/CommonController.java +++ b/skyeye-web/src/main/java/com/skyeye/common/controller/CommonController.java @@ -46,4 +46,20 @@ public class CommonController { commonService.uploadFileBase64(inputObject, outputObject); } + /** + * + * @Title: downloadFileByJsonData + * @Description: 代码生成器下载 + * @param @param inputObject + * @param @param outputObject + * @param @throws Exception 参数 + * @return void 返回类型 + * @throws + */ + @RequestMapping("/post/CommonController/downloadFileByJsonData") + @ResponseBody + public void downloadFileByJsonData(InputObject inputObject, OutputObject outputObject) throws Exception{ + commonService.downloadFileByJsonData(inputObject, outputObject); + } + } diff --git a/skyeye-web/src/main/resources/dbmapper/CommonMapper.xml b/skyeye-web/src/main/resources/dbmapper/CommonMapper.xml index ff7fe00f1186745b995efa8e465f9ad133f6d25e..151161c2ef55d27735d196fe8c5b71f568c25f5f 100644 --- a/skyeye-web/src/main/resources/dbmapper/CommonMapper.xml +++ b/skyeye-web/src/main/resources/dbmapper/CommonMapper.xml @@ -2,6 +2,13 @@ - + + insert into code_model_history + (id, table_name, group_id, model_id, content, file_path, create_id, create_time) + values + + (#{item.id}, #{item.tableName}, #{item.groupId}, #{item.modelId}, #{item.content}, #{item.filePath}, #{item.createId}, #{item.createTime}) + + \ No newline at end of file diff --git a/skyeye-web/src/main/resources/mapping/reqmapping.xml b/skyeye-web/src/main/resources/mapping/reqmapping.xml index 064c89a979fb0c1e7b1d0d1e7d559f7a70c660a4..a06f78109c37bdd1af7069f85aac5fdedd81dd76 100644 --- a/skyeye-web/src/main/resources/mapping/reqmapping.xml +++ b/skyeye-web/src/main/resources/mapping/reqmapping.xml @@ -337,6 +337,9 @@ + + + \ No newline at end of file diff --git a/skyeye-web/src/main/webapp/assets/lib/layui/layui.js b/skyeye-web/src/main/webapp/assets/lib/layui/layui.js index 3dfd67fc5b9b85aa2770929816c0b312894a6400..b45a876b9f030df8338ac0d48a91c75851d30a7a 100644 --- a/skyeye-web/src/main/webapp/assets/lib/layui/layui.js +++ b/skyeye-web/src/main/webapp/assets/lib/layui/layui.js @@ -672,6 +672,7 @@ var AjaxPostUtil = { // 状态改变的处理 readystatechange: function(xmlhttp) { var sessionstatus = xmlhttp.getResponseHeader("SESSIONSTATUS"); + var requestmation = xmlhttp.getResponseHeader("REQUESTMATION"); if (sessionstatus == "TIMEOUT") {//超时跳转 var win = window; while (win != win.top){ @@ -688,8 +689,12 @@ var AjaxPostUtil = { break; case "json": var jsonText = xmlhttp.responseText; - if(jsonText) { - returnValue = eval("(" + jsonText + ")"); + if(requestmation == 'DOWNLOAD'){ + returnValue = eval('(' + '{"returnMessage":"成功","returnCode":0,"total":0,"rows":"","bean":""}' + ')'); + }else{ + if(jsonText) { + returnValue = eval("(" + jsonText + ")"); + } } break; default: diff --git a/skyeye-web/src/main/webapp/js/codemodelgroup/usemodelgroup.js b/skyeye-web/src/main/webapp/js/codemodelgroup/usemodelgroup.js index 41ec9f8b3ef4f57c84d23c1548dc6e3ef68c813d..b85c7d8c65fa4bc73f125794f6065920657e01aa 100644 --- a/skyeye-web/src/main/webapp/js/codemodelgroup/usemodelgroup.js +++ b/skyeye-web/src/main/webapp/js/codemodelgroup/usemodelgroup.js @@ -5,7 +5,7 @@ layui.config({ version: skyeyeVersion }).define(['table', 'jquery', 'winui', 'form', 'codemirror', 'xml', 'clike', 'css', 'htmlmixed', 'javascript', 'nginx', 'solr', 'sql', 'vue'], function (exports) { - + var index = parent.layer.getFrameIndex(window.name); //获取窗口索引 winui.renderColor(); var $ = layui.$, @@ -25,13 +25,27 @@ layui.config({ // groupId:模板所属分组id // modelName:模板别名 // modelContent:默认内容 + // fileName:文件名称 + // modelType:模板类型 //} form.render(); form.on('submit(formSearch)', function (data) { //表单验证 if (winui.verifyForm(data.elem)) { - + var subData = list; + for(var i = 0; i < subData.length; i++){ + subData[i].modelContent = ""; + } + AjaxPostUtil.request({url:reqBasePath + "codemodel014", params:{jsonData: JSON.stringify(subData)}, type:'json', callback:function(json){ + if(json.returnCode == 0){ + top.winui.window.msg('保存成功,请前往生成历史下载。', {icon: 1,time: 2000}); + parent.layer.close(index); + parent.refreshCode = '0'; + }else{ + top.winui.window.msg(json.returnMessage, {icon: 2,time: 2000}); + } + }}); } return false; }); @@ -125,7 +139,9 @@ layui.config({ tableName: $("#tableName").val(), groupId: parent.rowId, modelName: row.modelName, - modelContent: row.modelContent + modelContent: row.modelContent, + fileName: $("#tableZhName").val() + row.modelName, + modelType: row.modelType, }; insertListIn(list, s); top.winui.window.msg('转换成功', {icon: 1,time: 2000}); @@ -165,6 +181,7 @@ layui.config({ list[i].content = replaceModelContent(list[i].modelContent, $("#ControllerPackageName").val(), $("#ServicePackageName").val(), $("#ServiceImplPackageName").val(), $("#DaoPackageName").val(), $("#tableZhName").val(), $("#tableFirstISlowerName").val(), $("#tableISlowerName").val(), $("#tableBzName").val()); + list[i].fileName = $("#tableZhName").val() + list[i].modelName; if(list[i].modelId == editId){ textEditor.setOption('readOnly', false); textEditor.setValue(list[i].content); diff --git a/skyeye-web/src/main/webapp/toolUtil.txt b/skyeye-web/src/main/webapp/toolUtil.txt index 912f545ac9bb3798de2e1ca517747cff3c12ddba..6f6cc239c9cca2158b9d236c2c7aaa00a9ae1903 100644 --- a/skyeye-web/src/main/webapp/toolUtil.txt +++ b/skyeye-web/src/main/webapp/toolUtil.txt @@ -23,3 +23,4 @@ deleteFile(); 删除单个文件 card(); 随机不重复的6-8位 replaceUnderLineAndUpperCase(); 将表名转为Java经常使用的名字,如code_model转CodeModel toLowerCaseFirstOne(); 字符串首字母转小写 +writeTxtFile(); 写入内容到文件