diff --git a/app/admin/apis/system/role.go b/app/admin/apis/system/role.go index 8a97c04..b235564 100644 --- a/app/admin/apis/system/role.go +++ b/app/admin/apis/system/role.go @@ -2,6 +2,7 @@ package system import ( "github.com/gin-gonic/gin" + "go-admin/app/admin/service/dto" "go-admin/app/admin/models" "go-admin/common/global" @@ -126,9 +127,12 @@ func UpdateRole(c *gin.Context) { func UpdateRoleDataScope(c *gin.Context) { var data models.SysRole + var req dto.RoleDataScopeReq data.UpdateBy = tools.GetUserIdStr(c) - err := c.Bind(&data) + err := c.Bind(&req) tools.HasError(err, "数据解析失败", -1) + data.RoleId = req.RoleId + data.DataScope = req.DataScope result, err := data.Update(data.RoleId) var t models.SysRoleDept diff --git a/app/admin/models/sys_file_dir.go b/app/admin/models/sys_file_dir.go index 7d28a84..d2f6c9e 100644 --- a/app/admin/models/sys_file_dir.go +++ b/app/admin/models/sys_file_dir.go @@ -6,10 +6,11 @@ import ( type SysFileDir struct { models.Model - Label string `json:"label" gorm:"type:varchar(255);comment:目录名称"` // 目录名称 - PId int `json:"pId" gorm:"type:int(11);comment:上级目录"` // 上级目录 - Sort string `json:"sort" gorm:"type:bigint(20);comment:排序"` // 排序 - Path string `json:"path" gorm:"type:varchar(255);comment:路径"` // 路径 + Label string `json:"label" gorm:"type:varchar(255);comment:目录名称"` // 目录名称 + PId int `json:"pId" gorm:"type:int(11);comment:上级目录"` // 上级目录 + Sort string `json:"sort" gorm:"type:bigint(20);comment:排序"` // 排序 + Path string `json:"path" gorm:"type:varchar(255);comment:路径"` // 路径 + Children []SysFileDir `json:"children,omitempty" gorm:"-"` // 下级信息 models.ControlBy models.ModelTime } @@ -22,7 +23,7 @@ type SysFileDirL struct { Path string `json:"path" gorm:"type:varchar(255);comment:路径"` // 路径 models.ControlBy models.ModelTime - Children []SysFileDirL `json:"children" gorm:"-"` // 下级信息 + Children []SysFileDirL `json:"children,omitempty" gorm:"-"` // 下级信息 } func (SysFileDir) TableName() string { /**/ diff --git a/app/admin/models/sys_meun.go b/app/admin/models/sys_menu.go similarity index 100% rename from app/admin/models/sys_meun.go rename to app/admin/models/sys_menu.go diff --git a/app/admin/models/syscontent.go b/app/admin/models/syscontent.go index cfa9302..c68a54b 100644 --- a/app/admin/models/syscontent.go +++ b/app/admin/models/syscontent.go @@ -106,7 +106,7 @@ func (e *SysContent) Update(id int) (update SysContent, err error) { //参数1:是要修改的数据 //参数2:是修改的数据 - if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil { + if err = orm.Eloquent.Table(e.TableName()).Model(&update).Where("id = ?", id).Updates(&e).Error; err != nil { return } return diff --git a/app/admin/models/system/sys_login_log.go b/app/admin/models/system/sys_login_log.go index 85719bb..74cf118 100644 --- a/app/admin/models/system/sys_login_log.go +++ b/app/admin/models/system/sys_login_log.go @@ -8,18 +8,19 @@ import ( type SysLoginLog struct { models.Model - Username string `json:"username" gorm:"type:varchar(128);comment:用户名"` // - Status string `json:"status" gorm:"type:varchar(4);comment:状态"` // - Ipaddr string `json:"ipaddr" gorm:"type:varchar(255);comment:ip地址"` // - LoginLocation string `json:"loginLocation" gorm:"type:varchar(255);comment:归属地"` // - Browser string `json:"browser" gorm:"type:varchar(255);comment:浏览器"` // - Os string `json:"os" gorm:"type:varchar(255);comment:系统"` // - Platform string `json:"platform" gorm:"type:varchar(255);comment:固件"` // - LoginTime time.Time `json:"loginTime" gorm:"type:timestamp;comment:登录时间"` // - Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"` // - Msg string `json:"msg" gorm:"type:varchar(255);comment:信息"` // + Username string `json:"username" gorm:"type:varchar(128);comment:用户名"` + Status string `json:"status" gorm:"type:varchar(4);comment:状态"` + Ipaddr string `json:"ipaddr" gorm:"type:varchar(255);comment:ip地址"` + LoginLocation string `json:"loginLocation" gorm:"type:varchar(255);comment:归属地"` + Browser string `json:"browser" gorm:"type:varchar(255);comment:浏览器"` + Os string `json:"os" gorm:"type:varchar(255);comment:系统"` + Platform string `json:"platform" gorm:"type:varchar(255);comment:固件"` + LoginTime time.Time `json:"loginTime" gorm:"type:timestamp;comment:登录时间"` + Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"` + Msg string `json:"msg" gorm:"type:varchar(255);comment:信息"` + CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"` + UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"` models.ControlBy - models.ModelTime } func (SysLoginLog) TableName() string { diff --git a/app/admin/models/system/sys_opera_log.go b/app/admin/models/system/sys_opera_log.go index 5029a2e..f238435 100644 --- a/app/admin/models/system/sys_opera_log.go +++ b/app/admin/models/system/sys_opera_log.go @@ -8,26 +8,27 @@ import ( type SysOperaLog struct { models.Model - Title string `json:"title" gorm:"type:varchar(255);comment:操作模块"` // - BusinessType string `json:"businessType" gorm:"type:varchar(128);comment:操作类型"` // - BusinessTypes string `json:"businessTypes" gorm:"type:varchar(128);comment:BusinessTypes"` // - Method string `json:"method" gorm:"type:varchar(128);comment:函数"` // - RequestMethod string `json:"requestMethod" gorm:"type:varchar(128);comment:请求方式"` // - OperatorType string `json:"operatorType" gorm:"type:varchar(128);comment:操作类型"` // - OperName string `json:"operName" gorm:"type:varchar(128);comment:操作者"` // - DeptName string `json:"deptName" gorm:"type:varchar(128);comment:部门名称"` // - OperUrl string `json:"operUrl" gorm:"type:varchar(255);comment:访问地址"` // - OperIp string `json:"operIp" gorm:"type:varchar(128);comment:客户端ip"` // - OperLocation string `json:"operLocation" gorm:"type:varchar(128);comment:访问位置"` // - OperParam string `json:"operParam" gorm:"type:varchar(255);comment:请求参数"` // - Status string `json:"status" gorm:"type:varchar(4);comment:操作状态"` // - OperTime time.Time `json:"operTime" gorm:"type:timestamp;comment:操作时间"` // - JsonResult string `json:"jsonResult" gorm:"type:varchar(255);comment:返回数据"` // - Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"` // - LatencyTime string `json:"latencyTime" gorm:"type:varchar(128);comment:耗时"` // - UserAgent string `json:"userAgent" gorm:"type:varchar(255);comment:ua"` // + Title string `json:"title" gorm:"type:varchar(255);comment:操作模块"` + BusinessType string `json:"businessType" gorm:"type:varchar(128);comment:操作类型"` + BusinessTypes string `json:"businessTypes" gorm:"type:varchar(128);comment:BusinessTypes"` + Method string `json:"method" gorm:"type:varchar(128);comment:函数"` + RequestMethod string `json:"requestMethod" gorm:"type:varchar(128);comment:请求方式"` + OperatorType string `json:"operatorType" gorm:"type:varchar(128);comment:操作类型"` + OperName string `json:"operName" gorm:"type:varchar(128);comment:操作者"` + DeptName string `json:"deptName" gorm:"type:varchar(128);comment:部门名称"` + OperUrl string `json:"operUrl" gorm:"type:varchar(255);comment:访问地址"` + OperIp string `json:"operIp" gorm:"type:varchar(128);comment:客户端ip"` + OperLocation string `json:"operLocation" gorm:"type:varchar(128);comment:访问位置"` + OperParam string `json:"operParam" gorm:"type:varchar(255);comment:请求参数"` + Status string `json:"status" gorm:"type:varchar(4);comment:操作状态"` + OperTime time.Time `json:"operTime" gorm:"type:timestamp;comment:操作时间"` + JsonResult string `json:"jsonResult" gorm:"type:varchar(255);comment:返回数据"` + Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"` + LatencyTime string `json:"latencyTime" gorm:"type:varchar(128);comment:耗时"` + UserAgent string `json:"userAgent" gorm:"type:varchar(255);comment:ua"` + CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"` + UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"` models.ControlBy - models.ModelTime } func (SysOperaLog) TableName() string { diff --git a/app/admin/service/dto/sys_file_dir.go b/app/admin/service/dto/sys_file_dir.go index 4f9655c..01be4cc 100644 --- a/app/admin/service/dto/sys_file_dir.go +++ b/app/admin/service/dto/sys_file_dir.go @@ -74,6 +74,10 @@ func (s *SysFileDirControl) GenerateM() (common.ActiveRecord, error) { PId: s.PId, //Sort: s.Sort, Path: s.Path, + ControlBy: common.ControlBy{ + CreateBy: s.CreateBy, + UpdateBy: s.UpdateBy, + }, }, nil } diff --git a/app/admin/service/dto/sys_login_log.go b/app/admin/service/dto/sys_login_log.go index e9ad057..62ec681 100644 --- a/app/admin/service/dto/sys_login_log.go +++ b/app/admin/service/dto/sys_login_log.go @@ -96,7 +96,7 @@ func (s *SysLoginLogById) Bind(ctx *gin.Context) error { log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error()) return err } - err = ctx.ShouldBind(s) + err = ctx.ShouldBind(&s.Ids) if err != nil { log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) } diff --git a/app/admin/service/dto/sys_opera_log.go b/app/admin/service/dto/sys_opera_log.go index 34155b2..b9cf977 100644 --- a/app/admin/service/dto/sys_opera_log.go +++ b/app/admin/service/dto/sys_opera_log.go @@ -118,7 +118,7 @@ func (s *SysOperaLogById) Bind(ctx *gin.Context) error { log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error()) return err } - err = ctx.ShouldBind(s) + err = ctx.ShouldBind(&s.Ids) if err != nil { log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) } @@ -126,5 +126,5 @@ func (s *SysOperaLogById) Bind(ctx *gin.Context) error { } func (s *SysOperaLogById) SetUpdateBy(id int) { - + } diff --git a/app/admin/service/dto/sys_role.go b/app/admin/service/dto/sys_role.go index 26effbe..40e5022 100644 --- a/app/admin/service/dto/sys_role.go +++ b/app/admin/service/dto/sys_role.go @@ -42,7 +42,7 @@ func (m *SysRoleSearch) Bind(ctx *gin.Context) error { // SysConfigControl 增、改使用的结构体 type SysRoleControl struct { - RoleId int `uri:"roleId" comment:"角色编码"` // 角色编码 + RoleId int `uri:"id" comment:"角色编码"` // 角色编码 RoleName string `form:"roleName" comment:"角色名称"` // 角色名称 Status string `form:"status" comment:"状态"` // 状态 RoleKey string `form:"roleKey" comment:"角色代码"` // 角色代码 @@ -56,15 +56,15 @@ type SysRoleControl struct { // Bind 映射上下文中的结构体数据 func (s *SysRoleControl) Bind(ctx *gin.Context) error { msgID := tools.GenerateMsgIDFromContext(ctx) - err := ctx.ShouldBindUri(s) + err := ctx.ShouldBindBodyWith(s, binding.JSON) + if err != nil { + log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) + } + err = ctx.ShouldBindUri(s) if err != nil { log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error()) return err } - err = ctx.ShouldBindBodyWith(s, binding.JSON) - if err != nil { - log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error()) - } var jsonStr []byte jsonStr, err = json.Marshal(s) if err != nil { @@ -126,3 +126,9 @@ func (s *SysRoleById) Bind(ctx *gin.Context) error { func (s *SysRoleById) GenerateM() (*models.SysRole, error) { return &models.SysRole{}, nil } + +// RoleDataScopeReq 角色数据权限修改 +type RoleDataScopeReq struct { + RoleId int `json:"roleId" binding:"required"` + DataScope string `json:"dataScope" binding:"required"` +} diff --git a/app/admin/service/sys_file_dir.go b/app/admin/service/sys_file_dir.go index b0690bf..e7267e8 100644 --- a/app/admin/service/sys_file_dir.go +++ b/app/admin/service/sys_file_dir.go @@ -2,13 +2,13 @@ package service import ( "errors" + "fmt" "go-admin/app/admin/models" "go-admin/app/admin/service/dto" "go-admin/common/actions" cDto "go-admin/common/dto" "go-admin/common/log" "go-admin/common/service" - "go-admin/tools" "gorm.io/gorm" ) @@ -59,31 +59,30 @@ func (e *SysFileDir) GetSysFileDir(d cDto.Control, model *models.SysFileDir) err // InsertSysFileDir 创建SysFileDir对象 func (e *SysFileDir) InsertSysFileDir(model *dto.SysFileDirControl) error { var err error - var data models.SysFileDir + data, _ := model.GenerateM() + msgID := e.MsgID - db := e.Orm.Model(&data). - Create(model) - if db.Error != nil { + err = e.Orm.Create(data).Error + if err != nil { log.Errorf("msgID[%s] db error:%s", msgID, err) return err } - id := model.GetId() - path := "/" + tools.IntToString(id.(int)) - db = e.Orm.Model(&data). - First(&data, model.GetId()) - err = db.Error + path := fmt.Sprintf("/%d", model.ID) + //db = e.Orm.Model(&data). + // First(&data, model.GetId()) + //err = db.Error - if data.PId != 0 { + if model.PId != 0 { var dept models.SysFileDir - e.Orm.Model(&data).Where("id = ?", data.PId).First(&dept) + e.Orm.Model(&models.SysFileDir{}).Where("id = ?", model.PId).First(&dept) path = dept.Path + path } else { path = "/0" + path } //var mp = map[string]string{} //mp["path"] = path - if err := e.Orm.Model(&data).Where("id = ?", data.ID).Update("path", path).Error; err != nil { + if err := e.Orm.Model(&models.SysFileDir{}).Where("id = ?", model.ID).Update("path", path).Error; err != nil { return err } @@ -93,13 +92,13 @@ func (e *SysFileDir) InsertSysFileDir(model *dto.SysFileDirControl) error { // UpdateSysFileDir 修改SysFileDir对象 func (e *SysFileDir) UpdateSysFileDir(c *dto.SysFileDirControl, p *actions.DataPermission) error { var err error - var data models.SysFileDir + data, _ := c.GenerateM() msgID := e.MsgID - db := e.Orm.Model(&data). + db := e.Orm. Scopes( actions.Permission(data.TableName(), p), - ).Where(c.ID).Updates(c) + ).Where(c.ID).Updates(data) if db.Error != nil { log.Errorf("msgID[%s] db error:%s", msgID, err) return err diff --git a/app/admin/service/sys_login_log.go b/app/admin/service/sys_login_log.go index dc6ccd6..69064d6 100644 --- a/app/admin/service/sys_login_log.go +++ b/app/admin/service/sys_login_log.go @@ -97,7 +97,7 @@ func (e *SysLoginLog) RemoveSysLoginLog(d *dto.SysLoginLogById, c common.ActiveR msgID := e.MsgID db := e.Orm.Model(&data). - Where(d.GetId()).Delete(c) + Where(d.Ids).Delete(c) if db.Error != nil { err = db.Error log.Errorf("MsgID[%s] Delete error: %s", msgID, err) @@ -108,4 +108,4 @@ func (e *SysLoginLog) RemoveSysLoginLog(d *dto.SysLoginLogById, c common.ActiveR return err } return nil -} \ No newline at end of file +} diff --git a/app/admin/service/sys_opera_log.go b/app/admin/service/sys_opera_log.go index 7bcd999..c14f80d 100644 --- a/app/admin/service/sys_opera_log.go +++ b/app/admin/service/sys_opera_log.go @@ -95,7 +95,7 @@ func (e *SysOperaLog) RemoveSysOperaLog(d *dto.SysOperaLogById) error { var data system.SysOperaLog msgID := e.MsgID - db := e.Orm.Model(&data).Delete(&data, d.GetId()) + db := e.Orm.Model(&data).Where(d.Ids).Delete(&data) if db.Error != nil { err = db.Error log.Errorf("MsgID[%s] Delete error: %s", msgID, err) diff --git a/app/admin/service/sys_role.go b/app/admin/service/sys_role.go index 637004e..599661d 100644 --- a/app/admin/service/sys_role.go +++ b/app/admin/service/sys_role.go @@ -84,7 +84,6 @@ func (e *SysRole) UpdateSysRole(c *system.SysRole) error { } if db.RowsAffected == 0 { return errors.New("无权更新该数据") - } return nil } diff --git a/cmd/migrate/migration/version/1599190683670_migrate.go b/cmd/migrate/migration/version/1599190683670_migrate.go index 9763e5c..c5cf05e 100644 --- a/cmd/migrate/migration/version/1599190683670_migrate.go +++ b/cmd/migrate/migration/version/1599190683670_migrate.go @@ -21,17 +21,17 @@ func _1599190683670Test(db *gorm.DB, version string) error { return db.Transaction(func(tx *gorm.DB) error { list3 := []models.SysDept{ - {DeptId: 1, ParentId: 0, DeptPath: "/0/1", DeptName: "爱拓科技", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 7, ParentId: 1, DeptPath: "/0/1/7", DeptName: "研发部", Sort: 1, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 8, ParentId: 1, DeptPath: "/0/1/8", DeptName: "运维部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 9, ParentId: 1, DeptPath: "/0/1/9", DeptName: "客服部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, - {DeptId: 10, ParentId: 1, DeptPath: "/0/1/10", DeptName: "人力资源", Sort: 3, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "1", ControlBy:common.ControlBy{ CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 1, ParentId: 0, DeptPath: "/0/1", DeptName: "爱拓科技", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 7, ParentId: 1, DeptPath: "/0/1/7", DeptName: "研发部", Sort: 1, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 8, ParentId: 1, DeptPath: "/0/1/8", DeptName: "运维部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 9, ParentId: 1, DeptPath: "/0/1/9", DeptName: "客服部", Sort: 0, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "2", ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, + {DeptId: 10, ParentId: 1, DeptPath: "/0/1/10", DeptName: "人力资源", Sort: 3, Leader: "aituo", Phone: "13782218188", Email: "atuo@aituo.com", Status: "1", ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}}, } list4 := []system.SysConfig{ - {Model: common.Model{ID: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: nil}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-默认皮肤样式名称", ConfigKey: "sys_index_skinName", ConfigValue: "skin-blue", ConfigType: "Y", Remark: "蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow"}, - {Model: common.Model{ID: 2}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: nil}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "用户管理-账号初始密码", ConfigKey: "sys.user.initPassword", ConfigValue: "123456", ConfigType: "Y", Remark: "初始化密码 123456"}, - {Model: common.Model{ID: 3}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now(), DeletedAt: nil}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-侧边栏主题", ConfigKey: "sys_index_sideTheme", ConfigValue: "theme-dark", ConfigType: "Y", Remark: "深色主题theme-dark,浅色主题theme-light"}, + {Model: common.Model{ID: 1}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-默认皮肤样式名称", ConfigKey: "sys_index_skinName", ConfigValue: "skin-blue", ConfigType: "Y", Remark: "蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow"}, + {Model: common.Model{ID: 2}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "用户管理-账号初始密码", ConfigKey: "sys.user.initPassword", ConfigValue: "123456", ConfigType: "Y", Remark: "初始化密码 123456"}, + {Model: common.Model{ID: 3}, ModelTime: common.ModelTime{CreatedAt: time.Now(), UpdatedAt: time.Now()}, ControlBy: common.ControlBy{CreateBy: 1, UpdateBy: 1}, ConfigName: "主框架页-侧边栏主题", ConfigKey: "sys_index_sideTheme", ConfigValue: "theme-dark", ConfigType: "Y", Remark: "深色主题theme-dark,浅色主题theme-light"}, } list5 := []models.Post{ diff --git a/cmd/migrate/migration/version/1613720892819_migrate.go b/cmd/migrate/migration/version/1613720892819_migrate.go new file mode 100644 index 0000000..66bf690 --- /dev/null +++ b/cmd/migrate/migration/version/1613720892819_migrate.go @@ -0,0 +1,76 @@ +package version + +import ( + "go-admin/app/admin/models" + //"go-admin/app/admin/models" + "gorm.io/gorm" + "runtime" + + "go-admin/cmd/migrate/migration" + common "go-admin/common/models" +) + +func init() { + _, fileName, _, _ := runtime.Caller(0) + migration.Migrate.SetVersion(migration.GetFilename(fileName), _1613720892819Test) +} + +func _1613720892819Test(db *gorm.DB, version string) error { + return db.Transaction(func(tx *gorm.DB) error { + + // TODO: 这里开始写入要变更的内容 + t := &models.DictType{ + DictName: "内容状态", + DictType: "sys_content_status", + Status: "2", + CreateBy: "1", + UpdateBy: "1", + } + err := tx.Create(t).Error + if err != nil { + return err + } + + data := make([]models.DictData, 2) + data[0] = models.DictData{ + DictSort: 0, + DictLabel: "正常", + DictValue: "1", + DictType: "sys_content_status", + Status: "1", + CreateBy: "1", + UpdateBy: "1", + } + data[1] = models.DictData{ + DictSort: 1, + DictLabel: "禁用", + DictValue: "2", + DictType: "sys_content_status", + Status: "1", + CreateBy: "1", + UpdateBy: "1", + } + err = tx.Create(data).Error + if err != nil { + return err + } + + // TODO: 例如 修改表字段 使用过程中请删除此段代码 + //err := tx.Migrator().RenameColumn(&models.SysConfig{}, "config_id", "id") + //if err != nil { + // return err + //} + + // TODO: 例如 新增表结构 使用过程中请删除此段代码 + //err = tx.Debug().Migrator().AutoMigrate( + // new(models.CasbinRule), + // ) + //if err != nil { + // return err + //} + + return tx.Create(&common.Migration{ + Version: version, + }).Error + }) +} diff --git a/cmd/migrate/server.go b/cmd/migrate/server.go index 0cd27d5..99da7ea 100644 --- a/cmd/migrate/server.go +++ b/cmd/migrate/server.go @@ -42,10 +42,9 @@ func init() { } func run() { - usage := `start init` - fmt.Println(usage) if !generate { + fmt.Println(`start init`) //1. 读取配置 config.Setup(file.NewSource, file.WithPath(configYml)) //2. 设置日志 @@ -54,6 +53,7 @@ func run() { global.RequestLogger.Logger = logger.SetupLogger(config.LoggerConfig.Path, "request") _ = initDB() } else { + fmt.Println(`generate migration file`) _ = genFile() } } diff --git a/common/dto/generate.go b/common/dto/generate.go index f8f7d6c..ec70b9b 100644 --- a/common/dto/generate.go +++ b/common/dto/generate.go @@ -13,7 +13,7 @@ type ObjectById struct { func (s *ObjectById) Bind(ctx *gin.Context) error { if ctx.Request.Method == http.MethodDelete { - err := ctx.Bind(s) + err := ctx.ShouldBind(&s.Ids) if err != nil { return err } @@ -21,7 +21,7 @@ func (s *ObjectById) Bind(ctx *gin.Context) error { return nil } } - return ctx.BindUri(s) + return ctx.ShouldBindUri(s) } func (s *ObjectById) GetId() interface{} { diff --git a/common/models/by.go b/common/models/by.go index 442fec9..48e31ad 100644 --- a/common/models/by.go +++ b/common/models/by.go @@ -1,6 +1,9 @@ package models -import "time" +import ( + "gorm.io/gorm" + "time" +) type ControlBy struct { CreateBy int `json:"createBy" gorm:"index;comment:创建者"` @@ -15,14 +18,12 @@ func (e *ControlBy) SetUpdateBy(updateBy int) { e.UpdateBy = updateBy } - type Model struct { ID int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"` } type ModelTime struct { - CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"` - UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"` - DeletedAt *time.Time `json:"-" gorm:"index;comment:删除时间"` + CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"` + UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"` } -