refactor(initdb): 重构数据库初始化逻辑

- 修改 FillInitData 函数签名,增加 ismod 参数用于指示是否成功插入数据
- 在 base 模块初始化中使用 If 函数处理数据库初始化结果
- 优化 login 模块中的日志输出
- 修复 websocket 中的错误处理
- 更新 dict 和 task 模块的初始化逻辑
This commit is contained in:
2025-07-11 18:04:15 +08:00
parent 67605778dd
commit c1065062fb
6 changed files with 41 additions and 24 deletions

View File

@@ -61,33 +61,37 @@ func CreateTable(model IModel) error {
} }
// FillInitData 数据库填充初始数据 // FillInitData 数据库填充初始数据
func FillInitData(ctx g.Ctx, moduleName string, model IModel) error { func FillInitData(ctx g.Ctx, moduleName string, model IModel, ismod *bool) (bool, error) {
mInit := g.DB("default").Model("base_sys_init") mInit := g.DB("default").Model("base_sys_init")
n, err := mInit.Clone().Where("group", model.GroupName()).Where("table", model.TableName()).Count() n, err := mInit.Clone().Where("group", model.GroupName()).Where("table", model.TableName()).Count()
if err != nil { if err != nil {
g.Log().Error(ctx, "读取表 base_sys_init 失败 ", err.Error()) g.Log().Error(ctx, "读取表 base_sys_init 失败 ", err.Error())
return err return false, err
} }
if n > 0 { if n > 0 {
g.Log().Debug(ctx, "分组", model.GroupName(), "中的表", model.TableName(), "已经初始化过,跳过本次初始化.") g.Log().Debug(ctx, "分组", model.GroupName(), "中的表", model.TableName(), "已经初始化过,跳过本次初始化.")
return nil return false, err
} }
m := g.DB(model.GroupName()).Model(model.TableName()) m := g.DB(model.GroupName()).Model(model.TableName())
jsonData, _ := gjson.LoadContent(gres.GetContent("modules/" + moduleName + "/resource/initjson/" + model.TableName() + ".json")) jsonData, _ := gjson.LoadContent(gres.GetContent("modules/" + moduleName + "/resource/initjson/" + model.TableName() + ".json"))
if jsonData.Var().Clone().IsEmpty() { if jsonData.Var().Clone().IsEmpty() {
g.Log().Debug(ctx, "分组", model.GroupName(), "中的表", model.TableName(), "无可用的初始化数据,跳过本次初始化.") g.Log().Debug(ctx, "分组", model.GroupName(), "中的表", model.TableName(), "无可用的初始化数据,跳过本次初始化.")
return nil return false, err
} }
_, err = m.Data(jsonData).Insert() _, err = m.Data(jsonData).Insert()
if err != nil { if err != nil {
g.Log().Error(ctx, err.Error()) g.Log().Error(ctx, err.Error())
return err return false, err
} }
_, err = mInit.Insert(g.Map{"group": model.GroupName(), "table": model.TableName()}) _, err = mInit.Insert(g.Map{"group": model.GroupName(), "table": model.TableName()})
if err != nil { if err != nil {
g.Log().Error(ctx, err.Error()) g.Log().Error(ctx, err.Error())
return err return false, err
} }
g.Log().Info(ctx, "分组", model.GroupName(), "中的表", model.TableName(), "初始化完成.") g.Log().Info(ctx, "分组", model.GroupName(), "中的表", model.TableName(), "初始化完成.")
return nil if ismod != nil {
*ismod = true
}
return true, err
} }

View File

