package v1
|
|
import (
|
"aps_crm/conf"
|
"aps_crm/model/request"
|
"aps_crm/model/response"
|
"aps_crm/pkg/contextx"
|
"aps_crm/pkg/ecode"
|
"aps_crm/pkg/logx"
|
"aps_crm/proto/product"
|
"fmt"
|
"github.com/gin-gonic/gin"
|
"github.com/spf13/cast"
|
"google.golang.org/grpc"
|
"google.golang.org/grpc/credentials/insecure"
|
)
|
|
type ProductApi struct{}
|
|
var (
|
productServiceConn *grpc.ClientConn
|
)
|
|
func InitProductServiceConn() {
|
fmt.Println(conf.Conf.GrpcServiceAddr.Aps)
|
var err error
|
productServiceConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Aps, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
if err != nil {
|
logx.Errorf("grpc dial product service error: %v", err.Error())
|
return
|
}
|
}
|
|
// List
|
//
|
// @Tags 产品
|
// @Summary 获取产品列表
|
// @Produce application/json
|
// @Param object query request.GetProductList true "参数"
|
// @Success 200 {object} response.ListResponse{data=[]product.Product}
|
//
|
// @Router /api/product/list [get]
|
func (ci *ProductApi) List(c *gin.Context) {
|
var params request.GetProductList
|
ctx, ok := contextx.NewContext(c, ¶ms)
|
if !ok {
|
return
|
}
|
if !params.Check() {
|
ctx.FailWithMsg(ecode.ParamsErr, "page, pageSize超出范围")
|
return
|
}
|
|
cli := product.NewProductServiceClient(productServiceConn)
|
getProductListResponse, err := cli.GetProductList(ctx.GetCtx(), &product.GetProductListRequest{
|
Page: cast.ToInt32(params.Page),
|
PageSize: cast.ToInt32(params.PageSize),
|
ProductNumber: params.ProductNumber,
|
ProductName: params.ProductName,
|
})
|
if err != nil {
|
logx.Errorf("GetProductList err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
if getProductListResponse.Code != 0 {
|
logx.Errorf("GetProductList err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
ctx.OkWithDetailed(response.ListResponse{
|
Data: getProductListResponse.List,
|
Count: getProductListResponse.Total,
|
})
|
}
|
|
// Info
|
// @Tags 产品
|
// @Summary 获取产品详情
|
// @Produce application/json
|
// @Param productNumber query string true "参数"
|
// @Success 200 {object} contextx.Response{data=product.Product} "成功"
|
// @Router /api/product/info [get]
|
func (ci *ProductApi) Info(c *gin.Context) {
|
ctx, ok := contextx.NewContext(c, nil)
|
if !ok {
|
return
|
}
|
|
// 获取产品ID
|
productId := c.Query("productNumber")
|
cli := product.NewProductServiceClient(productServiceConn)
|
|
getProductInfoResponse, err := cli.GetProductInfo(ctx.GetCtx(), &product.GetProductInfoRequest{ProductId: productId})
|
if err != nil {
|
logx.Errorf("GetProductInfo err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
if getProductInfoResponse.Code != 0 {
|
logx.Errorf("GetProductInfo err: %v", err.Error())
|
ctx.FailWithMsg(ecode.UnknownErr, "内部错误")
|
return
|
}
|
ctx.OkWithDetailed(getProductInfoResponse.Data)
|
}
|