Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop
This commit is contained in:
@@ -29,21 +29,3 @@ func main() {
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/backend.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
|
||||
// }
|
||||
|
||||
@@ -1,31 +1,38 @@
|
||||
package test
|
||||
package backendtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fusenapi/server/backend/internal/config"
|
||||
"fusenapi/server/backend/internal/handler"
|
||||
"fusenapi/server/backend/internal/svc"
|
||||
homeuserauthtest "fusenapi/server/home-user-auth/test"
|
||||
"fusenapi/utils/fstests"
|
||||
"log"
|
||||
"runtime"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var testConfigFile = "../etc/backend.yaml"
|
||||
var cnf config.Config
|
||||
var gserver *rest.Server
|
||||
var userver, gserver *rest.Server
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Llongfile)
|
||||
userver = homeuserauthtest.GetTestServer()
|
||||
gserver = GetTestServer()
|
||||
}
|
||||
|
||||
func GetTestServer() *rest.Server {
|
||||
|
||||
conf.MustLoad(testConfigFile, &cnf)
|
||||
conf.MustLoad(fstests.GetEtcYamlPathAuto(), &cnf)
|
||||
|
||||
server := rest.MustNewServer(cnf.RestConf)
|
||||
defer server.Stop()
|
||||
runtime.SetFinalizer(server, func(server *rest.Server) {
|
||||
if server != nil {
|
||||
server.Stop()
|
||||
}
|
||||
})
|
||||
|
||||
ctx := svc.NewServiceContext(cnf)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package backendtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package backendtest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
@@ -29,23 +29,3 @@ func main() {
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// 测试代码 可以直接复制新建
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/home-user-auth.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
|
||||
// }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fusenapi/server/home-user-auth/internal/handler"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/utils/fstests"
|
||||
"runtime"
|
||||
|
||||
"log"
|
||||
|
||||
@@ -13,7 +14,6 @@ import (
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var testConfigFile = "home-user-auth"
|
||||
var cnf config.Config
|
||||
var gserver *rest.Server
|
||||
|
||||
@@ -24,10 +24,14 @@ func init() {
|
||||
|
||||
func GetTestServer() *rest.Server {
|
||||
|
||||
conf.MustLoad(fstests.GetEtcYamlPathAuto(testConfigFile), &cnf)
|
||||
conf.MustLoad(fstests.GetEtcYamlPathAuto(), &cnf)
|
||||
|
||||
server := rest.MustNewServer(cnf.RestConf)
|
||||
defer server.Stop()
|
||||
runtime.SetFinalizer(server, func(server *rest.Server) {
|
||||
if server != nil {
|
||||
server.Stop()
|
||||
}
|
||||
})
|
||||
|
||||
ctx := svc.NewServiceContext(cnf)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package homeuserauthtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -29,21 +29,3 @@ func main() {
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/inventory.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
|
||||
// }
|
||||
|
||||
@@ -29,21 +29,3 @@ func main() {
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/map-library.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
|
||||
// }
|
||||
|
||||
@@ -29,21 +29,3 @@ func main() {
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/orders.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
|
||||
// }
|
||||
|
||||
@@ -29,21 +29,3 @@ func main() {
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/product-model.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
|
||||
// }
|
||||
|
||||
@@ -161,7 +161,7 @@ func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDeta
|
||||
optionModelInfoList := make([]interface{}, 0, len(model3dList))
|
||||
for _, v := range model3dList {
|
||||
info := make(map[string]interface{})
|
||||
if v.ModelInfo != nil {
|
||||
if v.ModelInfo != nil && *v.ModelInfo != "" {
|
||||
if err = json.Unmarshal([]byte(*v.ModelInfo), &info); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model info")
|
||||
|
||||
@@ -29,21 +29,3 @@ func main() {
|
||||
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
|
||||
// }
|
||||
|
||||
@@ -42,6 +42,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/product/info",
|
||||
Handler: GetProductInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/product/save-design",
|
||||
Handler: SaveDesignHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
78
server/product/internal/handler/savedesignhandler.go
Normal file
78
server/product/internal/handler/savedesignhandler.go
Normal file
@@ -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/internal/logic"
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
)
|
||||
|
||||
func SaveDesignHandler(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.SaveDesignReq
|
||||
// 如果端点有请求结构体,则使用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.NewSaveDesignLogic(r.Context(), svcCtx)
|
||||
resp := l.SaveDesign(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func (l *GetProductDesignLogic) GetProductDesign(req *types.GetProductDesignReq,
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductDesignRsp{
|
||||
ProductId: *designInfo.ProductId,
|
||||
TemplateId: *designInfo.TemplateId,
|
||||
MaterialId: *designInfo.MaterialId,
|
||||
//MaterialId: *designInfo.MaterialId,
|
||||
SizeId: *designInfo.SizeId,
|
||||
OptionalId: optionalId,
|
||||
Cover: *designInfo.Cover,
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"context"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/format"
|
||||
"fusenapi/utils/image"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
@@ -38,7 +27,7 @@ func NewGetProductInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
|
||||
func (l *GetProductInfoLogic) GetProductInfo(req *types.GetProductInfoReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
//获取产品信息
|
||||
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid)
|
||||
/*productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
|
||||
@@ -262,7 +251,8 @@ func (l *GetProductInfoLogic) GetProductInfo(req *types.GetProductInfoReq, useri
|
||||
temBytes, _ := json.Marshal(allModel3dList[key])
|
||||
_ = json.Unmarshal(temBytes, &thisInfo)
|
||||
}
|
||||
}
|
||||
}*/
|
||||
//**************************************************
|
||||
/*
|
||||
//循环处理组装模板信息
|
||||
foreach ($templates as $temp) {
|
||||
|
||||
168
server/product/internal/logic/savedesignlogic.go
Normal file
168
server/product/internal/logic/savedesignlogic.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/encryption_decryption"
|
||||
"fusenapi/utils/id_generator"
|
||||
"github.com/google/uuid"
|
||||
"github.com/nfnt/resize"
|
||||
"gorm.io/gorm"
|
||||
"image"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SaveDesignLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewSaveDesignLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveDesignLogic {
|
||||
return &SaveDesignLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SaveDesignLogic) SaveDesign(req *types.SaveDesignReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if userinfo.GetIdType() != auth.IDTYPE_User {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
|
||||
}
|
||||
//查询是否是加密的(不太合理)
|
||||
encryptWebsetting, err := l.svcCtx.AllModels.FsWebSet.FindValueByKey(l.ctx, "is_encrypt")
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "web setting is_encrypt is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get web setting")
|
||||
}
|
||||
var postInfo types.SaveDesignReqRealStruct
|
||||
//不加密
|
||||
if encryptWebsetting.Value == nil || *encryptWebsetting.Value == "0" {
|
||||
if err = json.Unmarshal([]byte(req.Data), &postInfo); err != nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json data,format may be invalid")
|
||||
}
|
||||
} else { //加密的
|
||||
//解密数据
|
||||
desData, err := encryption_decryption.CBCDecrypt(req.Data)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "failed to decryption data")
|
||||
}
|
||||
if err = json.Unmarshal([]byte(desData), &postInfo); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json data")
|
||||
}
|
||||
}
|
||||
infoBytes, _ := json.Marshal(postInfo.Data)
|
||||
info := string(infoBytes)
|
||||
now := time.Now()
|
||||
logoColorBytes, _ := json.Marshal(postInfo.Data.Logo.Colors)
|
||||
logoColor := string(logoColorBytes)
|
||||
saveData := gmodel.FsProductDesign{
|
||||
UserId: &userinfo.UserId,
|
||||
ProductId: &postInfo.ProductId,
|
||||
TemplateId: &postInfo.TemplateId,
|
||||
SizeId: &postInfo.SizeId,
|
||||
OptionalId: &postInfo.OptionalId,
|
||||
Cover: &postInfo.Cover,
|
||||
Info: &info,
|
||||
Utime: &now,
|
||||
LogoColor: &logoColor,
|
||||
PageGuid: &postInfo.PageGuid,
|
||||
}
|
||||
switch postInfo.Sn {
|
||||
case "": //新增
|
||||
status := int64(1)
|
||||
postInfo.Sn, err = id_generator.GenSnowFlakeId()
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to gen sn ")
|
||||
}
|
||||
saveData.Status = &status
|
||||
saveData.Sn = &postInfo.Sn
|
||||
err = l.svcCtx.AllModels.FsProductDesign.Create(l.ctx, &saveData)
|
||||
default: //更新
|
||||
err = l.svcCtx.AllModels.FsProductDesign.UpdateBySn(l.ctx, postInfo.Sn, &saveData)
|
||||
}
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to save design")
|
||||
}
|
||||
if postInfo.Cover == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.SaveDesignRsp{Sn: postInfo.Sn})
|
||||
}
|
||||
// TODO 图片待优化处理
|
||||
/*if err = l.CreateStepThumbnailImage(l.ctx, postInfo.Cover); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to create step thumbnail image ")
|
||||
}*/
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.SaveDesignRsp{Sn: postInfo.Sn})
|
||||
}
|
||||
|
||||
// 创建阶梯缩略图
|
||||
func (l *SaveDesignLogic) CreateStepThumbnailImage(ctx context.Context, coverImage string) error {
|
||||
httpRsp, err := http.Get(coverImage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer httpRsp.Body.Close()
|
||||
coverImg, _, err := image.Decode(httpRsp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
coverImgOrgWith := coverImg.Bounds().Dx()
|
||||
coverImgOrgHeight := coverImg.Bounds().Dy()
|
||||
fileExt := path.Ext(coverImage)
|
||||
for _, size := range constants.IMAGE_CROPPING_STEP_SIZE {
|
||||
//尺寸大于原图
|
||||
if size > coverImgOrgWith {
|
||||
continue
|
||||
}
|
||||
//缩放比例按照宽度来设定
|
||||
scale := size / coverImgOrgWith
|
||||
height := scale * coverImgOrgHeight
|
||||
tmpImage := resize.Resize(uint(size), uint(height), coverImg, resize.Lanczos3)
|
||||
fileName := fmt.Sprintf("%s_%d_%s", uuid.New().String(), size, fileExt)
|
||||
targetFile, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer targetFile.Close()
|
||||
switch fileExt {
|
||||
case ".png":
|
||||
err = png.Encode(targetFile, tmpImage)
|
||||
case ".jpg", ".jpeg":
|
||||
err = jpeg.Encode(targetFile, tmpImage, &jpeg.Options{Quality: 100})
|
||||
case ".gif":
|
||||
err = gif.Encode(targetFile, tmpImage, nil)
|
||||
default:
|
||||
err = errors.New("unSupport image format")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -88,7 +88,6 @@ type GetProductDesignReq struct {
|
||||
type GetProductDesignRsp struct {
|
||||
ProductId int64 `json:"product_id"`
|
||||
TemplateId int64 `json:"template_id"`
|
||||
MaterialId int64 `json:"material_id"`
|
||||
SizeId int64 `json:"size_id"`
|
||||
OptionalId int64 `json:"optional_id"`
|
||||
Cover string `json:"cover"`
|
||||
@@ -151,6 +150,75 @@ type MaterialItem struct {
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
type SaveDesignReq struct {
|
||||
Data string `json:"data"` //加密信息
|
||||
}
|
||||
|
||||
type SaveDesignRsp struct {
|
||||
Sn string `json:"sn"`
|
||||
}
|
||||
|
||||
type SaveDesignReqRealStruct struct {
|
||||
ProductId int64 `json:"product_id"`
|
||||
SizeId int64 `json:"size_id"`
|
||||
OptionalId int64 `json:"optional_id"`
|
||||
TemplateId int64 `json:"template_id"`
|
||||
Sn string `json:"sn"`
|
||||
Data DesignData `json:"data"`
|
||||
Cover string `json:"cover"`
|
||||
PageGuid string `json:"pageGuid"`
|
||||
}
|
||||
|
||||
type DesignData struct {
|
||||
MainColor ColorFill `json:"MainColor"`
|
||||
SecondaryColor ColorFill `json:"SecondaryColor"`
|
||||
Logo DesignLogo `json:"Logo"`
|
||||
Slogan DesignSlogan `json:"Slogan"`
|
||||
QRcode DesignQRcode `json:"QRcode"`
|
||||
Website DesignWebsite `json:"Website"`
|
||||
Phone DesignPhone `json:"Phone"`
|
||||
Address DesignAddress `json:"Address"`
|
||||
}
|
||||
|
||||
type DesignAddress struct {
|
||||
Text string `json:"text"`
|
||||
IfShow bool `json:"ifShow"`
|
||||
}
|
||||
|
||||
type DesignPhone struct {
|
||||
Text string `json:"text"`
|
||||
IfShow bool `json:"ifShow"`
|
||||
}
|
||||
|
||||
type DesignWebsite struct {
|
||||
Text string `json:"text"`
|
||||
IfShow bool `json:"ifShow"`
|
||||
}
|
||||
|
||||
type DesignQRcode struct {
|
||||
Text string `json:"text"`
|
||||
SvgPath string `json:"svgPath"`
|
||||
IfShow bool `json:"ifShow"`
|
||||
}
|
||||
|
||||
type DesignSlogan struct {
|
||||
Text string `json:"text"`
|
||||
IfShow bool `json:"ifShow"`
|
||||
}
|
||||
|
||||
type DesignLogo struct {
|
||||
Material string `json:"material"`
|
||||
MaterialName string `json:"materialName"`
|
||||
MaterialTime string `json:"materialTime"`
|
||||
Fill string `json:"fill"`
|
||||
FillName string `json:"fill_name"`
|
||||
Colors []string `json:"colors"`
|
||||
}
|
||||
|
||||
type ColorFill struct {
|
||||
Fill string `json:"fill"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
|
||||
15
server/upload/etc/upload.yaml
Normal file
15
server/upload/etc/upload.yaml
Normal file
@@ -0,0 +1,15 @@
|
||||
Name: upload
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
SourceMysql: "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest"
|
||||
Env: "test"
|
||||
Auth:
|
||||
AccessSecret: fusen2023
|
||||
AccessExpire: 604800
|
||||
RefreshAfter: 345600
|
||||
AWS:
|
||||
S3:
|
||||
Credentials:
|
||||
AccessKeyID: AKIAZB2JKUXDPNRP4YT2
|
||||
Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm
|
||||
Token:
|
||||
23
server/upload/internal/config/config.go
Normal file
23
server/upload/internal/config/config.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fusenapi/server/upload/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
Env string
|
||||
AWS struct {
|
||||
S3 struct {
|
||||
Credentials struct {
|
||||
AccessKeyID string
|
||||
Secret string
|
||||
Token string
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
server/upload/internal/handler/routes.go
Normal file
32
server/upload/internal/handler/routes.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/upload/up-file",
|
||||
Handler: UploadUpFileHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/upload/upload-file-frontend",
|
||||
Handler: UploadFileFrontendHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/upload/upload-file-backend",
|
||||
Handler: UploadFileBackendHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
89
server/upload/internal/handler/uploadfilebackendhandler.go
Normal file
89
server/upload/internal/handler/uploadfilebackendhandler.go
Normal file
@@ -0,0 +1,89 @@
|
||||
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/upload/internal/logic"
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
"fusenapi/server/upload/internal/types"
|
||||
)
|
||||
|
||||
func UploadFileBackendHandler(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.RequestUploadFileBackend
|
||||
// 如果端点有请求结构体,则使用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
|
||||
}
|
||||
|
||||
// 解析upload文件类型
|
||||
err = basic.RequestFileParse(r, &req)
|
||||
if err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||
Code: 510,
|
||||
Message: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUploadFileBackendLogic(r.Context(), svcCtx)
|
||||
resp := l.UploadFileBackend(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
78
server/upload/internal/handler/uploadfilefrontendhandler.go
Normal file
78
server/upload/internal/handler/uploadfilefrontendhandler.go
Normal file
@@ -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/upload/internal/logic"
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
"fusenapi/server/upload/internal/types"
|
||||
)
|
||||
|
||||
func UploadFileFrontendHandler(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.RequestUploadFileFrontend
|
||||
// 如果端点有请求结构体,则使用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.NewUploadFileFrontendLogic(r.Context(), svcCtx)
|
||||
resp := l.UploadFileFrontend(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
78
server/upload/internal/handler/uploadupfilehandler.go
Normal file
78
server/upload/internal/handler/uploadupfilehandler.go
Normal file
@@ -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/upload/internal/logic"
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
"fusenapi/server/upload/internal/types"
|
||||
)
|
||||
|
||||
func UploadUpFileHandler(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.RequestUpFile
|
||||
// 如果端点有请求结构体,则使用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.NewUploadUpFileLogic(r.Context(), svcCtx)
|
||||
resp := l.UploadUpFile(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
83
server/upload/internal/logic/uploadfilebackendlogic.go
Normal file
83
server/upload/internal/logic/uploadfilebackendlogic.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/check"
|
||||
"fusenapi/utils/format"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
"fusenapi/server/upload/internal/types"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UploadFileBackendLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUploadFileBackendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadFileBackendLogic {
|
||||
return &UploadFileBackendLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UploadFileBackendLogic) UploadFileBackend(req *types.RequestUploadFileBackend, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
if userinfo.IsOnlooker() {
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
|
||||
var uid int64
|
||||
var keytype format.TypeFormatS3KeyName
|
||||
if userinfo.IsGuest() {
|
||||
uid = userinfo.GuestId
|
||||
keytype = format.TypeS3KeyGuest
|
||||
} else {
|
||||
uid = userinfo.UserId
|
||||
keytype = format.TypeS3KeyUser
|
||||
}
|
||||
|
||||
l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1")
|
||||
svc := s3.New(l.svcCtx.AwsSession)
|
||||
|
||||
if !check.CheckCategory(req.Category) {
|
||||
return resp.SetStatus(basic.CodeS3CategoryErr)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
s3req, _ := svc.PutObjectRequest(
|
||||
&s3.PutObjectInput{
|
||||
Bucket: aws.String("storage.fusenpack.com"),
|
||||
Key: aws.String(format.FormatS3KeyName(
|
||||
keytype,
|
||||
uid,
|
||||
now,
|
||||
l.svcCtx.Config.Env,
|
||||
req.Category,
|
||||
req.File.Filename,
|
||||
)),
|
||||
},
|
||||
)
|
||||
|
||||
s3req.SetBufferBody(req.File.Data)
|
||||
err := s3req.Send()
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeS3PutObjectRequestErr)
|
||||
}
|
||||
|
||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||
"upload_url": s3req.HTTPRequest.URL.String(),
|
||||
})
|
||||
|
||||
}
|
||||
88
server/upload/internal/logic/uploadfilefrontendlogic.go
Normal file
88
server/upload/internal/logic/uploadfilefrontendlogic.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/check"
|
||||
"fusenapi/utils/format"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
"fusenapi/server/upload/internal/types"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UploadFileFrontendLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUploadFileFrontendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadFileFrontendLogic {
|
||||
return &UploadFileFrontendLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UploadFileFrontendLogic) UploadFileFrontend(req *types.RequestUploadFileFrontend, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
if userinfo.IsOnlooker() {
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
|
||||
var uid int64
|
||||
var keytype format.TypeFormatS3KeyName
|
||||
if userinfo.IsGuest() {
|
||||
uid = userinfo.GuestId
|
||||
keytype = format.TypeS3KeyGuest
|
||||
} else {
|
||||
uid = userinfo.UserId
|
||||
keytype = format.TypeS3KeyUser
|
||||
}
|
||||
|
||||
l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1")
|
||||
svc := s3.New(l.svcCtx.AwsSession)
|
||||
|
||||
if req.FileSize > 1024*1024*500 {
|
||||
return resp.SetStatus(basic.CodeS3PutSizeLimitErr)
|
||||
}
|
||||
|
||||
if !check.CheckCategory(req.Category) {
|
||||
return resp.SetStatus(basic.CodeS3CategoryErr)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
s3req, _ := svc.PutObjectRequest(
|
||||
&s3.PutObjectInput{
|
||||
Bucket: aws.String("storage.fusenpack.com"),
|
||||
Key: aws.String(format.FormatS3KeyName(
|
||||
keytype,
|
||||
uid,
|
||||
now,
|
||||
l.svcCtx.Config.Env,
|
||||
req.Category,
|
||||
req.FileName,
|
||||
)),
|
||||
ContentLength: aws.Int64(req.FileSize),
|
||||
},
|
||||
)
|
||||
|
||||
uri, err := s3req.Presign(time.Minute * 5)
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeS3PutObjectRequestErr)
|
||||
}
|
||||
// log.Println(uri)
|
||||
|
||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||
"upload_url": uri,
|
||||
})
|
||||
}
|
||||
34
server/upload/internal/logic/uploadupfilelogic.go
Normal file
34
server/upload/internal/logic/uploadupfilelogic.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
"fusenapi/server/upload/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UploadUpFileLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUploadUpFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadUpFileLogic {
|
||||
return &UploadUpFileLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UploadUpFileLogic) UploadUpFile(req *types.RequestUpFile, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
71
server/upload/internal/svc/servicecontext.go
Normal file
71
server/upload/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/upload/internal/config"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/model/gmodel"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/golang-jwt/jwt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
AwsSession *session.Session
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
config := aws.Config{
|
||||
Credentials: credentials.NewStaticCredentials(c.AWS.S3.Credentials.AccessKeyID, c.AWS.S3.Credentials.Secret, c.AWS.S3.Credentials.Token),
|
||||
}
|
||||
|
||||
// config.Region = aws.String("us-west-1")
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
93
server/upload/internal/types/types.go
Normal file
93
server/upload/internal/types/types.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type RequestUpFile struct {
|
||||
UpFile string `form:"upfile"`
|
||||
IsCut string `form:"is_cut"` // 是否裁剪
|
||||
}
|
||||
|
||||
type RequestUploadFileFrontend struct {
|
||||
FileName string `json:"file_name"` // 文件名
|
||||
FileType string `json:"file_type"` // Image / fbx / hdr
|
||||
FileSize int64 `json:"file_size"` // 文件大小
|
||||
Category string `json:"category"` // 类别
|
||||
}
|
||||
|
||||
type RequestUploadFileBackend struct {
|
||||
File File `file:"file"` // 文件名
|
||||
FileType string `form:"file_type"` // Image / fbx / hdr
|
||||
Category string `form:"category"` // 类别
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Filename string `fsfile:"filename"`
|
||||
Header map[string][]string `fsfile:"header"`
|
||||
Size int64 `fsfile:"size"`
|
||||
Data []byte `fsfile:"data"`
|
||||
}
|
||||
|
||||
type Meta struct {
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
PageCount int64 `json:"pageCount"`
|
||||
CurrentPage int `json:"currentPage"`
|
||||
PerPage int `json:"perPage"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -2,36 +2,38 @@ package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fusenapi/server/home-user-auth/test"
|
||||
homeuserauthtest "fusenapi/server/home-user-auth/test"
|
||||
"fusenapi/server/upload/internal/config"
|
||||
"fusenapi/server/upload/internal/handler"
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
"fusenapi/utils/fstests"
|
||||
"log"
|
||||
"runtime"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var env = "test"
|
||||
|
||||
var testConfigFile = "upload.yaml"
|
||||
var cnf config.Config
|
||||
var userServer *rest.Server
|
||||
var gserver *rest.Server
|
||||
|
||||
var userver, gserver *rest.Server
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Llongfile)
|
||||
userServer = test.GetTestServer()
|
||||
userver = homeuserauthtest.GetTestServer()
|
||||
gserver = GetTestServer()
|
||||
}
|
||||
|
||||
func GetTestServer() *rest.Server {
|
||||
|
||||
conf.MustLoad(fstests.GetEtcYamlPathAuto(fstests.GetCurrentServiceName()+".yaml"), &cnf)
|
||||
|
||||
// log.Println(fstests.GetEtcYamlPathAuto())
|
||||
conf.MustLoad(fstests.GetEtcYamlPathAuto(), &cnf)
|
||||
server := rest.MustNewServer(cnf.RestConf)
|
||||
defer server.Stop()
|
||||
runtime.SetFinalizer(server, func(server *rest.Server) {
|
||||
if server != nil {
|
||||
server.Stop()
|
||||
}
|
||||
})
|
||||
|
||||
ctx := svc.NewServiceContext(cnf)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestCaseUploadfileFrontend(t *testing.T) {
|
||||
var result gjson.Result
|
||||
|
||||
// 获取 session,并携带 JWT token
|
||||
ses := fstests.GetSessionWithUserToken(t, userServer, cnf.Host, cnf.Port)
|
||||
ses := fstests.GetSessionWithUserToken(t, userver, cnf.Host, cnf.Port)
|
||||
tp := ses.Post(fmt.Sprintf("http://%s:%d/upload/upload-file-frontend", cnf.Host, cnf.Port))
|
||||
|
||||
data, err := ioutil.ReadFile("./fusen.webp")
|
||||
@@ -27,7 +27,7 @@ func TestCaseUploadfileFrontend(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
req := types.RequestUploadFile{
|
||||
req := types.RequestUploadFileFrontend{
|
||||
FileName: "fusen.webp", FileType: "image", FileSize: int64(len(data)),
|
||||
Category: "personalization",
|
||||
}
|
||||
@@ -55,13 +55,14 @@ func TestCaseUploadfileFrontend(t *testing.T) {
|
||||
uri := result.Get("data.upload_url").String()
|
||||
|
||||
tpUpload := requests.NewSession().Put(uri)
|
||||
|
||||
tpUpload.SetBodyStream(data)
|
||||
|
||||
resp, err = tpUpload.Execute()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
log.Println(resp.Json())
|
||||
if resp.GetStatusCode() != 200 {
|
||||
t.Error("put failed")
|
||||
}
|
||||
}
|
||||
|
||||
31
server/upload/upload.go
Normal file
31
server/upload/upload.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"fusenapi/server/upload/internal/config"
|
||||
"fusenapi/server/upload/internal/handler"
|
||||
"fusenapi/server/upload/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/upload.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()
|
||||
}
|
||||
7
server/upload/upload_test.go
Normal file
7
server/upload/upload_test.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
main()
|
||||
}
|
||||
@@ -1,31 +1,38 @@
|
||||
package logic
|
||||
package wetset_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
homeuserauthtest "fusenapi/server/home-user-auth/test"
|
||||
"fusenapi/server/webset/internal/config"
|
||||
"fusenapi/server/webset/internal/handler"
|
||||
"fusenapi/server/webset/internal/svc"
|
||||
"fusenapi/utils/fstests"
|
||||
"log"
|
||||
"runtime"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var testConfigFile = "../etc/wetset.yaml"
|
||||
var cnf config.Config
|
||||
var gserver *rest.Server
|
||||
var userver, gserver *rest.Server
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Llongfile)
|
||||
userver = homeuserauthtest.GetTestServer()
|
||||
gserver = GetTestServer()
|
||||
}
|
||||
|
||||
func GetTestServer() *rest.Server {
|
||||
|
||||
conf.MustLoad(testConfigFile, &cnf)
|
||||
conf.MustLoad(fstests.GetEtcYamlPathAuto(), &cnf)
|
||||
|
||||
server := rest.MustNewServer(cnf.RestConf)
|
||||
defer server.Stop()
|
||||
runtime.SetFinalizer(server, func(server *rest.Server) {
|
||||
if server != nil {
|
||||
server.Stop()
|
||||
}
|
||||
})
|
||||
|
||||
ctx := svc.NewServiceContext(cnf)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package logic
|
||||
package wetset_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -29,21 +29,3 @@ func main() {
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
|
||||
// var testConfigFile = flag.String("f", "../etc/wetset.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
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user