feat(user): 实现基础用户系统
- 实现用户手机验证码、密码登录功能,支持 JWT 认证 - 实现用户注册功能 - 实现jwt中获取用户信息
This commit is contained in:
@@ -3,20 +3,23 @@ package ctxdata
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// CtxKeyJwtUserId get uid from ctx
|
||||
var CtxKeyJwtUserId = "jwtUserId"
|
||||
const CtxKeyJwtUserId = "userId"
|
||||
|
||||
func GetUidFromCtx(ctx context.Context) int64 {
|
||||
var uid int64
|
||||
if jsonUid, ok := ctx.Value(CtxKeyJwtUserId).(json.Number); ok {
|
||||
if int64Uid, err := jsonUid.Int64(); err == nil {
|
||||
uid = int64Uid
|
||||
} else {
|
||||
logx.WithContext(ctx).Errorf("GetUidFromCtx err : %+v", err)
|
||||
}
|
||||
// GetUidFromCtx 从 context 中获取用户 ID
|
||||
func GetUidFromCtx(ctx context.Context) (int64, error) {
|
||||
// 尝试从上下文中获取 jwtUserId
|
||||
jsonUid, ok := ctx.Value(CtxKeyJwtUserId).(json.Number)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("无法获取用户 ID, CtxKeyJwtUserId = %v", CtxKeyJwtUserId)
|
||||
}
|
||||
return uid
|
||||
// 转换为 int64
|
||||
uid, err := jsonUid.Int64()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("用户 ID 转换失败: %+v", err)
|
||||
}
|
||||
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user