2025-01-27 19:46:34 +08:00
|
|
|
// Package router ...
|
|
|
|
//
|
|
|
|
// Description : router ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2025-01-27 19:42
|
|
|
|
package router
|
|
|
|
|
2025-02-07 16:10:33 +08:00
|
|
|
import (
|
|
|
|
"git.zhangdeman.cn/zhangdeman/exception"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/gin/request"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/gin/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"reflect"
|
|
|
|
)
|
2025-01-27 19:46:34 +08:00
|
|
|
|
2025-02-07 17:19:25 +08:00
|
|
|
// RequestHandler 获取请求处理方法
|
2025-01-27 19:46:34 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 19:44 2025/1/27
|
|
|
|
func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
2025-02-07 16:10:33 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ok bool
|
|
|
|
e exception.IException
|
|
|
|
)
|
2025-01-27 19:46:34 +08:00
|
|
|
|
2025-02-17 16:31:21 +08:00
|
|
|
var formParam reflect.Value
|
|
|
|
if uriCfg.FormDataType.Kind() == reflect.Ptr {
|
|
|
|
formParam = reflect.New(uriCfg.FormDataType.Elem())
|
|
|
|
} else {
|
|
|
|
// 表单解析
|
|
|
|
formParam = reflect.New(uriCfg.FormDataType)
|
|
|
|
}
|
|
|
|
formValue := formParam.Interface()
|
|
|
|
if err = request.Form.Parse(ctx, &formParam); nil != err {
|
2025-02-07 18:01:05 +08:00
|
|
|
// 格式化验证错误的信息
|
2025-02-17 16:31:21 +08:00
|
|
|
err = GetValidateErr(formValue, err)
|
2025-02-07 16:57:15 +08:00
|
|
|
e = exception.NewFromError(400, err)
|
|
|
|
response.SendWithException(ctx, e, nil)
|
2025-02-07 16:10:33 +08:00
|
|
|
return
|
|
|
|
}
|
2025-02-17 16:31:21 +08:00
|
|
|
|
2025-02-07 16:10:33 +08:00
|
|
|
// 执行逻辑
|
2025-02-17 16:31:21 +08:00
|
|
|
if uriCfg.FormDataType.Kind() != reflect.Ptr {
|
|
|
|
formParam = formParam.Elem()
|
|
|
|
}
|
|
|
|
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, reflect.ValueOf(ctx), formParam})
|
2025-02-07 16:10:33 +08:00
|
|
|
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
|
2025-01-27 19:46:34 +08:00
|
|
|
}
|
|
|
|
}
|