完善 RequestHandler 逻辑
This commit is contained in:
@ -7,7 +7,13 @@
|
||||
// Date : 2025-01-27 19:42
|
||||
package router
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/request"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// RequestHandler 获取请求处理方法 TODO : 待完成
|
||||
//
|
||||
@ -16,6 +22,38 @@ import "github.com/gin-gonic/gin"
|
||||
// Date : 19:44 2025/1/27
|
||||
func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
ok bool
|
||||
e exception.IException
|
||||
)
|
||||
|
||||
formData := reflect.New(uriCfg.FormDataType).Interface()
|
||||
// 表单解析
|
||||
if err = request.Form.Parse(ctx, formData); nil != err {
|
||||
return
|
||||
}
|
||||
// 执行逻辑
|
||||
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(formData)})
|
||||
if resList[1].IsNil() {
|
||||
// 请求成功
|
||||
response.Success(ctx, resList[0].Interface())
|
||||
return
|
||||
}
|
||||
// 请求失败
|
||||
if e, ok = resList[1].Interface().(exception.IException); ok {
|
||||
response.SendWithException(ctx, e, nil)
|
||||
return
|
||||
}
|
||||
if err, ok = resList[1].Interface().(error); ok {
|
||||
e = exception.NewFromError(-1, err)
|
||||
response.SendWithException(ctx, e, nil)
|
||||
return
|
||||
}
|
||||
e = exception.NewWithCodeAndData(-1, map[string]any{
|
||||
"err": resList[1].Interface(),
|
||||
})
|
||||
response.SendWithException(ctx, e, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user