feat ✨ 优化api写法
This commit is contained in:
1 parent
5957fb280c
commit
13ff9d9e63
38 files changed
+777
-672
No files matched your search
+14
-13
@@ -12,39 +12,40 @@ import (
|
||||
)
|
||||
|
||||
type Api struct {
|
||||
Context *gin.Context
|
||||
}
|
||||
|
||||
// GetLogger 获取上下文提供的日志
|
||||
func (e *Api) GetLogger(c *gin.Context) *logger.Logger {
|
||||
return api.GetRequestLogger(c)
|
||||
func (e Api) GetLogger() *logger.Logger {
|
||||
return api.GetRequestLogger(e.Context)
|
||||
}
|
||||
|
||||
// GetOrm 获取Orm DB
|
||||
func (e *Api) GetOrm(c *gin.Context) (*gorm.DB, error) {
|
||||
db, err := pkg.GetOrm(c)
|
||||
func (e Api) GetOrm() (*gorm.DB, error) {
|
||||
db, err := pkg.GetOrm(e.Context)
|
||||
if err != nil {
|
||||
e.Error(c, http.StatusInternalServerError, err, "数据库连接获取失败")
|
||||
e.Error(http.StatusInternalServerError, err, "数据库连接获取失败")
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// Error 通常错误数据处理
|
||||
func (e *Api) Error(c *gin.Context, code int, err error, msg string) {
|
||||
response.Error(c, code, err, msg)
|
||||
func (e Api) Error(code int, err error, msg string) {
|
||||
response.Error(e.Context, code, err, msg)
|
||||
}
|
||||
|
||||
// OK 通常成功数据处理
|
||||
func (e *Api) OK(c *gin.Context, data interface{}, msg string) {
|
||||
response.OK(c, data, msg)
|
||||
func (e Api) OK(data interface{}, msg string) {
|
||||
response.OK(e.Context, data, msg)
|
||||
}
|
||||
|
||||
// PageOK 分页数据处理
|
||||
func (e *Api) PageOK(c *gin.Context, result interface{}, count int, pageIndex int, pageSize int, msg string) {
|
||||
response.PageOK(c, result, count, pageIndex, pageSize, msg)
|
||||
func (e Api) PageOK(result interface{}, count int, pageIndex int, pageSize int, msg string) {
|
||||
response.PageOK(e.Context, result, count, pageIndex, pageSize, msg)
|
||||
}
|
||||
|
||||
// Custom 兼容函数
|
||||
func (e *Api) Custom(c *gin.Context, data gin.H) {
|
||||
response.Custum(c, data)
|
||||
func (e Api) Custom(data gin.H) {
|
||||
response.Custum(e.Context, data)
|
||||
}
|
||||
Reference in new issue
Block a user