34 lines
		
	
	
		
			873 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			34 lines
		
	
	
		
			873 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package middleware
 | |
| 
 | |
| import (
 | |
| 	"net/http"
 | |
| 	"hm-server/app/main/model"
 | |
| 	"hm-server/common/ctxdata"
 | |
| 	"hm-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
 | |
| 		}
 | |
| 		if claims.UserType == model.UserTypeTemp {
 | |
| 			httpx.Error(w, errors.Wrapf(xerr.NewErrCode(xerr.USER_NEED_BIND_MOBILE), "token解析失败: %v", err))
 | |
| 			return
 | |
| 		}
 | |
| 		next(w, r)
 | |
| 	}
 | |
| }
 |