From 73eef005eca04462714777fa023fd5912fa63566 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 20 Jun 2023 16:38:03 +0800 Subject: [PATCH] fix --- .../etc/product-templatev2.yaml | 8 + .../internal/config/config.go | 12 + .../handler/gettemplatevdetailhandler.go | 78 ++++++ .../internal/handler/routes.go | 22 ++ .../internal/logic/gettemplatevdetaillogic.go | 34 +++ .../internal/svc/servicecontext.go | 50 ++++ .../internal/types/types.go | 241 ++++++++++++++++++ .../product-templatev2/product-templatev2.go | 49 ++++ server_api/product-templatev2.api | 176 +++++++++++++ 9 files changed, 670 insertions(+) create mode 100644 server/product-templatev2/etc/product-templatev2.yaml create mode 100644 server/product-templatev2/internal/config/config.go create mode 100644 server/product-templatev2/internal/handler/gettemplatevdetailhandler.go create mode 100644 server/product-templatev2/internal/handler/routes.go create mode 100644 server/product-templatev2/internal/logic/gettemplatevdetaillogic.go create mode 100644 server/product-templatev2/internal/svc/servicecontext.go create mode 100644 server/product-templatev2/internal/types/types.go create mode 100644 server/product-templatev2/product-templatev2.go create mode 100644 server_api/product-templatev2.api diff --git a/server/product-templatev2/etc/product-templatev2.yaml b/server/product-templatev2/etc/product-templatev2.yaml new file mode 100644 index 00000000..f2e296cd --- /dev/null +++ b/server/product-templatev2/etc/product-templatev2.yaml @@ -0,0 +1,8 @@ +Name: product-templatev2 +Host: 0.0.0.0 +Port: 8895 +SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest +Auth: + AccessSecret: fusen2023 + AccessExpire: 60 + RefreshAfter: 60 \ No newline at end of file diff --git a/server/product-templatev2/internal/config/config.go b/server/product-templatev2/internal/config/config.go new file mode 100644 index 00000000..ccc7c293 --- /dev/null +++ b/server/product-templatev2/internal/config/config.go @@ -0,0 +1,12 @@ +package config + +import ( + "fusenapi/server/product-templatev2/internal/types" + "github.com/zeromicro/go-zero/rest" +) + +type Config struct { + rest.RestConf + SourceMysql string + Auth types.Auth +} diff --git a/server/product-templatev2/internal/handler/gettemplatevdetailhandler.go b/server/product-templatev2/internal/handler/gettemplatevdetailhandler.go new file mode 100644 index 00000000..e71f6d84 --- /dev/null +++ b/server/product-templatev2/internal/handler/gettemplatevdetailhandler.go @@ -0,0 +1,78 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/product-templatev2/internal/logic" + "fusenapi/server/product-templatev2/internal/svc" + "fusenapi/server/product-templatev2/internal/types" +) + +func GetTemplatevDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + 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, userinfo) + // 如果响应不为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) + } + } +} diff --git a/server/product-templatev2/internal/handler/routes.go b/server/product-templatev2/internal/handler/routes.go new file mode 100644 index 00000000..d2b5fa8a --- /dev/null +++ b/server/product-templatev2/internal/handler/routes.go @@ -0,0 +1,22 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/server/product-templatev2/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), + }, + }, + ) +} diff --git a/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go b/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go new file mode 100644 index 00000000..865b4156 --- /dev/null +++ b/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go @@ -0,0 +1,34 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/product-templatev2/internal/svc" + "fusenapi/server/product-templatev2/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, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/product-templatev2/internal/svc/servicecontext.go b/server/product-templatev2/internal/svc/servicecontext.go new file mode 100644 index 00000000..2f214a7d --- /dev/null +++ b/server/product-templatev2/internal/svc/servicecontext.go @@ -0,0 +1,50 @@ +package svc + +import ( + "errors" + "fusenapi/initalize" + "fusenapi/server/product-templatev2/internal/config" + "github.com/golang-jwt/jwt" + "gorm.io/gorm" + "net/http" +) + +type ServiceContext struct { + Config config.Config + + MysqlConn *gorm.DB +} + +func NewServiceContext(c config.Config) *ServiceContext { + + return &ServiceContext{ + Config: c, + MysqlConn: initalize.InitMysql(c.SourceMysql), + } +} + +func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { + AuthKey := r.Header.Get("Authorization") + 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 svcCxt.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)) +} diff --git a/server/product-templatev2/internal/types/types.go b/server/product-templatev2/internal/types/types.go new file mode 100644 index 00000000..159c1c19 --- /dev/null +++ b/server/product-templatev2/internal/types/types.go @@ -0,0 +1,241 @@ +// 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 ProductModelInfo `json:"product_model_info"` + ProductTemplate ProductTemplate `json:"product_template"` + LightList []Light `json:"light_list"` + OptionModelInfo []interface{} `json:"option_model_info"` + Tag int64 `json:"tag"` +} + +type ProductModelInfo struct { + Id int64 `json:"id"` + Name string `json:"name"` + KnifeTerritory string `json:"knifeTerritory"` + Cover string `json:"cover"` + CameraData CameraData `json:"cameraData"` + ControlsData ControlsData `json:"controlsData"` + Material ModelMaterial `json:"material"` + ModelData ModelData `json:"modelData"` + LightData int64 `json:"lightData"` + LightList []int64 `json:"lightList"` + Parts string `json:"parts"` + PartsList []int64 `json:"partsList"` + Tag int64 `json:"tag"` +} + +type CameraData struct { + X int64 `json:"x"` + Y int64 `json:"y"` + Z int64 `json:"z"` +} + +type ControlsData struct { + MinDistance int64 `json:"minDistance"` + MaxDistance int64 `json:"maxDistance"` + MaxPolarAngle float64 `json:"maxPolarAngle"` + MinPolarAngle float64 `json:"minPolarAngle"` +} + +type Tag struct { + Id int64 `json:"id"` + Title string `json:"title"` +} + +type TemplateInfo struct { + Id int64 `json:"id"` + Name string `json:"name"` + Cover string `json:"cover"` + IsPublic bool `json:"isPublic"` + Material string `json:"material"` + MaterialList TemplateMateria `json:"materialList"` +} + +type ProductTemplate struct { + CoverImg string `json:"cover_img"` + Id int64 `json:"id"` + IsPublic bool `json:"is_public"` + LogoHeight int64 `json:"logo_height"` + LogoWidth int64 `json:"logo_width"` + MaterialImg string `json:"material_img"` + ModelId int64 `json:"model_id"` + Name string `json:"name"` + ProductId int64 `json:"product_id"` + Sort int64 `json:"sort"` + Tag Tag `json:"tag"` + TemplateInfo TemplateInfo `json:"template_info"` + Title string `json:"title"` +} + +type Light struct { + Id int64 `json:"id"` + Info LightInfo `json:"info"` +} + +type ModelMaterial struct { + AoMap string `json:"aoMap"` + AoMapint64ensity int64 `json:"aoMapint64ensity"` + NormalMap string `json:"normalMap"` + NormalScale []int64 `json:"normalScale"` + SpecularMap string `json:"specularMap"` + Roughness int64 `json:"roughness"` + MetalnessMap string `json:"metalnessMap"` + Metalness int64 `json:"metalness"` +} + +type ModelData struct { + Path string `json:"path"` + Shadow string `json:"shadow"` +} + +type TemplateMateria struct { + Id string `json:"id"` + Tag string `json:"tag"` + Title string `json:"title"` + Type string `json:"type"` + Text string `json:"text"` + Fill string `json:"fill"` + FontSize int64 `json:"fontSize"` + FontFamily string `json:"fontFamily"` + IfBr bool `json:"ifBr"` + IfShow bool `json:"ifShow"` + IfGroup bool `json:"ifGroup"` + MaxNum int64 `json:"maxNum"` + Rotation int64 `json:"rotation"` + LineHeight int64 `json:"lineHeight"` + Align string `json:"align"` + VerticalAlign string `json:"verticalAlign"` + Material string `json:"material"` + MaterialTime string `json:"materialTime"` + MaterialName string `json:"materialName"` + QRcodeType string `json:"QRcodeType"` + Width int64 `json:"width"` + Height int64 `json:"height"` + Proportion int64 `json:"proportion"` + X int64 `json:"x"` + Y int64 `json:"y"` + Opacity int64 `json:"opacity"` + OptionalColor []OptionalColor `json:"optionalColor"` + ZIndex int64 `json:"zIndex"` + SvgPath string `json:"svgPath"` + Follow Follow `json:"follow"` + Group []interface{} `json:"group"` + CameraStand CameraStand `json:"cameraStand"` +} + +type LightInfo struct { + Name string `json:"name"` + Hdr Hdr `json:"hdr"` + LightData []LightDataItem `json:"lightData"` +} + +type LightDataItem struct { + Color string `json:"color"` + Id string `json:"id"` + Intensity float64 `json:"intensity"` + Name string `json:"name"` + Type string `json:"type"` + X float64 `json:"x,omitempty"` + Y int64 `json:"y,omitempty"` + Z float64 `json:"z,omitempty"` + ShowHelper bool `json:"showHelper,omitempty"` + Width int64 `json:"width,omitempty"` + Height int64 `json:"height,omitempty"` + Rx int64 `json:"rx,omitempty"` + Ry int64 `json:"ry,omitempty"` + Rz int64 `json:"rz,omitempty"` +} + +type Hdr struct { + IfBg bool `json:"ifBg"` + ToneMappingExposure float64 `json:"toneMappingExposure"` + Url string `json:"url"` +} + +type OptionalColor struct { + Color string `json:"color"` + Name string `json:"name"` + Default bool `json:"default"` +} + +type Follow struct { + Fill string `json:"fill"` + IfShow string `json:"ifShow"` + Content string `json:"content"` +} + +type CameraStand struct { + X int64 `json:"x"` + Y int64 `json:"y"` + Z int64 `json:"z"` +} + +type Response struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` +} + +type ResponseJwt struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` +} + +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 +} diff --git a/server/product-templatev2/product-templatev2.go b/server/product-templatev2/product-templatev2.go new file mode 100644 index 00000000..52faa7ac --- /dev/null +++ b/server/product-templatev2/product-templatev2.go @@ -0,0 +1,49 @@ +package main + +import ( + "flag" + "fmt" + + "fusenapi/server/product-templatev2/internal/config" + "fusenapi/server/product-templatev2/internal/handler" + "fusenapi/server/product-templatev2/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/product-templatev2.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + + server := rest.MustNewServer(c.RestConf) + defer server.Stop() + + ctx := svc.NewServiceContext(c) + handler.RegisterHandlers(server, ctx) + + fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + server.Start() +} + +// var testConfigFile = flag.String("f", "../etc/product-templatev2.yaml", "the config file") +// var cnf config.Config + +// func GetTestServer() *rest.Server { +// flag.Parse() + +// conf.MustLoad(*testConfigFile, &cnf) + +// server := rest.MustNewServer(cnf.RestConf) +// defer server.Stop() + +// ctx := svc.NewServiceContext(cnf) +// handler.RegisterHandlers(server, ctx) + +// fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port) +// return server +// } diff --git a/server_api/product-templatev2.api b/server_api/product-templatev2.api new file mode 100644 index 00000000..e14ebc3f --- /dev/null +++ b/server_api/product-templatev2.api @@ -0,0 +1,176 @@ +syntax = "v1" + +info ( + title: "产品模板"// TODO: add title + desc: // TODO: add description + author: "" + email: "" +) +import "basic.api" +service product-templatev2 { + //获取产品模板详情 + @handler GetTemplatevDetailHandler + get /product-template/detail(GetTemplatevDetailReq) returns (response); +} + +//获取产品模板详情 +type GetTemplatevDetailReq { + ModelId int64 `form:"model_id"` + TemplateId int64 `form:"template_id"` +} +type GetTemplatevDetailRsp { + ProductModelInfo ProductModelInfo `json:"product_model_info"` + ProductTemplate ProductTemplate `json:"product_template"` + LightList []Light `json:"light_list"` + OptionModelInfo []interface{} `json:"option_model_info"` + Tag int64 `json:"tag"` +} + +type ProductModelInfo { + Id int64 `json:"id"` + Name string `json:"name"` + KnifeTerritory string `json:"knifeTerritory"` + Cover string `json:"cover"` + CameraData CameraData `json:"cameraData"` + ControlsData ControlsData `json:"controlsData"` + Material ModelMaterial `json:"material"` + ModelData ModelData `json:"modelData"` + LightData int64 `json:"lightData"` + LightList []int64 `json:"lightList"` + Parts string `json:"parts"` + PartsList []int64 `json:"partsList"` + Tag int64 `json:"tag"` +} + +type CameraData { + X int64 `json:"x"` + Y int64 `json:"y"` + Z int64 `json:"z"` +} +type ControlsData { + MinDistance int64 `json:"minDistance"` + MaxDistance int64 `json:"maxDistance"` + MaxPolarAngle float64 `json:"maxPolarAngle"` + MinPolarAngle float64 `json:"minPolarAngle"` +} +type Tag { + Id int64 `json:"id"` + Title string `json:"title"` +} +type TemplateInfo { + Id int64 `json:"id"` + Name string `json:"name"` + Cover string `json:"cover"` + IsPublic bool `json:"isPublic"` + Material string `json:"material"` + MaterialList TemplateMateria `json:"materialList"` +} +type ProductTemplate { + CoverImg string `json:"cover_img"` + Id int64 `json:"id"` + IsPublic bool `json:"is_public"` + LogoHeight int64 `json:"logo_height"` + LogoWidth int64 `json:"logo_width"` + MaterialImg string `json:"material_img"` + ModelId int64 `json:"model_id"` + Name string `json:"name"` + ProductId int64 `json:"product_id"` + Sort int64 `json:"sort"` + Tag Tag `json:"tag"` + TemplateInfo TemplateInfo `json:"template_info"` + Title string `json:"title"` +} +type Light { + Id int64 `json:"id"` + Info LightInfo `json:"info"` +} + +type ModelMaterial { + AoMap string `json:"aoMap"` + AoMapint64ensity int64 `json:"aoMapint64ensity"` + NormalMap string `json:"normalMap"` + NormalScale []int64 `json:"normalScale"` + SpecularMap string `json:"specularMap"` + Roughness int64 `json:"roughness"` + MetalnessMap string `json:"metalnessMap"` + Metalness int64 `json:"metalness"` +} +type ModelData { + Path string `json:"path"` + Shadow string `json:"shadow"` +} +type TemplateMateria { + Id string `json:"id"` + Tag string `json:"tag"` + Title string `json:"title"` + Type string `json:"type"` + Text string `json:"text"` + Fill string `json:"fill"` + FontSize int64 `json:"fontSize"` + FontFamily string `json:"fontFamily"` + IfBr bool `json:"ifBr"` + IfShow bool `json:"ifShow"` + IfGroup bool `json:"ifGroup"` + MaxNum int64 `json:"maxNum"` + Rotation int64 `json:"rotation"` + LineHeight int64 `json:"lineHeight"` + Align string `json:"align"` + VerticalAlign string `json:"verticalAlign"` + Material string `json:"material"` + MaterialTime string `json:"materialTime"` + MaterialName string `json:"materialName"` + QRcodeType string `json:"QRcodeType"` + Width int64 `json:"width"` + Height int64 `json:"height"` + Proportion int64 `json:"proportion"` + X int64 `json:"x"` + Y int64 `json:"y"` + Opacity int64 `json:"opacity"` + OptionalColor []OptionalColor `json:"optionalColor"` + ZIndex int64 `json:"zIndex"` + SvgPath string `json:"svgPath"` + Follow Follow `json:"follow"` + Group []interface{} `json:"group"` + CameraStand CameraStand `json:"cameraStand"` +} +type LightInfo { + Name string `json:"name"` + Hdr Hdr `json:"hdr"` + LightData []LightDataItem `json:"lightData"` +} +type LightDataItem { + Color string `json:"color"` + Id string `json:"id"` + Intensity float64 `json:"intensity"` + Name string `json:"name"` + Type string `json:"type"` + X float64 `json:"x,omitempty"` + Y int64 `json:"y,omitempty"` + Z float64 `json:"z,omitempty"` + ShowHelper bool `json:"showHelper,omitempty"` + Width int64 `json:"width,omitempty"` + Height int64 `json:"height,omitempty"` + Rx int64 `json:"rx,omitempty"` + Ry int64 `json:"ry,omitempty"` + Rz int64 `json:"rz,omitempty"` +} +type Hdr { + IfBg bool `json:"ifBg"` + ToneMappingExposure float64 `json:"toneMappingExposure"` + Url string `json:"url"` +} +type OptionalColor { + Color string `json:"color"` + Name string `json:"name"` + Default bool `json:"default"` +} +type Follow { + Fill string `json:"fill"` + IfShow string `json:"ifShow"` + Content string `json:"content"` +} +type CameraStand { + X int64 `json:"x"` + Y int64 `json:"y"` + Z int64 `json:"z"` +} \ No newline at end of file