u
This commit is contained in:
1 parent
e893ef2e48
commit
b6415c0d65
25 files changed
+1495
-112
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"allapp-go/internal/httpx"
|
||||
"allapp-go/internal/types"
|
||||
"allapp-go/pkg/db"
|
||||
"allapp-go/pkg/uniqueid"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
@@ -48,3 +49,22 @@ func LoadDataBySql(c fiber.Ctx) error {
|
||||
|
||||
return httpx.OK(c, data)
|
||||
}
|
||||
|
||||
func GetUniqueId(c fiber.Ctx) error {
|
||||
var req types.GetUniqueIdReq
|
||||
|
||||
if err := httpx.BindAndValidate(c, &req); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
count := req.Count
|
||||
|
||||
// 预分配,避免扩容
|
||||
ids := make([]int64, 0, count)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
ids = append(ids, uniqueid.NextId())
|
||||
}
|
||||
|
||||
return httpx.OK(c, ids)
|
||||
}
|
||||
@@ -21,6 +21,7 @@ func SaveData(c fiber.Ctx) error {
|
||||
}
|
||||
|
||||
ctx := c.Context()
|
||||
|
||||
dbClient := db.New()
|
||||
|
||||
err := dbClient.WithTx(ctx, func(tx *db.Client) error {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
@@ -8,10 +10,23 @@ import (
|
||||
var validate = validator.New()
|
||||
|
||||
func BindAndValidate(c fiber.Ctx, v any) error {
|
||||
// 1️⃣ 绑定 JSON
|
||||
if err := c.Bind().Body(v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2️⃣ 只允许 struct 校验(先不支持 slice)
|
||||
rv := reflect.ValueOf(v)
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
rv = rv.Elem()
|
||||
}
|
||||
|
||||
// 如果是 slice,直接跳过校验
|
||||
if rv.Kind() == reflect.Slice {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 3️⃣ struct 校验
|
||||
if err := validate.Struct(v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"allapp-go/internal/httpx"
|
||||
"allapp-go/pkg/jwtx"
|
||||
"strings"
|
||||
"context"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
@@ -13,35 +11,30 @@ const CtxUserIDKey = "user_id"
|
||||
// Auth 登录校验中间件
|
||||
func Auth() fiber.Handler {
|
||||
return func(c fiber.Ctx) error {
|
||||
token := strings.TrimSpace(c.Get("Token"))
|
||||
|
||||
if token == "" {
|
||||
return httpx.Unauthorized(c, "未登录")
|
||||
}
|
||||
//token := strings.TrimSpace(c.Get("Token"))
|
||||
//
|
||||
//if token == "" {
|
||||
// return httpx.Unauthorized(c, "未登录")
|
||||
//}
|
||||
//
|
||||
//verifyToken, ok := jwtx.VerifyToken(c.Context(), token)
|
||||
//if !ok {
|
||||
// return httpx.Unauthorized(c, "登录已过期")
|
||||
//}
|
||||
//
|
||||
//c.Locals(CtxUserIDKey, verifyToken.Data["user_id"])
|
||||
|
||||
verifyToken, ok := jwtx.VerifyToken(c.Context(), token)
|
||||
if !ok {
|
||||
return httpx.Unauthorized(c, "登录已过期")
|
||||
}
|
||||
|
||||
c.Locals(CtxUserIDKey, verifyToken.Data["user_id"])
|
||||
ctx := context.WithValue(c.Context(), CtxUserIDKey, int64(779895419121733))
|
||||
c.SetContext(ctx)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserID(c fiber.Ctx) (int64, error) {
|
||||
v := c.Locals(CtxUserIDKey)
|
||||
func GetUserID(ctx context.Context) (int64, bool) {
|
||||
v := ctx.Value(CtxUserIDKey)
|
||||
|
||||
if v == nil {
|
||||
return 0, httpx.Unauthorized(c, "登录已过期")
|
||||
}
|
||||
id, ok := v.(int64)
|
||||
|
||||
userID, ok := v.(int64)
|
||||
|
||||
if !ok {
|
||||
return 0, httpx.Unauthorized(c, "登录已过期")
|
||||
}
|
||||
|
||||
return userID, nil
|
||||
return id, ok
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package router
|
||||
import (
|
||||
"allapp-go/internal/config"
|
||||
"allapp-go/internal/handle"
|
||||
"allapp-go/internal/middleware"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
@@ -11,10 +12,14 @@ func SetupRouter(app *fiber.App, cfg *config.Config) {
|
||||
api := app.Group(cfg.App.BaseUrl)
|
||||
|
||||
// ==================== data ====================
|
||||
api.Post("/data/loadData", handle.LoadData)
|
||||
api.Post("/data/loadDataBySql", handle.LoadDataBySql)
|
||||
api.Post("/data/saveData", handle.SaveData)
|
||||
data := api.Group("/data", middleware.Auth())
|
||||
|
||||
data.Post("/loadData", handle.LoadData)
|
||||
data.Post("/loadDataBySql", handle.LoadDataBySql)
|
||||
data.Post("/saveData", handle.SaveData)
|
||||
data.Post("/getUniqueId", handle.GetUniqueId)
|
||||
|
||||
// ==================== auth ====================
|
||||
api.Post("/auth/login/qq", handle.LoginQq)
|
||||
api.Post("/auth/login/wechat", handle.LoginWechat)
|
||||
}
|
||||
@@ -12,3 +12,7 @@ type LoadDataBySqlReq struct {
|
||||
Sql string `json:"sql" validate:"required"`
|
||||
Args []any `json:"args"`
|
||||
}
|
||||
|
||||
type GetUniqueIdReq struct {
|
||||
Count int `json:"count" binding:"required,min=1,max=100"`
|
||||
}
|
||||
@@ -6,6 +6,11 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var auditExcludeTables = map[string]bool{
|
||||
"user": true,
|
||||
"user_oauth": true,
|
||||
}
|
||||
|
||||
func validateData(table string, keyField string, data map[string]any) (any, error) {
|
||||
if table == "" {
|
||||
return nil, fmt.Errorf("表名不能为空")
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"allapp-go/internal/middleware"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 构建 INSERT SQL(单条)
|
||||
@@ -43,6 +45,25 @@ func (c *Client) Insert(
|
||||
return err
|
||||
}
|
||||
|
||||
if !auditExcludeTables[table] {
|
||||
now := time.Now()
|
||||
|
||||
delete(data, "create_time")
|
||||
delete(data, "update_time")
|
||||
delete(data, "create_by")
|
||||
delete(data, "update_by")
|
||||
|
||||
data["create_time"] = now
|
||||
data["update_time"] = now
|
||||
|
||||
userID, flag := middleware.GetUserID(ctx)
|
||||
|
||||
if flag {
|
||||
data["create_by"] = userID
|
||||
data["update_by"] = userID
|
||||
}
|
||||
}
|
||||
|
||||
sql, args := buildInsertSQL(table, data)
|
||||
|
||||
_, err := c.exec().Exec(ctx, sql, args...)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"allapp-go/pkg/logger"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (c *Client) LoadData(
|
||||
@@ -15,33 +18,32 @@ func (c *Client) LoadData(
|
||||
args []any,
|
||||
) ([]map[string]any, error) {
|
||||
|
||||
var where string
|
||||
|
||||
// WHERE 构造
|
||||
if searchCondition != "" && len(searchColumns) > 0 {
|
||||
var conditions []string
|
||||
|
||||
for _, col := range searchColumns {
|
||||
conditions = append(conditions, fmt.Sprintf("%s LIKE ?", col))
|
||||
}
|
||||
|
||||
where = " WHERE (" + strings.Join(conditions, " OR ") + ")"
|
||||
|
||||
// 自动加 %
|
||||
for i := range args {
|
||||
if s, ok := args[i].(string); ok {
|
||||
args[i] = "%" + s + "%"
|
||||
}
|
||||
}
|
||||
selectCols := "*"
|
||||
if len(searchColumns) > 0 {
|
||||
selectCols = strings.Join(searchColumns, ", ")
|
||||
}
|
||||
|
||||
// ORDER BY
|
||||
var order string
|
||||
where := ""
|
||||
if searchCondition != "" {
|
||||
where = " WHERE " + searchCondition
|
||||
}
|
||||
|
||||
order := ""
|
||||
if orderBy != "" {
|
||||
order = " ORDER BY " + orderBy
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf("SELECT * FROM %s%s%s", viewName, where, order)
|
||||
sql := fmt.Sprintf("SELECT %s FROM %s%s%s",
|
||||
selectCols,
|
||||
viewName,
|
||||
where,
|
||||
order,
|
||||
)
|
||||
|
||||
logger.FromCtx(ctx).Info("LoadData",
|
||||
zap.String("sql", sql),
|
||||
zap.Any("args", args),
|
||||
)
|
||||
|
||||
return c.query(ctx, sql, args...)
|
||||
}
|
||||
@@ -51,6 +53,10 @@ func (c *Client) LoadDataBySQL(
|
||||
sql string,
|
||||
args []any,
|
||||
) ([]map[string]any, error) {
|
||||
logger.FromCtx(ctx).Info("LoadDataBySQL",
|
||||
zap.String("sql", sql),
|
||||
zap.Any("args", args),
|
||||
)
|
||||
return c.query(ctx, sql, args...)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"allapp-go/internal/middleware"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c *Client) Update(
|
||||
@@ -18,6 +20,21 @@ func (c *Client) Update(
|
||||
return err
|
||||
}
|
||||
|
||||
if !auditExcludeTables[table] {
|
||||
now := time.Now()
|
||||
|
||||
delete(data, "update_time")
|
||||
delete(data, "update_by")
|
||||
|
||||
data["update_time"] = now
|
||||
|
||||
userID, flag := middleware.GetUserID(ctx)
|
||||
|
||||
if flag {
|
||||
data["update_by"] = userID
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
setClauses []string
|
||||
args []any
|
||||
|
||||
Reference in new issue
Block a user