2025-11-27 13:09:54 +08:00
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-10 13:10:39 +08:00
|
|
|
|
"net/http"
|
2025-11-27 13:09:54 +08:00
|
|
|
|
"ycc-server/app/main/model"
|
|
|
|
|
|
"ycc-server/common/ctxdata"
|
|
|
|
|
|
"ycc-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
|
|
|
|
|
|
}
|
2025-12-10 13:10:39 +08:00
|
|
|
|
// 检查用户是否绑定了mobile(没有mobile表示是临时用户,不允许访问需要认证的接口)
|
|
|
|
|
|
// 注:临时用户现在基于 mobile 字段判断,而不是 UserType
|
2025-11-27 13:09:54 +08:00
|
|
|
|
if claims.UserType == model.UserTypeTemp {
|
2025-12-10 13:10:39 +08:00
|
|
|
|
httpx.Error(w, errors.Wrapf(xerr.NewErrCode(xerr.USER_NEED_BIND_MOBILE), "请先绑定手机号: %v", err))
|
2025-11-27 13:09:54 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
next(w, r)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|