This commit is contained in:
laodaming 2023-06-05 18:14:46 +08:00
parent 2ddc87ed59
commit b1dc8c7841
5 changed files with 108 additions and 49 deletions

View File

@ -1,32 +1,39 @@
package handler package handler
import ( import (
"errors"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"net/http" "net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/product/internal/logic" "fusenapi/product/internal/logic"
"fusenapi/product/internal/svc" "fusenapi/product/internal/svc"
"fusenapi/product/internal/types" "fusenapi/product/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
) )
// 获取产品详情
func GetProductInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { func GetProductInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
//检测登录权限 //用户登录信息
userInfo := auth.CheckAuth(r) userInfo := auth.CheckAuth(r)
var req types.GetProductInfoReq var req types.GetProductInfoReq
if err := httpx.Parse(r, &req); err != nil { if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err) httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return return
} }
l := logic.NewGetProductInfoLogic(r.Context(), svcCtx) l := logic.NewGetProductInfoLogic(r.Context(), svcCtx)
resp, err := l.GetProductInfo(&req, userInfo) resp := l.GetProductInfo(&req, userInfo)
if err != nil { if resp != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp) httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
} }
} }
} }

View File

@ -1,31 +1,40 @@
package handler package handler
import ( import (
"errors"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"net/http" "net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/product/internal/logic" "fusenapi/product/internal/logic"
"fusenapi/product/internal/svc" "fusenapi/product/internal/svc"
"fusenapi/product/internal/types" "fusenapi/product/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
) )
// 获取产品列表
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 := auth.CheckAuth(r) userInfo := auth.CheckAuth(r)
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.OkJsonCtx(r.Context(), w, types.Response{Code: 500, Message: err.Error()}) httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return return
} }
l := logic.NewGetProductListLogic(r.Context(), svcCtx) l := logic.NewGetProductListLogic(r.Context(), svcCtx)
resp, err := l.GetProductList(&req, userInfo) resp := l.GetProductList(&req, userInfo)
if err != nil { if resp != nil {
httpx.OkJsonCtx(r.Context(), w, types.Response{Code: 500, Message: err.Error()})
} else {
httpx.OkJsonCtx(r.Context(), w, resp) httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
} }
} }
} }

View File

