Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e47dd99816 | ||
|
|
272943d238 | ||
|
|
172c1cc506 | ||
|
|
915e1f65ec | ||
|
|
c512327358 | ||
|
|
c091ed4984 | ||
|
|
dc4dd5dc62 | ||
|
|
7e51069595 | ||
|
|
791c9905df | ||
|
|
6097209c48 |
@@ -0,0 +1 @@
|
||||
.git
|
||||
+3
-2
@@ -10,8 +10,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/bwmarrin/snowflake"
|
||||
"github.com/gogf/gf/v2/crypto/gmd5"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
@@ -439,6 +439,7 @@ var (
|
||||
|
||||
type Gfdb interface {
|
||||
GetAll(ctx context.Context, sql string, args ...any) (gdb.Result, error)
|
||||
GetOne(ctx context.Context, sql string, args ...any) (gdb.Record, error)
|
||||
Exec(ctx context.Context, sql string, args ...any) (sql.Result, error)
|
||||
Model(ctx context.Context, tableNameOrStruct ...any) *model
|
||||
Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) error
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/log/consts"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/consts"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
|
||||
+4
-4
@@ -11,12 +11,12 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/log/consts"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/consts"
|
||||
"go.mongodb.org/mongo-driver/v2/event"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/log/model/entity"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/model/entity"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
module common
|
||||
module gitea.redpowerfuture.com/red-future/common
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
|
||||
+67
-21
@@ -4,14 +4,15 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
_ "gitea.com/red-future/common/consul"
|
||||
"gitea.com/red-future/common/jaeger"
|
||||
"gitea.com/red-future/common/utils"
|
||||
_ "gitea.redpowerfuture.com/red-future/common/consul"
|
||||
"gitea.redpowerfuture.com/red-future/common/jaeger"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
@@ -69,10 +70,10 @@ func SkipMiddleware(h func(r *ghttp.Request), path string) (handler ghttp.Handle
|
||||
}
|
||||
|
||||
func RouteRegister(controllers []interface{}) {
|
||||
//Httpserver.Group("/log", func(group *ghttp.RouterGroup) {
|
||||
// group.Middleware(jaeger.NewTracer)
|
||||
// group.Bind(controller.OperationLog)
|
||||
//})
|
||||
Httpserver.Group("/log", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(jaeger.NewTracer)
|
||||
//group.Bind(controller.OperationLog)
|
||||
})
|
||||
re := regexp.MustCompile("[A-Z]")
|
||||
for _, t := range controllers {
|
||||
sName := reflect.ValueOf(t).Elem().Type().Name()
|
||||
@@ -80,19 +81,15 @@ func RouteRegister(controllers []interface{}) {
|
||||
return fmt.Sprintf("/%s", strings.ToLower(s))
|
||||
})
|
||||
Httpserver.Group(convertedStr, func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(jaeger.NewTracer)
|
||||
group.Bind(t)
|
||||
})
|
||||
}
|
||||
go Httpserver.Run()
|
||||
}
|
||||
|
||||
// doRequest 统一HTTP请求处理(DELETE用ContentJson发送body,gconv.Struct增加err检查)
|
||||
func doRequest(ctx context.Context, method string, url string, headers map[string]string, target any, data ...any) (err error) {
|
||||
err = utils.ValidStructPtr(target)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// doRequestRaw 执行HTTP请求,返回gclient.Response,调用方需自行Close
|
||||
// 统一处理:client克隆、ContentJson设置、请求头注入、GET查询参数转换
|
||||
func doRequestRaw(ctx context.Context, method string, url string, headers map[string]string, data ...any) (*gclient.Response, error) {
|
||||
client := Httpclient.Clone()
|
||||
|
||||
// POST/PUT/DELETE请求都需要显式用ContentJson序列化body
|
||||
@@ -110,9 +107,9 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
||||
// 修复:避免data...展开导致的双重包装问题
|
||||
// 当只有一个元素时,直接传递该元素,避免被包装成数组
|
||||
var response *gclient.Response
|
||||
var err error
|
||||
// 对于GET请求,将参数转换为map
|
||||
if method == http.MethodGet && len(data) > 0 && len(data)%2 == 0 {
|
||||
// 构建query参数map
|
||||
queryParams := make(map[string]string)
|
||||
for i := 0; i < len(data); i += 2 {
|
||||
if key, ok := data[i].(string); ok && i+1 < len(data) {
|
||||
@@ -126,17 +123,33 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
||||
} else {
|
||||
response, err = client.DoRequest(ctx, method, url, data...)
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
// doRequest 统一HTTP请求处理(同步/异步,解析内部API响应格式并填充target)
|
||||
func doRequest(ctx context.Context, method string, url string, headers map[string]string, target any, respParse bool, data ...any) (res []byte, err error) {
|
||||
if target != nil || respParse {
|
||||
err = utils.ValidStructPtr(target)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
response, err := doRequestRaw(ctx, method, url, headers, data...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer response.Close()
|
||||
result := response.ReadAll()
|
||||
|
||||
if !respParse {
|
||||
return result, nil
|
||||
}
|
||||
// 统一处理内部API响应格式:{code:200,message:"",data:{...}}
|
||||
resultStrut := &ghttp.DefaultHandlerResponse{}
|
||||
|
||||
if err = gconv.Struct(result, &resultStrut); err != nil { // 修复:增加err检查
|
||||
return errors.New("响应解析失败: " + err.Error())
|
||||
return nil, errors.New("响应解析失败: " + err.Error())
|
||||
}
|
||||
|
||||
// 添加调试日志:打印解析后的结构
|
||||
@@ -145,7 +158,7 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
||||
|
||||
if resultStrut.Code == 200 || resultStrut.Code == 0 {
|
||||
if err = gconv.Struct(resultStrut.Data, target); err != nil { // 修复:增加err检查
|
||||
return errors.New("数据解析失败: " + err.Error())
|
||||
return nil, errors.New("数据解析失败: " + err.Error())
|
||||
}
|
||||
// 添加调试日志:打印最终的target
|
||||
g.Log().Debugf(ctx, "[HTTP] 最终target: %+v", target)
|
||||
@@ -154,19 +167,52 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Get(ctx context.Context, url string, headers map[string]string, target any, data ...any) (err error) {
|
||||
err = doRequest(ctx, http.MethodGet, url, headers, target, data...)
|
||||
_, err = doRequest(ctx, http.MethodGet, url, headers, target, true, data...)
|
||||
return
|
||||
}
|
||||
func Post(ctx context.Context, url string, headers map[string]string, target any, data ...any) (err error) {
|
||||
err = doRequest(ctx, http.MethodPost, url, headers, target, data...)
|
||||
_, err = doRequest(ctx, http.MethodPost, url, headers, target, true, data...)
|
||||
return
|
||||
}
|
||||
func Put(ctx context.Context, url string, headers map[string]string, target any, data ...any) (err error) {
|
||||
err = doRequest(ctx, http.MethodPut, url, headers, target, data...)
|
||||
_, err = doRequest(ctx, http.MethodPut, url, headers, target, true, data...)
|
||||
return
|
||||
}
|
||||
func Delete(ctx context.Context, url string, headers map[string]string, target any, data ...any) (err error) {
|
||||
err = doRequest(ctx, http.MethodDelete, url, headers, target, data...)
|
||||
_, err = doRequest(ctx, http.MethodDelete, url, headers, target, true, data...)
|
||||
return
|
||||
}
|
||||
|
||||
func GetNotParse(ctx context.Context, url string, headers map[string]string, data ...any) (res []byte, err error) {
|
||||
res, err = doRequest(ctx, http.MethodGet, url, headers, nil, false, data...)
|
||||
return
|
||||
}
|
||||
func PostNotParse(ctx context.Context, url string, headers map[string]string, data ...any) (res []byte, err error) {
|
||||
res, err = doRequest(ctx, http.MethodPost, url, headers, nil, false, data...)
|
||||
return
|
||||
}
|
||||
|
||||
// DoStream 流式HTTP请求,返回响应体io.ReadCloser,由调用方自行控制读取和关闭
|
||||
// 注意:返回的是原始响应流,不会解析内部API响应格式,调用方必须在使用后Close
|
||||
func doStream(ctx context.Context, method string, url string, headers map[string]string, data ...any) (io.ReadCloser, error) {
|
||||
response, err := doRequestRaw(ctx, method, url, headers, data...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查HTTP状态码,提前返回错误
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
bodyBytes, _ := io.ReadAll(response.Body)
|
||||
response.Close()
|
||||
return nil, fmt.Errorf("[HTTP][Stream] 状态码异常: %d, body=%s", response.StatusCode, string(bodyBytes))
|
||||
}
|
||||
|
||||
return response.Body, nil
|
||||
}
|
||||
|
||||
// PostStream POST流式请求,返回响应体io.ReadCloser
|
||||
func PostStream(ctx context.Context, url string, headers map[string]string, data ...any) (io.ReadCloser, error) {
|
||||
return doStream(ctx, http.MethodPost, url, headers, data...)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/log/model/dto"
|
||||
"gitea.com/red-future/common/log/service"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/service"
|
||||
)
|
||||
|
||||
type operationLog struct{}
|
||||
|
||||
+5
-5
@@ -3,15 +3,15 @@ package dao
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.com/red-future/common/db/mongo"
|
||||
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/log/consts"
|
||||
"gitea.com/red-future/common/log/model/dto"
|
||||
"gitea.com/red-future/common/log/model/entity"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/consts"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/model/entity"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
)
|
||||
|
||||
// OperationLog 操作日志实体 - 用于记录数据增删改操作行为
|
||||
|
||||
@@ -2,11 +2,11 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/log/dao"
|
||||
"gitea.com/red-future/common/log/model/dto"
|
||||
logEntity "gitea.com/red-future/common/log/model/entity"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/dao"
|
||||
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
|
||||
logEntity "gitea.redpowerfuture.com/red-future/common/log/model/entity"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/utils"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/alibaba/sentinel-golang/api"
|
||||
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitea.com/red-future/common/beans"
|
||||
commonHttp "gitea.com/red-future/common/http"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||
commonHttp "gitea.redpowerfuture.com/red-future/common/http"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.com/red-future/common/utils"
|
||||
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@ package swagger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea.com/red-future/common/consul"
|
||||
"gitea.com/red-future/common/http"
|
||||
"gitea.redpowerfuture.com/red-future/common/consul"
|
||||
"gitea.redpowerfuture.com/red-future/common/http"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
|
||||
+3
-2
@@ -13,7 +13,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.redpowerfuture.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"
|
||||
@@ -583,7 +583,8 @@ func GetLocalBaseURL(ctx context.Context) string {
|
||||
|
||||
// GetCallbackURL 获取回调地址(完整 URL)
|
||||
func GetCallbackURL(ctx context.Context, path string) string {
|
||||
baseURL := GetLocalBaseURL(ctx)
|
||||
//baseURL := GetLocalBaseURL(ctx)
|
||||
baseURL := "http://" + GetLocalAddress(ctx)
|
||||
// 确保 path 以 / 开头
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
|
||||
Reference in New Issue
Block a user