user.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package system
  2. import (
  3. "kpt-pasture/http/middleware"
  4. "net/http"
  5. "strconv"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  8. "gitee.com/xuyiping_admin/pkg/apierr"
  9. "gitee.com/xuyiping_admin/pkg/ginutil"
  10. "gitee.com/xuyiping_admin/pkg/valid"
  11. "github.com/gin-gonic/gin"
  12. )
  13. // Login 用户登录
  14. func Login(c *gin.Context) {
  15. var req pasturePb.SearchUserRequest
  16. if err := ginutil.BindProto(c, &req); err != nil {
  17. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  18. return
  19. }
  20. if err := valid.ValidateStruct(&req,
  21. valid.Field(&req.Name, valid.Required),
  22. valid.Field(&req.Password, valid.Required),
  23. ); err != nil {
  24. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  25. return
  26. }
  27. res, err := middleware.Dependency(c).StoreEventHub.OpsService.Login(c, &req)
  28. if err != nil {
  29. apierr.ClassifiedAbort(c, err)
  30. return
  31. }
  32. ginutil.JSONResp(c, res)
  33. }
  34. // UserCreateOrUpdate 创建系统用户
  35. func UserCreateOrUpdate(c *gin.Context) {
  36. var req pasturePb.SearchUserRequest
  37. if err := ginutil.BindProto(c, &req); err != nil {
  38. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  39. return
  40. }
  41. if err := valid.ValidateStruct(&req,
  42. valid.Field(&req.Name, valid.Required),
  43. valid.Field(&req.DeptId, valid.Required),
  44. valid.Field(&req.Mobile, valid.Required),
  45. ); err != nil {
  46. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  47. return
  48. }
  49. if err := middleware.Dependency(c).StoreEventHub.OpsService.SystemUserCreateOrUpdate(c, &req); err != nil {
  50. apierr.ClassifiedAbort(c, err)
  51. return
  52. }
  53. ginutil.JSONResp(c, &operationPb.CommonOK{
  54. Code: http.StatusOK,
  55. Msg: "ok",
  56. Data: &operationPb.Success{Success: true},
  57. })
  58. }
  59. // DetailsSystemUser 系统用户详情
  60. func DetailsSystemUser(c *gin.Context) {
  61. userIdStr := c.Param("user_id")
  62. userId, _ := strconv.Atoi(userIdStr)
  63. if err := valid.Validate(userId, valid.Required, valid.Min(1)); err != nil {
  64. apierr.ClassifiedAbort(c, err)
  65. return
  66. }
  67. res, err := middleware.BackendOperation(c).OpsService.DetailsSystemUser(c, int64(userId))
  68. if err != nil {
  69. apierr.ClassifiedAbort(c, err)
  70. return
  71. }
  72. ginutil.JSONResp(c, res)
  73. }
  74. // GetUserInfo 获取用户信息
  75. func GetUserInfo(c *gin.Context) {
  76. token := middleware.GetToken(c)
  77. if err := valid.Validate(token, valid.Required); err != nil {
  78. apierr.ClassifiedAbort(c, err)
  79. return
  80. }
  81. res, err := middleware.BackendOperation(c).OpsService.GetUserInfo(c, token)
  82. if err != nil {
  83. apierr.ClassifiedAbort(c, err)
  84. return
  85. }
  86. ginutil.JSONResp(c, res)
  87. }
  88. // SearchSystemUserList 查询系统用户列表
  89. func SearchSystemUserList(c *gin.Context) {
  90. var req pasturePb.SearchUserRequest
  91. if err := ginutil.BindProto(c, &req); err != nil {
  92. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  93. return
  94. }
  95. if err := valid.ValidateStruct(&req,
  96. valid.Field(&req.IsShow, valid.Max(2), valid.Min(1)),
  97. ); err != nil {
  98. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  99. return
  100. }
  101. req.Pagination = &pasturePb.PaginationModel{
  102. Page: int32(c.GetInt(middleware.Page)),
  103. PageSize: int32(c.GetInt(middleware.PageSize)),
  104. PageOffset: int32(c.GetInt(middleware.PageOffset)),
  105. }
  106. res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchSystemUserList(c, &req)
  107. if err != nil {
  108. apierr.ClassifiedAbort(c, err)
  109. return
  110. }
  111. ginutil.JSONResp(c, res)
  112. }
  113. // DeleteUser 删除系统用户
  114. func DeleteUser(c *gin.Context) {
  115. userIdStr := c.Param("id")
  116. userId, _ := strconv.Atoi(userIdStr)
  117. if err := valid.Validate(userId, valid.Required, valid.Min(1)); err != nil {
  118. apierr.ClassifiedAbort(c, err)
  119. return
  120. }
  121. if err := middleware.BackendOperation(c).OpsService.DeleteSystemUser(c, int64(userId)); err != nil {
  122. apierr.ClassifiedAbort(c, err)
  123. return
  124. }
  125. ginutil.JSONResp(c, &operationPb.CommonOK{
  126. Code: http.StatusOK,
  127. Msg: "ok",
  128. Data: &operationPb.Success{Success: true},
  129. })
  130. }
  131. // IsShowSystemUser 系统用户启动开关
  132. func IsShowSystemUser(c *gin.Context) {
  133. userIdStr := c.Param("id")
  134. userId, _ := strconv.Atoi(userIdStr)
  135. if err := valid.Validate(userId, valid.Required, valid.Min(1)); err != nil {
  136. apierr.ClassifiedAbort(c, err)
  137. return
  138. }
  139. if err := middleware.Dependency(c).StoreEventHub.OpsService.IsShowSystemUser(c, int64(userId)); err != nil {
  140. apierr.ClassifiedAbort(c, err)
  141. return
  142. }
  143. ginutil.JSONResp(c, &operationPb.CommonOK{
  144. Code: http.StatusOK,
  145. Msg: "ok",
  146. Data: &operationPb.Success{Success: true},
  147. })
  148. }
  149. // GetSystemUserPermissions 获取系统用户菜单权限
  150. func GetSystemUserPermissions(c *gin.Context) {
  151. token := middleware.GetToken(c)
  152. if err := valid.Validate(token, valid.Required); err != nil {
  153. apierr.ClassifiedAbort(c, err)
  154. return
  155. }
  156. res, err := middleware.BackendOperation(c).OpsService.GetSystemUserPermissions(c, token)
  157. if err != nil {
  158. apierr.ClassifiedAbort(c, err)
  159. return
  160. }
  161. ginutil.JSONResp(c, res)
  162. }
  163. // ResetPasswordSystemUser 用户密码重置
  164. func ResetPasswordSystemUser(c *gin.Context) {
  165. var req pasturePb.ResetUserPasswordRequest
  166. if err := ginutil.BindProto(c, &req); err != nil {
  167. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  168. return
  169. }
  170. if err := valid.ValidateStruct(&req,
  171. valid.Field(&req.Id, valid.Required),
  172. valid.Field(&req.Password, valid.Required),
  173. ); err != nil {
  174. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  175. return
  176. }
  177. if err := middleware.BackendOperation(c).OpsService.ResetPasswordSystemUser(c, &req); err != nil {
  178. apierr.ClassifiedAbort(c, err)
  179. return
  180. }
  181. ginutil.JSONResp(c, &operationPb.CommonOK{
  182. Code: http.StatusOK,
  183. Msg: "ok",
  184. Data: &operationPb.Success{Success: true},
  185. })
  186. }
  187. // LogoutSystemUser 用户登出,
  188. func LogoutSystemUser(c *gin.Context) {
  189. ginutil.JSONResp(c, &operationPb.CommonOK{
  190. Code: http.StatusOK,
  191. Msg: "ok",
  192. Data: &operationPb.Success{Success: true},
  193. })
  194. }