refactor: 新增base.Repo与base.App,重构repo与app层代码

This commit is contained in:
meilin.huang
2023-10-26 17:15:49 +08:00
parent 10f6b03fb5
commit a1303b52eb
115 changed files with 1867 additions and 1696 deletions

View File

@@ -2,7 +2,6 @@ package req
import (
"errors"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/config"
"time"
@@ -10,7 +9,7 @@ import (
)
// 创建用户token
func CreateToken(userId uint64, username string) string {
func CreateToken(userId uint64, username string) (string, error) {
// 带权限创建令牌
// 设置有效期过期需要重新登录获取token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
@@ -21,8 +20,10 @@ func CreateToken(userId uint64, username string) string {
// 使用自定义字符串加密 and get the complete encoded token as a string
tokenString, err := token.SignedString([]byte(config.Conf.Jwt.Key))
biz.ErrIsNilAppendErr(err, "token创建失败: %s")
return tokenString
if err != nil {
return "", err
}
return tokenString, nil
}
// 解析token并返回登录者账号信息
@@ -38,6 +39,9 @@ func ParseToken(tokenStr string) (uint64, string, error) {
if err != nil || token == nil {
return 0, "", err
}
if !token.Valid {
return 0, "", errors.New("token invalid")
}
i := token.Claims.(jwt.MapClaims)
return uint64(i["id"].(float64)), i["username"].(string), nil
}