63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package web_contentexcel
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"videoweb/pub/common"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/web/service/srv_contentexcel"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// /api/web/contentexcel/export [GET]
|
|
func Export(ctx *gin.Context) {
|
|
req := &srv_contentexcel.ExportReq{}
|
|
if err := ctx.ShouldBindQuery(req); err != nil {
|
|
common.ServerJSON(ctx, srvdef.ErrParamError, err.Error())
|
|
return
|
|
}
|
|
|
|
ids, serr := srv_contentexcel.ParseExportIDs(ctx.QueryArray("ids"))
|
|
if serr != nil {
|
|
common.ServerJSON(ctx, serr.Code, serr.Tips)
|
|
return
|
|
}
|
|
if len(ids) > 0 {
|
|
req.Ids = ids
|
|
}
|
|
|
|
data, filename, serr := srv_contentexcel.Export(ctx, req)
|
|
if serr != nil {
|
|
common.ServerJSON(ctx, serr.Code, serr.Tips)
|
|
return
|
|
}
|
|
|
|
ctx.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
|
ctx.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", data)
|
|
}
|
|
|
|
// /api/web/contentexcel/import [POST]
|
|
func Import(ctx *gin.Context) {
|
|
contentType := ctx.PostForm("type")
|
|
fileHeader, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
common.ServerJSON(ctx, srvdef.ErrParamError, "请上传 file 字段")
|
|
return
|
|
}
|
|
|
|
file, err := fileHeader.Open()
|
|
if err != nil {
|
|
common.ServerJSON(ctx, srvdef.ErrParamError, err.Error())
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
result, serr := srv_contentexcel.Import(ctx, contentType, file)
|
|
if serr != nil {
|
|
common.ServerJSON(ctx, serr.Code, serr.Tips)
|
|
return
|
|
}
|
|
common.ServerJSON(ctx, srvdef.Success, result)
|
|
}
|