fix: harden auth flows and align api contracts
This commit is contained in:
@@ -27,14 +27,14 @@ func NewExportHandler(exportService *service.ExportService) *ExportHandler {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param format query string false "导出格式" default(csv) Enums(csv, excel)
|
||||
// @Param format query string false "导出格式" default(csv) Enums(csv, xlsx)
|
||||
// @Param fields query string false "导出字段,逗号分隔"
|
||||
// @Param keyword query string false "关键词过滤"
|
||||
// @Param status query int false "用户状态过滤"
|
||||
// @Success 200 {file} file "用户数据文件"
|
||||
// @Failure 401 {object} Response "未认证"
|
||||
// @Failure 500 {object} Response "服务器错误"
|
||||
// @Router /api/v1/exports/users [get]
|
||||
// @Router /api/v1/admin/users/export [get]
|
||||
func (h *ExportHandler) ExportUsers(c *gin.Context) {
|
||||
format := c.DefaultQuery("format", "csv")
|
||||
fieldsStr := c.Query("fields")
|
||||
@@ -49,9 +49,11 @@ func (h *ExportHandler) ExportUsers(c *gin.Context) {
|
||||
var status *int
|
||||
if statusStr != "" {
|
||||
s, err := strconvAtoi(statusStr)
|
||||
if err == nil {
|
||||
status = &s
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"})
|
||||
return
|
||||
}
|
||||
status = &s
|
||||
}
|
||||
|
||||
req := &service.ExportUsersRequest{
|
||||
@@ -81,12 +83,12 @@ func (h *ExportHandler) ExportUsers(c *gin.Context) {
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param file formData file true "导入文件"
|
||||
// @Param format query string false "文件格式" default(csv) Enums(csv, excel)
|
||||
// @Param format query string false "文件格式" default(csv) Enums(csv, xlsx)
|
||||
// @Success 200 {object} Response "导入结果"
|
||||
// @Failure 400 {object} Response "请求参数错误"
|
||||
// @Failure 401 {object} Response "未认证"
|
||||
// @Failure 500 {object} Response "服务器错误"
|
||||
// @Router /api/v1/exports/users [post]
|
||||
// @Router /api/v1/admin/users/import [post]
|
||||
func (h *ExportHandler) ImportUsers(c *gin.Context) {
|
||||
file, _, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
@@ -120,11 +122,11 @@ func (h *ExportHandler) ImportUsers(c *gin.Context) {
|
||||
// @Tags 数据导入导出
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param format query string false "模板格式" default(csv) Enums(csv, excel)
|
||||
// @Param format query string false "模板格式" default(csv) Enums(csv, xlsx)
|
||||
// @Success 200 {file} file "导入模板文件"
|
||||
// @Failure 401 {object} Response "未认证"
|
||||
// @Failure 500 {object} Response "服务器错误"
|
||||
// @Router /api/v1/exports/template [get]
|
||||
// @Router /api/v1/admin/users/import/template [get]
|
||||
func (h *ExportHandler) GetImportTemplate(c *gin.Context) {
|
||||
format := c.DefaultQuery("format", "csv")
|
||||
data, filename, contentType, err := h.exportService.GetImportTemplateByFormat(format)
|
||||
@@ -139,10 +141,13 @@ func (h *ExportHandler) GetImportTemplate(c *gin.Context) {
|
||||
}
|
||||
|
||||
func strconvAtoi(s string) (int, error) {
|
||||
if s == "" {
|
||||
return 0, http.ErrNoLocation
|
||||
}
|
||||
var n int
|
||||
for _, c := range s {
|
||||
if c < '0' || c > '9' {
|
||||
return 0, nil
|
||||
return 0, http.ErrNotSupported
|
||||
}
|
||||
n = n*10 + int(c-'0')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user