-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_handler.go
67 lines (61 loc) · 1.83 KB
/
import_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package imp
import (
"github.com/sirupsen/logrus"
"github.com/xuri/excelize/v2"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zjzjzjzj1874/best-pracrice-go-zero/constants/errors"
"github.com/zjzjzjzj1874/best-pracrice-go-zero/task/internal/logic/imp"
"github.com/zjzjzjzj1874/best-pracrice-go-zero/task/internal/svc"
"github.com/zjzjzjzj1874/best-pracrice-go-zero/task/internal/types"
"net/http"
)
func ImportHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ImportReq
if err := httpx.ParsePath(r, &req); err != nil { // 解析path中的参数
httpx.Error(w, err)
return
}
if err := httpx.ParseForm(r, &req); err != nil { // 解析form表单参数
httpx.Error(w, err)
return
}
logrus.Infof("req.id =================== %s", req.Id)
if r.MultipartForm == nil || r.MultipartForm.File["file"] == nil || len(r.MultipartForm.File["file"]) == 0 {
httpx.Error(w, errors.NewStatusError(errors.StatusBadRequestError).WithMsg("请传入文本校对自定义文本模板"))
return
}
fileHeaders := r.MultipartForm.File["file"]
file, err := fileHeaders[0].Open()
if err != nil {
httpx.Error(w, err)
return
}
defer func() { _ = file.Close() }()
excel, err := excelize.OpenReader(file)
if err != nil {
httpx.Error(w, err)
return
}
defer func() { _ = excel.Close() }()
// 获取Sheet1上所有单元格 => 下面-1是减去表头
rows, err := excel.GetRows("Sheet1")
if err != nil {
httpx.Error(w, err)
return
}
for idx, row := range rows {
if idx == 0 { // 表头忽略不计
logrus.Warnf("表头忽略不计:%v", row)
continue
}
}
l := imp.NewImportLogic(r.Context(), svcCtx)
resp, err := l.Import(&req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.OkJson(w, resp)
}
}
}