-
Notifications
You must be signed in to change notification settings - Fork 130
/
sys_api.go
498 lines (465 loc) · 13.9 KB
/
sys_api.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package system
import (
"context"
"encoding/json"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/gtime"
"github.com/sagoo-cloud/sagooiot/internal/consts"
"github.com/sagoo-cloud/sagooiot/internal/dao"
"github.com/sagoo-cloud/sagooiot/internal/logic/common"
"github.com/sagoo-cloud/sagooiot/internal/model"
"github.com/sagoo-cloud/sagooiot/internal/model/entity"
"github.com/sagoo-cloud/sagooiot/internal/service"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
type sSysApi struct {
}
func sysApiNew() *sSysApi {
return &sSysApi{}
}
func init() {
service.RegisterSysApi(sysApiNew())
}
// GetInfoByIds 根据接口APIID数组获取接口信息
func (s *sSysApi) GetInfoByIds(ctx context.Context, ids []int) (data []*entity.SysApi, err error) {
cache := common.Cache()
//获取缓存信息
var tmpData *gvar.Var
tmpData = cache.Get(ctx, consts.CacheSysApi)
if err != nil {
return
}
var tmpSysApiInfo []*entity.SysApi
var apiInfo []*entity.SysApi
//根据菜单ID数组获取菜单列表信息
if tmpData.Val() != nil {
json.Unmarshal([]byte(tmpData.Val().(string)), &tmpSysApiInfo)
for _, id := range ids {
for _, menuTmp := range tmpSysApiInfo {
if id == int(menuTmp.Id) {
apiInfo = append(apiInfo, menuTmp)
continue
}
}
}
}
if apiInfo != nil && len(apiInfo) >= 0 {
data = apiInfo
return
}
err = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().IsDeleted: 0,
dao.SysApi.Columns().Status: 1,
}).WhereIn(dao.SysApi.Columns().Id, ids).Scan(&data)
return
}
// GetApiByMenuId 根据ApiID获取接口信息
func (s *sSysApi) GetApiByMenuId(ctx context.Context, apiId int) (data []*entity.SysApi, err error) {
var apiApi []*entity.SysMenuApi
err = dao.SysMenuApi.Ctx(ctx).Where(g.Map{
dao.SysMenuApi.Columns().MenuId: apiId,
dao.SysMenuApi.Columns().IsDeleted: 0,
}).Scan(&apiApi)
//获取接口ID数组
if apiApi != nil {
var ids []int
for _, api := range apiApi {
ids = append(ids, api.ApiId)
}
err = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().IsDeleted: 0,
dao.SysApi.Columns().Status: 1,
}).WhereIn(dao.SysApi.Columns().Id, ids).Scan(&data)
}
return
}
// GetInfoById 根据ID获取API
func (s *sSysApi) GetInfoById(ctx context.Context, id int) (entity *entity.SysApi, err error) {
err = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Id: id,
}).Scan(&entity)
return
}
// GetApiAll 获取所有接口
func (s *sSysApi) GetApiAll(ctx context.Context) (data []*entity.SysApi, err error) {
cache := common.Cache()
err = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().IsDeleted: 0,
dao.SysApi.Columns().Status: 1,
dao.SysApi.Columns().Types: 2,
}).Scan(&data)
if data != nil && len(data) > 0 {
cache.Set(ctx, consts.CacheSysApi, data, 0)
} else {
cache.Remove(ctx, consts.CacheSysApi)
}
return
}
// GetApiTree 获取Api数结构数据
func (s *sSysApi) GetApiTree(ctx context.Context, name string, address string, status int, types int) (out []*model.SysApiTreeOut, err error) {
var e []*entity.SysApi
m := dao.SysApi.Ctx(ctx)
if name != "" {
m = m.WhereLike(dao.SysApi.Columns().Name, "%"+name+"%")
}
if address != "" {
m = m.WhereLike(dao.SysApi.Columns().Address, "%"+address+"%")
}
if status != -1 {
m = m.Where(dao.SysApi.Columns().Status, status)
}
if types != -1 {
m = m.Where(dao.SysApi.Columns().Types, types)
}
m = m.Where(dao.SysApi.Columns().IsDeleted, 0)
err = m.OrderAsc(dao.SysApi.Columns().Sort).Scan(&e)
if len(e) > 0 {
out, err = GetApiTree(e)
if err != nil {
return
}
}
return
}
// Add 添加Api列表
func (s *sSysApi) Add(ctx context.Context, input *model.AddApiInput) (err error) {
if input.Types == 2 {
if input.Address == "" || len(input.MenuIds) == 0 {
err = gerror.New("参数错误")
return
}
if input.ParentId == -1 {
err = gerror.New("接口不能为根节点")
return
}
} else {
if input.Address != "" || len(input.MenuIds) > 0 {
err = gerror.New("参数错误")
return
}
}
if input.ParentId != -1 {
var parentApiInfo *entity.SysApi
err = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Id: input.ParentId,
}).Scan(&parentApiInfo)
if parentApiInfo.IsDeleted != 0 {
return gerror.New("上级节点已删除,无法新增")
}
if parentApiInfo.Status != 1 {
return gerror.New("上级节点未启用,无法新增")
}
if parentApiInfo.Types != 1 {
return gerror.New("上级节点不是分类,无法新增")
}
}
err = dao.SysUser.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) (err error) {
var apiInfo *entity.SysApi
//根据名称查看是否存在
apiInfo = CheckApiName(ctx, input.Name, 0)
if apiInfo != nil {
return gerror.New("Api名字已存在,无法添加")
}
if input.Types == 2 {
//根据名称查看是否存在
apiInfo = CheckApiAddress(ctx, input.Address, 0)
if apiInfo != nil {
return gerror.New("Api地址,无法添加")
}
}
//获取当前登录用户ID
loginUserId := service.Context().GetUserId(ctx)
apiInfo = new(entity.SysApi)
if apiInfoErr := gconv.Scan(input, &apiInfo); apiInfoErr != nil {
return
}
apiInfo.IsDeleted = 0
apiInfo.CreateBy = uint(loginUserId)
apiInfoId, err := dao.SysApi.Ctx(ctx).Data(apiInfo).InsertAndGetId()
if err != nil {
return err
}
//绑定菜单
err = AddMenuApi(ctx, int(apiInfoId), input.MenuIds, loginUserId)
if err != nil {
return
}
//获取所有接口并添加缓存
_, err = s.GetApiAll(ctx)
return
})
return
}
// Detail Api列表详情
func (s *sSysApi) Detail(ctx context.Context, id int) (out *model.SysApiOut, err error) {
var e *entity.SysApi
_ = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Id: id,
}).Scan(&e)
if e != nil {
if err = gconv.Scan(e, &out); err != nil {
return nil, err
}
menuApiInfo, _ := service.SysMenuApi().GetInfoByApiId(ctx, out.Id)
var menuIds []int
for _, menuApi := range menuApiInfo {
menuIds = append(menuIds, menuApi.MenuId)
}
out.MenuIds = append(out.MenuIds, menuIds...)
}
return
}
func AddMenuApi(ctx context.Context, id int, menuIds []int, loginUserId int) (err error) {
cache := common.Cache()
//添加菜单
var sysMenuApis []*entity.SysMenuApi
for _, menuId := range menuIds {
var menuInfo *entity.SysMenu
err = dao.SysMenu.Ctx(ctx).Where(dao.SysMenu.Columns().Id, menuId).Scan(&menuInfo)
if menuInfo == nil {
err = gerror.New("菜单ID错误")
return
}
if menuInfo != nil && menuInfo.IsDeleted == 1 {
err = gerror.New(menuInfo.Name + "已删除,无法绑定")
return
}
if menuInfo != nil && menuInfo.Status == 0 {
err = gerror.New(menuInfo.Name + "已禁用,无法绑定")
return
}
//解除旧绑定关系
_, err = dao.SysMenuApi.Ctx(ctx).Data(g.Map{
dao.SysMenuApi.Columns().IsDeleted: 1,
dao.SysMenuApi.Columns().DeletedBy: loginUserId,
}).Where(dao.SysMenuApi.Columns().ApiId, id).Update()
_, err = dao.SysMenuApi.Ctx(ctx).Where(dao.SysMenuApi.Columns().ApiId, id).Delete()
var sysMenuApi = new(entity.SysMenuApi)
sysMenuApi.MenuId = menuId
sysMenuApi.ApiId = id
sysMenuApi.IsDeleted = 0
sysMenuApi.CreatedBy = uint(loginUserId)
sysMenuApis = append(sysMenuApis, sysMenuApi)
}
if sysMenuApis != nil {
//添加
_, addErr := dao.SysMenuApi.Ctx(ctx).Data(sysMenuApis).Insert()
if addErr != nil {
err = gerror.New("添加失败")
return
}
//查询菜单ID绑定的所有接口ID
var menuApiInfos []*entity.SysMenuApi
dao.SysMenuApi.Ctx(ctx).Where(g.Map{
dao.SysMenuApi.Columns().IsDeleted: 0,
}).WhereIn(dao.SysMenuApi.Columns().MenuId, menuIds).Scan(&menuApiInfos)
//添加缓存
for _, menuId := range menuIds {
var menuApi []*entity.SysMenuApi
for _, menuApiInfo := range menuApiInfos {
if menuId == menuApiInfo.MenuId {
menuApi = append(menuApi, menuApiInfo)
}
}
if menuApi != nil && len(menuApi) > 0 {
cache.Set(ctx, consts.CacheSysMenuApi+"_"+gconv.String(menuId), menuApi, 0)
}
}
//获取所有信息
var menuApiInfoAll []*entity.SysMenuApi
dao.SysMenuApi.Ctx(ctx).Where(g.Map{
dao.SysMenuApi.Columns().IsDeleted: 0,
}).WhereIn(dao.SysMenuApi.Columns().MenuId, menuIds).Scan(&menuApiInfoAll)
if menuApiInfoAll != nil && len(menuApiInfoAll) > 0 {
cache.Set(ctx, consts.CacheSysMenuApi, menuApiInfoAll, 0)
}
}
return
}
// Edit 修改Api列表
func (s *sSysApi) Edit(ctx context.Context, input *model.EditApiInput) (err error) {
if input.Types == 2 {
if input.Address == "" || len(input.MenuIds) == 0 {
err = gerror.New("参数错误")
return
}
if input.ParentId == -1 {
err = gerror.New("接口不能为根节点")
return
}
} else {
if input.Address != "" || len(input.MenuIds) > 0 {
err = gerror.New("参数错误")
return
}
}
if input.ParentId != -1 {
var parentApiInfo *entity.SysApi
err = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Id: input.ParentId,
}).Scan(&parentApiInfo)
if parentApiInfo.IsDeleted != 0 {
return gerror.New("上级节点已删除,无法新增")
}
if parentApiInfo.Status != 1 {
return gerror.New("上级节点已启用,无法新增")
}
if parentApiInfo.Types != 1 {
return gerror.New("上级节点不是分类,无法新增")
}
}
err = dao.SysUser.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) (err error) {
var apiInfo, apiInfo2 *entity.SysApi
//根据ID查看Api列表是否存在
apiInfo = CheckApiId(ctx, input.Id, apiInfo)
if apiInfo == nil {
return gerror.New("Api列表不存在")
}
apiInfo2 = CheckApiName(ctx, input.Name, input.Id)
if apiInfo2 != nil {
return gerror.New("相同Api名称已存在,无法修改")
}
if input.Types == 2 {
apiInfo2 = CheckApiAddress(ctx, input.Address, input.Id)
if apiInfo2 != nil {
return gerror.New("Api地址已存在,无法修改")
}
}
//获取当前登录用户ID
loginUserId := service.Context().GetUserId(ctx)
if apiInfoErr := gconv.Scan(input, &apiInfo); apiInfoErr != nil {
return
}
apiInfo.UpdatedBy = uint(loginUserId)
_, err = dao.SysApi.Ctx(ctx).Data(apiInfo).
Where(dao.SysApi.Columns().Id, input.Id).Update()
if err != nil {
return gerror.New("修改失败")
}
//绑定菜单
err = AddMenuApi(ctx, input.Id, input.MenuIds, loginUserId)
//获取所有接口并添加缓存
_, err = s.GetApiAll(ctx)
return
})
return
}
// Del 根据ID删除Api列表信息
func (s *sSysApi) Del(ctx context.Context, Id int) (err error) {
var apiColumn *entity.SysApi
_ = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Id: Id,
}).Scan(&apiColumn)
if apiColumn == nil {
return gerror.New("ID错误")
}
//判断是否为分类
if apiColumn.Types == 1 {
//判断是否存在子节点
num, _ := dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().ParentId: Id,
dao.SysApi.Columns().IsDeleted: 0,
}).Count()
if num > 0 {
return gerror.New("存在子节点,无法删除")
}
}
loginUserId := service.Context().GetUserId(ctx)
//获取当前时间
time, err := gtime.StrToTimeFormat(gtime.Datetime(), "2006-01-02 15:04:05")
if err != nil {
return
}
//开启事务管理
err = dao.SysApi.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) (err error) {
//更新Api列表信息
_, err = dao.SysApi.Ctx(ctx).
Data(g.Map{
dao.SysApi.Columns().DeletedBy: uint(loginUserId),
dao.SysApi.Columns().IsDeleted: 1,
dao.SysApi.Columns().DeletedAt: time,
}).Where(dao.SysApi.Columns().Id, Id).
Update()
//删除于菜单关系绑定
_, err = dao.SysMenuApi.Ctx(ctx).Data(g.Map{
dao.SysMenuApi.Columns().IsDeleted: 0,
dao.SysMenuApi.Columns().DeletedBy: loginUserId,
dao.SysMenuApi.Columns().DeletedAt: time,
}).Where(dao.SysMenuApi.Columns().ApiId, Id).Update()
//获取所有接口并添加缓存
_, err = s.GetApiAll(ctx)
return
})
return
}
// EditStatus 修改状态
func (s *sSysApi) EditStatus(ctx context.Context, id int, status int) (err error) {
var apiInfo *entity.SysApi
_ = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Id: id,
}).Scan(&apiInfo)
if apiInfo == nil {
return gerror.New("ID错误")
}
if apiInfo != nil && apiInfo.IsDeleted == 1 {
return gerror.New("列表字段已删除,无法修改")
}
if apiInfo != nil && apiInfo.Status == status {
return gerror.New("API已禁用或启用,无须重复修改")
}
loginUserId := service.Context().GetUserId(ctx)
apiInfo.Status = status
apiInfo.UpdatedBy = uint(loginUserId)
_, err = dao.SysApi.Ctx(ctx).Data(apiInfo).Where(g.Map{
dao.SysApi.Columns().Id: id,
}).Update()
//获取所有接口并添加缓存
_, err = s.GetApiAll(ctx)
return
}
// GetInfoByAddress 根据Address获取API
func (s *sSysApi) GetInfoByAddress(ctx context.Context, address string) (entity *entity.SysApi, err error) {
err = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Address: address,
dao.SysApi.Columns().IsDeleted: 0,
dao.SysApi.Columns().Status: 1,
}).Scan(&entity)
return
}
// 检查指定ID的数据是否存在
func CheckApiId(ctx context.Context, Id int, apiColumn *entity.SysApi) *entity.SysApi {
_ = dao.SysApi.Ctx(ctx).Where(g.Map{
dao.SysApi.Columns().Id: Id,
dao.SysApi.Columns().IsDeleted: 0,
}).Scan(&apiColumn)
return apiColumn
}
// 检查相同Api名称的数据是否存在
func CheckApiName(ctx context.Context, name string, tag int) *entity.SysApi {
var apiInfo *entity.SysApi
m := dao.SysApi.Ctx(ctx)
if tag > 0 {
m = m.WhereNot(dao.SysApi.Columns().Id, tag)
}
_ = m.Where(g.Map{
dao.SysApi.Columns().Name: name,
dao.SysApi.Columns().IsDeleted: 0,
}).Scan(&apiInfo)
return apiInfo
}
// 检查相同Api地址的数据是否存在
func CheckApiAddress(ctx context.Context, address string, tag int) *entity.SysApi {
var apiInfo *entity.SysApi
m := dao.SysApi.Ctx(ctx)
if tag > 0 {
m = m.WhereNot(dao.SysApi.Columns().Id, tag)
}
_ = m.Where(g.Map{
dao.SysApi.Columns().Address: address,
dao.SysApi.Columns().IsDeleted: 0,
}).Scan(&apiInfo)
return apiInfo
}