add: some user api and all api desc

This commit is contained in:
wwweww
2026-02-27 19:17:01 +08:00
parent a0c720eb2f
commit 5930fb0dde
156 changed files with 9457 additions and 1086 deletions
+96
View File
@@ -0,0 +1,96 @@
package contextx
import (
"context"
"errors"
"github.com/zeromicro/go-zero/core/logx"
)
var (
ERRILLEGALUSER = errors.New("illegal user")
ERRILLEGALTOKEN = errors.New("illegal token")
ERRILLEGALREQUESTID = errors.New("illegal request id")
ERRILLEGALISADMIN = errors.New("illegal is_admin")
)
func WithRequestId(c context.Context, requestId string) context.Context {
return context.WithValue(c, "request_id", requestId)
}
func RequestIdFrom(c context.Context) (string, error) {
requestID, ok := c.Value("request_id").(string)
if !ok {
return "", errors.New("request_id not found in context")
}
return requestID, nil
}
func WithToken(c context.Context, token string) context.Context {
return context.WithValue(c, "token", token)
}
func TokenFrom(c context.Context) (string, error) {
token, ok := c.Value("token").(string)
if !ok {
return "", errors.New("token not found in context")
}
return token, nil
}
func WithUserID(c context.Context, id int64) context.Context {
return context.WithValue(c, "user_id", id)
}
func UserIDFrom(c context.Context) (int64, error) {
if userID, ok := c.Value("user_id").(int64); !ok {
return 0, errors.New("user_id not found in context")
} else {
return userID, nil
}
}
// request_id is used for tracing and logging, not for authentication or authorization,
// so it can be set by clients or generated by the server.
func WithRequestID(c context.Context, requestID string) context.Context {
return context.WithValue(c, "request_id", requestID)
}
func RequestIDFrom(c context.Context) (string, error) {
if requestID, ok := c.Value("request_id").(string); !ok {
return "", errors.New("request_id not found in context")
} else {
return requestID, nil
}
}
func WithIsAdmin(c context.Context, isAdmin bool) context.Context {
return context.WithValue(c, "is_admin", isAdmin)
}
func IsAdminFrom(c context.Context) (bool, error) {
if isAdmin, ok := c.Value("is_admin").(bool); !ok {
return false, errors.New("is_admin not found in context")
} else {
return isAdmin, nil
}
}
func AdminIdFrom(c context.Context) (adminId int64, err error) {
adminId, err = UserIDFrom(c)
if err != nil {
logx.Errorf("get user id from context: %v", err)
return 0, ERRILLEGALUSER
}
isAdmin, err := IsAdminFrom(c)
if err != nil {
logx.Errorf("get isAdmin from context: %v", err)
return 0, ERRILLEGALUSER
}
if !isAdmin {
logx.Errorf("user %d is not admin", adminId)
return 0, ERRILLEGALUSER
}
return
}
+30
View File
@@ -0,0 +1,30 @@
package httpx
import (
"net/http"
"strconv"
)
func GetUserIdFromHeader(header http.Header) (int64, error) {
id := header.Get("x-auth-user-id")
if id == "" {
return 0, http.ErrNoCookie
}
intId, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return 0, err
}
return intId, nil
}
func GetUserIsAdminFromHeader(header http.Header) (bool, error) {
isAdmin := header.Get("x-auth-is-admin")
if isAdmin == "" {
return false, nil
}
boolIsAdmin, err := strconv.ParseBool(isAdmin)
if err != nil {
return false, err
}
return boolIsAdmin, nil
}
@@ -1,25 +1,25 @@
package utils
import (
"golang.org/x/crypto/bcrypt"
)
const (
// bcrypt 密钥成本
bcryptCost = bcrypt.DefaultCost
)
// HashPassword 对密码进行哈希加密
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
if err != nil {
return "", err
}
return string(hash), nil
}
// VerifyPassword 验证密码是否正确
func VerifyPassword(hashedPassword, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
return err == nil
}
package pwdUtils
import (
"golang.org/x/crypto/bcrypt"
)
const (
// bcrypt 密钥成本
bcryptCost = bcrypt.DefaultCost
)
// HashPassword 对密码进行哈希加密
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
if err != nil {
return "", err
}
return string(hash), nil
}
// VerifyPassword 验证密码是否正确
func VerifyPassword(hashedPassword, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
return err == nil
}
@@ -1,4 +1,4 @@
package utils
package responses
import "encoding/json"