æ·feat: commplete init process
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/validation"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"message-nest/pkg/e"
|
||||
"message-nest/pkg/util"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// BindAndValid binds and validates data
|
||||
func BindAndValid(c *gin.Context, form interface{}) (int, int) {
|
||||
err := c.Bind(form)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, e.INVALID_PARAMS
|
||||
}
|
||||
|
||||
valid := validation.Validation{}
|
||||
check, err := valid.Valid(form)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, e.ERROR
|
||||
}
|
||||
if !check {
|
||||
MarkErrors(valid.Errors)
|
||||
return http.StatusBadRequest, e.INVALID_PARAMS
|
||||
}
|
||||
|
||||
return http.StatusOK, e.SUCCESS
|
||||
}
|
||||
|
||||
func CommonPlaygroundValid(obj interface{}) (int, string) {
|
||||
if err := util.CustomerValidate.Struct(obj); err != nil {
|
||||
errs := err.(validator.ValidationErrors)
|
||||
errMsg := BuildValidationErrors(errs)
|
||||
return http.StatusBadRequest, errMsg
|
||||
}
|
||||
return http.StatusOK, ""
|
||||
}
|
||||
|
||||
func BindJsonAndPlayValid(c *gin.Context, req interface{}) (int, string) {
|
||||
err := c.ShouldBindJSON(req)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, err.Error()
|
||||
} else {
|
||||
return CommonPlaygroundValid(req)
|
||||
}
|
||||
}
|
||||
|
||||
func BuildValidationErrors(errors []validator.FieldError) string {
|
||||
var errorMsgBuilder strings.Builder
|
||||
for i, err := range errors {
|
||||
if i > 0 {
|
||||
errorMsgBuilder.WriteString("; ")
|
||||
}
|
||||
message := err.Translate(util.Trans)
|
||||
errorMsgBuilder.WriteString(fmt.Sprintf("%s", message))
|
||||
}
|
||||
return errorMsgBuilder.String()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/astaxie/beego/validation"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"message-nest/pkg/logging"
|
||||
)
|
||||
|
||||
// MarkErrors logs error logs
|
||||
func MarkErrors(errors []*validation.Error) {
|
||||
for _, err := range errors {
|
||||
logging.Logger.Error(err.Key, err.Message)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 从请求中获取当前用户
|
||||
func GetCurrentUserName(c *gin.Context) string {
|
||||
userName, ok := c.Get("currentUserName")
|
||||
if !ok {
|
||||
return ""
|
||||
} else {
|
||||
return fmt.Sprintf("%s", userName)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
||||
"message-nest/pkg/e"
|
||||
)
|
||||
|
||||
type Gin struct {
|
||||
C *gin.Context
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// Response setting gin.JSON
|
||||
func (g *Gin) Response(httpCode, errCode int, data interface{}) {
|
||||
g.C.JSON(httpCode, Response{
|
||||
Code: errCode,
|
||||
Msg: e.GetMsg(errCode),
|
||||
Data: data,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (g *Gin) CResponse(errCode int, Msg string, data interface{}) {
|
||||
g.C.JSON(http.StatusOK, Response{
|
||||
Code: errCode,
|
||||
Msg: Msg,
|
||||
Data: data,
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user