初始化项目

This commit is contained in:
lianghuanjie
2024-12-05 20:51:35 +08:00
commit e2ba6924b8
30 changed files with 1560 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
package jwt
import (
"errors"
"fmt"
jwt "github.com/golang-jwt/jwt/v4"
"github.com/spf13/cast"
"github.com/zeromicro/go-zero/rest/token"
"net/http"
"time"
)
type Config struct {
AccessSecret string `json:",default=ac2d27613e131be6286c0eb17139293d"`
AccessExpire time.Duration `json:",default=24h"`
}
type TokenBuilder struct {
config Config
}
func NewTokenBuilder(config Config) *TokenBuilder {
return &TokenBuilder{config: config}
}
// GenerateToken 生成token
func (b *TokenBuilder) GenerateToken(kvs map[string]any) (string, int64, error) {
// 创建一个新的 Token
tok := jwt.New(jwt.SigningMethodHS256)
// 设置 Token 的声明Payload
claims := tok.Claims.(jwt.MapClaims)
for k, v := range kvs {
claims[k] = v
}
expiredAt := time.Now().Add(b.config.AccessExpire).Unix()
claims["exp"] = expiredAt // 设置过期时间
// 使用密钥签名 Token
tkStr, err := tok.SignedString([]byte(b.config.AccessSecret))
return tkStr, expiredAt, err
}
func (b *TokenBuilder) ParseUidFromToken(tokenStr string) (string, string, string, int64, error) {
r := &http.Request{Header: http.Header{}}
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", tokenStr))
parser := token.NewTokenParser()
tok, err := parser.ParseToken(r, b.config.AccessSecret, "")
if err != nil {
return "", "", "", 0, err
}
if !tok.Valid {
return "", "", "", 0, errors.New("token is invalid")
}
var appid, userId, tgId string
var expiredAt int64
if claims, ok := tok.Claims.(jwt.MapClaims); ok {
if uid, ok := claims["uid"]; ok {
userId = cast.ToString(uid)
}
if aid, ok := claims["app_id"]; ok {
appid = cast.ToString(aid)
}
if tid, ok := claims["tg_id"]; ok {
tgId = cast.ToString(tid)
}
if exp, ok := claims["exp"]; ok {
expiredAt = cast.ToInt64(exp)
}
}
return appid, userId, tgId, expiredAt, nil
}
//// ParseUid 解析出uid
//func (b *TokenBuilder) ParseUid(r *http.Request) (string, error) {
// parser := token.NewTokenParser()
// tok, err := parser.ParseToken(r, b.config.AccessSecret, "")
// if err != nil {
// return "", err
// }
// if !tok.Valid {
// return "", errors.New("token is invalid")
// }
// if claims, ok := tok.Claims.(jwt.MapClaims); ok {
// if uid, ok := claims["uid"]; ok {
// return cast.ToString(uid), nil
// }
// }
// return "", errors.New("token not exist uid")
//}
//func ParseUid(r *http.Request, accessSecret string) (string, error) {
// parser := token.NewTokenParser()
// tok, err := parser.ParseToken(r, accessSecret, "")
// if err != nil {
// return "", err
// }
// if !tok.Valid {
// return "", errors.New("token is invalid")
// }
// if claims, ok := tok.Claims.(jwt.MapClaims); ok {
// if uid, ok := claims["uid"]; ok {
// return cast.ToString(uid), nil
// }
// }
// return "", errors.New("token not exist uid")
//}

View File

@@ -0,0 +1,31 @@
package md5
import (
"crypto/md5"
"encoding/hex"
"fmt"
)
func Md5(src []byte) string {
has := md5.Sum(src)
md5Str := hex.EncodeToString(has[:])
return md5Str
}
func Md5str(src string) string {
return Md5([]byte(src))
}
func Md516(src string) string {
data := []byte(src)
has := md5.Sum(data)
md5Str := hex.EncodeToString(has[:])
return md5Str[8:24]
}
func Md516Upper(src string) string {
data := []byte(src)
has := md5.Sum(data)
md5Str := fmt.Sprintf("%X", has)
return md5Str[8:24]
}

View File

@@ -0,0 +1,30 @@
package sha
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
)
func Sha1(src string) string {
h := sha1.New()
h.Write([]byte(src))
sh := hex.EncodeToString(h.Sum(nil))
return sh
}
func Sha256(src string) string {
m := sha256.New()
m.Write([]byte(src))
res := hex.EncodeToString(m.Sum(nil))
return res
}
func HmacSha256Base64(src, secret string) string {
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(src))
sign := base64.StdEncoding.EncodeToString(h.Sum(nil))
return sign
}

View File

@@ -0,0 +1,40 @@
package errs
import (
"fmt"
"github.com/spf13/cast"
)
type err struct {
code int
reason Reason
msg string
}
func New(code int, reason Reason, message any) error {
return err{
code: code,
reason: reason,
msg: cast.ToString(message),
}
}
// Error error
func (e err) Error() string {
return fmt.Sprintf("code=%d msg=%s", e.code, e.msg)
}
// Code return code
func (e err) Code() int {
return e.code
}
// Reason return reason
func (e err) Reason() Reason {
return e.reason
}
// Message return message
func (e err) Message() string {
return e.msg
}

