f5749b87dc
CI / changes (push) Successful in 1m30s
CI / ad (push) Successful in 31s
CI / admin (push) Successful in 22s
CI / bff (push) Successful in 20s
CI / chore (push) Successful in 21s
CI / equipment (push) Successful in 22s
CI / express (push) Successful in 21s
CI / factory (push) Successful in 19s
CI / message (push) Successful in 18s
CI / order (push) Successful in 19s
CI / payment (push) Successful in 20s
CI / percutaneous (push) Successful in 19s
CI / product (push) Successful in 20s
CI / record (push) Successful in 19s
CI / sale (push) Successful in 19s
CI / support (push) Successful in 19s
CI / task (push) Successful in 18s
CI / user (push) Successful in 19s
CI / wecom (push) Successful in 20s
106 lines
2.5 KiB
Go
106 lines
2.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"io"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/rpc/express/pb"
|
|
product "lone-services/rpc/product/pb"
|
|
"lone-services/services/express/internal/dao"
|
|
"lone-services/services/express/internal/svc"
|
|
"net/http"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type PrintProductLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewPrintProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PrintProductLogic {
|
|
return &PrintProductLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *PrintProductLogic) PrintProduct(in *express.EmtpyRequest) (*express.Response, error) {
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return l.fail(utils.ErrorNoLoginInfo)
|
|
}
|
|
|
|
items, err := l.getProducts()
|
|
if err != utils.Ok {
|
|
return l.fail(err)
|
|
}
|
|
|
|
return l.ok(items)
|
|
}
|
|
|
|
func (l *PrintProductLogic) getProducts() ([]dao.ProductPrint, utils.Status) {
|
|
cli, err := svc.GetRpcClient(l.svcCtx.ProductSvcName)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return nil, utils.ErrorInternalServer
|
|
}
|
|
|
|
var Type []uint32
|
|
Status := []uint32{utils.StatusOk, utils.StatusFail}
|
|
productClient := product.NewProductClient(cli.Conn())
|
|
productItems, err := productClient.InternalItems(l.ctx, &product.InternalItemsReq{Status: Status, Type: Type})
|
|
if err != nil {
|
|
logx.Errorf("get products err: %v", err)
|
|
return nil, utils.SetError(utils.Fail, "获取产品信息错误")
|
|
}
|
|
|
|
var info []dao.ProductPrint
|
|
for _, val := range productItems.Items {
|
|
client := &http.Client{
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
},
|
|
}
|
|
resp, err := client.Get(val.Waybill)
|
|
if err != nil {
|
|
return info, utils.Fail
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
utils.Logger.Error(err)
|
|
}
|
|
}(resp.Body)
|
|
|
|
// 读取图片二进制内容
|
|
imgData, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
utils.Logger.Error(err)
|
|
return info, utils.Fail
|
|
}
|
|
|
|
// 自动识别图片类型(png/jpg/gif 等)
|
|
contentType := http.DetectContentType(imgData)
|
|
|
|
// 转 base64
|
|
base64Str := base64.StdEncoding.EncodeToString(imgData)
|
|
|
|
// 拼接成可直接在 img 标签显示的格式
|
|
imageBase64 := "data:" + contentType + ";base64," + base64Str
|
|
info = append(info, dao.ProductPrint{
|
|
Id: int(val.Id),
|
|
Name: val.Name,
|
|
Weight: float32(val.Weight),
|
|
Cubage: val.Cubage,
|
|
Waybill: imageBase64,
|
|
})
|
|
}
|
|
|
|
return info, utils.Ok
|
|
}
|