2022-08-16 15:30:23 +00:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
2022-12-22 07:26:25 +00:00
|
|
|
"context"
|
2022-11-03 09:06:48 +00:00
|
|
|
"fmt"
|
2022-08-16 15:30:23 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2022-12-24 05:31:30 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-12-05 03:12:07 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
2022-08-16 15:30:23 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-12-05 03:12:07 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-08-16 15:30:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GeneratePaginationFromReq(c *gin.Context) (*dto.PageInfo, bool) {
|
|
|
|
p, ok1 := c.GetQuery("page")
|
|
|
|
ps, ok2 := c.GetQuery("pageSize")
|
|
|
|
if !(ok1 && ok2) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err := strconv.Atoi(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
pageSize, err := strconv.Atoi(ps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dto.PageInfo{Page: page, PageSize: pageSize}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorWithDetail(ctx *gin.Context, code int, msgKey string, err error) {
|
|
|
|
res := dto.Response{
|
2022-12-05 03:12:07 +00:00
|
|
|
Code: code,
|
|
|
|
Message: "",
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|
|
|
|
if msgKey == constant.ErrTypeInternalServer {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, constant.ErrRecordExist):
|
2022-12-21 04:21:27 +00:00
|
|
|
res.Message = i18n.GetMsgWithMap("ErrRecordExist", nil)
|
2022-08-16 15:30:23 +00:00
|
|
|
case errors.Is(constant.ErrRecordNotFound, err):
|
2022-12-21 04:21:27 +00:00
|
|
|
res.Message = i18n.GetMsgWithMap("ErrRecordNotFound", nil)
|
2022-12-24 05:31:30 +00:00
|
|
|
case errors.Is(constant.ErrInvalidParams, err):
|
|
|
|
res.Message = i18n.GetMsgWithMap("ErrInvalidParams", nil)
|
2022-08-16 15:30:23 +00:00
|
|
|
case errors.Is(constant.ErrStructTransform, err):
|
2022-12-05 03:12:07 +00:00
|
|
|
res.Message = i18n.GetMsgWithMap("ErrStructTransform", map[string]interface{}{"detail": err})
|
2022-08-16 15:30:23 +00:00
|
|
|
case errors.Is(constant.ErrCaptchaCode, err):
|
2022-12-05 03:12:07 +00:00
|
|
|
res.Code = constant.CodeAuth
|
|
|
|
res.Message = "ErrCaptchaCode"
|
2022-12-02 10:52:43 +00:00
|
|
|
case errors.Is(constant.ErrAuth, err):
|
2022-12-05 03:12:07 +00:00
|
|
|
res.Code = constant.CodeAuth
|
|
|
|
res.Message = "ErrAuth"
|
2022-12-02 10:52:43 +00:00
|
|
|
case errors.Is(constant.ErrInitialPassword, err):
|
2022-12-05 03:12:07 +00:00
|
|
|
res.Message = i18n.GetMsgWithMap("ErrInitialPassword", map[string]interface{}{"detail": err})
|
2022-12-04 04:47:19 +00:00
|
|
|
case errors.As(err, &buserr.BusinessError{}):
|
2022-12-05 03:12:07 +00:00
|
|
|
res.Message = err.Error()
|
2022-08-16 15:30:23 +00:00
|
|
|
default:
|
2022-12-05 03:12:07 +00:00
|
|
|
res.Message = i18n.GetMsgWithMap(msgKey, map[string]interface{}{"detail": err})
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-05 03:12:07 +00:00
|
|
|
res.Message = i18n.GetMsgWithMap(msgKey, map[string]interface{}{"detail": err})
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, res)
|
|
|
|
ctx.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SuccessWithData(ctx *gin.Context, data interface{}) {
|
|
|
|
if data == nil {
|
|
|
|
data = gin.H{}
|
|
|
|
}
|
|
|
|
res := dto.Response{
|
|
|
|
Code: constant.CodeSuccess,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, res)
|
|
|
|
ctx.Abort()
|
|
|
|
}
|
|
|
|
|
2022-09-03 10:41:52 +00:00
|
|
|
func SuccessWithMsg(ctx *gin.Context, msg string) {
|
|
|
|
res := dto.Response{
|
2022-12-05 03:12:07 +00:00
|
|
|
Code: constant.CodeSuccess,
|
|
|
|
Message: msg,
|
2022-09-03 10:41:52 +00:00
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, res)
|
|
|
|
ctx.Abort()
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:30:23 +00:00
|
|
|
func GetParamID(c *gin.Context) (uint, error) {
|
|
|
|
idParam, ok := c.Params.Get("id")
|
|
|
|
if !ok {
|
2022-09-23 09:21:27 +00:00
|
|
|
return 0, errors.New("error id in path")
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|
|
|
|
intNum, _ := strconv.Atoi(idParam)
|
|
|
|
return uint(intNum), nil
|
|
|
|
}
|
2022-11-03 09:06:48 +00:00
|
|
|
|
|
|
|
func GetIntParamByKey(c *gin.Context, key string) (uint, error) {
|
|
|
|
idParam, ok := c.Params.Get(key)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("error %s in path", key)
|
|
|
|
}
|
|
|
|
intNum, _ := strconv.Atoi(idParam)
|
|
|
|
return uint(intNum), nil
|
|
|
|
}
|
2022-12-22 07:26:25 +00:00
|
|
|
|
2023-02-08 09:09:21 +00:00
|
|
|
func GetStrParamByKey(c *gin.Context, key string) (string, error) {
|
|
|
|
idParam, ok := c.Params.Get(key)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("error %s in path", key)
|
|
|
|
}
|
|
|
|
return idParam, nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 07:26:25 +00:00
|
|
|
func GetTxAndContext() (tx *gorm.DB, ctx context.Context) {
|
|
|
|
tx = global.DB.Begin()
|
|
|
|
ctx = context.WithValue(context.Background(), constant.DB, tx)
|
|
|
|
return
|
|
|
|
}
|