@ -2,16 +2,9 @@ package logic
import ( import (
"context" "context"
"errors"
"fmt"
"fusenapi/model"
"fusenapi/utils/auth"
"fusenapi/utils/image"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"strings"
"fusenapi/product/internal/svc" "fusenapi/product/internal/svc"
"fusenapi/product/internal/types" "fusenapi/product/internal/types"
"fusenapi/utils/auth"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
@ -31,10 +24,10 @@ func NewGetProductInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
} }
// 获取产品详情 // 获取产品详情
func (l *GetProductInfoLogic) GetProductInfo(req *types.GetProductInfoReq, loginInfo auth.UserInfo) (resp *types.Response, err error) { func (l *GetProductInfoLogic) GetProductInfo(req *types.GetProductInfoReq, loginInfo auth.UserInfo) (resp *types.Response) {
//校验前台登录情况 //校验前台登录情况
if loginInfo.UserId == 0 { /*if loginInfo.UserId == 0 {
return &types.Response{Code: 402, Message: "please sign in"}, nil return &types.Response{Code: 402, Message: "please sign in"}
} }
req.Pid = strings.Trim(req.Pid, " ") req.Pid = strings.Trim(req.Pid, " ")
req.ClientNo = strings.Trim(req.ClientNo, " ") req.ClientNo = strings.Trim(req.ClientNo, " ")
@ -46,28 +39,28 @@ func (l *GetProductInfoLogic) GetProductInfo(req *types.GetProductInfoReq, login
productInfo, err := productModel.FindOneBySn(l.ctx, req.Pid) productInfo, err := productModel.FindOneBySn(l.ctx, req.Pid)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) { if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "failed to get product info"}, nil return &types.Response{Code: 510, Message: "failed to get product info"}
} }
if productInfo == nil { if productInfo == nil {
return &types.Response{Code: 510, Message: "product not found"}, nil return &types.Response{Code: 510, Message: "product not found"}
} }
//获取产品标签 //获取产品标签
tagModel := model.NewFsTagsModel(l.svcCtx.MysqlConn) tagModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagInfo, err := tagModel.FindOne(l.ctx, productInfo.Type) tagInfo, err := tagModel.FindOne(l.ctx, productInfo.Type)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "failed to get product tag"}, nil return &types.Response{Code: 510, Message: "failed to get product tag"}
} }
//获取产品尺寸列表 //获取产品尺寸列表
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn) productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.FindAllByStatus(l.ctx, 1, 1) productSizeList, err := productSizeModel.FindAllByStatus(l.ctx, 1, 1)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "failed to get product size list"}, nil return &types.Response{Code: 510, Message: "failed to get product size list"}
} }
sizeIds := make([]string, 0, len(productSizeList)) sizeIds := make([]string, 0, len(productSizeList))
for _, v := range productSizeList { for _, v := range productSizeList {
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.Id)) sizeIds = append(sizeIds, fmt.Sprintf("%d", v.Id))
} }*/
return return
} }

View File

@ -35,19 +35,19 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
} }
// 获取产品列表 // 获取产品列表
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo auth.UserInfo) (resp *types.Response, err error) { func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo auth.UserInfo) (resp *types.Response) {
//校验前台登录情况 //校验前台登录情况
if loginInfo.UserId == 0 { if loginInfo.UserId == 0 {
return &types.Response{Code: 402, Message: "please sign in"}, nil return &types.Response{Code: 402, Message: "please sign in"}
} }
//如果是demo //如果是demo
if req.IsDemo == 1 { if req.IsDemo == 1 {
var demo types.GetProductListRsp var demo types.GetProductListRsp
if err = json.Unmarshal([]byte(l.DemoProductList()), &demo); err != nil { if err := json.Unmarshal([]byte(l.DemoProductList()), &demo); err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "demo data format err"}, nil return &types.Response{Code: 510, Message: "demo data format err"}
} }
return &types.Response{Code: 200, Message: "success", Data: demo}, nil return &types.Response{Code: 200, Message: "success", Data: demo}
} }
if req.Page <= 0 { if req.Page <= 0 {
req.Page = 1 req.Page = 1
@ -61,22 +61,22 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId) userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) { if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "get user info err"}, nil return &types.Response{Code: 510, Message: "get user info err"}
} }
if userInfo == nil { if userInfo == nil {
return &types.Response{Code: 402, Message: "user not exists"}, nil return &types.Response{Code: 402, Message: "user not exists"}
} }
//查询符合的产品列表 //查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn) productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), 0, 1, "sort-desc") productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), 0, 1, "sort-desc")
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "failed to get product list"}, nil return &types.Response{Code: 510, Message: "failed to get product list"}
} }
fmt.Println(len(productList)) fmt.Println(len(productList))
productLen := len(productList) productLen := len(productList)
if productLen == 0 { if productLen == 0 {
return &types.Response{Code: 200, Message: "success"}, nil return &types.Response{Code: 200, Message: "success"}
} }
//提取产品ids //提取产品ids
productIds := make([]string, 0, productLen) productIds := make([]string, 0, productLen)
@ -87,7 +87,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
productPriceList, err := productPriceModel.GetPriceList(l.ctx, productIds) productPriceList, err := productPriceModel.GetPriceList(l.ctx, productIds)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "failed to get product min price list"}, nil return &types.Response{Code: 510, Message: "failed to get product min price list"}
} }
//存储产品最小价格 //存储产品最小价格
mapProductMinPrice := make(map[int64]int64) mapProductMinPrice := make(map[int64]int64)
@ -96,7 +96,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) priceSlice, err := format.StrSlicToIntSlice(priceStrSlic)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: err.Error()}, nil return &types.Response{Code: 510, Message: err.Error()}
} }
if len(priceSlice) == 0 { if len(priceSlice) == 0 {
continue continue
@ -109,7 +109,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds, 0, 1) productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds, 0, 1)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "get product template_v2 err"}, nil return &types.Response{Code: 510, Message: "get product template_v2 err"}
} }
mapProductTemplate := make(map[int64]struct{}) mapProductTemplate := make(map[int64]struct{})
for _, v := range productTemplatesV2 { for _, v := range productTemplatesV2 {
@ -120,17 +120,17 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid) tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) { if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "get classification err "}, nil return &types.Response{Code: 510, Message: "get classification err "}
} }
if tagInfo == nil { if tagInfo == nil {
return &types.Response{Code: 510, Message: "classification not exists "}, nil return &types.Response{Code: 510, Message: "classification not exists "}
} }
//获取产品尺寸数量 //获取产品尺寸数量
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn) productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1) productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return &types.Response{Code: 510, Message: "get product size count err "}, nil return &types.Response{Code: 510, Message: "get product size count err "}
} }
//拼接返回 //拼接返回
itemList := make([]types.Items, 0, productLen) itemList := make([]types.Items, 0, productLen)
@ -185,7 +185,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
}, },
TypeName: tagInfo.Title, TypeName: tagInfo.Title,
Description: tagInfo.Description, Description: tagInfo.Description,
}}, nil }}
return return
} }

View File

@ -1,6 +1,10 @@
// Code generated by goctl. DO NOT EDIT. // Code generated by goctl. DO NOT EDIT.
package types package types
import (
"fusenapi/utils/basic"
)
type GetProductListReq struct { type GetProductListReq struct {
Cid int64 `form:"cid"` Cid int64 `form:"cid"`
Size uint32 `form:"size"` Size uint32 `form:"size"`
@ -164,3 +168,49 @@ type Auth struct {
AccessSecret string `json:"AccessSecret"` AccessSecret string `json:"AccessSecret"`
AccessExpire int `json:"AccessExpire"` AccessExpire int `json:"AccessExpire"`
} }
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) {
resp.Code = Code
resp.Message = Message
}
// Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) {
resp.Code = Code
resp.Message = Message
resp.Data = Data
}
// SetMessage 设置Response的Message
func (resp *Response) SetMessage(msg string) {
resp.Message = msg
}
// SetWithData 设置Data
func (resp *Response) SetData(Data interface{}) {
resp.Data = Data
}
// SetWithData 设置Response的Code和Message值 带Data入参数
func (resp *Response) SetCode(Code int) {
resp.Code = Code
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) {
resp.Code = sr.Code
resp.Message = sr.Message
if len(data) == 1 {
resp.Data = data[0]
}
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) {
resp.Code = sr.Code
resp.Message = msg
if len(data) == 1 {
resp.Data = data[0]
}
}