diff --git a/product/internal/handler/getproductlisthandler.go b/product/internal/handler/getproductlisthandler.go index 22a3d962..b42aaf5a 100644 --- a/product/internal/handler/getproductlisthandler.go +++ b/product/internal/handler/getproductlisthandler.go @@ -13,18 +13,14 @@ import ( func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { //检测登录权限 - userInfo, err := auth.CheckAuth(r) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - return - } + userInfo := auth.CheckAuth(r) var req types.GetProductListReq if err := httpx.Parse(r, &req); err != nil { httpx.ErrorCtx(r.Context(), w, err) return } l := logic.NewGetProductListLogic(r.Context(), svcCtx) - resp, err := l.GetProductList(&req, userInfo.UserId) + resp, err := l.GetProductList(&req, userInfo) if err != nil { httpx.ErrorCtx(r.Context(), w, err) } else { diff --git a/product/internal/logic/getproductlistlogic.go b/product/internal/logic/getproductlistlogic.go index 599a678f..89bf2c7c 100644 --- a/product/internal/logic/getproductlistlogic.go +++ b/product/internal/logic/getproductlistlogic.go @@ -6,6 +6,7 @@ import ( "fusenapi/model" "fusenapi/product/internal/svc" "fusenapi/product/internal/types" + "fusenapi/utils/auth" "fusenapi/utils/image" "github.com/zeromicro/go-zero/core/logx" @@ -26,14 +27,21 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge } // 获取产品列表 -func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, uid int64) (resp *types.Response, err error) { +func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo auth.UserInfo) (resp *types.Response, err error) { + //校验前台登录情况 + if loginInfo.UserId == 0 { + return &types.Response{ + Code: 401, + Message: "please sign in", + }, nil + } //获取合适尺寸 if req.Size > 0 { req.Size = image.GetCurrentSize(req.Size) } //获取是否存在千人千面 userModel := model.NewFsUserModel(l.svcCtx.MysqlConn) - userInfo, err := userModel.FindOne(l.ctx, uid) + userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId) if err != nil { return nil, err } diff --git a/utils/auth/auth.go b/utils/auth/auth.go index df1e2487..6f62c4eb 100644 --- a/utils/auth/auth.go +++ b/utils/auth/auth.go @@ -2,14 +2,15 @@ package auth import ( "encoding/json" - "errors" "github.com/golang-jwt/jwt" + "log" "net/http" "time" ) type UserInfo struct { - UserId int64 `json:"user_id"` + UserId int64 `json:"user_id"` //网站前台登录uid + BackendUserId int64 `json:"backend_user_id"` //管理后台uid } // 签名key @@ -19,9 +20,10 @@ var expireTime = int64(3600) // 生成token func GenJwtToken(userInfo UserInfo) (token string, err error) { t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ - "user_id": userInfo.UserId, - "exp": time.Now().Add(time.Second * time.Duration(expireTime)).Unix(), //过期时间 - "iss": "fusen", + "user_id": userInfo.UserId, + "backend_user_id": userInfo.BackendUserId, + "exp": time.Now().Add(time.Second * time.Duration(expireTime)).Unix(), //过期时间 + "iss": "fusen", }) token, err = t.SignedString([]byte(signKey)) if err != nil { @@ -50,15 +52,20 @@ func ParseJwtToken(token string) (UserInfo, error) { } // 检测授权 -func CheckAuth(r *http.Request) (UserInfo, error) { +func CheckAuth(r *http.Request) UserInfo { token := r.Header.Get("Authorization") if token == "" { - return UserInfo{}, errors.New("token is required") + token = r.Header.Get("Auth-Key") + } + if token == "" { + log.Println("token is empty") + return UserInfo{} } //解析token userInfo, err := ParseJwtToken(token) if err != nil { - return UserInfo{}, err + log.Println(err) + return UserInfo{} } - return userInfo, nil + return userInfo }