fix
This commit is contained in:
12
server/product-template/internal/config/config.go
Normal file
12
server/product-template/internal/config/config.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/product-template/internal/logic"
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
)
|
||||
|
||||
func AddBaseMapHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AddBaseMapReq
|
||||
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||
Code: 510,
|
||||
Message: "parameter error",
|
||||
})
|
||||
logx.Info(err)
|
||||
return
|
||||
}
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewAddBaseMapLogic(r.Context(), svcCtx)
|
||||
resp := l.AddBaseMap(&req, r)
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
if resp != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"fusenapi/server/product-template/internal/logic"
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
)
|
||||
|
||||
func GetBaseMapListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
l := logic.NewGetBaseMapListLogic(r.Context(), svcCtx)
|
||||
resp := l.GetBaseMapList(r)
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
if resp != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/product-template/internal/logic"
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
)
|
||||
|
||||
func GetTemplatevDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetTemplatevDetailReq
|
||||
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||
Code: 510,
|
||||
Message: "parameter error",
|
||||
})
|
||||
logx.Info(err)
|
||||
return
|
||||
}
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewGetTemplatevDetailLogic(r.Context(), svcCtx)
|
||||
resp := l.GetTemplatevDetail(&req, r)
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
if resp != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
42
server/product-template/internal/handler/routes.go
Normal file
42
server/product-template/internal/handler/routes.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/product-template/detail",
|
||||
Handler: GetTemplatevDetailHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/product-template/base-map-list",
|
||||
Handler: GetBaseMapListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/product-template/base-map-update",
|
||||
Handler: SaveBaseMapHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/product-template/base-map-add",
|
||||
Handler: AddBaseMapHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/product-template/update-template",
|
||||
Handler: UpdateTemplateHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"fusenapi/server/product-template/internal/logic"
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
)
|
||||
|
||||
func SaveBaseMapHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewSaveBaseMapLogic(r.Context(), svcCtx)
|
||||
resp := l.SaveBaseMap(r)
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
if resp != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
|
||||
"fusenapi/server/product-template/internal/logic"
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
)
|
||||
|
||||
func UpdateTemplateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateTemplateReq
|
||||
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||
Code: 510,
|
||||
Message: "parameter error",
|
||||
})
|
||||
logx.Info(err)
|
||||
return
|
||||
}
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUpdateTemplateLogic(r.Context(), svcCtx)
|
||||
resp := l.UpdateTemplate(&req, r)
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
if resp != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
61
server/product-template/internal/logic/addbasemaplogic.go
Normal file
61
server/product-template/internal/logic/addbasemaplogic.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddBaseMapLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAddBaseMapLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddBaseMapLogic {
|
||||
return &AddBaseMapLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddBaseMapLogic) AddBaseMap(req *types.AddBaseMapReq, r *http.Request) (resp *basic.Response) {
|
||||
authKey := r.Header.Get("Auth-Key")
|
||||
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
||||
_, err := genentModel.Find(l.ctx, authKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
||||
}
|
||||
req.Name = strings.Trim(req.Name, " ")
|
||||
req.Url = strings.Trim(req.Url, " ")
|
||||
if req.Name == "" || req.Url == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param is empty")
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
err = l.svcCtx.AllModels.FsProductTemplateBasemap.Create(l.ctx, &gmodel.FsProductTemplateBasemap{
|
||||
Name: &req.Name,
|
||||
Url: &req.Url,
|
||||
Ctime: &now,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to add base map")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetBaseMapListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetBaseMapListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBaseMapListLogic {
|
||||
return &GetBaseMapListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetBaseMapListLogic) GetBaseMapList(r *http.Request) (resp *basic.Response) {
|
||||
authKey := r.Header.Get("Auth-Key")
|
||||
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
||||
_, err := genentModel.Find(l.ctx, authKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
||||
}
|
||||
baseMapFields := "id,name,url,ctime"
|
||||
baseMapList, err := l.svcCtx.AllModels.FsProductTemplateBasemap.GetAllEnabledList(l.ctx, baseMapFields)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product base map list")
|
||||
}
|
||||
list := make([]types.GetBaseMapListRsp, 0, len(baseMapList))
|
||||
for _, v := range baseMapList {
|
||||
list = append(list, types.GetBaseMapListRsp{
|
||||
Id: v.Id,
|
||||
Name: *v.Name,
|
||||
Url: *v.Url,
|
||||
Ctime: time.Unix(*v.Ctime, 0).Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/format"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetTemplatevDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTemplatevDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTemplatevDetailLogic {
|
||||
return &GetTemplatevDetailLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDetailReq, r *http.Request) (resp *basic.Response) {
|
||||
authKey := r.Header.Get("Auth-Key")
|
||||
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
||||
_, err := genentModel.Find(l.ctx, authKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
||||
}
|
||||
if req.TemplateId <= 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param template_id")
|
||||
}
|
||||
if req.ModelId <= 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param model_id")
|
||||
}
|
||||
//获取模型信息
|
||||
productModel3dModel := gmodel.NewFsProductModel3dModel(l.svcCtx.MysqlConn)
|
||||
model3dInfo, err := productModel3dModel.FindOne(l.ctx, req.ModelId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product model info is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model info")
|
||||
}
|
||||
//查询产品模板并检测数据完整
|
||||
productTemplatev2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
|
||||
fields := "cover_img,id,is_public,logo_height,logo_width,material_img,model_id,name,product_id,sort,tag,template_info,title"
|
||||
templatev2Info, err := productTemplatev2Model.FindByParam(l.ctx, req.TemplateId, req.ModelId, fields)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template info")
|
||||
}
|
||||
//获取模板标签
|
||||
templateTagModel := gmodel.NewFsProductTemplateTagsModel(l.svcCtx.MysqlConn)
|
||||
tagId, err := strconv.ParseInt(*templatev2Info.Tag, 10, 64)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "parse int tag id err")
|
||||
}
|
||||
templateTagInfo, err := templateTagModel.FindOne(l.ctx, tagId, "id,title")
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template tag info is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template tag info")
|
||||
}
|
||||
//配件ids
|
||||
partIds := make([]int64, 0, 10)
|
||||
if *model3dInfo.PartList != "" {
|
||||
partIds, err = format.StrSlicToInt64Slice(strings.Split(*model3dInfo.PartList, ","))
|
||||
}
|
||||
//灯光ids
|
||||
var lightIds []int64
|
||||
if err = json.Unmarshal([]byte(*model3dInfo.LightList), &lightIds); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "parse light list err")
|
||||
}
|
||||
//获取灯光列表
|
||||
productModel3dLightModel := gmodel.NewFsProductModel3dLightModel(l.svcCtx.MysqlConn)
|
||||
model3dLightList, err := productModel3dLightModel.GetAllByIds(l.ctx, lightIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model light list")
|
||||
}
|
||||
//组装灯光信息
|
||||
lightListRsp := make([]*types.Light, 0, len(model3dLightList))
|
||||
for _, v := range model3dLightList {
|
||||
var info interface{}
|
||||
if err = json.Unmarshal([]byte(*v.Info), &info); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse light info")
|
||||
}
|
||||
lightListRsp = append(lightListRsp, &types.Light{
|
||||
Id: v.Id,
|
||||
Info: info,
|
||||
})
|
||||
}
|
||||
//产品模型数据解析model_info
|
||||
var productModelInfoRsp interface{}
|
||||
if model3dInfo.ModelInfo != nil {
|
||||
if err = json.Unmarshal([]byte(*model3dInfo.ModelInfo), &productModelInfoRsp); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse product 3d model info")
|
||||
}
|
||||
}
|
||||
//产品模板数据解析template_info
|
||||
templateInfoRsp := make(map[string]interface{})
|
||||
if templatev2Info.TemplateInfo != nil {
|
||||
if err = json.Unmarshal([]byte(*templatev2Info.TemplateInfo), &templateInfoRsp); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse product v2 template info")
|
||||
}
|
||||
}
|
||||
templateInfoRsp["tag"] = map[string]interface{}{
|
||||
"id": templateTagInfo.Id,
|
||||
"title": *templateTagInfo.Title,
|
||||
}
|
||||
response := types.GetTemplatevDetailRsp{
|
||||
ProductModelInfo: productModelInfoRsp,
|
||||
ProductTemplate: templateInfoRsp,
|
||||
LightList: lightListRsp,
|
||||
OptionModelInfo: nil,
|
||||
Tag: *model3dInfo.Tag,
|
||||
}
|
||||
//查询使用该选项的模板
|
||||
if len(partIds) > 0 {
|
||||
model3dList, err := productModel3dModel.GetAllByIds(l.ctx, partIds, "id,model_info,option_template")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model list")
|
||||
}
|
||||
optionIds := make([]int64, 0, len(model3dList))
|
||||
for _, v := range model3dList {
|
||||
if v.OptionTemplate != nil {
|
||||
optionIds = append(optionIds, *v.OptionTemplate)
|
||||
}
|
||||
}
|
||||
//获取公共模板信息
|
||||
optionTemplateList, err := productTemplatev2Model.FindAllByIds(l.ctx, optionIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product v2 template list")
|
||||
}
|
||||
mapOptionTemplate := make(map[int64]int)
|
||||
for k, v := range optionTemplateList {
|
||||
mapOptionTemplate[v.Id] = k
|
||||
}
|
||||
//处理数据
|
||||
optionModelInfoList := make([]interface{}, 0, len(model3dList))
|
||||
for _, v := range model3dList {
|
||||
info := make(map[string]interface{})
|
||||
if v.ModelInfo != nil {
|
||||
if err = json.Unmarshal([]byte(*v.ModelInfo), &info); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model info")
|
||||
}
|
||||
if optionTemplateIndex, ok := mapOptionTemplate[*v.OptionTemplate]; ok {
|
||||
info["material_img"] = optionTemplateList[optionTemplateIndex].MaterialImg
|
||||
}
|
||||
info["id"] = v.Id
|
||||
}
|
||||
optionModelInfoList = append(optionModelInfoList, info)
|
||||
}
|
||||
response.OptionModelInfo = optionModelInfoList
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", response)
|
||||
}
|
||||
79
server/product-template/internal/logic/savebasemaplogic.go
Normal file
79
server/product-template/internal/logic/savebasemaplogic.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SaveBaseMapLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewSaveBaseMapLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBaseMapLogic {
|
||||
return &SaveBaseMapLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SaveBaseMapLogic) SaveBaseMap(r *http.Request) (resp *basic.Response) {
|
||||
authKey := r.Header.Get("Auth-Key")
|
||||
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
||||
_, err := genentModel.Find(l.ctx, authKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
||||
}
|
||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
var postData []types.SaveBaseMapReq
|
||||
if err = json.Unmarshal(bodyBytes, &postData); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid request param")
|
||||
}
|
||||
//空数组
|
||||
if len(postData) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param can`t be empty array")
|
||||
}
|
||||
updDataArr := make([]gmodel.FsProductTemplateBasemap, 0, len(postData))
|
||||
for _, v := range postData {
|
||||
val := v
|
||||
ctimeT, err := time.ParseInLocation("2006-01-02 15:04:05", v.Ctime, time.Local)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err create time format")
|
||||
}
|
||||
ctime := ctimeT.Unix()
|
||||
updDataArr = append(updDataArr, gmodel.FsProductTemplateBasemap{
|
||||
Id: val.Id,
|
||||
Name: &val.Name,
|
||||
Url: &val.Url,
|
||||
Ctime: &ctime,
|
||||
})
|
||||
}
|
||||
//更新
|
||||
err = l.svcCtx.AllModels.FsProductTemplateBasemap.UpdateBaseMapWithTransaction(l.ctx, updDataArr)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to update base map")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
102
server/product-template/internal/logic/updatetemplatelogic.go
Normal file
102
server/product-template/internal/logic/updatetemplatelogic.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product-template/internal/svc"
|
||||
"fusenapi/server/product-template/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateTemplateLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateTemplateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateTemplateLogic {
|
||||
return &UpdateTemplateLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateTemplateLogic) UpdateTemplate(req *types.UpdateTemplateReq, r *http.Request) (resp *basic.Response) {
|
||||
authKey := r.Header.Get("Auth-Key")
|
||||
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
||||
_, err := genentModel.Find(l.ctx, authKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
||||
}
|
||||
if req.ModelId <= 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param modelId is required")
|
||||
}
|
||||
if req.TemplateData == nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param template data is required")
|
||||
}
|
||||
if req.TemplateData.Id <= 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param template data`id is required")
|
||||
}
|
||||
//验证模板数据真实性
|
||||
templatev2Info, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, req.TemplateData.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get v2 template info")
|
||||
}
|
||||
if *templatev2Info.ModelId != req.ModelId {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "the template`s model id is not match")
|
||||
}
|
||||
templateInfoBytes, err := json.Marshal(req.TemplateData)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to marshal template data")
|
||||
}
|
||||
templateInfoJson := string(templateInfoBytes)
|
||||
//保存模板宽高
|
||||
var logoWidth int64
|
||||
var logoHeight int64
|
||||
for _, v := range req.TemplateData.MaterialList {
|
||||
//logo面板需保存宽高
|
||||
if v["tag"] == "Logo" {
|
||||
logoWidth, _ = v["width"].(json.Number).Int64()
|
||||
logoHeight, _ = v["height"].(json.Number).Int64()
|
||||
break
|
||||
}
|
||||
}
|
||||
isPublic := int64(0)
|
||||
if req.TemplateData.IsPublic {
|
||||
isPublic = 1
|
||||
}
|
||||
updData := gmodel.FsProductTemplateV2{
|
||||
TemplateInfo: &templateInfoJson,
|
||||
MaterialImg: &req.TemplateData.Material,
|
||||
Name: &req.TemplateData.Name,
|
||||
LogoWidth: &logoWidth,
|
||||
LogoHeight: &logoHeight,
|
||||
IsPublic: &isPublic,
|
||||
}
|
||||
if err = l.svcCtx.AllModels.FsProductTemplateV2.Update(l.ctx, req.TemplateData.Id, &updData); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to update template")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UpdateTemplateRsp{
|
||||
ModelId: req.ModelId,
|
||||
TemplateId: req.TemplateData.Id,
|
||||
})
|
||||
}
|
||||
59
server/product-template/internal/svc/servicecontext.go
Normal file
59
server/product-template/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/product-template/internal/config"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/model/gmodel"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
}
|
||||
}
|
||||
|
||||
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if AuthKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
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 []byte(svcCtx.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))
|
||||
}
|
||||
122
server/product-template/internal/types/types.go
Normal file
122
server/product-template/internal/types/types.go
Normal file
@@ -0,0 +1,122 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type GetTemplatevDetailReq struct {
|
||||
ModelId int64 `form:"model_id"`
|
||||
TemplateId int64 `form:"template_id"`
|
||||
}
|
||||
|
||||
type GetTemplatevDetailRsp struct {
|
||||
ProductModelInfo interface{} `json:"product_model_info"`
|
||||
ProductTemplate interface{} `json:"product_template"`
|
||||
LightList []*Light `json:"light_list"`
|
||||
OptionModelInfo []interface{} `json:"option_model_info"`
|
||||
Tag int64 `json:"tag"`
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
Id int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
type Light struct {
|
||||
Id int64 `json:"id"`
|
||||
Info interface{} `json:"info"`
|
||||
}
|
||||
|
||||
type GetBaseMapListRsp struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
Ctime string `json:"ctime"`
|
||||
}
|
||||
|
||||
type SaveBaseMapReq struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
Ctime string `json:"ctime"`
|
||||
}
|
||||
|
||||
type AddBaseMapReq struct {
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type UpdateTemplateReq struct {
|
||||
ModelId int64 `json:"modelId"`
|
||||
TemplateData *TemplateData `json:"templateData"`
|
||||
}
|
||||
|
||||
type TemplateData struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Cover string `json:"cover"`
|
||||
IsPublic bool `json:"isPublic"`
|
||||
Material string `json:"material"`
|
||||
MaterialList []map[string]interface{} `json:"materialList"`
|
||||
}
|
||||
|
||||
type UpdateTemplateRsp struct {
|
||||
ModelId int64 `json:"modelId"`
|
||||
TemplateId int64 `json:"templateId"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
AccessSecret string `json:"accessSecret"`
|
||||
AccessExpire int64 `json:"accessExpire"`
|
||||
RefreshAfter int64 `json:"refreshAfter"`
|
||||
}
|
||||
|
||||
// Set 设置Response的Code和Message值
|
||||
func (resp *Response) Set(Code int, Message string) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
}
|
||||
}
|
||||
|
||||
// Set 设置整个Response
|
||||
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
Data: Data,
|
||||
}
|
||||
}
|
||||
|
||||
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
|
||||
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
Message: msg,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
Reference in New Issue
Block a user