155 lines
3.9 KiB
Go
155 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"geo/internal/biz"
|
|
"geo/internal/config"
|
|
"geo/internal/data/impl"
|
|
"geo/internal/data/model"
|
|
"geo/internal/entitys"
|
|
"geo/pkg"
|
|
"geo/tmpl/dataTemp"
|
|
"geo/tmpl/errcode"
|
|
"github.com/go-viper/mapstructure/v2"
|
|
"github.com/gofiber/fiber/v2"
|
|
"io"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
type ProductService struct {
|
|
cfg *config.Config
|
|
productImpl *impl.ProductImpl
|
|
authBiz *biz.AuthBiz
|
|
productBiz *biz.ProductBiz
|
|
}
|
|
|
|
func NewProductService(cfg *config.Config, ProductImpl *impl.ProductImpl, authBiz *biz.AuthBiz, productBiz *biz.ProductBiz) *ProductService {
|
|
return &ProductService{
|
|
cfg: cfg,
|
|
productImpl: ProductImpl,
|
|
authBiz: authBiz,
|
|
productBiz: productBiz,
|
|
}
|
|
}
|
|
|
|
func (p *ProductService) Add(c *fiber.Ctx, req *entitys.CreateProductRequest) error {
|
|
_, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var data model.Product
|
|
err = mapstructure.Decode(req, &data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = p.productImpl.Add(c.UserContext(), &data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *ProductService) Detail(c *fiber.Ctx, req *entitys.ProductDetailRequest) error {
|
|
_, err := p.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var detail model.Product
|
|
cond := builder.NewCond().
|
|
And(builder.Eq{"id": req.Id})
|
|
err = p.productImpl.GetOneBySearchStruct(c.UserContext(), &cond, &detail)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return pkg.HandleResponse(c, detail)
|
|
}
|
|
|
|
func (p *ProductService) List(c *fiber.Ctx, req *entitys.ProductListRequest) error {
|
|
_, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
page := req.Page
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
pageSize := req.PageSize
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
if pageSize > 100 {
|
|
pageSize = 100
|
|
}
|
|
var list []*model.Product
|
|
cond := builder.NewCond().
|
|
And(builder.Eq{"user_index": req.UserIndex}).
|
|
And(builder.Eq{"status": 1})
|
|
total, err := p.productImpl.GetListToStruct(c.UserContext(), &cond, &dataTemp.ReqPageBo{Page: page, Limit: pageSize}, &list, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return pkg.SuccessWithPageMsg(c, list, total.Total, page, pageSize)
|
|
}
|
|
|
|
func (p *ProductService) Update(c *fiber.Ctx, req *entitys.ProductUpdateRequest) error {
|
|
_, err := p.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var data model.Product
|
|
err = mapstructure.Decode(req, &data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = p.productImpl.UpdateByKey(c.UserContext(), p.productImpl.PrimaryKey(), req.Id, &data)
|
|
return nil
|
|
}
|
|
|
|
func (p *ProductService) Del(c *fiber.Ctx, req *entitys.ProductDelRequest) error {
|
|
_, err := p.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = p.productImpl.DeleteByKey(c.UserContext(), p.productImpl.PrimaryKey(), req.Id)
|
|
return nil
|
|
}
|
|
|
|
func (p *ProductService) ImgUpload(c *fiber.Ctx) error {
|
|
access := c.FormValue("access_token", "")
|
|
if access == "" {
|
|
return errcode.ParamErr("access_token未找到")
|
|
}
|
|
// 验证token
|
|
_, err := p.authBiz.ValidateAccessToken(c.UserContext(), access)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileHeader, err := c.FormFile("file")
|
|
if err != nil {
|
|
return errcode.ParamErr("未找到上传文件")
|
|
}
|
|
file, err := fileHeader.Open()
|
|
if err != nil {
|
|
return errcode.ParamErrf("无法打开文件:%S", err.Error())
|
|
}
|
|
defer file.Close()
|
|
|
|
fileBytes, err := io.ReadAll(file)
|
|
if err != nil {
|
|
return errcode.ParamErrf("读取文件失败:%s", err.Error())
|
|
}
|
|
|
|
// 获取文件扩展名
|
|
ext := pkg.GetFileExtension(fileHeader.Filename)
|
|
|
|
// 根据图片类型生成文件名
|
|
fileName := pkg.GenerateImageFileName(ext)
|
|
|
|
url, err := p.productBiz.SourceUpload(c.UserContext(), fileBytes, fileName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return pkg.HandleResponse(c, fiber.Map{"url": url})
|
|
}
|