refactor🎨 api数据初始化调整
This commit is contained in:
1 parent
b2345039e4
commit
6b1c3d9226
10 files changed
+171
-201
No files matched your search
+11
-4
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"go-admin/common/service"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -21,7 +22,7 @@ type Api struct {
|
||||
Errors error
|
||||
}
|
||||
|
||||
func (e *Api) AddError(err error) {
|
||||
func (e *Api) AddError(err error) {
|
||||
if e.Errors == nil {
|
||||
e.Errors = err
|
||||
} else if err != nil {
|
||||
@@ -44,7 +45,7 @@ func (e Api) GetLogger() *logger.Logger {
|
||||
return api.GetRequestLogger(e.Context)
|
||||
}
|
||||
|
||||
func (e Api) Bind(d interface{}, bindings ...binding.Binding) error {
|
||||
func (e *Api) Bind(d interface{}, bindings ...binding.Binding) *Api {
|
||||
var err error
|
||||
if len(bindings) == 0 {
|
||||
bindings = append(bindings, binding.JSON, nil)
|
||||
@@ -75,10 +76,10 @@ func (e Api) Bind(d interface{}, bindings ...binding.Binding) error {
|
||||
err = e.Context.ShouldBindUri(d)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
e.AddError(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return e
|
||||
}
|
||||
|
||||
// GetOrm 获取Orm DB
|
||||
@@ -109,6 +110,12 @@ func (e *Api) MakeOrm() *Api {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Api) MakeService(c *service.Service) *Api {
|
||||
c.Log = e.Logger
|
||||
c.Orm = e.Orm
|
||||
return e
|
||||
}
|
||||
|
||||
// Error 通常错误数据处理
|
||||
func (e Api) Error(code int, err error, msg string) {
|
||||
response.Error(e.Context, code, err, msg)
|
||||
|
||||
Reference in new issue
Block a user