package middleware import ( "errors" "github.com/zeromicro/go-zero/core/logx" xhttp "github.com/zeromicro/x/http" "tianyuan-api/apps/user/user" "net/http" ) type EntAuthInterceptorMiddleware struct { UserRpc user.UserClient } func NewEntAuthInterceptorMiddleware(userRpc user.UserClient) *EntAuthInterceptorMiddleware { return &EntAuthInterceptorMiddleware{ UserRpc: userRpc, } } func (m *EntAuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { userId, ok := r.Context().Value("userId").(int64) if !ok { xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New("无法获取 userId")) } status, err := m.UserRpc.GetEnterpriseAuthStatus(r.Context(), &user.GetEnterpriseAuthStatusReq{UserId: userId}) if err != nil { logx.Error("校验认证状态错误: %v", err) xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New("系统错误")) return } if !status.IsAuth { xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New("请先通过企业认证")) return } next(w, r) } }