Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop
This commit is contained in:
commit
ceefccb3b2
@ -11,6 +11,11 @@ func (m *FsProductModel) TableName() string {
|
|||||||
return m.name
|
return m.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *FsProductModel) FindAll(ctx context.Context) (resp []FsProduct, err error) {
|
||||||
|
err = t.db.WithContext(ctx).Model(&FsProduct{}).Find(&resp).Error
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
func (p *FsProductModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProduct, err error) {
|
func (p *FsProductModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProduct, err error) {
|
||||||
db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id)
|
db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id)
|
||||||
if len(fields) != 0 {
|
if len(fields) != 0 {
|
||||||
|
@ -2,6 +2,10 @@ package gmodel
|
|||||||
|
|
||||||
import "context"
|
import "context"
|
||||||
|
|
||||||
|
func (t *FsProductModel3dLightModel) FindAll(ctx context.Context) (resp []FsProductModel3dLight, err error) {
|
||||||
|
err = t.db.WithContext(ctx).Model(&FsProductModel3dLight{}).Find(&resp).Error
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
func (l *FsProductModel3dLightModel) FindOne(ctx context.Context, id int64) (resp *FsProductModel3dLight, err error) {
|
func (l *FsProductModel3dLightModel) FindOne(ctx context.Context, id int64) (resp *FsProductModel3dLight, err error) {
|
||||||
err = l.db.WithContext(ctx).Model(&FsProductModel3dLight{}).Where("`id` = ? and `status` = ?", id, 1).Take(&resp).Error
|
err = l.db.WithContext(ctx).Model(&FsProductModel3dLight{}).Where("`id` = ? and `status` = ?", id, 1).Take(&resp).Error
|
||||||
return resp, err
|
return resp, err
|
||||||
|
@ -19,6 +19,11 @@ type StepPriceJsonStruct struct {
|
|||||||
MinBuyUnitsNum int64 `json:"min_buy_units_num"`
|
MinBuyUnitsNum int64 `json:"min_buy_units_num"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *FsProductModel3dModel) FindAll(ctx context.Context) (resp []FsProductModel3d, err error) {
|
||||||
|
err = t.db.WithContext(ctx).Model(&FsProductModel3d{}).Find(&resp).Error
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProductModel3d, err error) {
|
func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProductModel3d, err error) {
|
||||||
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? and `status` =? ", id, 1)
|
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? and `status` =? ", id, 1)
|
||||||
if len(fields) > 0 {
|
if len(fields) > 0 {
|
||||||
|
@ -4,6 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (t *FsProductTemplateTagsModel) FindAll(ctx context.Context) (resp []FsProductTemplateTags, err error) {
|
||||||
|
err = t.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Find(&resp).Error
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
func (pt *FsProductTemplateTagsModel) GetListByIds(ctx context.Context, ids []int64) (resp []FsProductTemplateTags, err error) {
|
func (pt *FsProductTemplateTagsModel) GetListByIds(ctx context.Context, ids []int64) (resp []FsProductTemplateTags, err error) {
|
||||||
if len(ids) == 0 {
|
if len(ids) == 0 {
|
||||||
return
|
return
|
||||||
|
@ -4,6 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (t *FsProductTemplateV2Model) FindAll(ctx context.Context) (resp []FsProductTemplateV2, err error) {
|
||||||
|
err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).Find(&resp).Error
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
func (t *FsProductTemplateV2Model) FindAllByProductIds(ctx context.Context, productIds []int64, sort string, fields ...string) (resp []FsProductTemplateV2, err error) {
|
func (t *FsProductTemplateV2Model) FindAllByProductIds(ctx context.Context, productIds []int64, sort string, fields ...string) (resp []FsProductTemplateV2, err error) {
|
||||||
if len(productIds) == 0 {
|
if len(productIds) == 0 {
|
||||||
return
|
return
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
package gmodel
|
package gmodel
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
// TODO: 使用model的属性做你想做的
|
// TODO: 使用model的属性做你想做的
|
||||||
|
func (t *FsToolLogsModel) FindAll(ctx context.Context) (resp []FsToolLogs, err error) {
|
||||||
|
err = t.db.WithContext(ctx).Model(&FsToolLogs{}).Find(&resp).Error
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
@ -24,4 +24,10 @@ type Config struct {
|
|||||||
Version string
|
Version string
|
||||||
Urls []string
|
Urls []string
|
||||||
}
|
}
|
||||||
|
AliyunOSS struct {
|
||||||
|
AccessKeyId string
|
||||||
|
AccessKeySecret string
|
||||||
|
BasePath string
|
||||||
|
Endpoint string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
35
server/resource/internal/handler/resourcebackuphandler.go
Normal file
35
server/resource/internal/handler/resourcebackuphandler.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"fusenapi/utils/basic"
|
||||||
|
|
||||||
|
"fusenapi/server/resource/internal/logic"
|
||||||
|
"fusenapi/server/resource/internal/svc"
|
||||||
|
"fusenapi/server/resource/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ResourceBackupHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
var req types.ResourceBackupReq
|
||||||
|
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个业务逻辑层实例
|
||||||
|
l := logic.NewResourceBackupLogic(r.Context(), svcCtx)
|
||||||
|
|
||||||
|
rl := reflect.ValueOf(l)
|
||||||
|
basic.BeforeLogic(w, r, rl)
|
||||||
|
|
||||||
|
resp := l.ResourceBackup(&req, userinfo)
|
||||||
|
|
||||||
|
if !basic.AfterLogic(w, r, rl, resp) {
|
||||||
|
basic.NormalAfterLogic(w, r, resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -32,6 +32,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||||||
Path: "/api/resource/logo-resize",
|
Path: "/api/resource/logo-resize",
|
||||||
Handler: LogoResizeHandler(serverCtx),
|
Handler: LogoResizeHandler(serverCtx),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Path: "/api/resource/backup",
|
||||||
|
Handler: ResourceBackupHandler(serverCtx),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
186
server/resource/internal/logic/resourcebackuplogic.go
Normal file
186
server/resource/internal/logic/resourcebackuplogic.go
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
package logic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fusenapi/utils/auth"
|
||||||
|
"fusenapi/utils/basic"
|
||||||
|
"fusenapi/utils/file"
|
||||||
|
"fusenapi/utils/s3url_to_s3id"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"fusenapi/server/resource/internal/svc"
|
||||||
|
"fusenapi/server/resource/internal/types"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ResourceBackupLogic struct {
|
||||||
|
logx.Logger
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResourceBackupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResourceBackupLogic {
|
||||||
|
return &ResourceBackupLogic{
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理进入前逻辑w,r
|
||||||
|
// func (l *ResourceBackupLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (l *ResourceBackupLogic) ResourceBackup(req *types.ResourceBackupReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||||
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||||
|
// userinfo 传入值时, 一定不为null
|
||||||
|
var urls = make(map[string]string, 1000)
|
||||||
|
switch req.TableName {
|
||||||
|
case "fs_product_template_v2":
|
||||||
|
// 查询所有数据
|
||||||
|
resList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAll(l.ctx)
|
||||||
|
if err != nil {
|
||||||
|
l.Logger.Errorf("查询所有数据失败, err: %v", err)
|
||||||
|
return resp.SetStatus(basic.CodeApiErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range resList {
|
||||||
|
if data.MaterialImg != nil {
|
||||||
|
urls[s3url_to_s3id.GetS3ResourceIdFormUrl(*data.MaterialImg)] = *data.MaterialImg
|
||||||
|
}
|
||||||
|
if data.TemplateInfo != nil && *data.TemplateInfo != "" {
|
||||||
|
regexStr(*data.TemplateInfo, &urls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "fs_product":
|
||||||
|
// 查询所有数据
|
||||||
|
resList, err := l.svcCtx.AllModels.FsProduct.FindAll(l.ctx)
|
||||||
|
if err != nil {
|
||||||
|
l.Logger.Errorf("查询所有数据失败, err: %v", err)
|
||||||
|
return resp.SetStatus(basic.CodeApiErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range resList {
|
||||||
|
if data.Cover != nil && *data.Cover != "" {
|
||||||
|
urls[s3url_to_s3id.GetS3ResourceIdFormUrl(*data.Cover)] = *data.Cover
|
||||||
|
}
|
||||||
|
if data.CoverImg != nil && *data.CoverImg != "" {
|
||||||
|
urls[s3url_to_s3id.GetS3ResourceIdFormUrl(*data.CoverImg)] = *data.CoverImg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "fs_product_template_tags":
|
||||||
|
// 查询所有数据
|
||||||
|
resList, err := l.svcCtx.AllModels.FsProductTemplateTags.FindAll(l.ctx)
|
||||||
|
if err != nil {
|
||||||
|
l.Logger.Errorf("查询所有数据失败, err: %v", err)
|
||||||
|
return resp.SetStatus(basic.CodeApiErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range resList {
|
||||||
|
if data.Cover != nil && *data.Cover != "" {
|
||||||
|
urls[s3url_to_s3id.GetS3ResourceIdFormUrl(*data.Cover)] = *data.Cover
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "fs_product_model3d":
|
||||||
|
// 查询所有数据
|
||||||
|
resList, err := l.svcCtx.AllModels.FsProductModel3d.FindAll(l.ctx)
|
||||||
|
if err != nil {
|
||||||
|
l.Logger.Errorf("查询所有数据失败, err: %v", err)
|
||||||
|
return resp.SetStatus(basic.CodeApiErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range resList {
|
||||||
|
if data.ModelInfo != nil && *data.ModelInfo != "" {
|
||||||
|
regexStr(*data.ModelInfo, &urls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "fs_product_model3d_light":
|
||||||
|
// 查询所有数据
|
||||||
|
resList, err := l.svcCtx.AllModels.FsProductModel3dLight.FindAll(l.ctx)
|
||||||
|
if err != nil {
|
||||||
|
l.Logger.Errorf("查询所有数据失败, err: %v", err)
|
||||||
|
return resp.SetStatus(basic.CodeApiErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range resList {
|
||||||
|
if data.Info != nil && *data.Info != "" {
|
||||||
|
regexStr(*data.Info, &urls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "fs_tool_logs":
|
||||||
|
// 查询所有数据
|
||||||
|
resList, err := l.svcCtx.AllModels.FsToolLogs.FindAll(l.ctx)
|
||||||
|
if err != nil {
|
||||||
|
l.Logger.Errorf("查询所有数据失败, err: %v", err)
|
||||||
|
return resp.SetStatus(basic.CodeApiErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range resList {
|
||||||
|
if data.Cover != nil && *data.Cover != "" {
|
||||||
|
urls[s3url_to_s3id.GetS3ResourceIdFormUrl(*data.Cover)] = *data.Cover
|
||||||
|
}
|
||||||
|
if data.Img != nil && *data.Img != "" {
|
||||||
|
urls[s3url_to_s3id.GetS3ResourceIdFormUrl(*data.Img)] = *data.Img
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 上传文件
|
||||||
|
var upload = file.Upload{
|
||||||
|
Ctx: l.ctx,
|
||||||
|
MysqlConn: l.svcCtx.MysqlConn,
|
||||||
|
AwsSession: l.svcCtx.AwsSession,
|
||||||
|
}
|
||||||
|
upload.UploadType = "oss"
|
||||||
|
upload.AliyunOSS.AccessKeyId = l.svcCtx.Config.AliyunOSS.AccessKeyId
|
||||||
|
upload.AliyunOSS.AccessKeySecret = l.svcCtx.Config.AliyunOSS.AccessKeySecret
|
||||||
|
upload.AliyunOSS.Endpoint = l.svcCtx.Config.AliyunOSS.Endpoint
|
||||||
|
upload.AliyunOSS.BasePath = l.svcCtx.Config.AliyunOSS.BasePath
|
||||||
|
for k, v := range urls {
|
||||||
|
var resourceId = k
|
||||||
|
time.AfterFunc(time.Second*1, func() {
|
||||||
|
err := upload.UploadFileByUrl(resourceId, v, 2)
|
||||||
|
if err != nil {
|
||||||
|
l.Logger.Errorf("更新文件镜像备份, err: %v, key: %v, val:%v", err, k, v)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return resp.SetStatus(basic.CodeOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func regexStr(unquotedStr string, urls *map[string]string) error {
|
||||||
|
// 创建一个正则表达式
|
||||||
|
regex := regexp.MustCompile(`"https:\\/\\/s3\.(.*?)"`)
|
||||||
|
// 查找符合正则表达式的字符串
|
||||||
|
matches := regex.FindAllString(unquotedStr, -1)
|
||||||
|
var urlsData = *urls
|
||||||
|
// 打印匹配到的字符串
|
||||||
|
for _, match := range matches {
|
||||||
|
var matchStr = match
|
||||||
|
// 将字符串去转义
|
||||||
|
matchStr = strings.Replace(matchStr, "\"", "", -1)
|
||||||
|
matchStr = strings.Replace(matchStr, "\\", "", -1)
|
||||||
|
urlsData[s3url_to_s3id.GetS3ResourceIdFormUrl(matchStr)] = matchStr
|
||||||
|
}
|
||||||
|
|
||||||
|
regex02 := regexp.MustCompile(`"https:\\/\\/fusenstorage\.s3\.(.*?)"`)
|
||||||
|
// 查找符合正则表达式的字符串
|
||||||
|
matches02 := regex02.FindAllString(unquotedStr, -1)
|
||||||
|
// 打印匹配到的字符串
|
||||||
|
for _, match := range matches02 {
|
||||||
|
var matchStr = match
|
||||||
|
// 将字符串去转义
|
||||||
|
matchStr = strings.Replace(matchStr, "\"", "", -1)
|
||||||
|
matchStr = strings.Replace(matchStr, "\\", "", -1)
|
||||||
|
urlsData[s3url_to_s3id.GetS3ResourceIdFormUrl(matchStr)] = matchStr
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||||
|
// func (l *ResourceBackupLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||||
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||||
|
// }
|
@ -5,6 +5,10 @@ import (
|
|||||||
"fusenapi/utils/basic"
|
"fusenapi/utils/basic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ResourceBackupReq struct {
|
||||||
|
TableName string `json:"table_name"`
|
||||||
|
}
|
||||||
|
|
||||||
type LogoResizeReq struct {
|
type LogoResizeReq struct {
|
||||||
ResourceId string `form:"resource_id"`
|
ResourceId string `form:"resource_id"`
|
||||||
Width int64 `form:"width"`
|
Width int64 `form:"width"`
|
||||||
|
@ -25,8 +25,17 @@ service resource {
|
|||||||
|
|
||||||
@handler LogoResizeHandler
|
@handler LogoResizeHandler
|
||||||
post /api/resource/logo-resize(LogoResizeReq) returns (response);
|
post /api/resource/logo-resize(LogoResizeReq) returns (response);
|
||||||
|
|
||||||
|
@handler ResourceBackupHandler
|
||||||
|
post /api/resource/backup(ResourceBackupReq) returns (response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
|
ResourceBackupReq {
|
||||||
|
TableName string `json:"table_name"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
LogoResizeReq {
|
LogoResizeReq {
|
||||||
ResourceId string `form:"resource_id"`
|
ResourceId string `form:"resource_id"`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user