This commit is contained in:
laodaming
2023-06-12 16:47:48 +08:00
parent ff05e127bd
commit b1393950f2
32 changed files with 729 additions and 196 deletions

View File

@@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
@@ -14,18 +16,43 @@ import (
func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.GetProductListReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: err.Error(),
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetProductListLogic(r.Context(), svcCtx)
resp := l.GetProductList(&req)
resp := l.GetProductList(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@@ -2,19 +2,46 @@ package handler
import (
"errors"
"fusenapi/server/product/internal/types"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
)
func GetSizeByProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
l := logic.NewGetSizeByProductLogic(r.Context(), svcCtx)
resp := l.GetSizeByProduct()
resp := l.GetSizeByProduct(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
@@ -14,18 +16,43 @@ import (
func GetSuccessRecommandHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.GetSuccessRecommandReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: err.Error(),
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetSuccessRecommandLogic(r.Context(), svcCtx)
resp := l.GetSuccessRecommand(&req)
resp := l.GetSuccessRecommand(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@@ -4,9 +4,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"fusenapi/utils/auth"
@@ -34,12 +33,8 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
}
// 获取产品列表
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp *types.Response) {
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo *auth.UserInfo) (resp *types.Response) {
resp = &types.Response{}
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get login user info err")
}
//如果是demo
if req.IsDemo == 1 {
var demo types.GetProductListRsp
@@ -57,18 +52,18 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
req.Size = image.GetCurrentSize(req.Size)
}
//查询用户信息
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err")
}
if userInfo == nil {
if userInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "user not exists")
}
//查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, []string{fmt.Sprintf("%d", req.Cid)}, "sort-desc")
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
@@ -78,11 +73,11 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
//提取产品ids
productIds := make([]string, 0, productLen)
productIds := make([]int64, 0, productLen)
for _, v := range productList {
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
productIds = append(productIds, v.Id)
}
productPriceModel := model.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceList, err := productPriceModel.GetPriceListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
@@ -104,28 +99,28 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
//获取模板
productTemplateModel := model.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds)
productTemplateModel := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
productTemplatesV2, err := productTemplateModel.FindAllByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err")
}
mapProductTemplate := make(map[int64]struct{})
for _, v := range productTemplatesV2 {
mapProductTemplate[v.ProductId] = struct{}{}
mapProductTemplate[*v.ProductId] = struct{}{}
}
//获取分类
tagsModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get tag err")
}
if tagInfo == nil {
if tagInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "tag is not exists")
}
//获取产品尺寸数量
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1)
if err != nil {
logx.Error(err)
@@ -142,11 +137,11 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
}
item := types.Items{
Id: v.Id,
Sn: v.Sn,
Title: v.Title,
Intro: v.Intro.String,
IsEnv: v.IsProtection,
IsMicro: v.IsMicrowave,
Sn: *v.Sn,
Title: *v.Title,
Intro: *v.Intro,
IsEnv: *v.IsProtection,
IsMicro: *v.IsMicrowave,
SizeNum: uint32(productSizeCount),
MiniPrice: format.CentoDollar(minPrice),
}
@@ -154,11 +149,11 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: int(userInfo.IsThousandFace),
Cover: v.Cover,
CoverImg: v.CoverImg,
CoverDefault: v.CoverImg,
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.CoverImg,
ProductId: v.Id,
UserInfo: *userInfo,
UserInfo: userInfo,
}
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
item.Cover = thousandFaceImageFormatReq.Cover
@@ -169,6 +164,6 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductListRsp{
Ob: types.Ob{
Items: itemList,
}, TypeName: tagInfo.Title, Description: tagInfo.Description,
}, TypeName: *tagInfo.Title, Description: *tagInfo.Description,
})
}

View File

