Compare commits

..

3 Commits

Author SHA1 Message Date
ayflying
2cb005e8ed 未读取到配置报错提示 2025-05-14 16:41:53 +08:00
ayflying
374fdac477 进行主机查找配置文件,最高5级目录 2025-05-14 16:22:07 +08:00
ayflying
595ababfde 更新路径拼接 2025-05-14 16:17:00 +08:00

View File

@@ -9,6 +9,7 @@ import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gres"
"github.com/gogf/gf/v2/text/gstr"
@@ -86,14 +87,22 @@ func (c *Cfg) GetFile(filename string, _pathStr ...string) (jsonObj *gjson.Json,
bytes = gres.GetContent(filePath) // 从打包资源中获取内容
}
//如果还是没有读取到配置,从当前目录返回上级读取
if bytes == nil {
// 拼接完整的文件路径
filePath = "../../" + pathStr + filename + ".json"
if gfile.IsFile(filePath) {
bytes = gfile.GetBytes(filePath) // 读取物理文件内容
for range 5 {
//如果还是没有读取到配置,从当前目录返回上级读取
if bytes == nil {
// 上级拼接完整的文件路径
filePath = "../" + filePath
if gfile.IsFile(filePath) {
bytes = gfile.GetBytes(filePath) // 读取物理文件内容
//找到配置了,跳过
break
}
}
}
if bytes == nil {
g.Log().Errorf(gctx.New(), "未读取到配置文件:%v", filePath)
}
// 解析 JSON 内容并返回结果
jsonObj, err = gjson.DecodeToJson(bytes)
return