Compare commits

...

3 Commits

Author SHA1 Message Date
black1552 60f0d8053d fix(database): 优化SQLite数据库初始化逻辑
- 在创建数据库文件前先检查文件是否存在
- 避免重复创建已存在的数据库文件
- 更新项目配置文件中的文件修改时间戳
2026-02-05 10:30:56 +08:00
black1552 258112d38e feat(valid): 更新验证功能支持多种绑定方式
- 修改 ValidToStruct 函数使用 c.Bind 替代 c.BindJSON
- 新增 ValidToMap 函数支持将参数验证后转换为 map 类型
- 更新 ValidToStructAndMap 函数使用 c.Bind 进行参数绑定
- 添加相应的错误处理和类型转换逻辑
- 更新项目缓存配置文件中的文件扫描路径信息
2026-02-04 16:19:36 +08:00
black1552 8927551779 refactor(valid): 重构验证函数命名和返回值
- 将 ValidAndStruct 函数重命名为 ValidToStruct
- 将 ValidAndMap 函数重命名为 ValidToStructAndMap
- 修改 ValidToStructAndMap 函数返回结构体和映射两个值
- 更新 .idea/GOHCache.xml 中的文件修改时间戳
2026-02-04 15:05:09 +08:00
3 changed files with 70 additions and 13 deletions

View File

@ -90,6 +90,7 @@
<entry key="NewsOne">
<value>
<set>
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
</set>
</value>
@ -97,6 +98,7 @@
<entry key="NewsSave">
<value>
<set>
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
</set>
</value>
@ -199,7 +201,7 @@
<entry key="file://$PROJECT_DIR$/database/database.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770100613057" />
<option name="lastModified" value="1770258641711" />
</ScannedPath>
</value>
</entry>
@ -340,7 +342,7 @@
<entry key="file://$PROJECT_DIR$/valid/valid.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770188345045" />
<option name="lastModified" value="1770197737203" />
</ScannedPath>
</value>
</entry>
@ -366,6 +368,47 @@
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/api/home.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770190095707" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/api/new.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191206242" />
<option name="schema">
<list>
<option value="NewsOne" />
<option value="NewsSave" />
</list>
</option>
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/houeRouter.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191001042" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/index.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770255253603" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/router.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770190557848" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/req/new.go">
<value>
<ScannedPath>
@ -382,7 +425,7 @@
<entry key="file://$PROJECT_DIR$/../gin_test/router/router.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770187001756" />
<option name="lastModified" value="1770191024510" />
</ScannedPath>
</value>
</entry>

View File

@ -68,10 +68,12 @@ func mysqlInit() {
}
func sqliteInit() {
_, err = gfile.Create(dns.String())
if err != nil {
log.Error("创建数据库文件失败: ", err)
return
if !gfile.Exists(dns.String()) {
_, err = gfile.Create(dns.String())
if err != nil {
log.Error("创建数据库文件失败: ", err)
return
}
}
Type = sqlite.Open(fmt.Sprintf("%s?cache=shared&mode=rwc&_busy_timeout=10000&_fk=1&_journal=WAL&_sync=FULL", dns.String()))
}

View File

@ -7,10 +7,10 @@ import (
"github.com/gogf/gf/v2/util/gconv"
)
// ValidAndStruct 验证参数并返回结构体
func ValidAndStruct[T any](c *gin.Context) (object *T) {
// ValidToStruct 验证参数并返回结构体
func ValidToStruct[T any](c *gin.Context) (object *T) {
obj := new(T)
if err := c.BindJSON(obj); err != nil {
if err := c.Bind(obj); err != nil {
panic(err)
}
if err := g.Validator().Data(obj).Run(c); err != nil {
@ -19,10 +19,10 @@ func ValidAndStruct[T any](c *gin.Context) (object *T) {
return obj
}
// ValidAndMap 验证参数并返回map
func ValidAndMap[T any](c *gin.Context) (object map[string]any) {
// ValidToMap 验证参数并返回结构体
func ValidToMap[T any](c *gin.Context) (object map[string]any) {
obj := new(T)
if err := c.BindJSON(obj); err != nil {
if err := c.Bind(obj); err != nil {
panic(err)
}
if err := g.Validator().Data(obj).Run(c); err != nil {
@ -30,3 +30,15 @@ func ValidAndMap[T any](c *gin.Context) (object map[string]any) {
}
return gconv.Map(obj)
}
// ValidToStructAndMap 验证参数并返回map
func ValidToStructAndMap[T any](c *gin.Context) (stru *T, object map[string]any) {
obj := new(T)
if err := c.Bind(obj); err != nil {
panic(err)
}
if err := g.Validator().Data(obj).Run(c); err != nil {
panic(gerror.Current(err).Error())
}
return obj, gconv.Map(obj)
}