04.管理员管理
# 01.创建管理员表
# 1.0 管理员相关表ER
# 1.1 models/manager.go
package models
import (
_ "github.com/jinzhu/gorm"
)
type Manager struct {
Id int
Username string
Password string
Mobile string
Email string
Status int
RoleId int
AddTime int
IsSuper int
Role Role `gorm:"foreignkey:Id;association_foreignkey:RoleId"`
}
func (Manager) TableName() string {
return "manager"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1.2 models/role.go
package models
import (
_ "github.com/jinzhu/gorm"
)
type Role struct {
Id int
Title string
Description string
Status int
AddTime int
}
func (Role) TableName() string {
return "role"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1.3 生成表结构
models/core.go
func init() {
// 创建表
DB.CreateTable(&Role{},&Manager{}) // 根据User结构体建表
DB.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Role{},&Manager{}) // 设置表结构的存储引擎为InnoDB
}
1
2
3
4
5
2
3
4
5
# 1.4 给Role表添加几个角色
# 02.配置基类
- 在面向对象设计中,被定义为包含所有实体共性的 class 类型,被称为“基类”。
# 2.1 controllers/admin/base.go
package admin
import (
"github.com/astaxie/beego"
"strings"
)
type BaseController struct {
beego.Controller
}
func (c *BaseController) Success(message string, redirect string) {
// message: 提示信息
// redirect: 重定向地址
c.Data["message"] = message
if strings.Contains(redirect, "http") {
c.Data["redirect"] = redirect
} else {
c.Data["redirect"] = "/admin" + redirect
}
c.TplName = "admin/public/success.html"
}
func (c *BaseController) Error(message string, redirect string) {
c.Data["message"] = message
if strings.Contains(redirect, "http") {
c.Data["redirect"] = redirect
} else {
c.Data["redirect"] = "/admin" + redirect
}
c.TplName = "admin/public/error.html"
}
func (c *BaseController) Goto(redirect string) {
c.Redirect("/admin"+redirect, 302)
}
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
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
# 2.2 admin/public/success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta http-equiv="refresh" content="2;url={{.redirect}}">
<title>提示信息</title>
<link rel="stylesheet" href="/static/admin/bootstrap/css/bootstrap.css">
</head>
<body>
<div class="container-fluid">
<div class="container" style="width: 480px;margin-top:100px;">
<div class="alert alert-success">
<h2>操作成功-{{.message}}</h2>
<br />
<p>如果您不做出选择,将在 2秒后跳转到上一个链接地址</p>
<br />
</div>
</div>
</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 2.3 admin/public/error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta http-equiv="refresh" content="2;url={{.redirect}}">
<title>提示信息</title>
<link rel="stylesheet" href="/static/admin/bootstrap/css/bootstrap.css">
</head>
<body>
<div class="container-fluid">
<div class="container" style="width: 480px;margin-top:100px;">
<div class="alert alert-danger">
<h2>操作失败-{{.message}}!</h2>
<br />
<p>如果您不做出选择,2秒后将自动返回上一个页面</p>
<br />
</div>
</div>
</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 03.管理员管理
# 3.1 controllers\admin\manager.go
package admin
import (
"beegosc/models"
"fmt"
"github.com/astaxie/beego"
"strconv"
"strings"
)
type ManagerController struct {
BaseController
}
func (c *ManagerController) Get() {
manager := []models.Manager{}
models.DB.Preload("Role").Find(&manager)
c.Data["managerList"] = manager
fmt.Println(manager)
c.TplName = "admin/manager/index.html"
}
// 1、获取角色列表
func (c *ManagerController) Add() {
role := []models.Role{}
models.DB.Find(&role)
c.Data["roleList"] = role
c.TplName = "admin/manager/add.html"
}
// 2、获取数据
func (c *ManagerController) DoAdd() {
roleId, err1 := c.GetInt("role_id")
if err1 != nil {
c.Error("非法请求", "/manager/add")
return
}
username := strings.Trim(c.GetString("username"), " ")
password := strings.Trim(c.GetString("password"), " ")
mobile := strings.Trim(c.GetString("mobile"), " ")
email := strings.Trim(c.GetString("email"), " ")
if len(username) < 2 || len(password) < 6 {
c.Error("用户名或者密码长度不合法", "/manager/add")
return
}
//判断数据库里面有没有当前用户
managerList := []models.Manager{}
models.DB.Where("username=?", username).Find(&managerList)
if len(managerList) > 0 {
c.Error("用户名已经存在", "/manager/add")
return
}
//增加管理员
manager := models.Manager{}
manager.Username = username
manager.Password = models.Md5(password)
manager.Mobile = mobile
manager.Email = email
manager.Status = 1
manager.AddTime = int(models.GetUnix())
manager.RoleId = roleId
err := models.DB.Create(&manager).Error
if err != nil {
c.Error("增加管理员失败", "/manager/add")
return
}
c.Success("增加管理员成功", "/manager")
}
// 3、获取管理员信息
func (c *ManagerController) Edit() {
id, err := c.GetInt("id")
if err != nil {
c.Error("非法请求", "/manager")
return
}
manager := models.Manager{Id: id}
models.DB.Find(&manager)
c.Data["manager"] = manager
//获取所有的角色
role := []models.Role{}
models.DB.Find(&role)
c.Data["roleList"] = role
c.TplName = "admin/manager/edit.html"
}
// 4、创建管理员用户
func (c *ManagerController) DoEdit() {
id, err1 := c.GetInt("id")
if err1 != nil {
c.Error("非法请求", "/manager")
return
}
roleId, err2 := c.GetInt("role_id")
if err2 != nil {
c.Error("非法请求", "/manager")
return
}
mobile := strings.Trim(c.GetString("mobile"), " ")
email := strings.Trim(c.GetString("email"), " ")
password := strings.Trim(c.GetString("password"), " ")
//获取数据
manager := models.Manager{Id: id}
models.DB.Find(&manager)
manager.RoleId = roleId
manager.Mobile = mobile
manager.Email = email
if password != "" {
if len(password) < 6 {
c.Error("密码长度不合法,密码长度不能小于6位", "/manager/edit?id="+strconv.Itoa(id))
return
}
manager.Password = models.Md5(password)
}
//执行修改
err := models.DB.Save(&manager).Error
if err != nil {
beego.Info(err)
c.Error("修改数据失败-检查一下数据是否合法", "/manager/edit?id="+strconv.Itoa(id))
} else {
c.Success("修改数据成功", "/manager")
}
}
// 5、删除管理员用户
func (c *ManagerController) Delete() {
id, err1 := c.GetInt("id")
if err1 != nil {
c.Error("传入参数错误", "/manager")
return
}
manager := models.Manager{Id: id}
models.DB.Delete(&manager)
c.Success("删除轮播图成功", "/manager")
}
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
135
136
137
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
135
136
137
# 3.2 views\admin\manager\add.html
{{template "../public/page_header.html" .}}
<div class="container-fluid">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
增加管理员
</div>
<div class="panel-body">
<div class="table-responsive input-form">
<form action="/{{config "String" "adminPath" ""}}/manager/doAdd" method="post">
<ul>
<li> 管理员名称: <input type="text" name="username" /></li>
<li> 管理员密码: <input type="password" name="password" /></li>
<li> 管理员电话: <input type="text" name="mobile" /></li>
<li> 管理员邮箱: <input type="text" name="email" /></li>
<li> 管理员角色:
<select name="role_id" id="role_id">
{{range $key,$value := .roleList}}
<option value="{{$value.Id}}">{{$value.Title}}</option>
{{end}}
</select>
</li>
<li>
<br/>
<button type="submit" class="btn btn-default">提交</button>
</li>
</ul>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
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
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
# 3.3 views\admin\manager\edit.html
{{template "../public/page_header.html" .}}
<div class="container-fluid">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
编辑管理员
</div>
<div class="panel-body">
<div class="table-responsive input-form">
<form action="/{{config "String" "adminPath" ""}}/manager/doEdit" method="post">
<ul>
<input type="hidden" name="id" value="{{.manager.Id}}">
<li> 管理员名称: <input readonly type="text" name="username" value="{{.manager.Username}}" /></li>
<li> 管理员密码: <input type="password" name="password" /></li>
<li> 管理员电话: <input type="text" name="mobile" value="{{.manager.Mobile}}" /></li>
<li> 管理员邮箱: <input type="text" name="email" value="{{.manager.Email}}" /></li>
<li> 管理员角色:
<select name="role_id" id="role_id">
{{$roleId := .manager.RoleId}}
{{range $key,$value := .roleList}}
{{if eq $value.Id $roleId}}
<option selected value="{{$value.Id}}">{{$value.Title}}</option>
{{else}}
<option value="{{$value.Id}}">{{$value.Title}}</option>
{{end}}
{{end}}
</select>
</li>
<li>
<br/>
<button type="submit" class="btn btn-default">提交</button>
</li>
</ul>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
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
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
上次更新: 2024/3/13 15:35:10