diff --git a/http/controller/admin/addressBook.go b/http/controller/admin/addressBook.go index 4983ecb..14db329 100644 --- a/http/controller/admin/addressBook.go +++ b/http/controller/admin/addressBook.go @@ -335,7 +335,11 @@ func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) { response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) return } - u := service.AllService.UserService.CurUser(c) + + if f.UserId == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")) + return + } if f.CollectionId != 0 { collection := service.AllService.AddressBookService.CollectionInfoById(f.CollectionId) @@ -343,17 +347,13 @@ func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) { response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) return } - if collection.UserId != u.Id { - response.Fail(c, 101, response.TranslateMsg(c, "NoAccess")) - return - } } - peers := service.AllService.PeerService.List(1, 999, func(tx *gorm.DB) { + pl := int64(len(f.PeerIds)) + peers := service.AllService.PeerService.List(1, uint(pl), func(tx *gorm.DB) { tx.Where("row_id in ?", f.PeerIds) - tx.Where("user_id = ?", u.Id) }) - if peers.Total == 0 { + if peers.Total == 0 || pl != peers.Total { response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) return } @@ -363,7 +363,8 @@ func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) { ab := service.AllService.AddressBookService.FromPeer(peer) ab.Tags = tags ab.CollectionId = f.CollectionId - ex := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(u.Id, ab.Id, ab.CollectionId) + ab.UserId = f.UserId + ex := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(f.UserId, ab.Id, ab.CollectionId) if ex.RowId != 0 { continue } diff --git a/http/controller/admin/my/addressBook.go b/http/controller/admin/my/addressBook.go new file mode 100644 index 0000000..c3c89c1 --- /dev/null +++ b/http/controller/admin/my/addressBook.go @@ -0,0 +1,59 @@ +package my + +import ( + "Gwen/http/request/admin" + "Gwen/http/response" + "Gwen/service" + "encoding/json" + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +type AddressBook struct{} + +func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) { + f := &admin.BatchCreateFromPeersForm{} + if err := c.ShouldBindJSON(f); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + u := service.AllService.UserService.CurUser(c) + + if f.CollectionId != 0 { + collection := service.AllService.AddressBookService.CollectionInfoById(f.CollectionId) + if collection.Id == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + return + } + if collection.UserId != u.Id { + response.Fail(c, 101, response.TranslateMsg(c, "NoAccess")) + return + } + } + if len(f.PeerIds) == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")) + return + } + pl := int64(len(f.PeerIds)) + peers := service.AllService.PeerService.List(1, uint(pl), func(tx *gorm.DB) { + tx.Where("row_id in ?", f.PeerIds) + tx.Where("user_id = ?", u.Id) + }) + if peers.Total == 0 || pl != peers.Total { + response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound")) + return + } + + tags, _ := json.Marshal(f.Tags) + for _, peer := range peers.Peers { + ab := service.AllService.AddressBookService.FromPeer(peer) + ab.Tags = tags + ab.CollectionId = f.CollectionId + ex := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(u.Id, ab.Id, ab.CollectionId) + if ex.RowId != 0 { + continue + } + service.AllService.AddressBookService.Create(ab) + } + response.Success(c, nil) +} diff --git a/http/request/admin/addressBook.go b/http/request/admin/addressBook.go index 94452e5..cf505ad 100644 --- a/http/request/admin/addressBook.go +++ b/http/request/admin/addressBook.go @@ -127,6 +127,7 @@ type BatchCreateFromPeersForm struct { CollectionId uint `json:"collection_id"` PeerIds []uint `json:"peer_ids"` Tags []string `json:"tags"` + UserId uint `json:"user_id"` } type BatchUpdateTagsForm struct { RowIds []uint `json:"row_ids"` diff --git a/http/router/admin.go b/http/router/admin.go index 6f7fba1..55ca164 100644 --- a/http/router/admin.go +++ b/http/router/admin.go @@ -111,11 +111,12 @@ func AddressBookBind(rg *gin.RouterGroup) { aR.POST("/update", cont.Update) aR.POST("/delete", cont.Delete) aR.POST("/shareByWebClient", cont.ShareByWebClient) - aR.POST("/batchCreateFromPeers", cont.BatchCreateFromPeers) + aR.POST("/batchUpdateTags", cont.BatchUpdateTags) arp := aR.Use(middleware.AdminPrivilege()) arp.POST("/batchCreate", cont.BatchCreate) + arp.POST("/batchCreateFromPeers", cont.BatchCreateFromPeers) } } @@ -227,10 +228,13 @@ func FileBind(rg *gin.RouterGroup) { func MyBind(rg *gin.RouterGroup) { { - sr := &my.ShareRecord{} - rg.GET("/my/share_record/list", sr.List) - rg.POST("/my/share_record/delete", sr.Delete) - rg.POST("/my/share_record/batchDelete", sr.BatchDelete) + 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) + + mab := &my.AddressBook{} + rg.POST("/my/address_book/batchCreateFromPeers", mab.BatchCreateFromPeers) } }