Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
2ecb82a5
Commit
2ecb82a5
authored
Nov 26, 2019
by
dqjdda
Browse files
新增代码生成打包下载功能
parent
2ad07c8f
Changes
6
Hide whitespace changes
Inline
Side-by-side
eladmin-common/pom.xml
View file @
2ecb82a5
...
...
@@ -9,7 +9,7 @@
</parent>
<modelVersion>
4.0.0
</modelVersion>
<properties>
<hutool.version>
[4.1.12,)
</hutool.version>
<hutool.version>
5.0.6
</hutool.version>
</properties>
<artifactId>
eladmin-common
</artifactId>
...
...
eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java
View file @
2ecb82a5
...
...
@@ -6,9 +6,11 @@ import cn.hutool.core.util.IdUtil;
import
cn.hutool.poi.excel.BigExcelWriter
;
import
cn.hutool.poi.excel.ExcelUtil
;
import
me.zhengjie.exception.BadRequestException
;
import
org.apache.poi.util.IOUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.activation.MimetypesFileTypeMap
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.security.MessageDigest
;
...
...
@@ -273,6 +275,37 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
return
null
;
}
/**
* 下载文件
* @param request /
* @param response /
* @param file /
*/
public
static
void
downloadFile
(
HttpServletRequest
request
,
HttpServletResponse
response
,
File
file
,
boolean
deleteOnExit
){
response
.
setCharacterEncoding
(
request
.
getCharacterEncoding
());
response
.
setContentType
(
"application/octet-stream"
);
FileInputStream
fis
=
null
;
try
{
fis
=
new
FileInputStream
(
file
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
file
.
getName
());
IOUtils
.
copy
(
fis
,
response
.
getOutputStream
());
response
.
flushBuffer
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
fis
!=
null
)
{
try
{
fis
.
close
();
if
(
deleteOnExit
){
file
.
deleteOnExit
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
static
String
getMd5
(
File
file
)
{
return
getMd5
(
getByte
(
file
));
}
...
...
eladmin-generator/src/main/java/me/zhengjie/rest/GeneratorController.java
View file @
2ecb82a5
...
...
@@ -11,6 +11,8 @@ import org.springframework.beans.factory.annotation.Value;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.List
;
/**
...
...
@@ -67,9 +69,9 @@ public class GeneratorController {
@ApiOperation
(
"生成代码"
)
@PostMapping
(
value
=
"/{tableName}/{type}"
)
public
ResponseEntity
generator
(
@PathVariable
String
tableName
,
@PathVariable
Integer
type
){
if
(!
generatorEnabled
){
throw
new
BadRequestException
(
"此环境不允许生成代码!"
);
public
ResponseEntity
generator
(
@PathVariable
String
tableName
,
@PathVariable
Integer
type
,
HttpServletRequest
request
,
HttpServletResponse
response
){
if
(!
generatorEnabled
&&
type
==
0
){
throw
new
BadRequestException
(
"此环境不允许生成代码
,请选择预览或者下载查看
!"
);
}
switch
(
type
){
// 生成代码
...
...
@@ -77,7 +79,10 @@ public class GeneratorController {
break
;
// 预览
case
1
:
return
generatorService
.
preview
(
genConfigService
.
find
(
tableName
),
generatorService
.
getColumns
(
tableName
));
default
:
break
;
// 打包
case
2
:
generatorService
.
download
(
genConfigService
.
find
(
tableName
),
generatorService
.
getColumns
(
tableName
),
request
,
response
);
break
;
default
:
throw
new
BadRequestException
(
"没有这个选项"
);
}
return
new
ResponseEntity
(
HttpStatus
.
OK
);
}
...
...
eladmin-generator/src/main/java/me/zhengjie/service/GeneratorService.java
View file @
2ecb82a5
...
...
@@ -4,7 +4,8 @@ import me.zhengjie.domain.GenConfig;
import
me.zhengjie.domain.ColumnInfo
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.scheduling.annotation.Async
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.List
;
/**
...
...
@@ -61,4 +62,13 @@ public interface GeneratorService {
* @return /
*/
ResponseEntity
preview
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
);
/**
* 打包下载
* @param genConfig 配置信息
* @param columns 字段信息
* @param request
* @param response
*/
void
download
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
,
HttpServletRequest
request
,
HttpServletResponse
response
);
}
eladmin-generator/src/main/java/me/zhengjie/service/impl/GeneratorServiceImpl.java
View file @
2ecb82a5
...
...
@@ -2,12 +2,14 @@ package me.zhengjie.service.impl;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.ZipUtil
;
import
me.zhengjie.domain.GenConfig
;
import
me.zhengjie.domain.ColumnInfo
;
import
me.zhengjie.domain.vo.TableInfo
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.repository.ColumnInfoRepository
;
import
me.zhengjie.service.GeneratorService
;
import
me.zhengjie.utils.FileUtil
;
import
me.zhengjie.utils.GenUtil
;
import
me.zhengjie.utils.PageUtil
;
import
me.zhengjie.utils.StringUtils
;
...
...
@@ -17,6 +19,9 @@ import org.springframework.stereotype.Service;
import
javax.persistence.EntityManager
;
import
javax.persistence.PersistenceContext
;
import
javax.persistence.Query
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -136,4 +141,19 @@ public class GeneratorServiceImpl implements GeneratorService {
List
<
Map
<
String
,
Object
>>
genList
=
GenUtil
.
preview
(
columns
,
genConfig
);
return
new
ResponseEntity
<>(
genList
,
HttpStatus
.
OK
);
}
@Override
public
void
download
(
GenConfig
genConfig
,
List
<
ColumnInfo
>
columns
,
HttpServletRequest
request
,
HttpServletResponse
response
)
{
if
(
genConfig
.
getId
()
==
null
){
throw
new
BadRequestException
(
"请先配置生成器"
);
}
try
{
File
file
=
new
File
(
GenUtil
.
download
(
columns
,
genConfig
));
String
zipPath
=
file
.
getPath
()
+
".zip"
;
ZipUtil
.
zip
(
file
.
getPath
(),
zipPath
);
FileUtil
.
downloadFile
(
request
,
response
,
new
File
(
zipPath
),
true
);
}
catch
(
IOException
e
)
{
throw
new
BadRequestException
(
"打包失败"
);
}
}
}
eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java
View file @
2ecb82a5
...
...
@@ -84,6 +84,44 @@ public class GenUtil {
return
genList
;
}
public
static
String
download
(
List
<
ColumnInfo
>
columns
,
GenConfig
genConfig
)
throws
IOException
{
String
tempPath
=
System
.
getProperty
(
"java.io.tmpdir"
)
+
"eladmin-gen-temp"
+
File
.
separator
+
genConfig
.
getTableName
()
+
File
.
separator
;
Map
<
String
,
Object
>
genMap
=
getGenMap
(
columns
,
genConfig
);
TemplateEngine
engine
=
TemplateUtil
.
createEngine
(
new
TemplateConfig
(
"template"
,
TemplateConfig
.
ResourceMode
.
CLASSPATH
));
// 生成后端代码
List
<
String
>
templates
=
getAdminTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/admin/"
+
templateName
+
".ftl"
);
String
filePath
=
getAdminFilePath
(
templateName
,
genConfig
,
genMap
.
get
(
"className"
).
toString
(),
tempPath
+
"eladmin"
+
File
.
separator
);
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
// 生成前端代码
templates
=
getFrontTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/front/"
+
templateName
+
".ftl"
);
String
path
=
tempPath
+
"eladmin-web"
+
File
.
separator
;
String
apiPath
=
path
+
"src"
+
File
.
separator
+
"api"
+
File
.
separator
;
String
srcPath
=
path
+
"src"
+
File
.
separator
+
"views"
+
File
.
separator
+
genMap
.
get
(
"changeClassName"
).
toString
()
+
File
.
separator
;
String
filePath
=
getFrontFilePath
(
templateName
,
apiPath
,
srcPath
,
genMap
.
get
(
"changeClassName"
).
toString
());
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
// 如果非覆盖生成
if
(!
genConfig
.
getCover
()
&&
FileUtil
.
exist
(
file
)){
continue
;
}
// 生成代码
genFile
(
file
,
template
,
genMap
);
}
return
tempPath
;
}
public
static
void
generatorCode
(
List
<
ColumnInfo
>
columnInfos
,
GenConfig
genConfig
)
throws
IOException
{
Map
<
String
,
Object
>
genMap
=
getGenMap
(
columnInfos
,
genConfig
);
TemplateEngine
engine
=
TemplateUtil
.
createEngine
(
new
TemplateConfig
(
"template"
,
TemplateConfig
.
ResourceMode
.
CLASSPATH
));
...
...
@@ -91,7 +129,7 @@ public class GenUtil {
List
<
String
>
templates
=
getAdminTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/admin/"
+
templateName
+
".ftl"
);
String
filePath
=
getAdminFilePath
(
templateName
,
genConfig
,
genMap
.
get
(
"className"
).
toString
());
String
filePath
=
getAdminFilePath
(
templateName
,
genConfig
,
genMap
.
get
(
"className"
).
toString
()
,
System
.
getProperty
(
"user.dir"
)
);
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
...
...
@@ -108,7 +146,7 @@ public class GenUtil {
templates
=
getFrontTemplateNames
();
for
(
String
templateName
:
templates
)
{
Template
template
=
engine
.
getTemplate
(
"generator/front/"
+
templateName
+
".ftl"
);
String
filePath
=
getFrontFilePath
(
templateName
,
genConfig
,
genMap
.
get
(
"changeClassName"
).
toString
());
String
filePath
=
getFrontFilePath
(
templateName
,
genConfig
.
getApiPath
(),
genConfig
.
getPath
()
,
genMap
.
get
(
"changeClassName"
).
toString
());
assert
filePath
!=
null
;
File
file
=
new
File
(
filePath
);
...
...
@@ -283,8 +321,8 @@ public class GenUtil {
/**
* 定义后端文件路径以及名称
*/
private
static
String
getAdminFilePath
(
String
templateName
,
GenConfig
genConfig
,
String
className
)
{
String
projectPath
=
System
.
getProperty
(
"user.dir"
)
+
File
.
separator
+
genConfig
.
getModuleName
();
private
static
String
getAdminFilePath
(
String
templateName
,
GenConfig
genConfig
,
String
className
,
String
rootPath
)
{
String
projectPath
=
rootPath
+
File
.
separator
+
genConfig
.
getModuleName
();
String
packagePath
=
projectPath
+
File
.
separator
+
"src"
+
File
.
separator
+
"main"
+
File
.
separator
+
"java"
+
File
.
separator
;
if
(!
ObjectUtils
.
isEmpty
(
genConfig
.
getPack
()))
{
packagePath
+=
genConfig
.
getPack
().
replace
(
"."
,
File
.
separator
)
+
File
.
separator
;
...
...
@@ -328,11 +366,10 @@ public class GenUtil {
/**
* 定义前端文件路径以及名称
*/
private
static
String
getFrontFilePath
(
String
templateName
,
GenConfig
genConfig
,
String
apiName
)
{
String
path
=
genConfig
.
getPath
();
private
static
String
getFrontFilePath
(
String
templateName
,
String
apiPath
,
String
path
,
String
apiName
)
{
if
(
"api"
.
equals
(
templateName
))
{
return
genConfig
.
getA
piPath
()
+
File
.
separator
+
apiName
+
".js"
;
return
a
piPath
+
File
.
separator
+
apiName
+
".js"
;
}
if
(
"index"
.
equals
(
templateName
))
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment