Compare commits
4 Commits
v0.0.4
...
ac6beab363
| Author | SHA1 | Date | |
|---|---|---|---|
| ac6beab363 | |||
| 0a38df71c9 | |||
| 3cf301275f | |||
| bb88eb1e47 |
@@ -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",
|
||||
|
||||
@@ -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]["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]["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 !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 {
|
||||
if !g.IsEmpty(userInfo.UserName) {
|
||||
data[i]["updater"] = userInfo.UserName
|
||||
} else {
|
||||
return nil, fmt.Errorf("user info cannot be empty")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -421,7 +440,7 @@ func (d *dataBase) Model(ctx context.Context, tableNameOrStruct ...any) *model {
|
||||
m.Sharding(shardingConfig).ShardingValue(user.TenantId)
|
||||
}
|
||||
|
||||
m.OmitNilData().OmitNilWhere().Hook(catchSQLHook())
|
||||
m.OmitNil().OmitEmpty().Hook(catchSQLHook())
|
||||
return &model{
|
||||
Model: m,
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -3,15 +3,15 @@ package minio
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
@@ -19,7 +19,6 @@ 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
|
||||
@@ -37,7 +36,6 @@ 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(),
|
||||
@@ -56,11 +54,16 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
func UploadFile(ctx context.Context, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) {
|
||||
return uploadFile(ctx, getBucketName(ctx), fileHeader)
|
||||
func UploadFile(ctx context.Context, fileHeader *ghttp.UploadFile) (imagesUrl string, fileName string, fileFormat string, err error) {
|
||||
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, fileName string, fileFormat 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 {
|
||||
@@ -122,25 +125,5 @@ func uploadFile(ctx context.Context, bucketName string, fileHeader *ghttp.Upload
|
||||
glog.Errorf(ctx, "上传图片失败: %v", err)
|
||||
return
|
||||
}
|
||||
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)
|
||||
return objectName, fileHeader.Filename, strings.ReplaceAll(fileExt, ".", ""), err
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -38,40 +37,6 @@ func RedisClient() *gredis.Redis {
|
||||
return getClient()
|
||||
}
|
||||
|
||||
// Lock 分布式锁
|
||||
func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context) error) (success bool, err error) {
|
||||
limit := 3
|
||||
LOOP:
|
||||
if limit < 0 {
|
||||
return false, errors.New("锁重试次数耗尽")
|
||||
}
|
||||
limit--
|
||||
client := getClient()
|
||||
if val, err := client.Set(ctx, key, true, gredis.SetOption{
|
||||
TTLOption: gredis.TTLOption{
|
||||
EX: &expireSeconds,
|
||||
},
|
||||
NX: true,
|
||||
}); err != nil {
|
||||
return false, err
|
||||
} else {
|
||||
if val.Bool() {
|
||||
defer func(client *gredis.Redis, ctx context.Context, key string) {
|
||||
if _, err = client.Del(ctx, key); err != nil {
|
||||
glog.Errorf(ctx, "redis client Del error: %v", err)
|
||||
}
|
||||
}(client, ctx, key)
|
||||
if err = fn(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
} else {
|
||||
time.Sleep(time.Second)
|
||||
goto LOOP
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetReadStream(ctx context.Context, msg ...QueueMessage) error {
|
||||
for _, t := range msg {
|
||||
err := GetReadFromStream(ctx, t.StreamKey, t.GroupName, t.ConsumerName, t.BatchSize, t.BlockMs, t.AutoAck, t.HandleFunc)
|
||||
|
||||
@@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
@@ -13,10 +14,12 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast-token/gftoken"
|
||||
@@ -385,3 +388,58 @@ 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
|
||||
}
|
||||
|
||||
// GetBucketName 获取bucket名称
|
||||
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
|
||||
}
|
||||
|
||||
// Lock 分布式锁
|
||||
func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context) error) (success bool, err error) {
|
||||
limit := 3
|
||||
LOOP:
|
||||
if limit < 0 {
|
||||
return false, errors.New("锁重试次数耗尽")
|
||||
}
|
||||
limit--
|
||||
var val *gvar.Var
|
||||
if val, err = g.Redis().Set(ctx, key, true, gredis.SetOption{
|
||||
TTLOption: gredis.TTLOption{
|
||||
EX: &expireSeconds,
|
||||
},
|
||||
NX: true,
|
||||
}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if val.Bool() {
|
||||
defer func(ctx context.Context, key string) {
|
||||
if _, err = g.Redis().Del(ctx, key); err != nil {
|
||||
glog.Errorf(ctx, "redis client Del error: %v", err)
|
||||
}
|
||||
}(ctx, key)
|
||||
if err = fn(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
goto LOOP
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user