@@ -5,7 +5,8 @@ import (
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"strings"
@@ -31,9 +32,9 @@ func NewGetSizeByProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
// 获取分类下的产品以及尺寸
func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
func (l *GetSizeByProductLogic) GetSizeByProduct(loginInfo *auth.UserInfo) (resp *types.Response) {
//获取所有网站目录
tagsModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsList, err := tagsModel.GetAllByLevel(l.ctx, constants.TYPE_WEBSITE)
if err != nil {
logx.Error(err)
@@ -42,41 +43,41 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
if len(tagsList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "tag list is null")
}
tagIds := make([]string, 0, len(tagsList))
tagIds := make([]int64, 0, len(tagsList))
for _, v := range tagsList {
tagIds = append(tagIds, fmt.Sprintf("%d", v.Id))
tagIds = append(tagIds, v.Id)
}
//获取这些类型的产品
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, tagIds, "sort-desc")
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByIds(l.ctx, tagIds, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag product list")
}
productIds := make([]string, 0, len(productList))
productIds := make([]int64, 0, len(productList))
for _, v := range productList {
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
productIds = append(productIds, v.Id)
}
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
sizeIds := make([]string, 0, len(productSizeList))
sizeIds := make([]int64, 0, len(productSizeList))
for _, v := range productSizeList {
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.Id))
sizeIds = append(sizeIds, v.Id)
}
//获取价格列表
productPriceModel := model.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceList, err := productPriceModel.GetPriceListBySizeIds(l.ctx, sizeIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product proce list")
}
mapProductPrice := make(map[int64]model.FsProductPrice)
mapProductPrice := make(map[int64]gmodel.FsProductPrice)
for _, v := range productPriceList {
mapProductPrice[v.SizeId] = v
mapProductPrice[*v.SizeId] = v
}
//组装返回
list := make([]types.GetSizeByProductRsp, 0, len(tagsList))
@@ -89,7 +90,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
}
data := types.GetSizeByProductRsp{
Id: tag.Id,
Name: tag.Title,
Name: *tag.Title,
Children: firstChildrenList,
}
list = append(list, data)
@@ -98,21 +99,21 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
}
// 第一层子层
func (l *GetSizeByProductLogic) GetFirstChildrenList(tag model.FsTags, productList []model.FsProduct, productSizeList []model.FsProductSize, mapProductPrice map[int64]model.FsProductPrice) (childrenList []types.Children, err error) {
func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productList []gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenList []types.Children, err error) {
childrenList = make([]types.Children, 0, len(productList))
for _, product := range productList {
if product.Type != tag.Id {
if *product.Type != tag.Id {
continue
}
childrenObjList, err := l.GetSecondChildrenList(tag, product, productSizeList, mapProductPrice)
childrenObjList, err := l.GetSecondChildrenList(product, productSizeList, mapProductPrice)
if err != nil {
return nil, err
}
//获取第二层子类
data := types.Children{
Id: product.Id,
Name: product.Title,
Cycle: int(product.DeliveryDays + product.ProduceDays),
Name: *product.Title,
Cycle: int(*product.DeliveryDays + *product.ProduceDays),
ChildrenList: childrenObjList,
}
childrenList = append(childrenList, data)
@@ -121,10 +122,10 @@ func (l *GetSizeByProductLogic) GetFirstChildrenList(tag model.FsTags, productLi
}
// 第2层子层
func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product model.FsProduct, productSizeList []model.FsProductSize, mapProductPrice map[int64]model.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) {
func (l *GetSizeByProductLogic) GetSecondChildrenList(product gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) {
childrenObjList = make([]types.ChildrenObj, 0, len(productSizeList))
for _, productSize := range productSizeList {
if product.Id != productSize.ProductId {
if product.Id != *productSize.ProductId {
continue
}
priceList := make([]types.PriceObj, 0, len(productSizeList))
@@ -133,7 +134,7 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product
if !ok {
childrenObjList = append(childrenObjList, types.ChildrenObj{
Id: productSize.Id,
Name: productSize.Capacity,
Name: *productSize.Capacity,
PriceList: []types.PriceObj{
{Num: 1, Price: 0},
{Num: 1, Price: 0},
@@ -142,18 +143,19 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product
})
continue
}
price.StepNum = strings.Trim(price.StepNum, " ")
price.StepPrice = strings.Trim(price.StepPrice, " ")
if price.StepNum == "" || price.StepPrice == "" {
if price.StepNum == nil || price.StepPrice == nil {
continue
}
*price.StepNum = strings.Trim(*price.StepNum, " ")
*price.StepPrice = strings.Trim(*price.StepPrice, " ")
//阶梯数量切片
stepNum, err := format.StrSlicToIntSlice(strings.Split(price.StepNum, ","))
stepNum, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ","))
if err != nil {
return nil, err
}
//阶梯价格切片
stepPrice, err := format.StrSlicToIntSlice(strings.Split(price.StepPrice, ","))
stepPrice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ","))
if err != nil {
return nil, err
}
@@ -162,17 +164,17 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product
}
index := 0
// 最小购买数量小于 最大阶梯数量+5
for int(price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 {
for int(*price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 {
priceList = append(priceList, types.PriceObj{
Num: int(price.MinBuyNum * price.EachBoxNum),
Price: l.GetPrice(int(price.MinBuyNum), stepNum, stepPrice),
Num: int(*price.MinBuyNum * *price.EachBoxNum),
Price: l.GetPrice(int(*price.MinBuyNum), stepNum, stepPrice),
})
price.MinBuyNum++
*price.MinBuyNum++
index++
}
data := types.ChildrenObj{
Id: productSize.Id,
Name: productSize.Capacity,
Name: *productSize.Capacity,
PriceList: priceList,
}
childrenObjList = append(childrenObjList, data)

View File

@@ -2,15 +2,13 @@ package logic
import (
"context"
"errors"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/image"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type GetSuccessRecommandLogic struct {
@@ -28,20 +26,16 @@ func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext
}
// 获取推荐的产品列表
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq) (resp *types.Response) {
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo *auth.UserInfo) (resp *types.Response) {
resp = &types.Response{}
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "get login user info err")
}
//获取用户信息
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && errors.Is(err, sqlx.ErrNotFound) {
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get user info")
}
if userInfo == nil {
if userInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
}
if req.Num == 0 || req.Num > 500 {
@@ -51,7 +45,7 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
req.Size = image.GetCurrentSize(req.Size)
}
//随机取8个产品
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
if err != nil {
logx.Error(err)
@@ -64,8 +58,8 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
for _, v := range productList {
data := types.GetSuccessRecommandRsp{
Title: v.Title,
Sn: v.Sn,
Title: *v.Title,
Sn: *v.Sn,
Id: v.Id,
SkuId: 0, //???????
}
@@ -73,11 +67,11 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: int(userInfo.IsThousandFace),
Cover: v.Cover,
CoverImg: v.CoverImg,
CoverDefault: v.CoverImg,
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.CoverImg,
ProductId: v.Id,
UserInfo: *userInfo,
UserInfo: userInfo,
}
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
data.Cover = thousandFaceImageFormatReq.Cover

View File

@@ -1,9 +1,13 @@
package svc
import (
"errors"
"fmt"
"fusenapi/initalize"
"fusenapi/server/product/internal/config"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
"net/http"
)
type ServiceContext struct {
@@ -13,8 +17,35 @@ type ServiceContext struct {
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: initalize.InitMysql(c.SourceMysql),
MysqlConn: initalize.InitMysql(c.DataSource),
}
}
func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization")
if len(AuthKey) <= 50 {
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
}
token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) {
// 检查签名方法是否为 HS256
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
// 返回用于验证签名的密钥
return svcCxt.Config.Auth.AccessSecret, nil
})
if err != nil {
return nil, errors.New(fmt.Sprint("Error parsing token:", err))
}
// 验证成功返回
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return claims, nil
}
return nil, errors.New(fmt.Sprint("Invalid token", err))
}