代码拉取完成,页面将自动刷新
package antd_apis
import (
"errors"
"fmt"
"gitee.com/sansaniot/sansan-core/sdk/service"
vd "github.com/bytedance/go-tagexpr/v2/validator"
"github.com/gin-gonic/gin/binding"
"net/http"
"strconv"
"gitee.com/sansaniot/sansan-core/logger"
"gitee.com/sansaniot/sansan-core/sdk/api"
"gitee.com/sansaniot/sansan-core/sdk/pkg"
"gitee.com/sansaniot/sansan-core/sdk/pkg/response/antd"
"github.com/gin-gonic/gin"
"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)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。