- 新增 response.ErrorWithData:校验失败要把逐行明细一起带回,不能只给一句话 - 新增 GET /api/material-import/template 与 POST /api/material-import - 路由挂在独立前缀 /material-import:materials 组已有 GET /:id,gin 的路由树 不允许同一段既有静态前缀又有通配参数 - 权限沿用物料新增的口径:登录 + 公司头校验,不额外要求 super_admin - 模板随代码生成不落盘,下载到的永远与当前校验规则一致
97 lines
2.0 KiB
Go
97 lines
2.0 KiB
Go
package response
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
type PaginatedData struct {
|
|
Items interface{} `json:"items"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
Total int64 `json:"total"`
|
|
HasMore bool `json:"has_more"`
|
|
}
|
|
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(200, Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func PaginatedSuccess(c *gin.Context, items interface{}, limit, offset int, total int64) {
|
|
hasMore := int64(offset+limit) < total
|
|
c.JSON(200, Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: PaginatedData{
|
|
Items: items,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
Total: total,
|
|
HasMore: hasMore,
|
|
},
|
|
})
|
|
}
|
|
|
|
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
|
|
c.JSON(200, Response{
|
|
Code: 0,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func Created(c *gin.Context, message string, data interface{}) {
|
|
c.JSON(201, Response{
|
|
Code: 0,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func Error(c *gin.Context, httpStatus int, message string) {
|
|
c.JSON(httpStatus, Response{
|
|
Code: httpStatus,
|
|
Message: message,
|
|
Data: nil,
|
|
})
|
|
}
|
|
|
|
// ErrorWithData 返回带业务数据的错误响应。批量导入校验失败时需要把逐行错误
|
|
// 一起带回去,不能只给一句话。
|
|
func ErrorWithData(c *gin.Context, httpStatus int, message string, data interface{}) {
|
|
c.JSON(httpStatus, Response{
|
|
Code: httpStatus,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func BadRequest(c *gin.Context, message string) {
|
|
Error(c, 400, message)
|
|
}
|
|
|
|
func NotFound(c *gin.Context, message string) {
|
|
Error(c, 404, message)
|
|
}
|
|
|
|
func Unauthorized(c *gin.Context, message string) {
|
|
Error(c, 401, message)
|
|
}
|
|
|
|
func Forbidden(c *gin.Context, message string) {
|
|
Error(c, 403, message)
|
|
}
|
|
|
|
func InternalError(c *gin.Context, message string) {
|
|
Error(c, 500, message)
|
|
}
|