addressBookCollectionRule.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package admin
  2. import (
  3. "Gwen/global"
  4. "Gwen/http/request/admin"
  5. "Gwen/http/response"
  6. "Gwen/model"
  7. "Gwen/service"
  8. "github.com/gin-gonic/gin"
  9. "gorm.io/gorm"
  10. "strconv"
  11. )
  12. type AddressBookCollectionRule struct {
  13. }
  14. // List 列表
  15. // @AddressBookCollectionRule 地址簿集合规则
  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. u := service.AllService.UserService.CurUser(c)
  36. if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 {
  37. query.UserId = int(u.Id)
  38. }
  39. res := service.AllService.AddressBookService.ListRules(query.Page, query.PageSize, func(tx *gorm.DB) {
  40. if query.UserId > 0 {
  41. tx.Where("user_id = ?", query.UserId)
  42. }
  43. if query.CollectionId > 0 {
  44. tx.Where("collection_id = ?", query.CollectionId)
  45. }
  46. })
  47. response.Success(c, res)
  48. }
  49. // Detail 地址簿集合规则
  50. // @AddressBookCollectionRule 地址簿集合规则
  51. // @Summary 地址簿集合规则详情
  52. // @Description 地址簿集合规则详情
  53. // @Accept json
  54. // @Produce json
  55. // @Param id path int true "ID"
  56. // @Success 200 {object} response.Response{data=model.AddressBookCollectionRule}
  57. // @Failure 500 {object} response.Response
  58. // @Router /admin/address_book_collection_rule/detail/{id} [get]
  59. // @Security token
  60. func (abcr *AddressBookCollectionRule) Detail(c *gin.Context) {
  61. id := c.Param("id")
  62. iid, _ := strconv.Atoi(id)
  63. t := service.AllService.AddressBookService.RuleInfoById(uint(iid))
  64. u := service.AllService.UserService.CurUser(c)
  65. if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
  66. response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
  67. return
  68. }
  69. if t.Id > 0 {
  70. response.Success(c, t)
  71. return
  72. }
  73. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  74. return
  75. }
  76. // Create 创建地址簿集合规则
  77. // @AddressBookCollectionRule 地址簿集合规则
  78. // @Summary 创建地址簿集合规则
  79. // @Description 创建地址簿集合规则
  80. // @Accept json
  81. // @Produce json
  82. // @Param body body model.AddressBookCollectionRule true "地址簿集合规则信息"
  83. // @Success 200 {object} response.Response{data=model.AddressBookCollection}
  84. // @Failure 500 {object} response.Response
  85. // @Router /admin/address_book_collection_rule/create [post]
  86. // @Security token
  87. func (abcr *AddressBookCollectionRule) Create(c *gin.Context) {
  88. f := &model.AddressBookCollectionRule{}
  89. if err := c.ShouldBindJSON(f); err != nil {
  90. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  91. return
  92. }
  93. errList := global.Validator.ValidStruct(c, f)
  94. if len(errList) > 0 {
  95. response.Fail(c, 101, errList[0])
  96. return
  97. }
  98. if f.Type != model.ShareAddressBookRuleTypePersonal && f.Type != model.ShareAddressBookRuleTypeGroup {
  99. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  100. return
  101. }
  102. //t := f.ToAddressBookCollection()
  103. t := f
  104. u := service.AllService.UserService.CurUser(c)
  105. if t.UserId == 0 {
  106. t.UserId = u.Id
  107. }
  108. msg, res := abcr.CheckForm(u, t)
  109. if !res {
  110. response.Fail(c, 101, response.TranslateMsg(c, msg))
  111. return
  112. }
  113. err := service.AllService.AddressBookService.CreateRule(t)
  114. if err != nil {
  115. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  116. return
  117. }
  118. response.Success(c, nil)
  119. }
  120. func (abcr *AddressBookCollectionRule) CheckForm(u *model.User, t *model.AddressBookCollectionRule) (string, bool) {
  121. if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
  122. return "NoAccess", false
  123. }
  124. if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
  125. return "ParamsError", false
  126. }
  127. //check to_id
  128. if t.Type == model.ShareAddressBookRuleTypePersonal {
  129. if t.ToId == t.UserId {
  130. return "ParamsError", false
  131. }
  132. tou := service.AllService.UserService.InfoById(t.ToId)
  133. if tou.Id == 0 {
  134. return "ItemNotFound", false
  135. }
  136. //非管理员不能分享给非本组织用户
  137. if tou.GroupId != u.GroupId && !service.AllService.UserService.IsAdmin(u) {
  138. return "NoAccess", false
  139. }
  140. } else if t.Type == model.ShareAddressBookRuleTypeGroup {
  141. if t.ToId != u.GroupId && !service.AllService.UserService.IsAdmin(u) {
  142. return "NoAccess", false
  143. }
  144. tog := service.AllService.GroupService.InfoById(t.ToId)
  145. if tog.Id == 0 {
  146. return "ItemNotFound", false
  147. }
  148. } else {
  149. return "ParamsError", false
  150. }
  151. // 重复检查
  152. ex := service.AllService.AddressBookService.RulePersonalInfoByToIdAndCid(t.ToId, t.CollectionId)
  153. if t.Id == 0 && ex.Id > 0 {
  154. return "ItemExists", false
  155. }
  156. if t.Id > 0 && ex.Id > 0 && t.Id != ex.Id {
  157. return "ItemExists", false
  158. }
  159. return "", true
  160. }
  161. // Update 编辑
  162. // @AddressBookCollectionRule 地址簿集合规则
  163. // @Summary 地址簿集合规则编辑
  164. // @Description 地址簿集合规则编辑
  165. // @Accept json
  166. // @Produce json
  167. // @Param body body model.AddressBookCollectionRule true "地址簿集合规则信息"
  168. // @Success 200 {object} response.Response{data=model.AddressBookCollection}
  169. // @Failure 500 {object} response.Response
  170. // @Router /admin/address_book_collection_rule/update [post]
  171. // @Security token
  172. func (abcr *AddressBookCollectionRule) Update(c *gin.Context) {
  173. f := &model.AddressBookCollectionRule{}
  174. if err := c.ShouldBindJSON(f); err != nil {
  175. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  176. return
  177. }
  178. errList := global.Validator.ValidStruct(c, f)
  179. if len(errList) > 0 {
  180. response.Fail(c, 101, errList[0])
  181. return
  182. }
  183. if f.Id == 0 {
  184. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  185. return
  186. }
  187. t := f //f.ToAddressBookCollection()
  188. u := service.AllService.UserService.CurUser(c)
  189. msg, res := abcr.CheckForm(u, t)
  190. if !res {
  191. response.Fail(c, 101, response.TranslateMsg(c, msg))
  192. return
  193. }
  194. err := service.AllService.AddressBookService.UpdateRule(t)
  195. if err != nil {
  196. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  197. return
  198. }
  199. response.Success(c, nil)
  200. }
  201. // Delete 删除
  202. // @AddressBookCollectionRule 地址簿集合规则
  203. // @Summary 地址簿集合规则删除
  204. // @Description 地址簿集合规则删除
  205. // @Accept json
  206. // @Produce json
  207. // @Param body body model.AddressBookCollectionRule true "地址簿集合规则信息"
  208. // @Success 200 {object} response.Response
  209. // @Failure 500 {object} response.Response
  210. // @Router /admin/address_book_collection_rule/delete [post]
  211. // @Security token
  212. func (abcr *AddressBookCollectionRule) Delete(c *gin.Context) {
  213. f := &model.AddressBookCollectionRule{}
  214. if err := c.ShouldBindJSON(f); err != nil {
  215. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  216. return
  217. }
  218. id := f.Id
  219. errList := global.Validator.ValidVar(c, id, "required,gt=0")
  220. if len(errList) > 0 {
  221. response.Fail(c, 101, errList[0])
  222. return
  223. }
  224. t := service.AllService.AddressBookService.RuleInfoById(f.Id)
  225. u := service.AllService.UserService.CurUser(c)
  226. if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
  227. response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
  228. return
  229. }
  230. if t.Id > 0 {
  231. err := service.AllService.AddressBookService.DeleteRule(t)
  232. if err == nil {
  233. response.Success(c, nil)
  234. return
  235. }
  236. response.Fail(c, 101, err.Error())
  237. return
  238. }
  239. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  240. }