diff --git a/beans/beans.go b/beans/beans.go index 65662ac..8f60540 100644 --- a/beans/beans.go +++ b/beans/beans.go @@ -41,6 +41,7 @@ type MongoBaseDO struct { // SQLBaseDO SQL数据库基础实体 type SQLBaseDO struct { Id int64 `orm:"id" json:"id"` // 主键ID + TenantId uint64 `orm:"tenant_id" json:"tenantId"` // 租户ID Creator string `orm:"creator" json:"creator"` // 创建人 CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 创建时间 Updater string `orm:"updater" json:"updater"` // 更新人 @@ -51,6 +52,7 @@ type SQLBaseDO struct { type SQLBaseCol struct { Id string + TenantId string Creator string CreatedAt string Updater string @@ -61,6 +63,7 @@ type SQLBaseCol struct { var DefSQLBaseCol = SQLBaseCol{ Id: "id", + TenantId: "tenant_id", Creator: "creator", CreatedAt: "created_at", Updater: "updater", diff --git a/db/gfdb/gfdb.go b/db/gfdb/gfdb.go index fad53d2..89b342a 100644 --- a/db/gfdb/gfdb.go +++ b/db/gfdb/gfdb.go @@ -173,12 +173,25 @@ func insertHook(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result if _, ok := in.Data[i]["id"]; ok { in.Data[i]["id"] = node.Generate().Int64() } - if !g.IsEmpty(userInfo.UserName) { - if _, ok := in.Data[i]["creator"]; ok { - in.Data[i]["creator"] = userInfo.UserName + if _, ok := in.Data[i]["tenant_id"]; ok { + if !g.IsEmpty(userInfo.TenantId) { + in.Data[i]["tenant_id"] = userInfo.TenantId + } else { + return nil, fmt.Errorf("tenantId cannot be empty") } - if _, ok := in.Data[i]["updater"]; ok { + } + if _, ok := in.Data[i]["creator"]; ok { + if !g.IsEmpty(userInfo.UserName) { + in.Data[i]["creator"] = userInfo.UserName + } else { + return nil, fmt.Errorf("user info cannot be empty") + } + } + if _, ok := in.Data[i]["updater"]; ok { + if !g.IsEmpty(userInfo.UserName) { in.Data[i]["updater"] = userInfo.UserName + } else { + return nil, fmt.Errorf("user info cannot be empty") } } } @@ -207,16 +220,22 @@ func updateHook(ctx context.Context, in *gdb.HookUpdateInput) (result sql.Result switch data := in.Data.(type) { case gdb.Map: - if !g.IsEmpty(userInfo.UserName) { - if _, ok := data["updater"]; ok { + if _, ok := data["updater"]; ok { + if !g.IsEmpty(userInfo.UserName) { data["updater"] = userInfo.UserName + } else { + return nil, fmt.Errorf("user info cannot be empty") } } case gdb.List: for i := range data { if !g.IsEmpty(userInfo.UserName) { if _, ok := data[i]["updater"]; ok { - data[i]["updater"] = userInfo.UserName + if !g.IsEmpty(userInfo.UserName) { + data[i]["updater"] = userInfo.UserName + } else { + return nil, fmt.Errorf("user info cannot be empty") + } } } } diff --git a/http/http.go b/http/http.go index a751095..ad348c4 100644 --- a/http/http.go +++ b/http/http.go @@ -35,7 +35,7 @@ func init() { } //s.Use(common.Cors) //中间件验证 //s.EnablePProf() //启用性能分析 - Httpserver.BindMiddlewareDefault(validateFilterPathMiddleware) + //Httpserver.BindMiddlewareDefault(validateFilterPathMiddleware) Httpserver.SetOpenApiPath("/api.json") Httpserver.SetDumpRouterMap(true) //关闭打印路由注册信息 Httpserver.BindMiddlewareDefault(ghttp.MiddlewareHandlerResponse) diff --git a/minio/minio.go b/minio/minio.go index e29fb41..466ff29 100644 --- a/minio/minio.go +++ b/minio/minio.go @@ -11,7 +11,6 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/glog" - "github.com/gogf/gf/v2/util/gconv" "github.com/google/uuid" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" @@ -19,12 +18,11 @@ import ( // IoConfig 映射 YAML 中的 minio 配置节点 type IoConfig struct { - FilePrefix string `yaml:"filePrefix"` // 文件前缀 - Endpoint string `yaml:"endpoint"` // MinIO API 地址 - AccessKey string `yaml:"accessKey"` // AK - SecretKey string `yaml:"secretKey"` // SK - Secure bool `yaml:"secure"` // 是否启用 SSL - Region string `yaml:"region"` // 区域 + Endpoint string `yaml:"endpoint"` // MinIO API 地址 + AccessKey string `yaml:"accessKey"` // AK + SecretKey string `yaml:"secretKey"` // SK + Secure bool `yaml:"secure"` // 是否启用 SSL + Region string `yaml:"region"` // 区域 } // 全局 MinIO 客户端(初始化一次,避免重复创建) @@ -37,12 +35,11 @@ func init() { if !g.Cfg().MustGet(ctx, "minio").IsEmpty() { // 加载 MinIO 配置(可从配置文件/环境变量读取,这里硬编码示例) minioCfg = IoConfig{ - FilePrefix: g.Cfg().MustGet(ctx, "filePrefix").String(), - Endpoint: g.Cfg().MustGet(ctx, "minio.endpoint").String(), - AccessKey: g.Cfg().MustGet(ctx, "minio.accessKey").String(), - SecretKey: g.Cfg().MustGet(ctx, "minio.secretKey").String(), - Secure: g.Cfg().MustGet(ctx, "minio.secure").Bool(), - Region: g.Cfg().MustGet(ctx, "minio.region").String(), + Endpoint: g.Cfg().MustGet(ctx, "minio.endpoint").String(), + AccessKey: g.Cfg().MustGet(ctx, "minio.accessKey").String(), + SecretKey: g.Cfg().MustGet(ctx, "minio.secretKey").String(), + Secure: g.Cfg().MustGet(ctx, "minio.secure").Bool(), + Region: g.Cfg().MustGet(ctx, "minio.region").String(), } // 创建 MinIO 客户端 var err error @@ -57,10 +54,15 @@ func init() { } func UploadFile(ctx context.Context, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) { - return uploadFile(ctx, getBucketName(ctx), fileHeader) + return uploadFile(ctx, fileHeader) } -func uploadFile(ctx context.Context, bucketName string, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) { +func uploadFile(ctx context.Context, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) { + bucketName, err := utils.GetBucketName(ctx) + if err != nil { + glog.Errorf(ctx, "获取桶名称失败: %v", err) + return + } // 检查/创建桶 exists, err := minioClient.BucketExists(ctx, bucketName) if err != nil { @@ -124,23 +126,3 @@ func uploadFile(ctx context.Context, bucketName string, fileHeader *ghttp.Upload } return objectName, err } - -// GetFileAddressPrefix 拼接图片前缀地址 -func GetFileAddressPrefix(ctx context.Context) (imageUrl string) { - // 拼接图片前缀地址 - var url = "http://" - if minioCfg.Secure { - url = "https://" - } - imgAddressPrefix := url + minioCfg.FilePrefix + "/" + getBucketName(ctx) - return imgAddressPrefix -} - -func getBucketName(ctx context.Context) (bucketName string) { - user, err := utils.GetUserInfo(ctx) - if err != nil { - glog.Errorf(ctx, "获取用户信息失败: %v", err) - return - } - return "tenantid-" + gconv.String(user.TenantId) -} diff --git a/utils/utils.go b/utils/utils.go index 4639379..0d5b937 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -385,3 +385,23 @@ func intPow10(n int) int { } return result } + +// GetFileAddressPrefix 拼接图片前缀地址 +func GetFileAddressPrefix(ctx context.Context) (imageUrl string, err error) { + // 拼接图片前缀地址 + bucketName, err := GetBucketName(ctx) + if err != nil { + return + } + imageUrl = fmt.Sprintf("%s/%s", g.Cfg().MustGet(ctx, "filePrefix").String(), bucketName) + return +} + +func GetBucketName(ctx context.Context) (bucketName string, err error) { + user, err := GetUserInfo(ctx) + if err != nil { + return + } + bucketName = fmt.Sprintf("tenantid-%d", user.TenantId) + return +}