Skip to content

Commit 668a3b5

Browse files
committed
feat(access): 增加get查询的参数条件和响应字段控制
# feat 1. 增加对get查询时的响应字段和查询字段的控制 # doc 1. 增加roadmap记录下规划思路内容 # tests 1. 调整demo/todo的部分测试, 方便后期有调整流程时验证功能是否仍有效
1 parent 81465d6 commit 668a3b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+753
-631
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@
3737
1. [Get开放查询](./doc/query.md)
3838
2. [非开放请求](./doc/action.md)
3939
3. [权限控制](./doc/access.md)
40+
4. [roadmap](./doc/roadmap.md)
4041

4142

4243
# 开发指南
4344

4445
1. go >= 1.18
45-
2. 创建mysql数据库
46-
3. 导入demo/todo/todo/todo.sql文件
46+
2. 创建 mysql 数据库
47+
3. 导入 demo/todo/doc/todo.sql文件
4748
4. demo/todo/config.yaml.example 改成 demo/todo/config.yaml, 然后修改配置文件 config.yaml 中数据库连接
48-
5. 在demo/todo目录运行go run main.go
49-
6. 查看测试 demo/todo/todo/tests
49+
5. 在demo/todo 目录运行 go run main.go 或者 查看测试 demo/todo/tests
50+
5051

5152

5253

README.zh-CN.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@
3737
1. [Get开放查询](./doc/query.md)
3838
2. [非开放请求](./doc/action.md)
3939
3. [权限控制](./doc/access.md)
40-
40+
4. [roadmap](./doc/roadmap.md)
4141

4242
# 开发指南
4343

4444
1. go >= 1.18
45-
2. 创建mysql数据库
46-
3. 导入demo/todo/todo/todo.sql文件
45+
2. 创建 mysql 数据库
46+
3. 导入 demo/todo/doc/todo.sql文件
4747
4. demo/todo/config.yaml.example 改成 demo/todo/config.yaml, 然后修改配置文件 config.yaml 中数据库连接
48-
5. 在demo/todo目录运行go run main.go
49-
6. 查看测试 demo/todo/todo/tests
48+
5. 在demo/todo 目录运行 go run main.go 或者 查看测试 demo/todo/tests
5049

5150

5251

action/action.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111
)
1212

