三端用户手机号联通,增加临时用户
This commit is contained in:
@@ -2,11 +2,8 @@ package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"qnc-server/app/main/api/internal/config"
|
||||
"qnc-server/common/ctxdata"
|
||||
jwtx "qnc-server/common/jwt"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
@@ -41,17 +38,14 @@ func (m *AuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFu
|
||||
}
|
||||
|
||||
// 解析JWT令牌
|
||||
userId, err := jwtx.ParseJwtToken(authHeader, m.Config.JwtAuth.AccessSecret)
|
||||
claims, err := jwtx.ParseJwtToken(authHeader, m.Config.JwtAuth.AccessSecret)
|
||||
if err != nil {
|
||||
// JWT解析失败,返回401错误
|
||||
httpx.Error(w, errors.Wrapf(xerr.NewErrCode(ErrCodeUnauthorized), "token解析失败: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户ID转换为json.Number类型后添加到请求上下文
|
||||
userIdStr := fmt.Sprintf("%d", userId)
|
||||
userIdJsonNum := json.Number(userIdStr)
|
||||
ctx := context.WithValue(r.Context(), ctxdata.CtxKeyJwtUserId, userIdJsonNum)
|
||||
ctx := context.WithValue(r.Context(), jwtx.ExtraKey, claims)
|
||||
|
||||
// 使用新的上下文继续处理请求
|
||||
next(w, r.WithContext(ctx))
|
||||
|
||||
@@ -6,28 +6,16 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
BrandKey = "X-Brand"
|
||||
PlatformKey = "X-Platform"
|
||||
)
|
||||
|
||||
type SourceInterceptorMiddleware struct {
|
||||
}
|
||||
|
||||
func NewSourceInterceptorMiddleware() *SourceInterceptorMiddleware {
|
||||
return &SourceInterceptorMiddleware{}
|
||||
}
|
||||
|
||||
func (m *SourceInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
func GlobalSourceInterceptor(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// 获取请求头 X-Brand 和 X-Platform 的值
|
||||
brand := r.Header.Get(BrandKey)
|
||||
// 获取请求头 X-Platform 的值
|
||||
platform := r.Header.Get(PlatformKey)
|
||||
|
||||
// 将值放入新的 context 中
|
||||
ctx := r.Context()
|
||||
if brand != "" {
|
||||
ctx = context.WithValue(ctx, "brand", brand)
|
||||
}
|
||||
if platform != "" {
|
||||
ctx = context.WithValue(ctx, "platform", platform)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
type UserAuthInterceptorMiddleware struct {
|
||||
}
|
||||
|
||||
func NewUserAuthInterceptorMiddleware() *UserAuthInterceptorMiddleware {
|
||||
return &UserAuthInterceptorMiddleware{}
|
||||
}
|
||||
|
||||
func (m *UserAuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
claims, err := ctxdata.GetClaimsFromCtx(r.Context())
|
||||
if err != nil {
|
||||
httpx.Error(w, errors.Wrapf(xerr.NewErrCode(ErrCodeUnauthorized), "token解析失败: %v", err))
|
||||
return
|
||||
}
|
||||
fmt.Println(claims)
|
||||
if claims.UserType == model.UserTypeTemp {
|
||||
httpx.Error(w, errors.Wrapf(xerr.NewErrCode(xerr.USER_NEED_BIND_MOBILE), "token解析失败: %v", err))
|
||||
return
|
||||
}
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user