From cdd549a039d2fedc82a6f7c383728323cd063b36 Mon Sep 17 00:00:00 2001 From: weizhiqiang <598748873@qq.com> Date: Thu, 1 Nov 2018 16:54:13 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E6=96=87=E4=BB=B6=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/skyeye/common/util/ToolUtil.java | 28 +++++++ .../java/com/skyeye/common/dao/CommonDao.java | 5 ++ .../skyeye/common/service/CommonService.java | 2 + .../service/impl/CommonServiceImpl.java | 83 +++++++++++++++++++ .../common/controller/CommonController.java | 16 ++++ .../main/resources/dbmapper/CommonMapper.xml | 9 +- .../src/main/resources/mapping/reqmapping.xml | 3 + .../src/main/webapp/assets/lib/layui/layui.js | 9 +- .../webapp/js/codemodelgroup/usemodelgroup.js | 23 ++++- skyeye-web/src/main/webapp/toolUtil.txt | 1 + 10 files changed, 173 insertions(+), 6 deletions(-) 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 bd21c8926..d6f4cfdeb 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 d4a33dfd3..b75d6a84c 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 762e5f98b..887656fb3 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 dc595196e..a9d991bd5 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 98d707616..c9a60007f 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 ff7fe00f1..151161c2e 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 064c89a97..a06f78109 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 3dfd67fc5..b45a876b9 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 41ec9f8b3..b85c7d8c6 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 912f545ac..6f6cc239c 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(); 写入内容到文件 -- GitLab