13-
// Structure https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/Operation.java
1413
type Structure struct {
1514
Must []string `json:"MUST,omitempty"`
1615
Refuse []string `json:"REFUSE,omitempty"`

action/node.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (n *Node) parseReq(method string) {
3434
n.data = g.Map{}
3535
n.where = g.Map{}
3636
for key, val := range n.req {
37-
if key == "@role" {
37+
if key == consts.Role {
3838
n.role = gconv.String(val)
3939
} else {
4040
if method == consts.MethodDelete {
@@ -62,7 +62,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
6262
}
6363

6464
n.tableName = access.Name
65-
n.rowKey = "id" // 暂固定
65+
n.rowKey = access.RowKey
6666

6767
n.parseReq(method)
6868

@@ -101,13 +101,13 @@ func (n *Node) parse(ctx context.Context, method string) error {
101101

102102
func (n *Node) roleUpdate() error {
103103

104-
if val, exists := n.structure.Insert["@role"]; exists {
104+
if val, exists := n.structure.Insert[consts.Role]; exists {
105105
if n.role == "" {
106106
n.role = gconv.String(val)
107107
}
108108
}
109109

110-
if val, exists := n.structure.Update["@role"]; exists {
110+
if val, exists := n.structure.Update[consts.Role]; exists {
111111
n.role = gconv.String(val)
112112
}
113113

config/access.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ var (
3939
)
4040

4141
// 自定义设置从ctx获取用户id和角色的key
42+
4243
var (
43-
RoleKey = "ajg.role" // ctx 中role 的key
4444
UserIdKey = "ajg.userId"
4545
)
4646

4747
// 设置 _access/_request 自定义表名
4848
var (
49-
TableAccess = "_access"
50-
TableRequest = "_request"
49+
TableAccess = "_access"
50+
TableAccessExt = "_access_ext"
51+
TableRequest = "_request"
52+
TableRequestExt = "_request_ext"
5153
)
5254

5355
// ========================= 角色 =======================

consts/node.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
package consts
22

3-
// 嵌套的最大深度 https://github.com/Tencent/APIJSON/issues/147
4-
53
const MaxTreeWidth = 5
64
const MaxTreeDeep = 5

consts/op.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package consts
2+
3+
const (
4+
Role = "@role"
5+
)

db/access.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import (
66
"github.com/gogf/gf/v2/errors/gerror"
77
"github.com/gogf/gf/v2/frame/g"
88
"github.com/gogf/gf/v2/os/gtime"
9+
"github.com/samber/lo"
910
)
1011

1112
var accessMap = map[string]Access{}
1213

14+
type FieldsGetValue struct {
15+
In map[string][]string
16+
Out map[string]string
17+
}
18+
1319
type Access struct {
1420
Debug int8
1521
Name string
@@ -26,29 +32,67 @@ type Access struct {
2632

2733
// ext
2834

29-
RowKey string
35+
RowKey string
36+
FieldsGet map[string]FieldsGetValue
37+
}
38+
39+
func (a *Access) GetFieldsGetOutByRole(role string) []string {
40+
var fieldsMap map[string]string
41+
42+
if val, exists := a.FieldsGet[role]; exists {
43+
fieldsMap = val.Out
44+
} else {
45+
fieldsMap = a.FieldsGet["default"].Out
46+
}
47+
return lo.Keys(fieldsMap)
48+
}
49+
50+
func (a *Access) GetFieldsGetInByRole(role string) map[string][]string {
51+
var inFieldsMap map[string][]string
52+
53+
if val, exists := a.FieldsGet[role]; exists {
54+
inFieldsMap = val.In
55+
} else {
56+
inFieldsMap = a.FieldsGet["default"].In
57+
}
58+
59+
return inFieldsMap
3060
}
3161

3262
func loadAccessMap() {
3363
_accessMap := make(map[string]Access)
3464

3565
var accessList []Access
36-
g.DB().Model(config.TableAccess).Scan(&accessList)
66+
67+
db := g.DB()
68+
69+
db.Model(config.TableAccess).Scan(&accessList)
70+
71+
type AccessExt struct {
72+
RowKey string
73+
FieldsGet map[string]FieldsGetValue
74+
}
3775

3876
for _, access := range accessList {
3977
name := access.Alias
4078
if name == "" {
4179
name = access.Name
4280
}
81+
82+
var ext *AccessExt
83+
db.Model(config.TableAccessExt).Where("table", access.Name).Scan(&ext)
84+
if ext != nil {
85+
access.RowKey = ext.RowKey
86+
access.FieldsGet = ext.FieldsGet
87+
}
88+
4389
_accessMap[name] = access
4490
}
4591

4692
accessMap = _accessMap
4793
}
4894

4995
func GetAccess(table string, accessVerify bool) (*Access, error) {
50-
// 暂未使用version
51-
// 读取配置时将最新的版本额外增加一个@latest的版本, 传入为-1时候, 读取最新版本
5296
access, ok := accessMap[table]
5397

5498
if !ok {
@@ -66,8 +110,6 @@ func GetAccess(table string, accessVerify bool) (*Access, error) {
66110
}
67111

68112
func GetAccessRole(table string, method string) ([]string, string, error) {
69-
// 暂未使用version
70-
// 读取配置时将最新的版本额外增加一个@latest的版本, 传入为-1时候, 读取最新版本
71113
access, ok := accessMap[table]
72114

73115
if !ok {

db/op.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package db
2+
3+
const (
4+
Like = "$"
5+
Regexp = "~"
6+
)
7+
8+
const (
9+
SqlLike = "LIKE"
10+
SqlEqual = "="
11+
SqlRegexp = "REGEXP"
12+
)

db/request.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import (
1111
var requestMap = map[string]Request{}
1212

1313
type Request struct {
14-
Debug int8
15-
Version int16
16-
Method string
17-
Tag string
18-
// https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/Operation.java
14+
Debug int8
15+
Version int16
16+
Method string
17+
Tag string
1918
Structure g.Map
2019
Detail string
2120
CreatedAt *gtime.Time
@@ -32,7 +31,6 @@ func loadRequestMap() {
3231
tag := item.Tag
3332
if strings.ToLower(tag) != tag {
3433
// 本身大写, 如果没有外层, 则套一层
35-
// https://github.com/Tencent/APIJSON/issues/115#issuecomment-565733254
3634
if _, ok := item.Structure[tag]; !ok {
3735
item.Structure = g.Map{
3836
tag: item.Structure,

0 commit comments

Comments
 (0)