This commit is contained in:
laodaming 2023-06-25 11:28:37 +08:00
parent aceb4c926f
commit 1a48387e92
8 changed files with 261 additions and 11 deletions

View File

@ -35,3 +35,6 @@ func (bm *FsProductTemplateBasemapModel) UpdateBaseMapWithTransaction(ctx contex
Where("`status` = ? and `id` not in (?)", 1, notInIds).Update("status", 0).Error
})
}
func (bm *FsProductTemplateBasemapModel) Create(ctx context.Context, data *FsProductTemplateBasemap) error {
return bm.db.WithContext(ctx).Create(&data).Error
}

View File

@ -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-templatev2/internal/logic"
"fusenapi/server/product-templatev2/internal/svc"
"fusenapi/server/product-templatev2/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)
}
}
}

View File

@ -27,6 +27,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
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),
},
},
)
}

View File

@ -0,0 +1,42 @@
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 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)
}
}
}

View 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-templatev2/internal/svc"
"fusenapi/server/product-templatev2/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")
}

View File

@ -0,0 +1,47 @@
package logic
import (
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/basic"
"gorm.io/gorm"
"net/http"
"context"
"fusenapi/server/product-templatev2/internal/svc"
"fusenapi/server/product-templatev2/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")
}
return resp.SetStatus(basic.CodeOK)
}

View File

@ -42,20 +42,39 @@ type SaveBaseMapReq struct {
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 []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 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"`

View File

@ -1,7 +1,7 @@
syntax = "v1"
info (
title: "产品模板"// TODO: add title
title: "产品模板底图服务"// TODO: add title
desc: // TODO: add description
author: ""
email: ""
@ -16,10 +16,15 @@ service product-templatev2 {
//获取底图列表
@handler GetBaseMapListHandler
get /product-template/base-map-list( ) returns (response);
//保存底图列表信息
//底图批量保存
@handler SaveBaseMapHandler
post /product-template/base-map-update ( ) returns (response);
//新增底图
@handler AddBaseMapHandler
post /product-template/base-map-add (AddBaseMapReq) returns (response);
//更新模板
@handler UpdateTemplateHandler
post /product-template/update-template (UpdateTemplateReq) returns (response);
}
//获取产品模板详情
@ -57,4 +62,26 @@ type SaveBaseMapReq {
Name string `json:"name"`
Url string `json:"url"`
Ctime string `json:"ctime"`
}
//新增底图
type AddBaseMapReq {
Name string `json:"name"`
Url string `json:"url"`
}
//更新模板
type UpdateTemplateReq {
ModelId int64 `json:"modelId"`
TemplateData TemplateData `json:"templateData"`
}
type TemplateData {
Id int64 `json:"id"`
Name string `json:"name"`
Cover string `json:"cover"`
IsPublic bool `json:"isPublic"`
Material string `json:"material"`
MaterialList []interface{} `json:"materialList"`
}
type UpdateTemplateRsp {
ModelId int64 `json:"modelId"`
TemplateId int64 `json:"templateId"`
}