View File

@@ -0,0 +1,56 @@
package errs
import (
"net/http"
)
func Success() error {
return New(http.StatusOK, ErrSucceed, "success")
}
func NotFound(reason Reason, v any) error {
return New(http.StatusNotFound, reason, v)
}
func BadRequest(reason Reason, v any) error {
return New(http.StatusBadRequest, reason, v)
}
func InternalServer(reason Reason, v any) error {
return New(http.StatusInternalServerError, reason, v)
}
// Unauthorized new Unauthorized error that is mapped to a 401 response.
func Unauthorized(reason Reason, v any) error {
return New(http.StatusUnauthorized, reason, v)
}
// Forbidden new Forbidden error that is mapped to a 403 response.
func Forbidden(reason Reason, v any) error {
return New(http.StatusForbidden, reason, v)
}
// Conflict new Conflict error that is mapped to a 409 response.
func Conflict(reason Reason, v any) error {
return New(http.StatusConflict, reason, v)
}
// ServiceUnavailable new ServiceUnavailable error that is mapped to an HTTP 503 response.
func ServiceUnavailable(reason Reason, v any) error {
return New(http.StatusServiceUnavailable, reason, v)
}
// GatewayTimeout new GatewayTimeout error that is mapped to an HTTP 504 response.
func GatewayTimeout(reason Reason, v any) error {
return New(http.StatusGatewayTimeout, reason, v)
}
// BadGateway new BadGateway error that is mapped to an HTTP 504 response.
func BadGateway(reason Reason, v any) error {
return New(http.StatusBadGateway, reason, v)
}
// ClientClosed new ClientClosed error that is mapped to an HTTP 499 response.
func ClientClosed(reason Reason, v any) error {
return New(499, reason, v)
}

View File

@@ -0,0 +1,64 @@
package errs
import (
"context"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
"os"
"strings"
)
var debug bool
func init() {
if strings.ToLower(os.Getenv("VANS_API_DEBUG")) == "on" {
debug = true
}
httpx.SetErrorHandlerCtx(ErrorHandleCtx)
httpx.SetErrorHandler(ErrorHandle)
}
func SetDebug(d bool) {
debug = d
}
func ErrorHandle(err error) (int, any) {
return ErrorHandleCtx(context.Background(), err)
}
func ErrorHandleCtx(ctx context.Context, err error) (int, any) {
code := http.StatusBadRequest
reason := ErrInternalServer
var msg string
if ec, ok := err.(interface{ Code() int }); ok {
code = ec.Code()
}
if ec, ok := err.(interface{ Message() string }); ok {
msg = ec.Message()
} else {
msg = err.Error()
}
if ec, ok := err.(interface{ Reason() Reason }); ok {
reason = ec.Reason()
}
var errMsg string
if reason < ErrUnknownLogicError && reason != ErrSucceed {
errMsg = msg
msg = "system error"
}
body := map[string]any{
"code": reason,
"message": msg,
}
if errMsg != "" && debug {
body["err"] = errMsg
}
if ec, ok := err.(interface{ Metadata() any }); ok {
if md := ec.Metadata(); md != nil {
body["metadata"] = md
}
}
return code, body
}

View File

@@ -0,0 +1,33 @@
package errs
type Reason int
const (
// ======= 系统错误0~999 =======
ErrUnknownReason Reason = 0 // 未知错误
ErrSucceed Reason = 200 // 成功
ErrOverload Reason = 403 // 请求超载
// ======= 服务器内部错误1000~9999 =======
ErrInternalServer Reason = 1000 // 未知的服务器内部错误
ErrDatabaseOperate Reason = 1001 // 数据库错误
ErrRedisOperate Reason = 1002 // redis错误
ErrEncodePassword Reason = 1003 // 密码加密错误
ErrGenerateUUid Reason = 1004 // 生成uuid错误
ErrGenerateToken Reason = 1005 // 生成token错误
ErrGetExchangeRate Reason = 1005 // 获取汇率错误
// ======= 业务层错误10000~99999 =======
ErrUnknownLogicError Reason = 10000 // 未知的业务错误
ErrInvalidParam Reason = 10001 // 无效参数错误
ErrInvalidSignature Reason = 10002 // 无效签名错误
ErrInvalidToken Reason = 10003 // 无效的token
ErrInvoiceHasPaid Reason = 10004 // 订单已支付
ErrInvalidAppId Reason = 10005 // 应用id无效
ErrUserNotHasInviter Reason = 10006 // 用户没有邀请人
// ========= admin 业务相关错误码: 30000~39999 =========
ErrUnknownAdminError Reason = 30000 // 未知的admin错误
ErrInvalidPassword Reason = 30001 // 无效密码
ErrInvalidAccount Reason = 30002 // 无效账号
)