role.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package v1
  2. /*
  3. import (
  4. "io/ioutil"
  5. "net/http"
  6. "tmr-watch/middleware/inject"
  7. "tmr-watch/pkg/app"
  8. "tmr-watch/pkg/e"
  9. "tmr-watch/conf/setting"
  10. "tmr-watch/pkg/util"
  11. "github.com/Anderson-Lu/gofasion/gofasion"
  12. "github.com/Unknwon/com"
  13. "github.com/astaxie/beego/validation"
  14. "github.com/gin-gonic/gin"
  15. Role_service "github.com/hequan2017/go-admin/service/role_service"
  16. )
  17. // @Summary 获取单个角色
  18. // @Tags role
  19. // @Accept json
  20. // @Produce json
  21. // @Param id path string true "id"
  22. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  23. // @Router /authdata/roles/:id [GET]
  24. func GetRole(c *gin.Context) {
  25. appG := app.Gin{C: c}
  26. id := com.StrTo(c.Param("id")).MustInt()
  27. valid := validation.Validation{}
  28. valid.Min(id, 1, "id").Message("ID必须大于0")
  29. if valid.HasErrors() {
  30. app.MarkErrors(valid.Errors)
  31. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  32. return
  33. }
  34. RoleService := Role_service.Role{ID: id}
  35. exists, err := RoleService.ExistByID()
  36. if err != nil {
  37. appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
  38. return
  39. }
  40. if !exists {
  41. appG.Response(http.StatusOK, e.ERROR_NOT_EXIST, nil)
  42. return
  43. }
  44. article, err := RoleService.Get()
  45. if err != nil {
  46. appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
  47. return
  48. }
  49. appG.Response(http.StatusOK, e.SUCCESS, article)
  50. }
  51. // @Summary 获取所有角色
  52. // @Tags role
  53. // @Accept json
  54. // @Produce json
  55. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  56. // @Router /authdata/roles [GET]
  57. func GetRoles(c *gin.Context) {
  58. appG := app.Gin{C: c}
  59. name := c.Query("name")
  60. valid := validation.Validation{}
  61. if valid.HasErrors() {
  62. app.MarkErrors(valid.Errors)
  63. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  64. return
  65. }
  66. RoleService := Role_service.Role{
  67. Name: name,
  68. PageNum: util.GetPage(c),
  69. PageSize: setting.AppSetting.PageSize,
  70. }
  71. total, err := RoleService.Count()
  72. if err != nil {
  73. appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_FAIL, nil)
  74. return
  75. }
  76. articles, err := RoleService.GetAll()
  77. if err != nil {
  78. appG.Response(http.StatusInternalServerError, e.ERROR_GET_S_FAIL, nil)
  79. return
  80. }
  81. data := make(map[string]interface{})
  82. data["lists"] = articles
  83. data["total"] = total
  84. appG.Response(http.StatusOK, e.SUCCESS, data)
  85. }
  86. // @Summary 增加角色
  87. // @Tags role
  88. // @Accept json
  89. // @Produce json
  90. // @Param name query string true "name"
  91. // @Param menu_id query string true "menu_id"
  92. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  93. // @Router /authdata/roles [POST]
  94. func AddRole(c *gin.Context) {
  95. var (
  96. appG = app.Gin{C: c}
  97. )
  98. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  99. fsion := gofasion.NewFasion(string(dataByte))
  100. name := fsion.Get("username").ValueStr()
  101. menuId := com.StrTo(fsion.Get("menu_id").ValueInt()).MustInt()
  102. valid := validation.Validation{}
  103. valid.MaxSize(name, 100, "path").Message("名称最长为100字符")
  104. if valid.HasErrors() {
  105. app.MarkErrors(valid.Errors)
  106. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  107. return
  108. }
  109. RoleService := Role_service.Role{
  110. Name: name,
  111. Menu: menuId,
  112. }
  113. if id, err := RoleService.Add(); err != nil {
  114. err = inject.Obj.Common.RoleAPI.LoadPolicy(id)
  115. if err != nil {
  116. appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
  117. return
  118. }
  119. appG.Response(http.StatusOK, e.SUCCESS, nil)
  120. } else {
  121. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  122. return
  123. }
  124. }
  125. // @Summary 更新角色
  126. // @Tags role
  127. // @Accept json
  128. // @Produce json
  129. // @Param id path string true "id"
  130. // @Param name query string true "name"
  131. // @Param menu_id query string true "menu_id"
  132. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  133. // @Router /authdata/roles/:id [PUT]
  134. func EditRole(c *gin.Context) {
  135. var (
  136. appG = app.Gin{C: c}
  137. )
  138. id := com.StrTo(c.Param("id")).MustInt()
  139. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  140. fsion := gofasion.NewFasion(string(dataByte))
  141. name := fsion.Get("username").ValueStr()
  142. menuId := com.StrTo(fsion.Get("menu_id").ValueInt()).MustInt()
  143. valid := validation.Validation{}
  144. valid.MaxSize(name, 100, "path").Message("名称最长为100字符")
  145. if valid.HasErrors() {
  146. app.MarkErrors(valid.Errors)
  147. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  148. return
  149. }
  150. RoleService := Role_service.Role{
  151. ID: id,
  152. Name: name,
  153. Menu: menuId,
  154. }
  155. exists, err := RoleService.ExistByID()
  156. if err != nil {
  157. appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
  158. return
  159. }
  160. if !exists {
  161. appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
  162. return
  163. }
  164. err = RoleService.Edit()
  165. if err != nil {
  166. appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
  167. return
  168. }
  169. err = inject.Obj.Common.RoleAPI.LoadPolicy(id)
  170. if err != nil {
  171. appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
  172. return
  173. }
  174. appG.Response(http.StatusOK, e.SUCCESS, nil)
  175. }
  176. // @Summary 删除角色
  177. // @Tags role
  178. // @Accept json
  179. // @Produce json
  180. // @Param id path string true "id"
  181. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  182. // @Router /authdata/roles/:id [DELETE]
  183. func DeleteRole(c *gin.Context) {
  184. appG := app.Gin{C: c}
  185. valid := validation.Validation{}
  186. id := com.StrTo(c.Param("id")).MustInt()
  187. valid.Min(id, 1, "id").Message("ID必须大于0")
  188. if valid.HasErrors() {
  189. app.MarkErrors(valid.Errors)
  190. appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
  191. return
  192. }
  193. RoleService := Role_service.Role{ID: id}
  194. exists, err := RoleService.ExistByID()
  195. if err != nil {
  196. appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
  197. return
  198. }
  199. if !exists {
  200. appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
  201. return
  202. }
  203. role, err := RoleService.Get()
  204. err = RoleService.Delete()
  205. if err != nil {
  206. appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_FAIL, nil)
  207. return
  208. }
  209. inject.Obj.Enforcer.DeleteUser(role.Name)
  210. appG.Response(http.StatusOK, e.SUCCESS, nil)
  211. }
  212. */