addressBookCollectionRule.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package admin
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/lejianwen/rustdesk-api/global"
  5. "github.com/lejianwen/rustdesk-api/http/request/admin"
  6. "github.com/lejianwen/rustdesk-api/http/response"
  7. "github.com/lejianwen/rustdesk-api/model"
  8. "github.com/lejianwen/rustdesk-api/service"
  9. "gorm.io/gorm"
  10. "strconv"
  11. )
  12. type AddressBookCollectionRule struct {
  13. }
  14. // List 列表
  15. // @Tags 地址簿规则
  16. // @Summary 地址簿规则列表
  17. // @Description 地址簿规则列表
  18. // @Accept json
  19. // @Produce json
  20. // @Param page query int false "页码"
  21. // @Param page_size query int false "页大小"
  22. // @Param is_my query int false "是否是我的"
  23. // @Param user_id query int false "用户id"
  24. // @Param collection_id query int false "地址簿集合id"
  25. // @Success 200 {object} response.Response{data=model.AddressBookCollectionList}
  26. // @Failure 500 {object} response.Response
  27. // @Router /admin/address_book_collection_rule/list [get]
  28. // @Security token
  29. func (abcr *AddressBookCollectionRule) List(c *gin.Context) {
  30. query := &admin.AddressBookCollectionRuleQuery{}
  31. if err := c.ShouldBindQuery(query); err != nil {
  32. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  33. return
  34. }
  35. res := service.AllService.AddressBookService.ListRules(query.Page, query.PageSize, func(tx *gorm.DB) {
  36. if query.UserId > 0 {
  37. tx.Where("user_id = ?", query.UserId)
  38. }
  39. if query.CollectionId > 0 {
  40. tx.Where("collection_id = ?", query.CollectionId)
  41. }
  42. })
  43. response.Success(c, res)
  44. }
  45. // Detail 地址簿规则
  46. // @Tags 地址簿规则
  47. // @Summary 地址簿规则详情
  48. // @Description 地址簿规则详情
  49. // @Accept json
  50. // @Produce json
  51. // @Param id path int true "ID"
  52. // @Success 200 {object} response.Response{data=model.AddressBookCollectionRule}
  53. // @Failure 500 {object} response.Response
  54. // @Router /admin/address_book_collection_rule/detail/{id} [get]
  55. // @Security token
  56. func (abcr *AddressBookCollectionRule) Detail(c *gin.Context) {
  57. id := c.Param("id")
  58. iid, _ := strconv.Atoi(id)
  59. t := service.AllService.AddressBookService.RuleInfoById(uint(iid))
  60. if t.Id > 0 {
  61. response.Success(c, t)
  62. return
  63. }
  64. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  65. }
  66. // Create 创建地址簿规则
  67. // @Tags 地址簿规则
  68. // @Summary 创建地址簿规则
  69. // @Description 创建地址簿规则
  70. // @Accept json
  71. // @Produce json
  72. // @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
  73. // @Success 200 {object} response.Response{data=model.AddressBookCollection}
  74. // @Failure 500 {object} response.Response
  75. // @Router /admin/address_book_collection_rule/create [post]
  76. // @Security token
  77. func (abcr *AddressBookCollectionRule) Create(c *gin.Context) {
  78. f := &model.AddressBookCollectionRule{}
  79. if err := c.ShouldBindJSON(f); err != nil {
  80. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  81. return
  82. }
  83. errList := global.Validator.ValidStruct(c, f)
  84. if len(errList) > 0 {
  85. response.Fail(c, 101, errList[0])
  86. return
  87. }
  88. if f.Type != model.ShareAddressBookRuleTypePersonal && f.Type != model.ShareAddressBookRuleTypeGroup {
  89. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  90. return
  91. }
  92. t := f
  93. msg, res := abcr.CheckForm(t)
  94. if !res {
  95. response.Fail(c, 101, response.TranslateMsg(c, msg))
  96. return
  97. }
  98. err := service.AllService.AddressBookService.CreateRule(t)
  99. if err != nil {
  100. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  101. return
  102. }
  103. response.Success(c, nil)
  104. }
  105. func (abcr *AddressBookCollectionRule) CheckForm(t *model.AddressBookCollectionRule) (string, bool) {
  106. if t.UserId == 0 {
  107. return "ParamsError", false
  108. }
  109. if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
  110. return "ParamsError", false
  111. }
  112. //check to_id
  113. if t.Type == model.ShareAddressBookRuleTypePersonal {
  114. if t.ToId == t.UserId {
  115. return "ParamsError", false
  116. }
  117. tou := service.AllService.UserService.InfoById(t.ToId)
  118. if tou.Id == 0 {
  119. return "ItemNotFound", false
  120. }
  121. } else if t.Type == model.ShareAddressBookRuleTypeGroup {
  122. tog := service.AllService.GroupService.InfoById(t.ToId)
  123. if tog.Id == 0 {
  124. return "ItemNotFound", false
  125. }
  126. } else {
  127. return "ParamsError", false
  128. }
  129. // 重复检查
  130. ex := service.AllService.AddressBookService.RulePersonalInfoByToIdAndCid(t.ToId, t.CollectionId)
  131. if t.Id == 0 && ex.Id > 0 {
  132. return "ItemExists", false
  133. }
  134. if t.Id > 0 && ex.Id > 0 && t.Id != ex.Id {
  135. return "ItemExists", false
  136. }
  137. return "", true
  138. }
  139. // Update 编辑
  140. // @Tags 地址簿规则
  141. // @Summary 地址簿规则编辑
  142. // @Description 地址簿规则编辑
  143. // @Accept json
  144. // @Produce json
  145. // @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
  146. // @Success 200 {object} response.Response{data=model.AddressBookCollection}
  147. // @Failure 500 {object} response.Response
  148. // @Router /admin/address_book_collection_rule/update [post]
  149. // @Security token
  150. func (abcr *AddressBookCollectionRule) Update(c *gin.Context) {
  151. f := &model.AddressBookCollectionRule{}
  152. if err := c.ShouldBindJSON(f); err != nil {
  153. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  154. return
  155. }
  156. errList := global.Validator.ValidStruct(c, f)
  157. if len(errList) > 0 {
  158. response.Fail(c, 101, errList[0])
  159. return
  160. }
  161. if f.Id == 0 {
  162. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  163. return
  164. }
  165. t := f
  166. msg, res := abcr.CheckForm(t)
  167. if !res {
  168. response.Fail(c, 101, response.TranslateMsg(c, msg))
  169. return
  170. }
  171. err := service.AllService.AddressBookService.UpdateRule(t)
  172. if err != nil {
  173. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  174. return
  175. }
  176. response.Success(c, nil)
  177. }
  178. // Delete 删除
  179. // @Tags 地址簿规则
  180. // @Summary 地址簿规则删除
  181. // @Description 地址簿规则删除
  182. // @Accept json
  183. // @Produce json
  184. // @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
  185. // @Success 200 {object} response.Response
  186. // @Failure 500 {object} response.Response
  187. // @Router /admin/address_book_collection_rule/delete [post]
  188. // @Security token
  189. func (abcr *AddressBookCollectionRule) Delete(c *gin.Context) {
  190. f := &model.AddressBookCollectionRule{}
  191. if err := c.ShouldBindJSON(f); err != nil {
  192. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  193. return
  194. }
  195. id := f.Id
  196. errList := global.Validator.ValidVar(c, id, "required,gt=0")
  197. if len(errList) > 0 {
  198. response.Fail(c, 101, errList[0])
  199. return
  200. }
  201. ex := service.AllService.AddressBookService.RuleInfoById(f.Id)
  202. if ex.Id == 0 {
  203. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  204. return
  205. }
  206. err := service.AllService.AddressBookService.DeleteRule(ex)
  207. if err == nil {
  208. response.Success(c, nil)
  209. return
  210. }
  211. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  212. }