Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into feature/auth
This commit is contained in:
commit
e565565627
5
constants/blm_service.go
Normal file
5
constants/blm_service.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package constants
|
||||
|
||||
const (
|
||||
BLMServiceUrlLogoCombine string = "/LogoCombine"
|
||||
)
|
24
initalize/service.go
Normal file
24
initalize/service.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package initalize
|
||||
|
||||
import (
|
||||
"fusenapi/service/repositories"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Repositories struct {
|
||||
ImageHandle repositories.ImageHandle
|
||||
}
|
||||
|
||||
type NewAllRepositorieData struct {
|
||||
GormDB *gorm.DB
|
||||
BLMServiceUrl *string
|
||||
AwsSession *session.Session
|
||||
}
|
||||
|
||||
func NewAllRepositories(newData *NewAllRepositorieData) *Repositories {
|
||||
return &Repositories{
|
||||
ImageHandle: repositories.NewImageHandle(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession),
|
||||
}
|
||||
}
|
|
@ -1,8 +1,15 @@
|
|||
package gmodel
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"fusenapi/utils/handlers"
|
||||
"reflect"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TODO: 使用model的属性做你想做的
|
||||
|
||||
func (m *FsMerchantCategoryModel) FindOne(ctx context.Context, id int64) (resp *FsMerchantCategory, err error) {
|
||||
err = m.db.WithContext(ctx).Model(&FsMerchantCategory{}).Where("id = ? and status = ?", id, 1).Take(&resp).Error
|
||||
return resp, err
|
||||
|
@ -11,3 +18,37 @@ func (m *FsMerchantCategoryModel) FindRandOne(ctx context.Context) (resp *FsMerc
|
|||
err = m.db.WithContext(ctx).Model(&FsMerchantCategory{}).Where("status = ?", 1).Order("RAND()").Take(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (m *FsMerchantCategoryModel) BuilderDB(ctx context.Context, selectData []string) *gorm.DB {
|
||||
if selectData != nil {
|
||||
return m.db.WithContext(ctx).Select(selectData)
|
||||
} else {
|
||||
return m.db.WithContext(ctx).Select("*")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *FsMerchantCategoryModel) FindAll(gormDB *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsMerchantCategory, error) {
|
||||
var resp []*FsMerchantCategory
|
||||
|
||||
// 过滤
|
||||
if filterMap != nil {
|
||||
gormDB = gormDB.Scopes(handlers.FilterData(filterMap))
|
||||
}
|
||||
|
||||
// 排序
|
||||
if orderBy != "" {
|
||||
var fieldsMap = make(map[string]struct{})
|
||||
s := reflect.TypeOf(&FsOrder{}).Elem() //通过反射获取type定义
|
||||
for i := 0; i < s.NumField(); i++ {
|
||||
fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{}
|
||||
}
|
||||
gormDB = gormDB.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
|
||||
}
|
||||
|
||||
result := gormDB.Find(&resp)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
} else {
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,9 +8,10 @@ import (
|
|||
type FsProductTemplateTags struct {
|
||||
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID
|
||||
Title *string `gorm:"default:'';" json:"title"` // 标题
|
||||
CoverImg *string `gorm:"default:'';" json:"cover_img"` // 封面图
|
||||
Cover *string `gorm:"default:'';" json:"cover"` // 封面图
|
||||
Status *int64 `gorm:"default:0;" json:"status"` // 状态 1:可用
|
||||
CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 创建时间
|
||||
Groups *string `gorm:"default:'';" json:"groups"` // 分组信息
|
||||
}
|
||||
type FsProductTemplateTagsModel struct {
|
||||
db *gorm.DB
|
||||
|
|
|
@ -32,3 +32,14 @@ func (pt *FsProductTemplateTagsModel) GetList(ctx context.Context, page, limit i
|
|||
err = db.Offset(offset).Limit(limit).Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
func (pt *FsProductTemplateTagsModel) GetListByTitles(ctx context.Context, titles []string, limit int, orderBy string) (resp []FsProductTemplateTags, err error) {
|
||||
if len(titles) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
db := pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where("`title` in (?) and `status` = ?", titles, 1)
|
||||
if orderBy != "" {
|
||||
db = db.Order(orderBy)
|
||||
}
|
||||
err = db.Limit(limit).Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
|
25
model/gmodel/fs_user_info_gen.go
Normal file
25
model/gmodel/fs_user_info_gen.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package gmodel
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// fs_user_info 用户信息表
|
||||
type FsUserInfo struct {
|
||||
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID
|
||||
Module *string `gorm:"default:'';" json:"module"` // 所属模块
|
||||
UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID
|
||||
GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID
|
||||
Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式
|
||||
MetaKey1 *string `gorm:"default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加
|
||||
Ctime *int64 `gorm:"default:0;" json:"ctime"` //
|
||||
Utime *int64 `gorm:"default:0;" json:"utime"` //
|
||||
}
|
||||
type FsUserInfoModel struct {
|
||||
db *gorm.DB
|
||||
name string
|
||||
}
|
||||
|
||||
func NewFsUserInfoModel(db *gorm.DB) *FsUserInfoModel {
|
||||
return &FsUserInfoModel{db: db, name: "fs_user_info"}
|
||||
}
|
44
model/gmodel/fs_user_info_logic.go
Normal file
44
model/gmodel/fs_user_info_logic.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package gmodel
|
||||
|
||||
// TODO: 使用model的属性做你想做的
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fusenapi/utils/handlers"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TODO: 使用model的属性做你想做的
|
||||
|
||||
func (m *FsUserInfoModel) BuilderDB(ctx context.Context, selectData []string) *gorm.DB {
|
||||
if selectData != nil {
|
||||
return m.db.WithContext(ctx).Select(selectData)
|
||||
} else {
|
||||
return m.db.WithContext(ctx).Select("*")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *FsUserInfoModel) FindOne(gormDB *gorm.DB, filterMap map[string]string) (*FsUserInfo, error) {
|
||||
var resp FsUserInfo
|
||||
|
||||
if filterMap != nil {
|
||||
gormDB = gormDB.Scopes(handlers.FilterData(filterMap))
|
||||
}
|
||||
|
||||
result := gormDB.Limit(1).Find(&resp)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
} else {
|
||||
return &resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *FsUserInfoModel) CreateOrUpdate(gormDB *gorm.DB, req *FsUserInfo) (resp *FsUserInfo, err error) {
|
||||
if req.Id > 0 {
|
||||
err = gormDB.Save(req).Error
|
||||
} else {
|
||||
err = gormDB.Create(req).Error
|
||||
}
|
||||
return req, err
|
||||
}
|
|
@ -61,12 +61,9 @@ func (m *FsUserMaterialModel) FindLatestOne(ctx context.Context, userId int64, g
|
|||
if userId == 0 && guestId == 0 {
|
||||
return FsUserMaterial{}, nil
|
||||
}
|
||||
db := m.db.WithContext(ctx).Model(&FsUserMaterial{}).Order("id DESC")
|
||||
if userId != 0 {
|
||||
db = db.Where("`user_id` = ?", userId)
|
||||
} else {
|
||||
db = db.Where("`guest_id` = ?", guestId)
|
||||
}
|
||||
db := m.db.WithContext(ctx).Model(&FsUserMaterial{}).
|
||||
Where("`user_id` = ? and `guest_id` = ?", userId, guestId).
|
||||
Order("id DESC")
|
||||
err = db.Take(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
@ -75,3 +72,8 @@ func (m *FsUserMaterialModel) FindOneById(ctx context.Context, id int64) (resp *
|
|||
err = m.db.WithContext(ctx).Model(&FsUserMaterial{}).Where("id = ?", id).Take(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
func (m *FsUserMaterialModel) GetListByUser(ctx context.Context, userId, guestId int64, limit int) (resp *FsUserMaterial, err error) {
|
||||
err = m.db.WithContext(ctx).Model(&FsUserMaterial{}).
|
||||
Where("`user_id` = ? and `guest_id` = ?", userId, guestId).Order("id DESC").Limit(limit).Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -90,6 +90,7 @@ type AllModelsGen struct {
|
|||
FsTrade *FsTradeModel // fs_trade
|
||||
FsUser *FsUserModel // fs_user 用户表
|
||||
FsUserDesign *FsUserDesignModel // fs_user_design 废弃表
|
||||
FsUserInfo *FsUserInfoModel // fs_user_info 用户信息表
|
||||
FsUserMaterial *FsUserMaterialModel // fs_user_material 用户素材表
|
||||
FsUserStock *FsUserStockModel // fs_user_stock 用户云仓库存
|
||||
FsWebSet *FsWebSetModel // fs_web_set 网站配置表
|
||||
|
@ -184,6 +185,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
|
|||
FsTrade: NewFsTradeModel(gdb),
|
||||
FsUser: NewFsUserModel(gdb),
|
||||
FsUserDesign: NewFsUserDesignModel(gdb),
|
||||
FsUserInfo: NewFsUserInfoModel(gdb),
|
||||
FsUserMaterial: NewFsUserMaterialModel(gdb),
|
||||
FsUserStock: NewFsUserStockModel(gdb),
|
||||
FsWebSet: NewFsWebSetModel(gdb),
|
||||
|
|
36
server/base/base.go
Normal file
36
server/base/base.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"fusenapi/utils/auth"
|
||||
|
||||
"fusenapi/server/base/internal/config"
|
||||
"fusenapi/server/base/internal/handler"
|
||||
"fusenapi/server/base/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/base.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
c.Timeout = int64(time.Second * 15)
|
||||
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
|
||||
}))
|
||||
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()
|
||||
}
|
9
server/base/base_test.go
Normal file
9
server/base/base_test.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
main()
|
||||
}
|
10
server/base/etc/base.yaml
Normal file
10
server/base/etc/base.yaml
Normal file
|
@ -0,0 +1,10 @@
|
|||
Name: base
|
||||
Host: 0.0.0.0
|
||||
Port: 9920
|
||||
Timeout: 15000 #服务超时时间(毫秒)
|
||||
SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
|
||||
Auth:
|
||||
AccessSecret: fusen2023
|
||||
AccessExpire: 2592000
|
||||
RefreshAfter: 1592000
|
||||
SourceRabbitMq: amqp://rabbit001:rabbit001129@110.41.19.98:5672
|
14
server/base/internal/config/config.go
Normal file
14
server/base/internal/config/config.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fusenapi/server/base/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
SourceRabbitMq string
|
||||
}
|
35
server/base/internal/handler/merchantcategorylisthandler.go
Normal file
35
server/base/internal/handler/merchantcategorylisthandler.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/base/internal/logic"
|
||||
"fusenapi/server/base/internal/svc"
|
||||
"fusenapi/server/base/internal/types"
|
||||
)
|
||||
|
||||
func MerchantCategoryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.MerchantCategoryListReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewMerchantCategoryListLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.MerchantCategoryList(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
22
server/base/internal/handler/routes.go
Normal file
22
server/base/internal/handler/routes.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/server/base/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/base/merchant_category_list",
|
||||
Handler: MerchantCategoryListHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
53
server/base/internal/logic/merchantcategorylistlogic.go
Normal file
53
server/base/internal/logic/merchantcategorylistlogic.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/base/internal/svc"
|
||||
"fusenapi/server/base/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type MerchantCategoryListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewMerchantCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MerchantCategoryListLogic {
|
||||
return &MerchantCategoryListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *MerchantCategoryListLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *MerchantCategoryListLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *MerchantCategoryListLogic) MerchantCategoryList(req *types.MerchantCategoryListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
fsMerchantCategoryModel := gmodel.NewFsMerchantCategoryModel(l.svcCtx.MysqlConn)
|
||||
BuilderDB := fsMerchantCategoryModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsMerchantCategory{})
|
||||
resourceInfo, err := fsMerchantCategoryModel.FindAll(BuilderDB, nil, "sort desc")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr, "MerchantCategoryList error system failed")
|
||||
}
|
||||
// 返回成功
|
||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||
"list": resourceInfo,
|
||||
})
|
||||
}
|
62
server/base/internal/svc/servicecontext.go
Normal file
62
server/base/internal/svc/servicecontext.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package svc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/base/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
|
||||
RabbitMq *initalize.RabbitMqHandle
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if AuthKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
AuthKey = AuthKey[7:]
|
||||
|
||||
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))
|
||||
}
|
78
server/base/internal/types/types.go
Normal file
78
server/base/internal/types/types.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type MerchantCategoryListReq struct {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
|
@ -82,6 +82,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Path: "/api/user/one-more-order",
|
||||
Handler: UserAgainOrderHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/user/set_user_info",
|
||||
Handler: UserInfoSetHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
35
server/home-user-auth/internal/handler/userinfosethandler.go
Normal file
35
server/home-user-auth/internal/handler/userinfosethandler.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/logic"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
)
|
||||
|
||||
func UserInfoSetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.UserInfoSetReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUserInfoSetLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.UserInfoSet(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
95
server/home-user-auth/internal/logic/userinfosetlogic.go
Normal file
95
server/home-user-auth/internal/logic/userinfosetlogic.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/validate"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserInfoSetLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserInfoSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoSetLogic {
|
||||
return &UserInfoSetLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *UserInfoSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *UserInfoSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *UserInfoSetLogic) UserInfoSet(req *types.UserInfoSetReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
if userinfo.IsOnlooker() {
|
||||
// 如果是,返回未授权的错误码
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
|
||||
var userId int64
|
||||
var guestId int64
|
||||
|
||||
// 检查用户是否是游客
|
||||
if userinfo.IsGuest() {
|
||||
// 如果是,使用游客ID和游客键名格式
|
||||
guestId = userinfo.GuestId
|
||||
} else {
|
||||
// 否则,使用用户ID和用户键名格式
|
||||
userId = userinfo.UserId
|
||||
}
|
||||
|
||||
_, err := validate.Validate(&req.Module, &req.Metadata)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeRequestParamsErr, "UserInfoSet error Validate failed")
|
||||
}
|
||||
|
||||
// 根据模块类型检查数据
|
||||
var userInfo = &gmodel.FsUserInfo{}
|
||||
fsUserInfoModel := gmodel.NewFsUserInfoModel(l.svcCtx.MysqlConn)
|
||||
BuilderDB := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{})
|
||||
BuilderDB1 := BuilderDB.Where("module = ?", req.Module).Where("user_id=?", userId).Where("guest_id=?", guestId)
|
||||
userInfo, err = fsUserInfoModel.FindOne(BuilderDB1, nil)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
|
||||
}
|
||||
var nowTime = time.Now().Unix()
|
||||
if userInfo.Id != 0 {
|
||||
userInfo.Metadata = &req.Metadata
|
||||
userInfo.Utime = &nowTime
|
||||
} else {
|
||||
userInfo.GuestId = &guestId
|
||||
userInfo.UserId = &userId
|
||||
userInfo.Module = &req.Module
|
||||
userInfo.Metadata = &req.Metadata
|
||||
userInfo.Ctime = &nowTime
|
||||
}
|
||||
_, err = fsUserInfoModel.CreateOrUpdate(BuilderDB, userInfo)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
|
||||
}
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
|
@ -5,6 +5,11 @@ import (
|
|||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type UserInfoSetReq struct {
|
||||
Module string `form:"module,options=[merchant_category]"` // json格式字符串
|
||||
Metadata string `form:"metadata"` // json格式字符串
|
||||
}
|
||||
|
||||
type UserAgainOrderReq struct {
|
||||
Sn string `form:"sn"` // 订单编号
|
||||
}
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"context"
|
||||
|
||||
|
@ -37,9 +41,39 @@ func NewGetProductTemplateTagsLogic(ctx context.Context, svcCtx *svc.ServiceCont
|
|||
|
||||
func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProductTemplateTagsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Limit <= 0 || req.Limit > 100 {
|
||||
req.Limit = 4
|
||||
req.Limit = 5
|
||||
}
|
||||
var (
|
||||
productTemplateTags []gmodel.FsProductTemplateTags
|
||||
)
|
||||
//获取用户元数据
|
||||
userMaterial, err := l.svcCtx.AllModels.FsUserMaterial.GetListByUser(l.ctx, userinfo.UserId, userinfo.GuestId, req.Limit)
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get ai recommend product template tag list")
|
||||
}
|
||||
// 返回固定模板标签列表
|
||||
productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, "`id` DESC")
|
||||
} else {
|
||||
//元数据是空的
|
||||
if userMaterial.Metadata == nil || *userMaterial.Metadata == "" {
|
||||
// 返回固定模板标签列表
|
||||
productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, "`id` DESC")
|
||||
} else {
|
||||
//解析元数据
|
||||
var metaData map[string]interface{}
|
||||
if err = json.Unmarshal([]byte(*userMaterial.Metadata), &metaData); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse user metadata")
|
||||
}
|
||||
templateTagNameList, ok := metaData["template_tagid"].([]string)
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "invalid format of metadata`template_tagid")
|
||||
}
|
||||
productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTitles(l.ctx, templateTagNameList, req.Limit, "id DESC")
|
||||
}
|
||||
}
|
||||
productTemplateTags, err := l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, "`id` DESC")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tags")
|
||||
|
@ -49,7 +83,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu
|
|||
list = append(list, types.GetProductTemplateTagsRsp{
|
||||
Id: v.Id,
|
||||
Tag: *v.Title,
|
||||
Cover: *v.CoverImg,
|
||||
Cover: *v.Cover,
|
||||
})
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
|
||||
|
|
|
@ -88,7 +88,7 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq,
|
|||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list")
|
||||
}
|
||||
rsp := make(map[string][]interface{})
|
||||
rsp := make(map[string]interface{})
|
||||
for _, templateInfo := range templateList {
|
||||
//没有设置模板据不要
|
||||
if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
|
||||
|
@ -99,7 +99,7 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq,
|
|||
continue
|
||||
}
|
||||
//基础模板信息
|
||||
var info map[string]map[string]interface{}
|
||||
var info interface{}
|
||||
if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse json product template info(may be old data):%d", templateInfo.Id))
|
||||
|
@ -110,16 +110,12 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq,
|
|||
_ = json.Unmarshal([]byte(*templateInfo.SwitchInfo), &switchInfo)
|
||||
}
|
||||
modelInfo := modelList[modelIndex]
|
||||
var material interface{}
|
||||
if info["module_data"] != nil && info["module_data"]["material"] != nil {
|
||||
material = info["module_data"]["material"]
|
||||
}
|
||||
mapKey := fmt.Sprintf("_%d", *modelInfo.SizeId)
|
||||
rsp[mapKey] = append(rsp[mapKey], map[string]interface{}{
|
||||
rsp[mapKey] = map[string]interface{}{
|
||||
"id": templateInfo.Id,
|
||||
"material": material,
|
||||
"material": *templateInfo.MaterialImg,
|
||||
"material_data": switchInfo,
|
||||
})
|
||||
}
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", rsp)
|
||||
}
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
package consumer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/server/render/internal/svc"
|
||||
"fusenapi/utils/curl"
|
||||
"fusenapi/utils/file"
|
||||
"fusenapi/service/repositories"
|
||||
"fusenapi/utils/hash"
|
||||
"fusenapi/utils/websocket_data"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
@ -55,7 +51,7 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
|
|||
return nil //不返回错误就删除消息
|
||||
}
|
||||
rabbitmq := initalize.RabbitMqHandle{}
|
||||
//获取模板(产品第一个sku的模板)
|
||||
//获取模板(模板标签下的对一个物料的的模板)
|
||||
productTemplate, err := svcCtx.AllModels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
@ -65,23 +61,45 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
|
|||
logx.Error("failed to get template info:", err)
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
combineImage := "" //刀版图
|
||||
combineHash := hash.JsonHashKey(parseInfo) //区别于云渲染的taskid,这个用获取刀版图缓存
|
||||
//获取该hash值下有没有对应的资源
|
||||
resource, err := svcCtx.AllModels.FsResource.FindOneById(ctx, combineHash)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logx.Error("failed to get resource :", err)
|
||||
return nil //不返回错误就删除消息
|
||||
time.Now().UTC()
|
||||
resourceKey := hash.JsonHashKey(parseInfo)
|
||||
combineParam := map[string]interface{}{
|
||||
"logo_url": parseInfo.RenderData.Logo,
|
||||
"website": "",
|
||||
"slogan": "",
|
||||
"address": "",
|
||||
"phone": "",
|
||||
"colors": []string{},
|
||||
"template_tagid": "b1a",
|
||||
"is_crop": false,
|
||||
"shape": "rectangle",
|
||||
"ratio": 0,
|
||||
"line": "",
|
||||
"other": "",
|
||||
"other1": "",
|
||||
}
|
||||
//如果不存在,则请求生成刀版图
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
combineImage, err = getCombineImage(ctx, svcCtx, parseInfo, productTemplate, combineHash)
|
||||
if err != nil {
|
||||
return nil //不返回错误就删除消息
|
||||
}
|
||||
combineParamBytes, _ := json.Marshal(combineParam)
|
||||
//获取刀版图
|
||||
res, err := svcCtx.Repositories.ImageHandle.LogoCombine(ctx, &repositories.LogoCombineReq{
|
||||
ResourceKey: resourceKey,
|
||||
TemplateId: productTemplate.Id,
|
||||
CombineParam: string(combineParamBytes),
|
||||
UserId: parseInfo.RenderData.UserId,
|
||||
GuestId: parseInfo.RenderData.GuestId,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logx.Error("合成刀版图失败:", err)
|
||||
return nil
|
||||
}
|
||||
combineImage := "" //刀版图
|
||||
if res != nil && res.ResourceUrl != nil {
|
||||
combineImage = *res.ResourceUrl
|
||||
} else {
|
||||
combineImage = *resource.ResourceUrl
|
||||
logx.Error("合成刀版图失败,合成的刀版图是空指针:", err)
|
||||
return nil
|
||||
}
|
||||
logx.Info("合成刀版图成功")
|
||||
//获取渲染设置信息
|
||||
element, err := svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(ctx, *productTemplate.ModelId)
|
||||
if err != nil {
|
||||
|
@ -167,106 +185,3 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
|
|||
logx.Info("发送渲染组装数据到unity成功")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取刀版图
|
||||
func getCombineImage(ctx context.Context, svcCtx *svc.ServiceContext, parseInfo websocket_data.AssembleRenderData, productTemplate *gmodel.FsProductTemplateV2, combineHash string) (image string, err error) {
|
||||
if productTemplate.TemplateInfo == nil || *productTemplate.TemplateInfo == "" {
|
||||
logx.Error("product template info`template_info is empty")
|
||||
return "", errors.New("product template info`template_info is empty")
|
||||
}
|
||||
//反序列化替换其中一些参数
|
||||
var combineInfo map[string]interface{}
|
||||
if err = json.Unmarshal([]byte(*productTemplate.TemplateInfo), &combineInfo); err != nil {
|
||||
logx.Error("failed to parse json:template_info:", err)
|
||||
return "", err
|
||||
}
|
||||
//需要替换的参数
|
||||
replaceData := map[string]interface{}{
|
||||
"logo_url": parseInfo.RenderData.Logo,
|
||||
"website": parseInfo.RenderData.Website,
|
||||
"slogan": parseInfo.RenderData.Slogan,
|
||||
"address": parseInfo.RenderData.Address,
|
||||
"phone": parseInfo.RenderData.Phone,
|
||||
"colors": []string{},
|
||||
"template_tagid": []string{"b1a"},
|
||||
"is_crop": false,
|
||||
"shape": "",
|
||||
"ratio": 0,
|
||||
"line": "",
|
||||
"other": "",
|
||||
"other1": "",
|
||||
}
|
||||
//获取用户素材信息
|
||||
if parseInfo.RenderData.UserMaterialId > 0 {
|
||||
userMaterial, err := svcCtx.AllModels.FsUserMaterial.FindOneById(ctx, parseInfo.RenderData.UserMaterialId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logx.Error("user material not exists:", parseInfo.RenderData.UserMaterialId)
|
||||
return "", errors.New("user material not exists")
|
||||
}
|
||||
logx.Error("err failed to get user material info")
|
||||
}
|
||||
if userMaterial.Metadata != nil && *userMaterial.Metadata != "" {
|
||||
//解析元数据
|
||||
var materialMetaData map[string]interface{}
|
||||
if err = json.Unmarshal([]byte(*userMaterial.Metadata), &materialMetaData); err != nil {
|
||||
logx.Error("failed to parse user material`matadata: ", err)
|
||||
return "", err
|
||||
}
|
||||
//赋值
|
||||
replaceData["colors"] = materialMetaData["colors"]
|
||||
replaceData["logo_url"] = materialMetaData["logo_url"]
|
||||
replaceData["shape"] = materialMetaData["shape"]
|
||||
replaceData["is_crop"] = materialMetaData["is_crop"]
|
||||
replaceData["ratio"] = materialMetaData["ratio"]
|
||||
replaceData["line"] = materialMetaData["line"]
|
||||
replaceData["other"] = materialMetaData["other"]
|
||||
replaceData["other1"] = materialMetaData["other1"]
|
||||
}
|
||||
}
|
||||
combineInfo["param_data"] = replaceData
|
||||
postData, _ := json.Marshal(combineInfo)
|
||||
//请求合成图片
|
||||
url := svcCtx.Config.PythonApi.CombineImageUrl
|
||||
header := make(map[string]string)
|
||||
header["content-type"] = "application/json"
|
||||
/*f, _ := os.Create("a.txt")
|
||||
defer f.Close()
|
||||
f.Write(postData)*/
|
||||
httpRsp, err := curl.ApiCall(url, "POST", header, bytes.NewReader(postData), time.Second*20)
|
||||
if err != nil {
|
||||
logx.Error("failed to combine logo:", err)
|
||||
return "", err
|
||||
}
|
||||
defer httpRsp.Body.Close()
|
||||
bytes, err := ioutil.ReadAll(httpRsp.Body)
|
||||
if err != nil {
|
||||
logx.Error("failed to read python api rsp body:", err)
|
||||
return "", err
|
||||
}
|
||||
var pythonApiInfo pythonApiRsp
|
||||
if err = json.Unmarshal(bytes, &pythonApiInfo); err != nil {
|
||||
logx.Error("failed to parse python api rsp:", err)
|
||||
return "", err
|
||||
}
|
||||
//fmt.Println("××××××××××××××××××××:", pythonApiInfo)
|
||||
//上传刀版图
|
||||
var upload = file.Upload{
|
||||
Ctx: ctx,
|
||||
MysqlConn: svcCtx.MysqlConn,
|
||||
AwsSession: svcCtx.AwsSession,
|
||||
}
|
||||
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
||||
FileHash: combineHash,
|
||||
FileData: pythonApiInfo.Result,
|
||||
UploadBucket: 1,
|
||||
ApiType: 2,
|
||||
UserId: parseInfo.RenderData.UserId,
|
||||
GuestId: parseInfo.RenderData.GuestId,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error("上传刀版图到s3失败:", err)
|
||||
return "", err
|
||||
}
|
||||
return uploadRes.ResourceUrl, nil
|
||||
}
|
||||
|
|
|
@ -14,5 +14,8 @@ AWS:
|
|||
AccessKeyID: AKIAZB2JKUXDPNRP4YT2
|
||||
Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm
|
||||
Token:
|
||||
PythonApi: #python接口
|
||||
CombineImageUrl: http://192.168.1.7:45678/LogoCombine #合成刀版图接口
|
||||
BLMService:
|
||||
Url: "http://18.119.109.254:8999"
|
||||
LogoCombine:
|
||||
#Url: "http://192.168.1.7:8999/LogoCombine"
|
||||
Url: "http://18.119.109.254:8999/LogoCombine"
|
|
@ -21,7 +21,10 @@ type Config struct {
|
|||
}
|
||||
}
|
||||
}
|
||||
PythonApi struct {
|
||||
CombineImageUrl string //合图url
|
||||
BLMService struct {
|
||||
Url string
|
||||
LogoCombine struct {
|
||||
Url string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,8 @@ type ServiceContext struct {
|
|||
AllModels *gmodel.AllModelsGen
|
||||
RabbitMq *initalize.RabbitMqHandle
|
||||
AwsSession *session.Session
|
||||
|
||||
Repositories *initalize.Repositories
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
@ -41,6 +43,11 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
|||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil),
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{
|
||||
GormDB: initalize.InitMysql(c.SourceMysql),
|
||||
BLMServiceUrl: &c.BLMService.Url,
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ AWS:
|
|||
Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm
|
||||
Token:
|
||||
BLMService:
|
||||
Url: "http://18.119.109.254:8999"
|
||||
LogoCombine:
|
||||
#Url: "http://192.168.1.7:8999/LogoCombine"
|
||||
Url: "http://18.119.109.254:8999/LogoCombine"
|
|
@ -21,6 +21,7 @@ type Config struct {
|
|||
}
|
||||
}
|
||||
BLMService struct {
|
||||
Url string
|
||||
LogoCombine struct {
|
||||
Url string
|
||||
}
|
||||
|
|
|
@ -1,17 +1,9 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/service/repositories"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/curl"
|
||||
"fusenapi/utils/file"
|
||||
"fusenapi/utils/hash"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
|
@ -19,7 +11,6 @@ import (
|
|||
"fusenapi/server/resource/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type LogoCombineLogic struct {
|
||||
|
@ -65,125 +56,22 @@ func (l *LogoCombineLogic) LogoCombine(req *types.LogoCombineReq, userinfo *auth
|
|||
// 否则,使用用户ID和用户键名格式
|
||||
userId = userinfo.UserId
|
||||
}
|
||||
|
||||
// 根据hash 查询数据资源
|
||||
var resourceId string = hash.JsonHashKey(req.ResourceKey)
|
||||
resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn)
|
||||
resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId)
|
||||
if err == nil && resourceInfo.ResourceId != "" {
|
||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||
"resource_id": resourceId,
|
||||
"resource_url": resourceInfo.ResourceUrl,
|
||||
"resource_metadata": resourceInfo.Metadata,
|
||||
})
|
||||
} else {
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 没有查到,先根据模版id 查询模版数据 请求算法数据
|
||||
productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
|
||||
productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, req.TemplateId)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error")
|
||||
}
|
||||
var groupOptions map[string]interface{}
|
||||
if productTemplateV2Info.GroupOptions != nil {
|
||||
err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal groupOptions")
|
||||
}
|
||||
}
|
||||
|
||||
var materialList []interface{}
|
||||
if productTemplateV2Info.TemplateInfo != nil {
|
||||
var templateInfo map[string]interface{}
|
||||
err = json.Unmarshal([]byte(*productTemplateV2Info.TemplateInfo), &templateInfo)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal templateInfo")
|
||||
}
|
||||
materialList = templateInfo["materialList"].([]interface{})
|
||||
}
|
||||
|
||||
var moduleDataMap = make(map[string]interface{}, 4)
|
||||
moduleDataMap["id"] = productTemplateV2Info.Id
|
||||
moduleDataMap["material"] = productTemplateV2Info.MaterialImg
|
||||
moduleDataMap["groupOptions"] = groupOptions
|
||||
moduleDataMap["materialList"] = materialList
|
||||
|
||||
var combineParam map[string]interface{}
|
||||
json.Unmarshal([]byte(req.CombineParam), &combineParam)
|
||||
var postMap = make(map[string]interface{}, 2)
|
||||
postMap["module_data"] = moduleDataMap
|
||||
postMap["param_data"] = combineParam
|
||||
postMapB, _ := json.Marshal(postMap)
|
||||
|
||||
//result, err := http.Post(l.svcCtx.Config.BLMService.LogoCombine.Url, "application/json", strings.NewReader(string(postMapB)))
|
||||
var headerData = make(map[string]string, 1)
|
||||
headerData["Content-Type"] = "application/json"
|
||||
result, err := curl.ApiCall(l.svcCtx.Config.BLMService.LogoCombine.Url, "POST", headerData, strings.NewReader(string(postMapB)), time.Second*20)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service post fail")
|
||||
}
|
||||
defer result.Body.Close()
|
||||
b, err := io.ReadAll(result.Body)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail")
|
||||
}
|
||||
ress := string(b)
|
||||
|
||||
if ress == "Internal Server Error" {
|
||||
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail")
|
||||
}
|
||||
|
||||
var resultData map[string]interface{}
|
||||
err = json.Unmarshal(b, &resultData)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail")
|
||||
}
|
||||
// {
|
||||
// id: "",
|
||||
// logo_url:"https://s3.amazon.com/xxxx",
|
||||
// result: "$saa541afaldjaldjasldjsadjsapsaasda"
|
||||
// }
|
||||
var fileBase = resultData["result"]
|
||||
|
||||
// 上传文件
|
||||
var upload = file.Upload{
|
||||
Ctx: l.ctx,
|
||||
MysqlConn: l.svcCtx.MysqlConn,
|
||||
AwsSession: l.svcCtx.AwsSession,
|
||||
}
|
||||
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
||||
FileHash: resourceId,
|
||||
FileData: fileBase.(string),
|
||||
UploadBucket: 1,
|
||||
ApiType: 2,
|
||||
res, err := l.svcCtx.Repositories.ImageHandle.LogoCombine(l.ctx, &repositories.LogoCombineReq{
|
||||
ResourceKey: req.ResourceKey,
|
||||
TemplateId: req.TemplateId,
|
||||
CombineParam: req.CombineParam,
|
||||
UserId: userId,
|
||||
GuestId: guestId,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeFileLogoCombineErr, "LogoCombine error upload file failed")
|
||||
return resp.SetStatus(basic.CodeServiceErr)
|
||||
}
|
||||
|
||||
// 返回成功的响应和上传URL
|
||||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||||
"resource_id": resourceId,
|
||||
"resource_url": uploadRes.ResourceUrl,
|
||||
"resource_id": res.ResourceId,
|
||||
"resource_url": res.ResourceUrl,
|
||||
"resource_metadata": "",
|
||||
})
|
||||
}
|
||||
|
|
|
@ -21,10 +21,11 @@ type ServiceContext struct {
|
|||
Config config.Config
|
||||
SharedState *shared.SharedState
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
RabbitMq *initalize.RabbitMqHandle
|
||||
AwsSession *session.Session
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
RabbitMq *initalize.RabbitMqHandle
|
||||
AwsSession *session.Session
|
||||
Repositories *initalize.Repositories
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
@ -37,6 +38,11 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
|||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil),
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{
|
||||
GormDB: initalize.InitMysql(c.SourceMysql),
|
||||
BLMServiceUrl: &c.BLMService.Url,
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
20
server_api/base.api
Normal file
20
server_api/base.api
Normal file
|
@ -0,0 +1,20 @@
|
|||
syntax = "v1"
|
||||
|
||||
info (
|
||||
title: // TODO: add title
|
||||
desc: // TODO: add description
|
||||
author: ""
|
||||
email: ""
|
||||
)
|
||||
|
||||
import "basic.api"
|
||||
|
||||
service base {
|
||||
@handler MerchantCategoryListHandler
|
||||
get /api/base/merchant_category_list(MerchantCategoryListReq) returns (response);
|
||||
}
|
||||
|
||||
type (
|
||||
MerchantCategoryListReq {
|
||||
}
|
||||
)
|
|
@ -63,8 +63,19 @@ service home-user-auth {
|
|||
// 再来一单
|
||||
@handler UserAgainOrderHandler
|
||||
get /api/user/one-more-order (UserAgainOrderReq) returns (response);
|
||||
|
||||
// 保存商户信息
|
||||
@handler UserInfoSetHandler
|
||||
post /api/user/set_user_info (UserInfoSetReq) returns (response);
|
||||
}
|
||||
|
||||
type (
|
||||
UserInfoSetReq {
|
||||
Module string `form:"module,options=[merchant_category]"` // json格式字符串
|
||||
Metadata string `form:"metadata"` // json格式字符串
|
||||
}
|
||||
)
|
||||
|
||||
type (
|
||||
UserAgainOrderReq {
|
||||
Sn string `form:"sn"` // 订单编号
|
||||
|
|
179
service/repositories/image_handle.go
Normal file
179
service/repositories/image_handle.go
Normal file
|
@ -0,0 +1,179 @@
|
|||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/curl"
|
||||
"fusenapi/utils/file"
|
||||
"fusenapi/utils/hash"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NewImageHandle(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) ImageHandle {
|
||||
return &defaultImageHandle{
|
||||
MysqlConn: gormDB,
|
||||
BLMServiceUrl: bLMServiceUrl,
|
||||
AwsSession: awsSession,
|
||||
}
|
||||
}
|
||||
|
||||
type (
|
||||
defaultImageHandle struct {
|
||||
MysqlConn *gorm.DB
|
||||
BLMServiceUrl *string
|
||||
AwsSession *session.Session
|
||||
}
|
||||
|
||||
ImageHandle = interface {
|
||||
|
||||
// logo合图
|
||||
LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error)
|
||||
}
|
||||
)
|
||||
|
||||
/* logo合图 */
|
||||
type (
|
||||
LogoCombineReq struct {
|
||||
ResourceKey string `json:"resource_key"`
|
||||
TemplateId int64 `json:"template_id"`
|
||||
CombineParam string `json:"combine_param"`
|
||||
UserId int64 `json:"user_id"`
|
||||
GuestId int64 `json:"guest_id"`
|
||||
}
|
||||
LogoCombineRes struct {
|
||||
ResourceId string
|
||||
ResourceUrl *string
|
||||
Metadata *string
|
||||
}
|
||||
)
|
||||
|
||||
func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) {
|
||||
// 根据hash 查询数据资源
|
||||
var resourceId string = hash.JsonHashKey(in.ResourceKey)
|
||||
resourceModel := gmodel.NewFsResourceModel(l.MysqlConn)
|
||||
resourceInfo, err := resourceModel.FindOneById(ctx, resourceId)
|
||||
if err == nil && resourceInfo.ResourceId != "" {
|
||||
return &LogoCombineRes{
|
||||
ResourceId: resourceId,
|
||||
ResourceUrl: resourceInfo.ResourceUrl,
|
||||
}, nil
|
||||
} else {
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 没有查到,先根据模版id 查询模版数据 请求算法数据
|
||||
productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.MysqlConn)
|
||||
productTemplateV2Info, err := productTemplateV2Model.FindOne(ctx, in.TemplateId)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
var groupOptions map[string]interface{}
|
||||
if productTemplateV2Info.GroupOptions != nil {
|
||||
err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var materialList []interface{}
|
||||
if productTemplateV2Info.TemplateInfo != nil {
|
||||
var templateInfo map[string]interface{}
|
||||
err = json.Unmarshal([]byte(*productTemplateV2Info.TemplateInfo), &templateInfo)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
materialList = templateInfo["materialList"].([]interface{})
|
||||
}
|
||||
|
||||
var moduleDataMap = make(map[string]interface{}, 4)
|
||||
moduleDataMap["id"] = productTemplateV2Info.Id
|
||||
moduleDataMap["material"] = productTemplateV2Info.MaterialImg
|
||||
moduleDataMap["groupOptions"] = groupOptions
|
||||
moduleDataMap["materialList"] = materialList
|
||||
|
||||
var combineParam map[string]interface{}
|
||||
json.Unmarshal([]byte(in.CombineParam), &combineParam)
|
||||
var postMap = make(map[string]interface{}, 2)
|
||||
postMap["module_data"] = moduleDataMap
|
||||
postMap["param_data"] = combineParam
|
||||
postMapB, _ := json.Marshal(postMap)
|
||||
|
||||
var headerData = make(map[string]string, 1)
|
||||
headerData["Content-Type"] = "application/json"
|
||||
result, err := curl.ApiCall(*l.BLMServiceUrl+constants.BLMServiceUrlLogoCombine, "POST", headerData, strings.NewReader(string(postMapB)), time.Second*20)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
defer result.Body.Close()
|
||||
b, err := io.ReadAll(result.Body)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
ress := string(b)
|
||||
|
||||
if ress == "Internal Server Error" {
|
||||
logx.Error(errors.New("BLMService fail Internal Server Error"))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resultData map[string]interface{}
|
||||
err = json.Unmarshal(b, &resultData)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
// {
|
||||
// id: "",
|
||||
// logo_url:"https://s3.amazon.com/xxxx",
|
||||
// result: "$saa541afaldjaldjasldjsadjsapsaasda"
|
||||
// }
|
||||
var fileBase = resultData["result"]
|
||||
|
||||
// 上传文件
|
||||
var upload = file.Upload{
|
||||
Ctx: ctx,
|
||||
MysqlConn: l.MysqlConn,
|
||||
AwsSession: l.AwsSession,
|
||||
}
|
||||
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
||||
FileHash: resourceId,
|
||||
FileData: fileBase.(string),
|
||||
UploadBucket: 1,
|
||||
ApiType: 2,
|
||||
UserId: in.UserId,
|
||||
GuestId: in.GuestId,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &LogoCombineRes{
|
||||
ResourceId: uploadRes.ResourceId,
|
||||
ResourceUrl: &uploadRes.ResourceUrl,
|
||||
}, nil
|
||||
}
|
||||
|
||||
/* logo合图 */
|
|
@ -2,6 +2,7 @@ package basic
|
|||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"fusenapi/shared"
|
||||
"fusenapi/utils/auth"
|
||||
"net/http"
|
||||
|
|
26
utils/validate/user_info.go
Normal file
26
utils/validate/user_info.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package validate
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type MerchantCategory struct {
|
||||
CategoryId int64 `json:"category_id"`
|
||||
}
|
||||
|
||||
func Validate(module *string, metadata *string) (interface{}, error) {
|
||||
if *module == "merchant_category" {
|
||||
var merchantCategory MerchantCategory
|
||||
err := json.Unmarshal([]byte(*metadata), &merchantCategory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if merchantCategory.CategoryId == 0 {
|
||||
return nil, errors.New("merchant_category.category_id is required")
|
||||
}
|
||||
return merchantCategory, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
|
@ -16,12 +16,12 @@ type RenderData struct {
|
|||
ProductId int64 `json:"product_id"` //产品id
|
||||
UserMaterialId int64 `json:"user_material_id"` //用户素材id
|
||||
Logo string `json:"logo"` //log资源地址(websocket连接建立再赋值)
|
||||
Website string `json:"website"` //网站
|
||||
/*Website string `json:"website"` //网站
|
||||
Slogan string `json:"slogan"` //slogan
|
||||
Address string `json:"address"` //地址
|
||||
Phone string `json:"phone"` //电话
|
||||
UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值)
|
||||
GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值)
|
||||
Phone string `json:"phone"` //电话*/
|
||||
UserId int64 `json:"user_id"` //用户id(websocket连接建立再赋值)
|
||||
GuestId int64 `json:"guest_id"` //游客id(websocket连接建立再赋值)
|
||||
}
|
||||
|
||||
// websocket发送渲染完的数据
|
||||
|
|
Loading…
Reference in New Issue
Block a user