This commit is contained in:
laodaming 2023-06-02 12:12:51 +08:00
parent 4df573bbc7
commit efd0f28d51
3 changed files with 28 additions and 17 deletions

View File

@ -13,18 +13,14 @@ import (
func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
//检测登录权限 //检测登录权限
userInfo, err := auth.CheckAuth(r) userInfo := auth.CheckAuth(r)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
var req types.GetProductListReq var req types.GetProductListReq
if err := httpx.Parse(r, &req); err != nil { if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err) httpx.ErrorCtx(r.Context(), w, err)
return return
} }
l := logic.NewGetProductListLogic(r.Context(), svcCtx) l := logic.NewGetProductListLogic(r.Context(), svcCtx)
resp, err := l.GetProductList(&req, userInfo.UserId) resp, err := l.GetProductList(&req, userInfo)
if err != nil { if err != nil {
httpx.ErrorCtx(r.Context(), w, err) httpx.ErrorCtx(r.Context(), w, err)
} else { } else {

View File

@ -6,6 +6,7 @@ import (
"fusenapi/model" "fusenapi/model"
"fusenapi/product/internal/svc" "fusenapi/product/internal/svc"
"fusenapi/product/internal/types" "fusenapi/product/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/image" "fusenapi/utils/image"
"github.com/zeromicro/go-zero/core/logx" "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 { if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size) req.Size = image.GetCurrentSize(req.Size)
} }
//获取是否存在千人千面 //获取是否存在千人千面
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn) userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
userInfo, err := userModel.FindOne(l.ctx, uid) userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -2,14 +2,15 @@ package auth
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"log"
"net/http" "net/http"
"time" "time"
) )
type UserInfo struct { type UserInfo struct {
UserId int64 `json:"user_id"` UserId int64 `json:"user_id"` //网站前台登录uid
BackendUserId int64 `json:"backend_user_id"` //管理后台uid
} }
// 签名key // 签名key
@ -19,9 +20,10 @@ var expireTime = int64(3600)
// 生成token // 生成token
func GenJwtToken(userInfo UserInfo) (token string, err error) { func GenJwtToken(userInfo UserInfo) (token string, err error) {
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user_id": userInfo.UserId, "user_id": userInfo.UserId,
"exp": time.Now().Add(time.Second * time.Duration(expireTime)).Unix(), //过期时间 "backend_user_id": userInfo.BackendUserId,
"iss": "fusen", "exp": time.Now().Add(time.Second * time.Duration(expireTime)).Unix(), //过期时间
"iss": "fusen",
}) })
token, err = t.SignedString([]byte(signKey)) token, err = t.SignedString([]byte(signKey))
if err != nil { 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") token := r.Header.Get("Authorization")
if token == "" { 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 //解析token
userInfo, err := ParseJwtToken(token) userInfo, err := ParseJwtToken(token)
if err != nil { if err != nil {
return UserInfo{}, err log.Println(err)
return UserInfo{}
} }
return userInfo, nil return userInfo
} }