@@ -12,7 +12,6 @@ import (
"github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd" "github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/glog"
) )
var ( var (
@@ -46,6 +45,6 @@ var (
) )
func beforeServeHook(r *ghttp.Request) { func beforeServeHook(r *ghttp.Request) {
glog.Debugf(r.GetCtx(), "beforeServeHook [is file:%v] URI:%s", r.IsFileRequest(), r.RequestURI) //glog.Debugf(r.GetCtx(), "beforeServeHook [is file:%v] URI:%s", r.IsFileRequest(), r.RequestURI)
r.Response.CORSDefault() r.Response.CORSDefault()
} }

View File

@@ -17,23 +17,36 @@ import (
"github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/util/gconv"
) )
// trueVal: 条件为 true 时返回的值
// falseVal: 条件为 false 时返回的值
// 返回值类型与 trueVal、falseVal 一致(需保证两者类型相同)
func If[T any](condition bool, trueVal, falseVal T) T {
if condition {
return trueVal
}
return falseVal
}
func init() { func init() {
var ( var (
ctx = gctx.GetInitCtx() ctx = gctx.GetInitCtx()
) )
g.Log().Debug(ctx, "module base init start ...") g.Log().Debug(ctx, "module base init start ...")
var t bool
cool.FillInitData(ctx, "base", &model.BaseSysMenu{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysUser{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysUserRole{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysRole{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysRoleMenu{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysDepartment{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysRoleDepartment{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysParam{}, &t)
cool.FillInitData(ctx, "base", &model.BaseSysConf{}, &t)
if t {
ResetAllSequences(ctx)
}
cool.FillInitData(ctx, "base", &model.BaseSysMenu{})
//fmt.Println(err)
cool.FillInitData(ctx, "base", &model.BaseSysUser{})
cool.FillInitData(ctx, "base", &model.BaseSysUserRole{})
cool.FillInitData(ctx, "base", &model.BaseSysRole{})
cool.FillInitData(ctx, "base", &model.BaseSysRoleMenu{})
cool.FillInitData(ctx, "base", &model.BaseSysDepartment{})
cool.FillInitData(ctx, "base", &model.BaseSysRoleDepartment{})
cool.FillInitData(ctx, "base", &model.BaseSysParam{})
cool.FillInitData(ctx, "base", &model.BaseSysConf{})
ResetAllSequences(ctx)
// g.DB("default").Exec(context.Background(), `CREATE OR REPLACE FUNCTION reset_all_sequences() // g.DB("default").Exec(context.Background(), `CREATE OR REPLACE FUNCTION reset_all_sequences()
// RETURNS void AS $$ // RETURNS void AS $$
// DECLARE // DECLARE

View File

@@ -43,7 +43,7 @@ func (c *Handler) OnOpen(socket *gws.Conn) {
packlen, err := reader.Peek(4) packlen, err := reader.Peek(4)
if err != nil { if err != nil {
socket.WriteClose(1000, nil)
break LOOP break LOOP
} }
@@ -64,6 +64,7 @@ func (c *Handler) OnOpen(socket *gws.Conn) {
//err = <-errChan //err = <-errChan
if err != io.EOF { if err != io.EOF {
log.Println("proxy error:", err) log.Println("proxy error:", err)
} }
} }

View File

@@ -17,7 +17,7 @@ func init() {
ctx = gctx.GetInitCtx() ctx = gctx.GetInitCtx()
) )
g.Log().Debug(ctx, "module dict init start ...") g.Log().Debug(ctx, "module dict init start ...")
cool.FillInitData(ctx, "dict", &model.DictInfo{}) cool.FillInitData(ctx, "dict", &model.DictInfo{}, nil)
cool.FillInitData(ctx, "dict", &model.DictType{}) cool.FillInitData(ctx, "dict", &model.DictType{}, nil)
g.Log().Debug(ctx, "module dict init finished ...") g.Log().Debug(ctx, "module dict init finished ...")
} }

View File

@@ -20,7 +20,7 @@ func init() {
ctx = gctx.GetInitCtx() ctx = gctx.GetInitCtx()
) )
g.Log().Debug(ctx, "module task init start ...") g.Log().Debug(ctx, "module task init start ...")
cool.FillInitData(ctx, "task", taskInfo) cool.FillInitData(ctx, "task", taskInfo, nil)
result, err := cool.DBM(taskInfo).Where("status", 1).All() result, err := cool.DBM(taskInfo).Where("status", 1).All()
if err != nil { if err != nil {