Skip to content

Commit 7771cfd

Browse files
author
Bitxx
committed
路由新增或更新时,检测是否重复
1 parent 2ac58c1 commit 7771cfd

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

app/admin/sys/service/sys_menu.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ func (e *SysMenu) Insert(c *dto.SysMenuInsertReq) (int64, int, error) {
135135
now := time.Now()
136136
data := models.SysMenu{}
137137
if c.MenuType == constant.MenuM || c.MenuType == constant.MenuC {
138+
//确保路由地址不重复
139+
if c.Path != "" {
140+
query := dto.SysMenuQueryReq{}
141+
query.Path = c.Path
142+
count, respCode, err := e.Count(&query)
143+
if err != nil && respCode != baseLang.DataNotFoundCode {
144+
return 0, respCode, err
145+
}
146+
if count > 0 {
147+
return 0, baseLang.SysMenuPathExistCode, lang.MsgErr(baseLang.SysMenuPathExistCode, e.Lang)
148+
}
149+
}
138150
data.Path = c.Path
139151
data.IsHidden = c.IsHidden
140152
if c.MenuType == constant.MenuM {
@@ -214,6 +226,18 @@ func (e *SysMenu) Update(c *dto.SysMenuUpdateReq, p *middleware.DataPermission)
214226

215227
now := time.Now()
216228
if c.MenuType == constant.MenuM || c.MenuType == constant.MenuC {
229+
//确保路由地址唯一
230+
if c.Path != "" && data.Path != c.Path {
231+
req := dto.SysMenuQueryReq{}
232+
req.Path = c.Path
233+
resp, respCode, err := e.QueryOne(&req, p)
234+
if err != nil && respCode != baseLang.DataNotFoundCode {
235+
return false, respCode, err
236+
}
237+
if respCode == baseLang.SuccessCode && resp.Id != data.Id {
238+
return false, baseLang.SysMenuPathExistCode, lang.MsgErr(baseLang.SysMenuPathExistCode, e.Lang)
239+
}
240+
}
217241
data.Path = c.Path
218242
data.IsHidden = c.IsHidden
219243
if c.MenuType == constant.MenuM {

0 commit comments

Comments
 (0)