This commit is contained in:
oneao committed 2026-04-19 21:30:51 +08:00
1 parent b6415c0d65
commit 89aaef1d6b
29 files changed
+1118 -277

No files matched your search

+2 -2
View File
@@ -7,8 +7,8 @@ import (
)
var auditExcludeTables = map[string]bool{
"user": true,
"user_oauth": true,
"b_user": true,
"b_user_oauth": true,
}
func validateData(table string, keyField string, data map[string]any) (any, error) {
+23 -3
View File
@@ -16,11 +16,21 @@ func (c *Client) Delete(
return fmt.Errorf("keyField 不能为空")
}
keyVal, err := validateData(table, keyField, data)
rawTable := table
keyVal, err := validateData(rawTable, keyField, data)
if err != nil {
return err
}
tableParts := strings.Split(table, ".")
for i, p := range tableParts {
tableParts[i] = `"` + p + `"`
}
table = strings.Join(tableParts, ".")
keyField = `"` + keyField + `"`
sql := fmt.Sprintf(
"DELETE FROM %s WHERE %s = $1",
table,
@@ -50,6 +60,16 @@ func (c *Client) BatchDelete(
return fmt.Errorf("数据不能为空")
}
rawTable := table
tableParts := strings.Split(table, ".")
for i, p := range tableParts {
tableParts[i] = `"` + p + `"`
}
table = strings.Join(tableParts, ".")
keyFieldQuoted := `"` + keyField + `"`
var (
placeholders []string
args []any
@@ -57,7 +77,7 @@ func (c *Client) BatchDelete(
)
for _, data := range dataList {
keyVal, err := validateData(table, keyField, data)
keyVal, err := validateData(rawTable, keyField, data)
if err != nil {
return err
}
@@ -74,7 +94,7 @@ func (c *Client) BatchDelete(
sql := fmt.Sprintf(
"DELETE FROM %s WHERE %s IN (%s)",
table,
keyField,
keyFieldQuoted,
strings.Join(placeholders, ", "),
)
+24 -5
View File
@@ -17,9 +17,18 @@ func buildInsertSQL(table string, data map[string]any) (string, []any) {
args []any
)
// 处理 schema.table 的情况
tableParts := strings.Split(table, ".")
for i, p := range tableParts {
tableParts[i] = `"` + p + `"`
}
table = strings.Join(tableParts, ".")
i := 1
for col, val := range data {
columns = append(columns, col)
// 列名加引号
columns = append(columns, `"`+col+`"`)
placeholders = append(placeholders, fmt.Sprintf("$%d", i))
args = append(args, val)
i++
@@ -80,12 +89,18 @@ func (c *Client) BatchInsert(
return fmt.Errorf("数据不能为空")
}
// 用第一条确定列顺序
first := dataList[0]
if _, err := validateData(table, keyField, first); err != nil {
return err
}
// 👉 表名加引号(支持 schema)
tableParts := strings.Split(table, ".")
for i, p := range tableParts {
tableParts[i] = `"` + p + `"`
}
table = strings.Join(tableParts, ".")
var columns []string
for col := range first {
columns = append(columns, col)
@@ -93,6 +108,12 @@ func (c *Client) BatchInsert(
sort.Strings(columns)
// 👉 列名加引号
var quotedColumns []string
for _, col := range columns {
quotedColumns = append(quotedColumns, `"`+col+`"`)
}
var (
valueStrings []string
args []any
@@ -100,12 +121,10 @@ func (c *Client) BatchInsert(
)
for _, data := range dataList {
// 统一校验
if _, err := validateData(table, keyField, data); err != nil {
return err
}
// 字段数量检查
if len(data) != len(columns) {
return fmt.Errorf("批量插入失败:数据字段不一致")
}
@@ -132,7 +151,7 @@ func (c *Client) BatchInsert(
sql := fmt.Sprintf(
"INSERT INTO %s (%s) VALUES %s",
table,
strings.Join(columns, ", "),
strings.Join(quotedColumns, ", "),
strings.Join(valueStrings, ", "),
)
+29 -11
View File
@@ -29,12 +29,21 @@ func (c *Client) Update(
data["update_time"] = now
userID, flag := middleware.GetUserID(ctx)
if flag {
data["update_by"] = userID
}
}
// 👉 表名加引号(支持 schema)
tableParts := strings.Split(table, ".")
for i, p := range tableParts {
tableParts[i] = `"` + p + `"`
}
table = strings.Join(tableParts, ".")
// 👉 keyField 加引号
keyField = `"` + keyField + `"`
var (
setClauses []string
args []any
@@ -45,12 +54,15 @@ func (c *Client) Update(
if col == keyField {
continue
}
setClauses = append(setClauses, fmt.Sprintf("%s=$%d", col, i))
// 👉 列名加引号
colQuoted := `"` + col + `"`
setClauses = append(setClauses, fmt.Sprintf("%s=$%d", colQuoted, i))
args = append(args, val)
i++
}
// WHERE 条件
where := fmt.Sprintf("%s=$%d", keyField, i)
args = append(args, keyVal)
@@ -76,13 +88,21 @@ func (c *Client) BatchUpdate(
return fmt.Errorf("数据不能为空")
}
// 用第一条数据确定字段
first := dataList[0]
if _, err := validateData(table, keyField, first); err != nil {
return err
}
// 提取字段(排除主键)+ 排序(关键)
// 👉 表名加引号(支持 schema)
tableParts := strings.Split(table, ".")
for i, p := range tableParts {
tableParts[i] = `"` + p + `"`
}
table = strings.Join(tableParts, ".")
// 👉 keyField 加引号
keyFieldQuoted := `"` + keyField + `"`
var columns []string
for col := range first {
if col != keyField {
@@ -96,16 +116,15 @@ func (c *Client) BatchUpdate(
argIndex = 1
)
// CASE 语句
var setClauses []string
for _, col := range columns {
var caseBuilder strings.Builder
colQuoted := `"` + col + `"`
caseBuilder.WriteString(fmt.Sprintf("%s = CASE %s ", col, keyField))
var caseBuilder strings.Builder
caseBuilder.WriteString(fmt.Sprintf("%s = CASE %s ", colQuoted, keyFieldQuoted))
for _, data := range dataList {
// 校验 key
keyVal, err := validateData(table, keyField, data)
if err != nil {
return err
@@ -130,7 +149,6 @@ func (c *Client) BatchUpdate(
setClauses = append(setClauses, caseBuilder.String())
}
// WHERE IN
var wherePlaceholders []string
for _, data := range dataList {
@@ -148,7 +166,7 @@ func (c *Client) BatchUpdate(
"UPDATE %s SET %s WHERE %s IN (%s)",
table,
strings.Join(setClauses, ", "),
keyField,
keyFieldQuoted,
strings.Join(wherePlaceholders, ", "),
)
+7 -5
View File
@@ -48,8 +48,12 @@ func getManager() *jwtManager {
// Claims(通用结构)
// ==================
type TokenData struct {
UserID int64 `json:"user_id"`
}
type CustomClaims struct {
Data map[string]any `json:"data"`
Data TokenData `json:"data"`
jwt.RegisteredClaims
}
@@ -58,7 +62,7 @@ type CustomClaims struct {
// ==================
// CreateToken 生成 JWT
func CreateToken(ctx context.Context, data map[string]any) (string, error) {
func CreateToken(ctx context.Context, data TokenData) (string, error) {
m := getManager()
return m.createToken(data)
}
@@ -91,7 +95,7 @@ func VerifyToken(ctx context.Context, tokenString string) (*CustomClaims, bool)
// 内部实现
// ==================
func (j *jwtManager) createToken(data map[string]any) (string, error) {
func (j *jwtManager) createToken(data TokenData) (string, error) {
now := time.Now()
claims := &CustomClaims{
@@ -101,8 +105,6 @@ func (j *jwtManager) createToken(data map[string]any) (string, error) {
},
}
// ✅ 只有 >0 才设置过期时间
// -1 / 0 都表示不过期
if j.tokenExpiry > 0 {
claims.ExpiresAt = jwt.NewNumericDate(now.Add(j.tokenExpiry))
}
@@ -0,0 +1,52 @@
package s3store
import (
"allapp-go/internal/config"
"context"
"fmt"
"mime/multipart"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
var S3Client *s3.Client
var presignClient *s3.PresignClient
var bucket string
func NewS3Client(cfg *config.Config) {
credProvider := credentials.NewStaticCredentialsProvider(
cfg.AwsS3.AccessKey,
cfg.AwsS3.SecretKey,
"",
)
awsCfg := aws.Config{
Region: cfg.AwsS3.Region,
Credentials: credProvider,
}
S3Client = s3.NewFromConfig(awsCfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(cfg.AwsS3.Endpoint)
o.UsePathStyle = true
})
presignClient = s3.NewPresignClient(S3Client)
bucket = cfg.AwsS3.Bucket
fmt.Println("S3 client and Presign client initialized successfully!")
}
func UploadToRustFS(ctx context.Context, key string, file multipart.File, size int64, contentType string) error {
_, err := S3Client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: file,
ContentLength: aws.Int64(size),
ContentType: aws.String(contentType),
})
return err
}