切换导航条
此项目
正在载入...
登录
allied-creation
/
performance
·
提交
转到一个项目
GitLab
转到群组
项目
活动
文件
提交
管道
0
构建
0
图表
里程碑
问题
0
合并请求
0
成员
标记
维基
派生
网络
创建新的问题
下载为
邮件补丁
差异文件
浏览文件
作者
郑周
2 years ago
提交
1a3aeae0e56845bbe085d23a2b06101aec47d470
1 个父辈
ffc31b58
1. 获取zip文件 ->临时保存到本地 ->解压 ->解析xml ->创建模板
2. 返回key, 前端通过访问key验证数据处理结果
隐藏空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
58 行增加
和
23 行删除
go.mod
pkg/port/beego/controllers/import_controller.go
go.mod
查看文件 @
1a3aeae
...
...
@@ -14,4 +14,5 @@ require (
github.com/go-redsync/redsync/v4 v4.8.1
github.com/linmadan/egglib-go v0.0.0-20210827085852-177fa745932d
github.com/xuri/excelize/v2 v2.6.1
golang.org/x/text v0.3.7
)
...
...
pkg/port/beego/controllers/import_controller.go
查看文件 @
1a3aeae
...
...
@@ -3,6 +3,7 @@ package controllers
import
(
"archive/zip"
"bufio"
"bytes"
"encoding/json"
"fmt"
service
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_template"
...
...
@@ -11,7 +12,10 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/xredis"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path"
...
...
@@ -204,17 +208,21 @@ func (controller *ImportController) ZipVerify() {
controller
.
Response
(
nil
,
application
.
ThrowError
(
application
.
ARG_ERROR
,
err
.
Error
()))
}
else
{
bytes
,
err
:=
xredis
.
GetBytes
(
in
.
VerifyKey
)
if
err
!=
nil
{
controller
.
Response
(
nil
,
application
.
ThrowError
(
application
.
ARG_ERROR
,
err
.
Error
()))
}
else
{
if
err
==
nil
{
out
:=
&
command
.
OutResult
{}
err
:=
json
.
Unmarshal
(
bytes
,
out
)
if
err
!=
nil
{
controller
.
Response
(
nil
,
application
.
ThrowError
(
application
.
ARG_ERROR
,
err
.
Error
()))
}
else
{
err
=
json
.
Unmarshal
(
bytes
,
out
)
if
err
==
nil
{
controller
.
Response
(
out
,
nil
)
return
}
}
// 有异常直接返回完成标记
if
err
!=
nil
{
out
:=
&
command
.
OutResult
{}
out
.
Status
=
"completed"
controller
.
Response
(
out
,
nil
)
}
}
}
...
...
@@ -247,15 +255,14 @@ func (controller *ImportController) ZipImport() {
controller
.
Response
(
map
[
string
]
string
{
"key"
:
key
},
nil
)
go
func
()
{
// 解压目标文件夹
dstDir
:=
constant
.
UPLOAD_ZIP_PATH
+
"/"
+
day
+
"
/
"
+
id
dstDir
:=
constant
.
UPLOAD_ZIP_PATH
+
"/"
+
day
+
"
-
"
+
id
fileNames
,
err
:=
controller
.
Unzip
(
zipPath
,
dstDir
)
if
err
!=
nil
{
_
=
xredis
.
Remove
(
key
)
_
=
os
.
RemoveAll
(
dstDir
)
}
else
{
success
:=
make
([]
command
.
ErrorI
,
0
)
failure
:=
make
([]
command
.
ErrorI
,
0
)
for
i
:=
range
fileNames
{
fn
:=
fileNames
[
i
]
f
,
err
:=
os
.
Open
(
path
.
Join
(
dstDir
,
fn
))
...
...
@@ -266,7 +273,7 @@ func (controller *ImportController) ZipImport() {
})
continue
}
if
err
:=
controller
.
parserAndInsert
(
f
);
err
!=
nil
{
if
err
:=
controller
.
parserAndInsert
(
f
,
fn
);
err
!=
nil
{
failure
=
append
(
failure
,
command
.
ErrorI
{
FileName
:
fn
,
Message
:
err
.
Error
(),
...
...
@@ -279,12 +286,18 @@ func (controller *ImportController) ZipImport() {
Message
:
""
,
})
}
out
=
command
.
OutResult
{
Status
:
"completed"
,
Success
:
success
,
Failure
:
failure
,
}
_
=
xredis
.
Set
(
key
,
out
,
1
*
time
.
Hour
)
// 30分钟后失效
_
=
xredis
.
Set
(
key
,
out
,
30
*
time
.
Minute
)
// 删除临时文件夹
_
=
os
.
RemoveAll
(
dstDir
)
}
}()
...
...
@@ -299,7 +312,7 @@ func (controller *ImportController) writeLocal(file multipart.File) (string, str
}
id2
:=
fmt
.
Sprintf
(
"%v"
,
id
)
day
:=
time
.
Now
()
.
Format
(
"2006-01-02"
)
zipPath
:=
constant
.
UPLOAD_ZIP_PATH
+
"/"
+
day
+
"
/
"
+
id2
+
"/"
+
"temp"
zipPath
:=
constant
.
UPLOAD_ZIP_PATH
+
"/"
+
day
+
"
-
"
+
id2
+
"/"
+
"temp"
var
fd
*
os
.
File
...
...
@@ -347,18 +360,35 @@ func (controller *ImportController) Unzip(zipPath, dstDir string) ([]string, err
}
defer
reader
.
Close
()
for
_
,
file
:=
range
reader
.
File
{
if
err
:=
controller
.
unzipFile
(
file
,
dstDir
);
err
!=
nil
{
var
decodeName
string
for
_
,
f
:=
range
reader
.
File
{
if
f
.
Flags
==
0
{
// 如果标致位是0,则是默认的本地编码(默认为gbk),如果标志为是 1 << 11也就是 2048(则是utf-8编码)
r
:=
bytes
.
NewReader
([]
byte
(
f
.
Name
))
decoder
:=
transform
.
NewReader
(
r
,
simplifiedchinese
.
GB18030
.
NewDecoder
())
content
,
_
:=
ioutil
.
ReadAll
(
decoder
)
decodeName
=
string
(
content
)
}
else
{
decodeName
=
f
.
Name
}
if
err
:=
controller
.
unzipFile
(
f
,
decodeName
,
dstDir
);
err
!=
nil
{
return
fileNames
,
err
}
else
{
fileNames
=
append
(
fileNames
,
file
.
Name
)
fileNames
=
append
(
fileNames
,
decode
Name
)
}
}
return
fileNames
,
nil
}
// 解析文件并插入数据
func
(
controller
*
ImportController
)
parserAndInsert
(
file
*
os
.
File
)
error
{
func
(
controller
*
ImportController
)
parserAndInsert
(
file
*
os
.
File
,
fileName
string
)
error
{
defer
func
()
{
err
:=
file
.
Close
()
if
err
!=
nil
{
return
}
}()
reader
,
err
:=
excelize
.
OpenReader
(
file
)
if
err
!=
nil
{
return
application
.
ThrowError
(
application
.
INTERNAL_SERVER_ERROR
,
"上传错误:"
+
err
.
Error
())
...
...
@@ -379,27 +409,31 @@ func (controller *ImportController) parserAndInsert(file *os.File) error {
return
application
.
ThrowError
(
application
.
ARG_ERROR
,
"没有数据内容"
)
}
var
filenameWithSuffix
=
path
.
Base
(
fileName
)
var
fileSuffix
=
path
.
Ext
(
filenameWithSuffix
)
var
filenameOnly
=
strings
.
TrimSuffix
(
filenameWithSuffix
,
fileSuffix
)
ruService
:=
service
.
NewEvaluationTemplateService
()
in
:=
&
templateCommand
.
CreateTemplateCommand
{}
ua
:=
middlewares
.
GetUser
(
controller
.
Ctx
)
in
.
CompanyId
=
ua
.
CompanyId
in
.
CreatorId
=
ua
.
UserId
in
.
Name
=
file
.
Name
()
in
.
Name
=
file
nameOnly
in
.
Describe
=
""
in
.
NodeContents
=
list
if
_
,
err
:=
ruService
.
Create
(
in
);
err
!=
nil
{
return
application
.
ThrowError
(
application
.
ARG_ERROR
,
"数据创建错误"
)
return
application
.
ThrowError
(
application
.
ARG_ERROR
,
err
.
Error
()
)
}
}
return
nil
}
func
(
controller
*
ImportController
)
unzipFile
(
f
ile
*
zip
.
File
,
dstDir
string
)
error
{
func
(
controller
*
ImportController
)
unzipFile
(
f
*
zip
.
File
,
fName
string
,
dstDir
string
)
error
{
// create the directory of file
filePath
:=
path
.
Join
(
dstDir
,
file
.
Name
)
if
file
.
FileInfo
()
.
IsDir
()
{
filePath
:=
path
.
Join
(
dstDir
,
fName
)
if
f
.
FileInfo
()
.
IsDir
()
{
if
err
:=
os
.
MkdirAll
(
filePath
,
os
.
ModePerm
);
err
!=
nil
{
return
err
}
...
...
@@ -410,7 +444,7 @@ func (controller *ImportController) unzipFile(file *zip.File, dstDir string) err
}
// open the file
rc
,
err
:=
f
ile
.
Open
()
rc
,
err
:=
f
.
Open
()
if
err
!=
nil
{
return
err
}
...
...
请
注册
或
登录
后发表评论