| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package admin
- import (
- "github.com/gin-gonic/gin"
- "github.com/lejianwen/rustdesk-api/v2/global"
- "github.com/lejianwen/rustdesk-api/v2/http/request/admin"
- "github.com/lejianwen/rustdesk-api/v2/http/response"
- "github.com/lejianwen/rustdesk-api/v2/model"
- "github.com/lejianwen/rustdesk-api/v2/service"
- "gorm.io/gorm"
- "strconv"
- )
- 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/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
- }
- res := service.AllService.AddressBookService.ListRules(query.Page, query.PageSize, func(tx *gorm.DB) {
- if query.UserId > 0 {
- tx.Where("user_id = ?", query.UserId)
- }
- if query.CollectionId > 0 {
- tx.Where("collection_id = ?", query.CollectionId)
- }
- })
- response.Success(c, res)
- }
- // Detail 地址簿规则
- // @Tags 地址簿规则
- // @Summary 地址簿规则详情
- // @Description 地址簿规则详情
- // @Accept json
- // @Produce json
- // @Param id path int true "ID"
- // @Success 200 {object} response.Response{data=model.AddressBookCollectionRule}
- // @Failure 500 {object} response.Response
- // @Router /admin/address_book_collection_rule/detail/{id} [get]
- // @Security token
- func (abcr *AddressBookCollectionRule) Detail(c *gin.Context) {
- id := c.Param("id")
- iid, _ := strconv.Atoi(id)
- t := service.AllService.AddressBookService.RuleInfoById(uint(iid))
- if t.Id > 0 {
- response.Success(c, t)
- return
- }
- response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
- }
- // 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/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
- msg, res := abcr.CheckForm(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(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
- }
- //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
- }
- } else if t.Type == model.ShareAddressBookRuleTypeGroup {
- 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/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
- }
- t := f
- msg, res := abcr.CheckForm(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/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
- }
- err := service.AllService.AddressBookService.DeleteRule(ex)
- if err == nil {
- response.Success(c, nil)
- return
- }
- response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
- }
|