mirror of
https://github.com/lejianwen/rustdesk-api.git
synced 2025-12-02 18:13:15 +00:00
split my from admin
This commit is contained in:
@@ -31,11 +31,6 @@ func (ct *AddressBook) Detail(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
iid, _ := strconv.Atoi(id)
|
||||
t := service.AllService.AddressBookService.InfoByRowId(uint(iid))
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
if t.RowId > 0 {
|
||||
response.Success(c, t)
|
||||
return
|
||||
@@ -67,9 +62,9 @@ func (ct *AddressBook) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
t := f.ToAddressBook()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || t.UserId == 0 {
|
||||
t.UserId = u.Id
|
||||
if t.UserId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
@@ -99,7 +94,7 @@ func (ct *AddressBook) Create(c *gin.Context) {
|
||||
// @Param body body admin.AddressBookForm true "地址簿信息"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBook}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/address_book/create [post]
|
||||
// @Router /admin/address_book/batchCreate [post]
|
||||
// @Security token
|
||||
func (ct *AddressBook) BatchCreate(c *gin.Context) {
|
||||
f := &admin.AddressBookForm{}
|
||||
@@ -162,10 +157,6 @@ func (ct *AddressBook) List(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 {
|
||||
query.UserId = int(u.Id)
|
||||
}
|
||||
res := service.AllService.AddressBookService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
tx.Preload("Collection", func(txc *gorm.DB) *gorm.DB {
|
||||
return txc.Select("id,name")
|
||||
@@ -191,11 +182,6 @@ func (ct *AddressBook) List(c *gin.Context) {
|
||||
for _, ab := range res.AddressBooks {
|
||||
abCIds = append(abCIds, ab.CollectionId)
|
||||
}
|
||||
//获取地址簿名称
|
||||
//cRes := service.AllService.AddressBookService.ListCollection(1, 999, func(tx *gorm.DB) {
|
||||
// tx.Where("id in ?", abCIds)
|
||||
//})
|
||||
//
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
@@ -222,15 +208,15 @@ func (ct *AddressBook) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if f.RowId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
ex := service.AllService.AddressBookService.InfoByRowId(f.RowId)
|
||||
if ex.RowId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
t := f.ToAddressBook()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
@@ -271,21 +257,12 @@ func (ct *AddressBook) Delete(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
err := service.AllService.AddressBookService.Delete(t)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
if u.Id > 0 {
|
||||
err := service.AllService.AddressBookService.Delete(t)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
}
|
||||
|
||||
// ShareByWebClient
|
||||
@@ -372,27 +349,3 @@ func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) {
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
func (ct *AddressBook) BatchUpdateTags(c *gin.Context) {
|
||||
f := &admin.BatchUpdateTagsForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
|
||||
abs := service.AllService.AddressBookService.List(1, 999, func(tx *gorm.DB) {
|
||||
tx.Where("row_id in ?", f.RowIds)
|
||||
tx.Where("user_id = ?", u.Id)
|
||||
})
|
||||
if abs.Total == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.BatchUpdateTags(abs.AddressBooks, f.Tags)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
@@ -29,11 +29,6 @@ func (abc *AddressBookCollection) Detail(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
iid, _ := strconv.Atoi(id)
|
||||
t := service.AllService.AddressBookService.CollectionInfoById(uint(iid))
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
if t.Id > 0 {
|
||||
response.Success(c, t)
|
||||
return
|
||||
@@ -64,12 +59,11 @@ func (abc *AddressBookCollection) Create(c *gin.Context) {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
//t := f.ToAddressBookCollection()
|
||||
t := f
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || t.UserId == 0 {
|
||||
t.UserId = u.Id
|
||||
if f.UserId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
t := f
|
||||
err := service.AllService.AddressBookService.CreateCollection(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
@@ -98,10 +92,6 @@ func (abc *AddressBookCollection) List(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 {
|
||||
query.UserId = int(u.Id)
|
||||
}
|
||||
res := service.AllService.AddressBookService.ListCollection(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
if query.UserId > 0 {
|
||||
tx.Where("user_id = ?", query.UserId)
|
||||
@@ -137,11 +127,6 @@ func (abc *AddressBookCollection) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
t := f //f.ToAddressBookCollection()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.UpdateCollection(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
@@ -173,20 +158,15 @@ func (abc *AddressBookCollection) Delete(c *gin.Context) {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
t := service.AllService.AddressBookService.CollectionInfoById(f.Id)
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
ex := service.AllService.AddressBookService.CollectionInfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if u.Id > 0 {
|
||||
err := service.AllService.AddressBookService.DeleteCollection(t)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, err.Error())
|
||||
err := service.AllService.AddressBookService.DeleteCollection(ex)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
}
|
||||
|
||||
@@ -35,10 +35,6 @@ func (abcr *AddressBookCollectionRule) List(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 {
|
||||
query.UserId = int(u.Id)
|
||||
}
|
||||
|
||||
res := service.AllService.AddressBookService.ListRules(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
if query.UserId > 0 {
|
||||
@@ -66,17 +62,11 @@ func (abcr *AddressBookCollectionRule) Detail(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
iid, _ := strconv.Atoi(id)
|
||||
t := service.AllService.AddressBookService.RuleInfoById(uint(iid))
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
if t.Id > 0 {
|
||||
response.Success(c, t)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建地址簿规则
|
||||
@@ -105,13 +95,8 @@ func (abcr *AddressBookCollectionRule) Create(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
//t := f.ToAddressBookCollection()
|
||||
t := f
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if t.UserId == 0 {
|
||||
t.UserId = u.Id
|
||||
}
|
||||
msg, res := abcr.CheckForm(u, t)
|
||||
msg, res := abcr.CheckForm(t)
|
||||
if !res {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, msg))
|
||||
return
|
||||
@@ -124,9 +109,9 @@ func (abcr *AddressBookCollectionRule) Create(c *gin.Context) {
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
func (abcr *AddressBookCollectionRule) CheckForm(u *model.User, t *model.AddressBookCollectionRule) (string, bool) {
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
return "NoAccess", false
|
||||
func (abcr *AddressBookCollectionRule) CheckForm(t *model.AddressBookCollectionRule) (string, bool) {
|
||||
if t.UserId == 0 {
|
||||
return "ParamsError", false
|
||||
}
|
||||
if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
|
||||
return "ParamsError", false
|
||||
@@ -141,15 +126,7 @@ func (abcr *AddressBookCollectionRule) CheckForm(u *model.User, t *model.Address
|
||||
if tou.Id == 0 {
|
||||
return "ItemNotFound", false
|
||||
}
|
||||
//非管理员不能分享给非本组织用户
|
||||
if tou.GroupId != u.GroupId && !service.AllService.UserService.IsAdmin(u) {
|
||||
return "NoAccess", false
|
||||
}
|
||||
} else if t.Type == model.ShareAddressBookRuleTypeGroup {
|
||||
if t.ToId != u.GroupId && !service.AllService.UserService.IsAdmin(u) {
|
||||
return "NoAccess", false
|
||||
}
|
||||
|
||||
tog := service.AllService.GroupService.InfoById(t.ToId)
|
||||
if tog.Id == 0 {
|
||||
return "ItemNotFound", false
|
||||
@@ -194,9 +171,8 @@ func (abcr *AddressBookCollectionRule) Update(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
t := f //f.ToAddressBookCollection()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
msg, res := abcr.CheckForm(u, t)
|
||||
t := f
|
||||
msg, res := abcr.CheckForm(t)
|
||||
if !res {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, msg))
|
||||
return
|
||||
@@ -232,20 +208,15 @@ func (abcr *AddressBookCollectionRule) Delete(c *gin.Context) {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
t := service.AllService.AddressBookService.RuleInfoById(f.Id)
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
ex := service.AllService.AddressBookService.RuleInfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if t.Id > 0 {
|
||||
err := service.AllService.AddressBookService.DeleteRule(t)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, err.Error())
|
||||
err := service.AllService.AddressBookService.DeleteRule(ex)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package my
|
||||
|
||||
import (
|
||||
"Gwen/global"
|
||||
"Gwen/http/request/admin"
|
||||
"Gwen/http/response"
|
||||
"Gwen/service"
|
||||
@@ -11,6 +12,193 @@ import (
|
||||
|
||||
type AddressBook struct{}
|
||||
|
||||
// List 列表
|
||||
// @Tags 我的地址簿
|
||||
// @Summary 地址簿列表
|
||||
// @Description 地址簿列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "页大小"
|
||||
// @Param user_id query int false "用户id"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBookList}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book/list [get]
|
||||
// @Security token
|
||||
func (ct *AddressBook) List(c *gin.Context) {
|
||||
query := &admin.AddressBookQuery{}
|
||||
if err := c.ShouldBindQuery(query); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
query.UserId = int(u.Id)
|
||||
res := service.AllService.AddressBookService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
//预加载地址簿名称
|
||||
tx.Preload("Collection", func(txc *gorm.DB) *gorm.DB {
|
||||
return txc.Select("id,name")
|
||||
})
|
||||
if query.Id != "" {
|
||||
tx.Where("id like ?", "%"+query.Id+"%")
|
||||
}
|
||||
tx.Where("user_id = ?", query.UserId)
|
||||
if query.Username != "" {
|
||||
tx.Where("username like ?", "%"+query.Username+"%")
|
||||
}
|
||||
if query.Hostname != "" {
|
||||
tx.Where("hostname like ?", "%"+query.Hostname+"%")
|
||||
}
|
||||
if query.CollectionId != nil && *query.CollectionId >= 0 {
|
||||
tx.Where("collection_id = ?", query.CollectionId)
|
||||
}
|
||||
})
|
||||
|
||||
abCIds := make([]uint, 0)
|
||||
for _, ab := range res.AddressBooks {
|
||||
abCIds = append(abCIds, ab.CollectionId)
|
||||
}
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// Create 创建地址簿
|
||||
// @Tags 我的地址簿
|
||||
// @Summary 创建地址簿
|
||||
// @Description 创建地址簿
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body admin.AddressBookForm true "地址簿信息"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBook}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book/create [post]
|
||||
// @Security token
|
||||
func (ct *AddressBook) Create(c *gin.Context) {
|
||||
f := &admin.AddressBookForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
t := f.ToAddressBook()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
t.UserId = u.Id
|
||||
if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
|
||||
ex := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(t.UserId, t.Id, t.CollectionId)
|
||||
if ex.RowId > 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemExists"))
|
||||
return
|
||||
}
|
||||
|
||||
err := service.AllService.AddressBookService.Create(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Update 编辑
|
||||
// @Tags 我的地址簿
|
||||
// @Summary 地址簿编辑
|
||||
// @Description 地址簿编辑
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body admin.AddressBookForm true "地址簿信息"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBook}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book/update [post]
|
||||
// @Security token
|
||||
func (ct *AddressBook) Update(c *gin.Context) {
|
||||
f := &admin.AddressBookForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
if f.RowId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if f.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
|
||||
ex := service.AllService.AddressBookService.InfoByRowId(f.RowId)
|
||||
if ex.RowId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
t := f.ToAddressBook()
|
||||
if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.UpdateAll(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
// @Tags 我的地址簿
|
||||
// @Summary 地址簿删除
|
||||
// @Description 地址簿删除
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body admin.AddressBookForm true "地址簿信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book/delete [post]
|
||||
// @Security token
|
||||
func (ct *AddressBook) Delete(c *gin.Context) {
|
||||
f := &admin.AddressBookForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
id := f.RowId
|
||||
errList := global.Validator.ValidVar(c, id, "required,gt=0")
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
ex := service.AllService.AddressBookService.InfoByRowId(f.RowId)
|
||||
if ex.RowId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.Delete(ex)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) {
|
||||
f := &admin.BatchCreateFromPeersForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
@@ -57,3 +245,27 @@ func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) {
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
func (ct *AddressBook) BatchUpdateTags(c *gin.Context) {
|
||||
f := &admin.BatchUpdateTagsForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
|
||||
abs := service.AllService.AddressBookService.List(1, 999, func(tx *gorm.DB) {
|
||||
tx.Where("row_id in ?", f.RowIds)
|
||||
tx.Where("user_id = ?", u.Id)
|
||||
})
|
||||
if abs.Total == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.BatchUpdateTags(abs.AddressBooks, f.Tags)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
162
http/controller/admin/my/addressBookCollection.go
Normal file
162
http/controller/admin/my/addressBookCollection.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package my
|
||||
|
||||
import (
|
||||
"Gwen/global"
|
||||
"Gwen/http/request/admin"
|
||||
"Gwen/http/response"
|
||||
"Gwen/model"
|
||||
"Gwen/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AddressBookCollection struct {
|
||||
}
|
||||
|
||||
// Create 创建地址簿名称
|
||||
// @Tags 我的地址簿名称
|
||||
// @Summary 创建地址簿名称
|
||||
// @Description 创建地址簿名称
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.AddressBookCollection true "地址簿名称信息"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBookCollection}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection/create [post]
|
||||
// @Security token
|
||||
func (abc *AddressBookCollection) Create(c *gin.Context) {
|
||||
f := &model.AddressBookCollection{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
f.UserId = u.Id
|
||||
err := service.AllService.AddressBookService.CreateCollection(f)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// List 列表
|
||||
// @Tags 我的地址簿名称
|
||||
// @Summary 地址簿名称列表
|
||||
// @Description 地址簿名称列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "页大小"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBookCollectionList}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection/list [get]
|
||||
// @Security token
|
||||
func (abc *AddressBookCollection) List(c *gin.Context) {
|
||||
query := &admin.AddressBookCollectionQuery{}
|
||||
if err := c.ShouldBindQuery(query); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
query.UserId = int(u.Id)
|
||||
res := service.AllService.AddressBookService.ListCollection(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
tx.Where("user_id = ?", query.UserId)
|
||||
})
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// Update 编辑
|
||||
// @Tags 我的地址簿名称
|
||||
// @Summary 地址簿名称编辑
|
||||
// @Description 地址簿名称编辑
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.AddressBookCollection true "地址簿名称信息"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBookCollection}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection/update [post]
|
||||
// @Security token
|
||||
func (abc *AddressBookCollection) Update(c *gin.Context) {
|
||||
f := &model.AddressBookCollection{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
if f.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if f.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
ex := service.AllService.AddressBookService.CollectionInfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
|
||||
err := service.AllService.AddressBookService.UpdateCollection(f)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
// @Tags 我的地址簿名称
|
||||
// @Summary 地址簿名称删除
|
||||
// @Description 地址簿名称删除
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.AddressBookCollection true "地址簿名称信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection/delete [post]
|
||||
// @Security token
|
||||
func (abc *AddressBookCollection) Delete(c *gin.Context) {
|
||||
f := &model.AddressBookCollection{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
id := f.Id
|
||||
errList := global.Validator.ValidVar(c, id, "required,gt=0")
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
ex := service.AllService.AddressBookService.CollectionInfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.DeleteCollection(ex)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
}
|
||||
228
http/controller/admin/my/addressBookCollectionRule.go
Normal file
228
http/controller/admin/my/addressBookCollectionRule.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package my
|
||||
|
||||
import (
|
||||
"Gwen/global"
|
||||
"Gwen/http/request/admin"
|
||||
"Gwen/http/response"
|
||||
"Gwen/model"
|
||||
"Gwen/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AddressBookCollectionRule struct {
|
||||
}
|
||||
|
||||
// List 列表
|
||||
// @Tags 我的地址簿规则
|
||||
// @Summary 地址簿规则列表
|
||||
// @Description 地址簿规则列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "页大小"
|
||||
// @Param is_my query int false "是否是我的"
|
||||
// @Param user_id query int false "用户id"
|
||||
// @Param collection_id query int false "地址簿集合id"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBookCollectionList}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection_rule/list [get]
|
||||
// @Security token
|
||||
func (abcr *AddressBookCollectionRule) List(c *gin.Context) {
|
||||
query := &admin.AddressBookCollectionRuleQuery{}
|
||||
if err := c.ShouldBindQuery(query); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
query.UserId = int(u.Id)
|
||||
|
||||
res := service.AllService.AddressBookService.ListRules(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
tx.Where("user_id = ?", query.UserId)
|
||||
if query.CollectionId > 0 {
|
||||
tx.Where("collection_id = ?", query.CollectionId)
|
||||
}
|
||||
})
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// Create 创建地址簿规则
|
||||
// @Tags 我的地址簿规则
|
||||
// @Summary 创建地址簿规则
|
||||
// @Description 创建地址簿规则
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBookCollection}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection_rule/create [post]
|
||||
// @Security token
|
||||
func (abcr *AddressBookCollectionRule) Create(c *gin.Context) {
|
||||
f := &model.AddressBookCollectionRule{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
if f.Type != model.ShareAddressBookRuleTypePersonal && f.Type != model.ShareAddressBookRuleTypeGroup {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
//t := f.ToAddressBookCollection()
|
||||
t := f
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
t.UserId = u.Id
|
||||
msg, res := abcr.CheckForm(u, t)
|
||||
if !res {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, msg))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.CreateRule(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
func (abcr *AddressBookCollectionRule) CheckForm(u *model.User, t *model.AddressBookCollectionRule) (string, bool) {
|
||||
if t.UserId != u.Id {
|
||||
return "NoAccess", false
|
||||
}
|
||||
if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
|
||||
return "ParamsError", false
|
||||
}
|
||||
|
||||
//check to_id
|
||||
if t.Type == model.ShareAddressBookRuleTypePersonal {
|
||||
if t.ToId == t.UserId {
|
||||
return "ParamsError", false
|
||||
}
|
||||
tou := service.AllService.UserService.InfoById(t.ToId)
|
||||
if tou.Id == 0 {
|
||||
return "ItemNotFound", false
|
||||
}
|
||||
//非管理员不能分享给非本组织用户
|
||||
if tou.GroupId != u.GroupId {
|
||||
return "NoAccess", false
|
||||
}
|
||||
} else if t.Type == model.ShareAddressBookRuleTypeGroup {
|
||||
//非管理员不能分享给其他组
|
||||
if t.ToId != u.GroupId {
|
||||
return "NoAccess", false
|
||||
}
|
||||
|
||||
tog := service.AllService.GroupService.InfoById(t.ToId)
|
||||
if tog.Id == 0 {
|
||||
return "ItemNotFound", false
|
||||
}
|
||||
} else {
|
||||
return "ParamsError", false
|
||||
}
|
||||
// 重复检查
|
||||
ex := service.AllService.AddressBookService.RulePersonalInfoByToIdAndCid(t.ToId, t.CollectionId)
|
||||
if t.Id == 0 && ex.Id > 0 {
|
||||
return "ItemExists", false
|
||||
}
|
||||
if t.Id > 0 && ex.Id > 0 && t.Id != ex.Id {
|
||||
return "ItemExists", false
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
|
||||
// Update 编辑
|
||||
// @Tags 我的地址簿规则
|
||||
// @Summary 地址簿规则编辑
|
||||
// @Description 地址簿规则编辑
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
|
||||
// @Success 200 {object} response.Response{data=model.AddressBookCollection}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection_rule/update [post]
|
||||
// @Security token
|
||||
func (abcr *AddressBookCollectionRule) Update(c *gin.Context) {
|
||||
f := &model.AddressBookCollectionRule{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
if f.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
|
||||
ex := service.AllService.AddressBookService.RuleInfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
t := f
|
||||
msg, res := abcr.CheckForm(u, t)
|
||||
if !res {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, msg))
|
||||
return
|
||||
}
|
||||
err := service.AllService.AddressBookService.UpdateRule(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
// @Tags 我的地址簿规则
|
||||
// @Summary 地址簿规则删除
|
||||
// @Description 地址簿规则删除
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/address_book_collection_rule/delete [post]
|
||||
// @Security token
|
||||
func (abcr *AddressBookCollectionRule) Delete(c *gin.Context) {
|
||||
f := &model.AddressBookCollectionRule{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
id := f.Id
|
||||
errList := global.Validator.ValidVar(c, id, "required,gt=0")
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
ex := service.AllService.AddressBookService.RuleInfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
|
||||
err := service.AllService.AddressBookService.DeleteRule(ex)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
}
|
||||
59
http/controller/admin/my/peer.go
Normal file
59
http/controller/admin/my/peer.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package my
|
||||
|
||||
import (
|
||||
"Gwen/http/request/admin"
|
||||
"Gwen/http/response"
|
||||
"Gwen/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Peer struct {
|
||||
}
|
||||
|
||||
// List 列表
|
||||
// @Tags 我的设备
|
||||
// @Summary 设备列表
|
||||
// @Description 设备列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "页大小"
|
||||
// @Param time_ago query int false "时间"
|
||||
// @Param id query string false "ID"
|
||||
// @Param hostname query string false "主机名"
|
||||
// @Param uuids query string false "uuids 用逗号分隔"
|
||||
// @Success 200 {object} response.Response{data=model.PeerList}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/peer/list [get]
|
||||
// @Security token
|
||||
func (ct *Peer) List(c *gin.Context) {
|
||||
query := &admin.PeerQuery{}
|
||||
if err := c.ShouldBindQuery(query); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
res := service.AllService.PeerService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
tx.Where("user_id = ?", u.Id)
|
||||
if query.TimeAgo > 0 {
|
||||
lt := time.Now().Unix() - int64(query.TimeAgo)
|
||||
tx.Where("last_online_time < ?", lt)
|
||||
}
|
||||
if query.TimeAgo < 0 {
|
||||
lt := time.Now().Unix() + int64(query.TimeAgo)
|
||||
tx.Where("last_online_time > ?", lt)
|
||||
}
|
||||
if query.Id != "" {
|
||||
tx.Where("id like ?", "%"+query.Id+"%")
|
||||
}
|
||||
if query.Hostname != "" {
|
||||
tx.Where("hostname like ?", "%"+query.Hostname+"%")
|
||||
}
|
||||
if query.Uuids != "" {
|
||||
tx.Where("uuid in (?)", query.Uuids)
|
||||
}
|
||||
})
|
||||
response.Success(c, res)
|
||||
}
|
||||
@@ -13,7 +13,7 @@ type ShareRecord struct {
|
||||
}
|
||||
|
||||
// List 分享记录列表
|
||||
// @Tags 我的
|
||||
// @Tags 我的分享记录
|
||||
// @Summary 分享记录列表
|
||||
// @Description 分享记录列表
|
||||
// @Accept json
|
||||
@@ -38,7 +38,7 @@ func (sr *ShareRecord) List(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Delete 分享记录删除
|
||||
// @Tags 我的
|
||||
// @Tags 我的分享记录
|
||||
// @Summary 分享记录删除
|
||||
// @Description 分享记录删除
|
||||
// @Accept json
|
||||
|
||||
176
http/controller/admin/my/tag.go
Normal file
176
http/controller/admin/my/tag.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package my
|
||||
|
||||
import (
|
||||
"Gwen/global"
|
||||
"Gwen/http/request/admin"
|
||||
"Gwen/http/response"
|
||||
"Gwen/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Tag struct{}
|
||||
|
||||
// List 列表
|
||||
// @Tags 我的标签
|
||||
// @Summary 标签列表
|
||||
// @Description 标签列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "页大小"
|
||||
// @Param is_my query int false "是否是我的"
|
||||
// @Param user_id query int false "用户id"
|
||||
// @Success 200 {object} response.Response{data=model.TagList}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/tag/list [get]
|
||||
// @Security token
|
||||
func (ct *Tag) List(c *gin.Context) {
|
||||
query := &admin.TagQuery{}
|
||||
if err := c.ShouldBindQuery(query); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
query.UserId = int(u.Id)
|
||||
res := service.AllService.TagService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
tx.Preload("Collection", func(txc *gorm.DB) *gorm.DB {
|
||||
return txc.Select("id,name")
|
||||
})
|
||||
tx.Where("user_id = ?", query.UserId)
|
||||
if query.CollectionId != nil && *query.CollectionId >= 0 {
|
||||
tx.Where("collection_id = ?", query.CollectionId)
|
||||
}
|
||||
})
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// Create 创建标签
|
||||
// @Tags 我的标签
|
||||
// @Summary 创建标签
|
||||
// @Description 创建标签
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body admin.TagForm true "标签信息"
|
||||
// @Success 200 {object} response.Response{data=model.Tag}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/tag/create [post]
|
||||
// @Security token
|
||||
func (ct *Tag) Create(c *gin.Context) {
|
||||
f := &admin.TagForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
t := f.ToTag()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
t.UserId = u.Id
|
||||
err := service.AllService.TagService.Create(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Update 编辑
|
||||
// @Tags 我的标签
|
||||
// @Summary 标签编辑
|
||||
// @Description 标签编辑
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body admin.TagForm true "标签信息"
|
||||
// @Success 200 {object} response.Response{data=model.Tag}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/tag/update [post]
|
||||
// @Security token
|
||||
func (ct *Tag) Update(c *gin.Context) {
|
||||
f := &admin.TagForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
errList := global.Validator.ValidStruct(c, f)
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
if f.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if f.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
ex := service.AllService.TagService.InfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
|
||||
t := f.ToTag()
|
||||
if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.TagService.Update(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
return
|
||||
}
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
// @Tags 标签
|
||||
// @Summary 标签删除
|
||||
// @Description 标签删除
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body admin.TagForm true "标签信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/tag/delete [post]
|
||||
// @Security token
|
||||
func (ct *Tag) Delete(c *gin.Context) {
|
||||
f := &admin.TagForm{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
id := f.Id
|
||||
errList := global.Validator.ValidVar(c, id, "required,gt=0")
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
ex := service.AllService.TagService.InfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if ex.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.TagService.Delete(ex)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, err.Error())
|
||||
return
|
||||
}
|
||||
@@ -64,9 +64,9 @@ func (ct *Tag) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
t := f.ToTag()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || t.UserId == 0 {
|
||||
t.UserId = u.Id
|
||||
if t.UserId == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.TagService.Create(t)
|
||||
if err != nil {
|
||||
@@ -96,10 +96,6 @@ func (ct *Tag) List(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 {
|
||||
query.UserId = int(u.Id)
|
||||
}
|
||||
res := service.AllService.TagService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
tx.Preload("Collection", func(txc *gorm.DB) *gorm.DB {
|
||||
return txc.Select("id,name")
|
||||
@@ -140,12 +136,12 @@ func (ct *Tag) Update(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
t := f.ToTag()
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
ex := service.AllService.TagService.InfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
t := f.ToTag()
|
||||
err := service.AllService.TagService.Update(t)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||
@@ -177,20 +173,15 @@ func (ct *Tag) Delete(c *gin.Context) {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
t := service.AllService.TagService.InfoById(f.Id)
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
ex := service.AllService.TagService.InfoById(f.Id)
|
||||
if ex.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if u.Id > 0 {
|
||||
err := service.AllService.TagService.Delete(t)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, err.Error())
|
||||
err := service.AllService.TagService.Delete(ex)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
response.Fail(c, 101, err.Error())
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -295,51 +294,6 @@ func (ct *User) MyOauth(c *gin.Context) {
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// MyPeer 列表
|
||||
// @Tags 设备
|
||||
// @Summary 我的设备列表
|
||||
// @Description 我的设备列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "页大小"
|
||||
// @Param time_ago query int false "时间"
|
||||
// @Param id query string false "ID"
|
||||
// @Param hostname query string false "主机名"
|
||||
// @Param uuids query string false "uuids 用逗号分隔"
|
||||
// @Success 200 {object} response.Response{data=model.PeerList}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/user/myPeer [get]
|
||||
// @Security token
|
||||
func (ct *User) MyPeer(c *gin.Context) {
|
||||
query := &admin.PeerQuery{}
|
||||
if err := c.ShouldBindQuery(query); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
res := service.AllService.PeerService.ListFilterByUserId(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
if query.TimeAgo > 0 {
|
||||
lt := time.Now().Unix() - int64(query.TimeAgo)
|
||||
tx.Where("last_online_time < ?", lt)
|
||||
}
|
||||
if query.TimeAgo < 0 {
|
||||
lt := time.Now().Unix() + int64(query.TimeAgo)
|
||||
tx.Where("last_online_time > ?", lt)
|
||||
}
|
||||
if query.Id != "" {
|
||||
tx.Where("id like ?", "%"+query.Id+"%")
|
||||
}
|
||||
if query.Hostname != "" {
|
||||
tx.Where("hostname like ?", "%"+query.Hostname+"%")
|
||||
}
|
||||
if query.Uuids != "" {
|
||||
tx.Where("uuid in (?)", query.Uuids)
|
||||
}
|
||||
}, u.Id)
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// groupUsers
|
||||
func (ct *User) GroupUsers(c *gin.Context) {
|
||||
q := &admin.GroupUsersQuery{}
|
||||
|
||||
@@ -62,7 +62,7 @@ func UserBind(rg *gin.RouterGroup) {
|
||||
aR.GET("/current", cont.Current)
|
||||
aR.POST("/changeCurPwd", cont.ChangeCurPwd)
|
||||
aR.POST("/myOauth", cont.MyOauth)
|
||||
aR.GET("/myPeer", cont.MyPeer)
|
||||
//aR.GET("/myPeer", cont.MyPeer)
|
||||
aR.POST("/groupUsers", cont.GroupUsers)
|
||||
}
|
||||
aRP := rg.Group("/user").Use(middleware.AdminPrivilege())
|
||||
@@ -90,7 +90,7 @@ func GroupBind(rg *gin.RouterGroup) {
|
||||
}
|
||||
|
||||
func TagBind(rg *gin.RouterGroup) {
|
||||
aR := rg.Group("/tag")
|
||||
aR := rg.Group("/tag").Use(middleware.AdminPrivilege())
|
||||
{
|
||||
cont := &admin.Tag{}
|
||||
aR.GET("/list", cont.List)
|
||||
@@ -105,16 +105,14 @@ func AddressBookBind(rg *gin.RouterGroup) {
|
||||
aR := rg.Group("/address_book")
|
||||
{
|
||||
cont := &admin.AddressBook{}
|
||||
aR.GET("/list", cont.List)
|
||||
aR.GET("/detail/:id", cont.Detail)
|
||||
aR.POST("/create", cont.Create)
|
||||
aR.POST("/update", cont.Update)
|
||||
aR.POST("/delete", cont.Delete)
|
||||
aR.POST("/shareByWebClient", cont.ShareByWebClient)
|
||||
|
||||
aR.POST("/batchUpdateTags", cont.BatchUpdateTags)
|
||||
|
||||
arp := aR.Use(middleware.AdminPrivilege())
|
||||
arp.GET("/list", cont.List)
|
||||
//arp.GET("/detail/:id", cont.Detail)
|
||||
arp.POST("/create", cont.Create)
|
||||
arp.POST("/update", cont.Update)
|
||||
arp.POST("/delete", cont.Delete)
|
||||
arp.POST("/batchCreate", cont.BatchCreate)
|
||||
arp.POST("/batchCreateFromPeers", cont.BatchCreateFromPeers)
|
||||
|
||||
@@ -176,7 +174,7 @@ func AuditBind(rg *gin.RouterGroup) {
|
||||
afR.POST("/batchDelete", cont.BatchFileDelete)
|
||||
}
|
||||
func AddressBookCollectionBind(rg *gin.RouterGroup) {
|
||||
aR := rg.Group("/address_book_collection")
|
||||
aR := rg.Group("/address_book_collection").Use(middleware.AdminPrivilege())
|
||||
{
|
||||
cont := &admin.AddressBookCollection{}
|
||||
aR.GET("/list", cont.List)
|
||||
@@ -188,7 +186,7 @@ func AddressBookCollectionBind(rg *gin.RouterGroup) {
|
||||
|
||||
}
|
||||
func AddressBookCollectionRuleBind(rg *gin.RouterGroup) {
|
||||
aR := rg.Group("/address_book_collection_rule")
|
||||
aR := rg.Group("/address_book_collection_rule").Use(middleware.AdminPrivilege())
|
||||
{
|
||||
cont := &admin.AddressBookCollectionRule{}
|
||||
aR.GET("/list", cont.List)
|
||||
@@ -228,13 +226,49 @@ func FileBind(rg *gin.RouterGroup) {
|
||||
|
||||
func MyBind(rg *gin.RouterGroup) {
|
||||
{
|
||||
msr := &my.ShareRecord{}
|
||||
rg.GET("/my/share_record/list", msr.List)
|
||||
rg.POST("/my/share_record/delete", msr.Delete)
|
||||
rg.POST("/my/share_record/batchDelete", msr.BatchDelete)
|
||||
cont := &my.ShareRecord{}
|
||||
rg.GET("/my/share_record/list", cont.List)
|
||||
rg.POST("/my/share_record/delete", cont.Delete)
|
||||
rg.POST("/my/share_record/batchDelete", cont.BatchDelete)
|
||||
}
|
||||
|
||||
{
|
||||
cont := &my.AddressBook{}
|
||||
rg.GET("/my/address_book/list", cont.List)
|
||||
rg.POST("/my/address_book/create", cont.Create)
|
||||
rg.POST("/my/address_book/update", cont.Update)
|
||||
rg.POST("/my/address_book/delete", cont.Delete)
|
||||
rg.POST("/my/address_book/batchCreateFromPeers", cont.BatchCreateFromPeers)
|
||||
rg.POST("/my/address_book/batchUpdateTags", cont.BatchUpdateTags)
|
||||
}
|
||||
|
||||
{
|
||||
cont := &my.Tag{}
|
||||
rg.GET("/my/tag/list", cont.List)
|
||||
rg.POST("/my/tag/create", cont.Create)
|
||||
rg.POST("/my/tag/update", cont.Update)
|
||||
rg.POST("/my/tag/delete", cont.Delete)
|
||||
}
|
||||
|
||||
{
|
||||
cont := &my.AddressBookCollection{}
|
||||
rg.GET("/my/address_book_collection/list", cont.List)
|
||||
rg.POST("/my/address_book_collection/create", cont.Create)
|
||||
rg.POST("/my/address_book_collection/update", cont.Update)
|
||||
rg.POST("/my/address_book_collection/delete", cont.Delete)
|
||||
}
|
||||
|
||||
{
|
||||
cont := &my.AddressBookCollectionRule{}
|
||||
rg.GET("/my/address_book_collection_rule/list", cont.List)
|
||||
rg.POST("/my/address_book_collection_rule/create", cont.Create)
|
||||
rg.POST("/my/address_book_collection_rule/update", cont.Update)
|
||||
rg.POST("/my/address_book_collection_rule/delete", cont.Delete)
|
||||
}
|
||||
{
|
||||
cont := &my.Peer{}
|
||||
rg.GET("/my/peer/list", cont.List)
|
||||
|
||||
mab := &my.AddressBook{}
|
||||
rg.POST("/my/address_book/batchCreateFromPeers", mab.BatchCreateFromPeers)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user