Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现对不可编辑字段的处理(前端暂未对file、iconpicker进行处理) #34

Merged
merged 9 commits into from May 25, 2019
23 changes: 11 additions & 12 deletions admincli/cli.go
Expand Up @@ -337,9 +337,10 @@ import (
"github.com/chenhg5/go-admin/plugins/admin/models"
)

func Get` + strings.Title(table) + `Table() (` + table + `Table models.Table) {
func Get` + strings.Title(table) + `Table() models.Table {

` + table + `Table.Info.FieldList = []types.Field{`
` + table + `Table := models.NewDefaultTable("` + driver + `", true, true, true)
` + table + `Table.GetInfo().FieldList = []types.Field{`

for _, model := range columnsModel {
content += `{
Expand All @@ -355,11 +356,11 @@ func Get` + strings.Title(table) + `Table() (` + table + `Table models.Table) {

content += `}

` + table + `Table.Info.Table = "` + table + `"
` + table + `Table.Info.Title = "` + strings.Title(table) + `"
` + table + `Table.Info.Description = "` + strings.Title(table) + `"
` + table + `Table.GetInfo().Table = "` + table + `"
` + table + `Table.GetInfo().Title = "` + strings.Title(table) + `"
` + table + `Table.GetInfo().Description = "` + strings.Title(table) + `"

` + table + `Table.Form.FormList = []types.Form{`
` + table + `Table.GetForm().FormList = []types.Form{`

for _, model := range columnsModel {

Expand All @@ -383,14 +384,12 @@ func Get` + strings.Title(table) + `Table() (` + table + `Table models.Table) {

content += ` }

` + table + `Table.Form.Table = "` + table + `"
` + table + `Table.Form.Title = "` + strings.Title(table) + `"
` + table + `Table.Form.Description = "` + strings.Title(table) + `"
` + table + `Table.GetForm().Table = "` + table + `"
` + table + `Table.GetForm().Title = "` + strings.Title(table) + `"
` + table + `Table.GetForm().Description = "` + strings.Title(table) + `"

` + table + `Table.Editable = true
` + table + `Table.ConnectionDriver = "` + driver + `"

return
return ` + table + `Table
}`

err := ioutil.WriteFile(outputPath+"/"+table+".go", []byte(content), 0644)
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/controller/delete.go
Expand Up @@ -9,7 +9,7 @@ import (

func DeleteData(ctx *context.Context) {
prefix := ctx.Query("prefix")
if !models.TableList[prefix].Deletable {
if !models.TableList[prefix].GetDeletable() {
ctx.Html(http.StatusNotFound, "page not found")
return
}
Expand Down
17 changes: 15 additions & 2 deletions plugins/admin/controller/edit.go
Expand Up @@ -17,7 +17,7 @@ import (
func ShowForm(ctx *context.Context) {

prefix := ctx.Query("prefix")
if !models.TableList[prefix].Editable {
if !models.TableList[prefix].GetEditable() {
ctx.Html(http.StatusNotFound, "page not found")
return
}
Expand Down Expand Up @@ -47,7 +47,7 @@ func ShowForm(ctx *context.Context) {
// 编辑数据
func EditForm(ctx *context.Context) {
prefix := ctx.Query("prefix")
if !models.TableList[prefix].Editable {
if !models.TableList[prefix].GetEditable() {
ctx.Html(http.StatusNotFound, "page not found")
return
}
Expand Down Expand Up @@ -75,6 +75,19 @@ func EditForm(ctx *context.Context) {
} else if prefix == "roles" { // 管理员角色管理编辑
EditRole((*form).Value)
} else {
val := (*form).Value
for _, f := range models.TableList[prefix].GetForm().FormList {
if f.Editable {
continue
}
if len(val[f.Field]) > 0 && f.Field != "id"{
ctx.Json(http.StatusBadRequest, map[string]interface{}{
"code": 400,
"msg": "字段[" + f.Field + "]不可编辑",
})
return
}
}
models.TableList[prefix].UpdateDataFromDatabase((*form).Value)
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/admin/controller/handler.go
Expand Up @@ -91,13 +91,13 @@ func GlobalDeferHandler(ctx *context.Context) {
buf := template.Excecute(tmpl, tmplName, user, types.Panel{
Content: alert + template.Get(Config.THEME).Form().
SetPrefix(Config.PREFIX).
SetContent(models.GetNewFormList(models.TableList[prefix].Form.FormList)).
SetContent(models.GetNewFormList(models.TableList[prefix].GetForm().FormList)).
SetUrl(Config.PREFIX+"/new/"+prefix).
SetToken(auth.TokenHelper.AddToken()).
SetInfoUrl(Config.PREFIX+"/info/"+prefix+queryParam).
GetContent(),
Description: models.TableList[prefix].Form.Description,
Title: models.TableList[prefix].Form.Title,
Description: models.TableList[prefix].GetForm().Description,
Title: models.TableList[prefix].GetForm().Title,
}, Config, menu.GetGlobalMenu(user).SetActiveClass(strings.Replace(ctx.Path(), Config.PREFIX, "", 1)))
ctx.Html(http.StatusOK, buf.String())
ctx.AddHeader("X-PJAX-URL", Config.PREFIX+"/info/"+prefix+"/new"+queryParam)
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/controller/menu.go
Expand Up @@ -207,7 +207,7 @@ func GetMenuInfoPanel(ctx *context.Context) {
SetUrl(Config.PREFIX + "/menu/new").
SetInfoUrl(Config.PREFIX + "/menu").
SetTitle("New").
SetContent(models.GetNewFormList(models.TableList["menu"].Form.FormList)).
SetContent(models.GetNewFormList(models.TableList["menu"].GetForm().FormList)).
GetContent()

col2 := template.Get(Config.THEME).Col().SetSize(map[string]string{"md": "6"}).SetContent(newForm).GetContent()
Expand Down
14 changes: 9 additions & 5 deletions plugins/admin/controller/new.go
Expand Up @@ -15,34 +15,38 @@ import (
// 显示新建表单
func ShowNewForm(ctx *context.Context) {
prefix := ctx.Query("prefix")
if !models.TableList[prefix].CanAdd {
if !models.TableList[prefix].GetCanAdd() {
ctx.Html(http.StatusNotFound, "page not found")
return
}
params := models.GetParam(ctx.Request.URL.Query())

user := auth.Auth(ctx)

formList := models.GetNewFormList(models.TableList[prefix].GetForm().FormList)
for i := 0; i < len(formList); i++ {
formList[i].Editable = true
}
tmpl, tmplName := template.Get(Config.THEME).GetTemplate(ctx.Headers("X-PJAX") == "true")
buf := template.Excecute(tmpl, tmplName, user, types.Panel{
Content: template.Get(Config.THEME).Form().
SetPrefix(Config.PREFIX).
SetContent(models.GetNewFormList(models.TableList[prefix].Form.FormList)).
SetContent(formList).
SetUrl(Config.PREFIX + "/new/" + prefix).
SetToken(auth.TokenHelper.AddToken()).
SetTitle("New").
SetInfoUrl(Config.PREFIX + "/info/" + prefix + params.GetRouteParamStr()).
GetContent(),
Description: models.TableList[prefix].Form.Description,
Title: models.TableList[prefix].Form.Title,
Description: models.TableList[prefix].GetForm().Description,
Title: models.TableList[prefix].GetForm().Title,
}, Config, menu.GetGlobalMenu(user).SetActiveClass(strings.Replace(ctx.Path(), Config.PREFIX, "", 1)))
ctx.Html(http.StatusOK, buf.String())
}

// 新建数据
func NewForm(ctx *context.Context) {
prefix := ctx.Query("prefix")
if !models.TableList[prefix].CanAdd {
if !models.TableList[prefix].GetCanAdd() {
ctx.Html(http.StatusNotFound, "page not found")
return
}
Expand Down
96 changes: 45 additions & 51 deletions plugins/admin/models/generators.go
Expand Up @@ -12,8 +12,8 @@ import (
)

func GetManagerTable() (ManagerTable Table) {

ManagerTable.Info.FieldList = []types.Field{
ManagerTable = NewDefaultTable(config.Get().DATABASE[0].DRIVER, true, true, true)
ManagerTable.GetInfo().FieldList = []types.Field{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -76,9 +76,9 @@ func GetManagerTable() (ManagerTable Table) {
},
}

ManagerTable.Info.Table = "goadmin_users"
ManagerTable.Info.Title = language.Get("Managers")
ManagerTable.Info.Description = language.Get("Managers")
ManagerTable.GetInfo().Table = "goadmin_users"
ManagerTable.GetInfo().Title = language.Get("Managers")
ManagerTable.GetInfo().Description = language.Get("Managers")

var roles, permissions []map[string]string
rolesModel, _ := db.Table("goadmin_roles").Select("id", "slug").Where("id", ">", 0).All()
Expand All @@ -97,7 +97,7 @@ func GetManagerTable() (ManagerTable Table) {
})
}

ManagerTable.Form.FormList = []types.Form{
ManagerTable.GetForm().FormList = []types.Form{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -203,18 +203,16 @@ func GetManagerTable() (ManagerTable Table) {
},
}

ManagerTable.Form.Table = "goadmin_users"
ManagerTable.Form.Title = language.Get("Managers")
ManagerTable.Form.Description = language.Get("Managers")

ManagerTable.ConnectionDriver = config.Get().DATABASE[0].DRIVER
ManagerTable.GetForm().Table = "goadmin_users"
ManagerTable.GetForm().Title = language.Get("Managers")
ManagerTable.GetForm().Description = language.Get("Managers")

return
}

func GetPermissionTable() (PermissionTable Table) {

PermissionTable.Info.FieldList = []types.Field{
PermissionTable = NewDefaultTable(config.Get().DATABASE[0].DRIVER, true, true, true)
PermissionTable.GetInfo().FieldList = []types.Field{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -280,11 +278,11 @@ func GetPermissionTable() (PermissionTable Table) {
},
}

PermissionTable.Info.Table = "goadmin_permissions"
PermissionTable.Info.Title = language.Get("Permission Manage")
PermissionTable.Info.Description = language.Get("Permission Manage")
PermissionTable.GetInfo().Table = "goadmin_permissions"
PermissionTable.GetInfo().Title = language.Get("Permission Manage")
PermissionTable.GetInfo().Description = language.Get("Permission Manage")

PermissionTable.Form.FormList = []types.Form{
PermissionTable.GetForm().FormList = []types.Form{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -367,17 +365,16 @@ func GetPermissionTable() (PermissionTable Table) {
},
}

PermissionTable.Form.Table = "goadmin_permissions"
PermissionTable.Form.Title = language.Get("Permission Manage")
PermissionTable.Form.Description = language.Get("Permission Manage")
PermissionTable.GetForm().Table = "goadmin_permissions"
PermissionTable.GetForm().Title = language.Get("Permission Manage")
PermissionTable.GetForm().Description = language.Get("Permission Manage")

PermissionTable.ConnectionDriver = config.Get().DATABASE[0].DRIVER

return
}

func GetRolesTable() (RolesTable Table) {

RolesTable = NewDefaultTable(config.Get().DATABASE[0].DRIVER, true, true, true)
var permissions []map[string]string
permissionsModel, _ := db.Table("goadmin_permissions").Select("id", "slug").Where("id", ">", 0).All()

Expand All @@ -388,7 +385,7 @@ func GetRolesTable() (RolesTable Table) {
})
}

RolesTable.Info.FieldList = []types.Field{
RolesTable.GetInfo().FieldList = []types.Field{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -436,11 +433,11 @@ func GetRolesTable() (RolesTable Table) {
},
}

RolesTable.Info.Table = "goadmin_roles"
RolesTable.Info.Title = language.Get("Roles Manage")
RolesTable.Info.Description = language.Get("Roles Manage")
RolesTable.GetInfo().Table = "goadmin_roles"
RolesTable.GetInfo().Title = language.Get("Roles Manage")
RolesTable.GetInfo().Description = language.Get("Roles Manage")

RolesTable.Form.FormList = []types.Form{
RolesTable.GetForm().FormList = []types.Form{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -513,18 +510,17 @@ func GetRolesTable() (RolesTable Table) {
},
}

RolesTable.Form.Table = "goadmin_roles"
RolesTable.Form.Title = language.Get("Roles Manage")
RolesTable.Form.Description = language.Get("Roles Manage")
RolesTable.GetForm().Table = "goadmin_roles"
RolesTable.GetForm().Title = language.Get("Roles Manage")
RolesTable.GetForm().Description = language.Get("Roles Manage")

RolesTable.ConnectionDriver = config.Get().DATABASE[0].DRIVER

return
}

func GetOpTable() (OpTable Table) {

OpTable.Info.FieldList = []types.Field{
OpTable = NewDefaultTable(config.Get().DATABASE[0].DRIVER, true, true, true)
OpTable.GetInfo().FieldList = []types.Field{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -599,11 +595,11 @@ func GetOpTable() (OpTable Table) {
},
}

OpTable.Info.Table = "goadmin_operation_log"
OpTable.Info.Title = language.Get("operation log")
OpTable.Info.Description = language.Get("operation log")
OpTable.GetInfo().Table = "goadmin_operation_log"
OpTable.GetInfo().Title = language.Get("operation log")
OpTable.GetInfo().Description = language.Get("operation log")

OpTable.Form.FormList = []types.Form{
OpTable.GetForm().FormList = []types.Form{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -687,18 +683,17 @@ func GetOpTable() (OpTable Table) {
},
}

OpTable.Form.Table = "goadmin_operation_log"
OpTable.Form.Title = language.Get("operation log")
OpTable.Form.Description = language.Get("operation log")
OpTable.GetForm().Table = "goadmin_operation_log"
OpTable.GetForm().Title = language.Get("operation log")
OpTable.GetForm().Description = language.Get("operation log")

OpTable.ConnectionDriver = config.Get().DATABASE[0].DRIVER

return
}

func GetMenuTable() (MenuTable Table) {

MenuTable.Info.FieldList = []types.Field{
MenuTable = NewDefaultTable(config.Get().DATABASE[0].DRIVER, true, true, true)
MenuTable.GetInfo().FieldList = []types.Field{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -773,9 +768,9 @@ func GetMenuTable() (MenuTable Table) {
},
}

MenuTable.Info.Table = "goadmin_menu"
MenuTable.Info.Title = language.Get("Menus Manage")
MenuTable.Info.Description = language.Get("Menus Manage")
MenuTable.GetInfo().Table = "goadmin_menu"
MenuTable.GetInfo().Title = language.Get("Menus Manage")
MenuTable.GetInfo().Description = language.Get("Menus Manage")

var roles, parents []map[string]string
rolesModel, _ := db.Table("goadmin_roles").Select("id", "slug").Where("id", ">", 0).All()
Expand Down Expand Up @@ -804,7 +799,7 @@ func GetMenuTable() (MenuTable Table) {
"value": "0",
}}, parents...)

MenuTable.Form.FormList = []types.Form{
MenuTable.GetForm().FormList = []types.Form{
{
Head: "ID",
Field: "id",
Expand Down Expand Up @@ -902,11 +897,10 @@ func GetMenuTable() (MenuTable Table) {
},
}

MenuTable.Form.Table = "goadmin_menu"
MenuTable.Form.Title = language.Get("Menus Manage")
MenuTable.Form.Description = language.Get("Menus Manage")
MenuTable.GetForm().Table = "goadmin_menu"
MenuTable.GetForm().Title = language.Get("Menus Manage")
MenuTable.GetForm().Description = language.Get("Menus Manage")

MenuTable.ConnectionDriver = config.Get().DATABASE[0].DRIVER

return
}