-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from go-admin-team/dev
Dev
- Loading branch information
Showing
8 changed files
with
287 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package antd_apis | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
vd "github.com/bytedance/go-tagexpr/v2/validator" | ||
"github.com/gin-gonic/gin/binding" | ||
"github.com/go-admin-team/go-admin-core/sdk/service" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-admin-team/go-admin-core/logger" | ||
"github.com/go-admin-team/go-admin-core/sdk/api" | ||
"github.com/go-admin-team/go-admin-core/sdk/pkg" | ||
"github.com/go-admin-team/go-admin-core/sdk/pkg/response/antd" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Api struct { | ||
Context *gin.Context | ||
Logger *logger.Helper | ||
Orm *gorm.DB | ||
Errors error | ||
} | ||
|
||
// GetLogger 获取上下文提供的日志 | ||
func (e Api) GetLogger() *logger.Helper { | ||
return api.GetRequestLogger(e.Context) | ||
} | ||
// GetOrm 获取Orm DB | ||
func (e *Api) GetOrm(c *gin.Context) (*gorm.DB, error) { | ||
db, err := pkg.GetOrm(c) | ||
if err != nil { | ||
e.Error( http.StatusInternalServerError, "数据库连接获取失败", "9") | ||
return nil, err | ||
} | ||
return db, nil | ||
} | ||
|
||
// Error 通常错误数据处理 | ||
// showType error display type: 0 silent; 1 message.warn; 2 message.error; 4 notification; 9 page | ||
func (e *Api) Error( errCode int, errMsg string, showType string) { | ||
if showType == "" { | ||
showType = "2" | ||
} | ||
antd.Error(e.Context, strconv.Itoa(errCode), errMsg, showType) | ||
} | ||
|
||
// OK 通常成功数据处理 | ||
func (e *Api) OK( data interface{}) { | ||
antd.OK(e.Context, data) | ||
} | ||
|
||
// PageOK 分页数据处理 | ||
func (e *Api) PageOK( result interface{}, total int, current int, pageSize int) { | ||
antd.PageOK(e.Context, result, total, current, pageSize) | ||
} | ||
|
||
// Custom 兼容函数 | ||
func (e *Api) Custom( data gin.H) { | ||
antd.Custum(e.Context, data) | ||
} | ||
|
||
|
||
// MakeContext 设置http上下文 | ||
func (e *Api) MakeContext(c *gin.Context) *Api { | ||
e.Context = c | ||
e.Logger = api.GetRequestLogger(c) | ||
return e | ||
} | ||
|
||
|
||
// Bind 参数校验 | ||
func (e *Api) Bind(d interface{}, bindings ...binding.Binding) *Api { | ||
var err error | ||
if len(bindings) == 0 { | ||
bindings = constructor.GetBindingForGin(d) | ||
} | ||
for i := range bindings { | ||
if bindings[i] == nil { | ||
err = e.Context.ShouldBindUri(d) | ||
} else { | ||
err = e.Context.ShouldBindWith(d, bindings[i]) | ||
} | ||
if err != nil && err.Error() == "EOF" { | ||
e.Logger.Warn("request body is not present anymore. ") | ||
err = nil | ||
continue | ||
} | ||
if err != nil { | ||
e.AddError(err) | ||
break | ||
} | ||
} | ||
if err1 := vd.Validate(d); err1 != nil { | ||
e.AddError(err1) | ||
} | ||
return e | ||
} | ||
|
||
// MakeOrm 设置Orm DB | ||
func (e *Api) MakeOrm() *Api { | ||
var err error | ||
if e.Logger == nil { | ||
err = errors.New("at MakeOrm logger is nil") | ||
e.AddError(err) | ||
return e | ||
} | ||
db, err := pkg.GetOrm(e.Context) | ||
if err != nil { | ||
e.Logger.Error(http.StatusInternalServerError, err, "数据库连接获取失败") | ||
e.AddError(err) | ||
} | ||
e.Orm = db | ||
return e | ||
} | ||
|
||
func (e *Api) MakeService(c *service.Service) *Api { | ||
c.Log = e.Logger | ||
c.Orm = e.Orm | ||
return e | ||
} | ||
|
||
func (e *Api) AddError(err error) { | ||
if e.Errors == nil { | ||
e.Errors = err | ||
} else if err != nil { | ||
e.Logger.Error(err) | ||
e.Errors = fmt.Errorf("%v; %w", e.Error, err) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package antd_apis | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gin-gonic/gin/binding" | ||
"reflect" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
const ( | ||
_ uint8 = iota | ||
json | ||
xml | ||
yaml | ||
form | ||
query | ||
) | ||
|
||
var constructor = &bindConstructor{} | ||
|
||
type bindConstructor struct { | ||
cache map[string][]uint8 | ||
mux sync.Mutex | ||
} | ||
|
||
func (e *bindConstructor) GetBindingForGin(d interface{}) []binding.Binding { | ||
bs := e.getBinding(reflect.TypeOf(d).String()) | ||
if bs == nil { | ||
//重新构建 | ||
bs = e.resolve(d) | ||
} | ||
gbs := make([]binding.Binding, 0) | ||
mp := make(map[uint8]binding.Binding, 0) | ||
for _, b := range bs { | ||
switch b { | ||
case json: | ||
mp[json] = binding.JSON | ||
case xml: | ||
mp[xml] = binding.XML | ||
case yaml: | ||
mp[yaml] = binding.YAML | ||
case form: | ||
mp[form] = binding.Form | ||
case query: | ||
mp[query] = binding.Query | ||
default: | ||
mp[0] = nil | ||
} | ||
} | ||
for e := range mp { | ||
gbs=append(gbs, mp[e]) | ||
} | ||
return gbs | ||
} | ||
|
||
func (e *bindConstructor) resolve(d interface{}) []uint8 { | ||
bs := make([]uint8, 0) | ||
qType := reflect.TypeOf(d).Elem() | ||
var tag reflect.StructTag | ||
var ok bool | ||
fmt.Println(qType.Kind()) | ||
for i := 0; i < qType.NumField(); i++ { | ||
tag = qType.Field(i).Tag | ||
if _, ok = tag.Lookup("json"); ok { | ||
bs = append(bs, json) | ||
} | ||
if _, ok = tag.Lookup("xml"); ok { | ||
bs = append(bs, xml) | ||
} | ||
if _, ok = tag.Lookup("yaml"); ok { | ||
bs = append(bs, yaml) | ||
} | ||
if _, ok = tag.Lookup("form"); ok { | ||
bs = append(bs, form) | ||
} | ||
if _, ok = tag.Lookup("query"); ok { | ||
bs = append(bs, query) | ||
} | ||
if _, ok = tag.Lookup("uri"); ok { | ||
bs = append(bs, 0) | ||
} | ||
if t, ok := tag.Lookup("binding"); ok && strings.Index(t, "dive") > -1 { | ||
qValue := reflect.ValueOf(d) | ||
bs = append(bs, e.resolve(qValue.Field(i))...) | ||
continue | ||
} | ||
if t, ok := tag.Lookup("validate"); ok && strings.Index(t, "dive") > -1 { | ||
qValue := reflect.ValueOf(d) | ||
bs = append(bs, e.resolve(qValue.Field(i))...) | ||
} | ||
} | ||
return bs | ||
} | ||
|
||
func (e *bindConstructor) getBinding(name string) []uint8 { | ||
e.mux.Lock() | ||
defer e.mux.Unlock() | ||
return e.cache[name] | ||
} | ||
|
||
func (e *bindConstructor) setBinding(name string, bs []uint8) { | ||
e.mux.Lock() | ||
defer e.mux.Unlock() | ||
if e.cache == nil { | ||
e.cache = make(map[string][]uint8) | ||
} | ||
e.cache[name] = bs | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pkg | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func Translate(form, to interface{}) { | ||
fType := reflect.TypeOf(form) | ||
fValue := reflect.ValueOf(form) | ||
if fType.Kind() == reflect.Ptr { | ||
fType = fType.Elem() | ||
fValue = fValue.Elem() | ||
} | ||
tType := reflect.TypeOf(to) | ||
tValue := reflect.ValueOf(to) | ||
if tType.Kind() == reflect.Ptr { | ||
tType = tType.Elem() | ||
tValue = tValue.Elem() | ||
} | ||
for i := 0; i < fType.NumField(); i++ { | ||
for j := 0; j < tType.NumField(); j++ { | ||
if fType.Field(i).Name == tType.Field(j).Name && | ||
fType.Field(i).Type.ConvertibleTo(tType.Field(j).Type) { | ||
tValue.Field(j).Set(fValue.Field(i)) | ||
} | ||
} | ||
} | ||
} |