forked from yunnet/gardens
/
BackendConfController.go
134 lines (112 loc) · 3.51 KB
/
BackendConfController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package controllers
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/orm"
"github.com/yunnet/gardens/enums"
"github.com/yunnet/gardens/models"
"strconv"
"strings"
"time"
)
type BackendConfController struct {
BaseController
}
func (this *BackendConfController) Prepare() {
this.BaseController.Prepare()
this.checkAuthor("DataGrid", "DataList")
}
func (this *BackendConfController) Index() {
this.Data["pageTitle"] = "后台APP配置"
this.Data["showMoreQuery"] = true
this.Data["activeSidebarUrl"] = this.URLFor(this.controllerName + "." + this.actionName)
this.setTpl()
this.LayoutSections = make(map[string]string)
this.LayoutSections["headcssjs"] = "backendconf/index_headcssjs.html"
this.LayoutSections["footerjs"] = "backendconf/index_footerjs.html"
//页面里按钮权限控制
this.Data["canEdit"] = this.checkActionAuthor("BackendConfController", "Edit")
this.Data["canDelete"] = this.checkActionAuthor("BackendConfController", "Delete")
}
func (this *BackendConfController) DataGrid() {
var params models.BackendConfQueryParam
json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms)
data, total := models.BackendConfPageList(¶ms)
result := make(map[string]interface{})
result["total"] = total
result["rows"] = data
this.Data["json"] = result
this.ServeJSON()
}
func (this *BackendConfController) DataList() {
var params = models.BackendConfQueryParam{}
data := models.BackendConfDataList(¶ms)
this.jsonResult(enums.JRCodeSucc, "", data)
}
func (this *BackendConfController) Edit() {
if this.Ctx.Request.Method == "POST" {
this.Save()
}
Id, _ := this.GetInt(":id", 0)
m := models.BackendConf{Id: Id}
if Id > 0 {
o := orm.NewOrm()
err := o.Read(&m)
if err != nil {
this.pageError("数据无效,请刷新后重试")
}
} else {
m.Used = enums.Enabled
}
this.Data["m"] = m
this.setTpl("backendconf/edit.html", "shared/layout_pullbox.html")
this.LayoutSections = make(map[string]string)
this.LayoutSections["footerjs"] = "backendconf/edit_footerjs.html"
}
//add | update
func (this *BackendConfController) Save() {
var err error
m := models.BackendConf{}
//获取form里的值
if err = this.ParseForm(&m); err != nil {
this.jsonResult(enums.JRCodeFailed, "获取数据失败", m.Id)
}
id := this.Input().Get("Id")
m.Id, _ = strconv.Atoi(id)
m.AppName = this.GetString("AppName")
m.AppVersion = this.GetString("AppVersion")
m.Deploy = this.GetString("Deploy")
m.ConfigText = this.GetString("ConfigText")
o := orm.NewOrm()
if m.Id == 0 {
m.CreateUser = this.curUser.RealName
m.CreateDate = time.Now()
if _, err = o.Insert(&m); err == nil {
this.jsonResult(enums.JRCodeSucc, "添加成功", m.Id)
} else {
this.jsonResult(enums.JRCodeFailed, "添加失败", m.Id)
}
} else {
m.ChangeUser = this.curUser.RealName
m.ChangeDate = time.Now()
if _, err = o.Update(&m, "AppName", "AppVersion", "Deploy", "ConfigText", "Used", "ChangeUser", "ChangeDate"); err == nil {
this.jsonResult(enums.JRCodeSucc, "编辑成功", m.Id)
} else {
this.jsonResult(enums.JRCodeFailed, "编辑失败", m.Id)
}
}
}
func (this *BackendConfController) Delete() {
strs := this.GetString("ids")
ids := make([]int, 0, len(strs))
for _, str := range strings.Split(strs, ",") {
if id, err := strconv.Atoi(str); err == nil {
ids = append(ids, id)
}
}
if num, err := models.BackendConfBatchDelete(ids); err == nil {
this.jsonResult(enums.JRCodeSucc, fmt.Sprintf("成功删除 %d 项", num), 0)
} else {
this.jsonResult(enums.JRCodeFailed, "删除失败", 0)
}
}