system_user.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package v1
  2. import (
  3. "git.getensh.com/common/gopkgs/logger"
  4. "git.getensh.com/common/gopkgs/tasker/httptasker"
  5. "git.getensh.com/common/gopkgs/util"
  6. "github.com/gin-gonic/gin"
  7. "go.uber.org/zap"
  8. "net/http"
  9. "property-system-gateway/errors"
  10. param_v1 "property-system-gateway/param/v1"
  11. "property-system-gateway/pb"
  12. "property-system-gateway/pb/v1"
  13. "property-system-gateway/utils"
  14. "strconv"
  15. "strings"
  16. )
  17. //
  18. // @Summary 添加用户
  19. // @Description 添加用户
  20. // @Tags 用户管理
  21. // @Accept json
  22. // @Produce json
  23. // @Param token header string true "token"
  24. // @Param body body v1.UserAddBody true "用户信息"
  25. // @Success 200 {object} v1.UserAddResponse
  26. // @Failure 500 {object} base.HTTPError
  27. // @Router /api/v1/system_user [post]
  28. func (c *Controller) UserAdd(ctx *gin.Context) {
  29. // 解析参数
  30. req := &param_v1.UserAddRequest{}
  31. parseParamTask := func() error {
  32. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserAddBody)
  33. if err != nil {
  34. logger.Error("func",
  35. zap.String("call", "util.ShouldBind"),
  36. zap.String("error", err.Error()))
  37. return errors.ParamsError
  38. }
  39. return nil
  40. }
  41. // 业务处理
  42. handleServiceTask := func() error {
  43. tokenInfo, err := utils.GetSubjectValue(ctx)
  44. if err != nil {
  45. return err
  46. }
  47. // 响应数据
  48. resp := param_v1.UserAddResponse{}
  49. rpcReq := &v1.UserAddRequest{
  50. UserName: req.UserName,
  51. Password: req.Password,
  52. Phone: req.Phone,
  53. Email: req.Email,
  54. Cid: tokenInfo.Cid,
  55. GardenId: tokenInfo.GardenId,
  56. Uid: tokenInfo.Uid,
  57. GardenName: tokenInfo.GardenName,
  58. GroupId: req.GroupId,
  59. RealName: req.RealName,
  60. DepartmentId: req.DepartmentId,
  61. ByCompany: tokenInfo.ByCompany,
  62. BasePermission: req.BasePermission,
  63. Gender: req.Gender,
  64. }
  65. rpcRsp, err := pb.System.UserAdd(ctx, rpcReq)
  66. if err != nil {
  67. s, _ := json.MarshalToString(req)
  68. logger.Error("func",
  69. zap.String("call", "pb.System.UserAdd"),
  70. zap.String("params", s),
  71. zap.String("error", err.Error()))
  72. return errors.ErrorTransForm(err)
  73. }
  74. resp.Data = *rpcRsp
  75. ctx.JSON(http.StatusOK, resp)
  76. logReq := OperationLogRequest{
  77. Module: ModuleUser,
  78. Action: ActionUserAdd,
  79. Origin: nil,
  80. Target: req.UserAddBody,
  81. UserName: tokenInfo.UserName,
  82. Uid: tokenInfo.Uid,
  83. Cid: tokenInfo.Cid,
  84. GardenId: tokenInfo.GardenId,
  85. }
  86. go OperationLogAdd(&logReq)
  87. return nil
  88. }
  89. // 执行任务
  90. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  91. }
  92. //
  93. // @Summary 更改用户
  94. // @Description 更改用户
  95. // @Tags 用户管理
  96. // @Accept json
  97. // @Produce json
  98. // @Param token header string true "token"
  99. // @Param body body v1.UserUpdateBody true "用户信息"
  100. // @Success 200 {object} v1.UserUpdateResponse
  101. // @Failure 500 {object} base.HTTPError
  102. // @Router /api/v1/system_user [put]
  103. func (c *Controller) UserUpdate(ctx *gin.Context) {
  104. // 解析参数
  105. req := &param_v1.UserUpdateRequest{}
  106. parseParamTask := func() error {
  107. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserUpdateBody)
  108. if err != nil {
  109. logger.Error("func",
  110. zap.String("call", "util.ShouldBind"),
  111. zap.String("error", err.Error()))
  112. return errors.ParamsError
  113. }
  114. return nil
  115. }
  116. // 业务处理
  117. handleServiceTask := func() error {
  118. tokenInfo, err := utils.GetSubjectValue(ctx)
  119. if err != nil {
  120. return err
  121. }
  122. // 响应数据
  123. resp := param_v1.UserUpdateResponse{}
  124. rpcReq := &v1.UserUpdateRequest{
  125. UserName: req.UserName,
  126. Password: req.Password,
  127. Phone: req.Phone,
  128. Email: req.Email,
  129. Cid: tokenInfo.Cid,
  130. GardenId: tokenInfo.GardenId,
  131. Uid: tokenInfo.Uid,
  132. GardenName: tokenInfo.GardenName,
  133. GroupId: req.GroupId,
  134. Id: req.Id,
  135. RealName: req.RealName,
  136. DepartmentId: req.DepartmentId,
  137. ByCompany: tokenInfo.ByCompany,
  138. BasePermission: req.BasePermission,
  139. Gender: req.Gender,
  140. }
  141. rpcRsp, err := pb.System.UserUpdate(ctx, rpcReq)
  142. if err != nil {
  143. s, _ := json.MarshalToString(req)
  144. logger.Error("func",
  145. zap.String("call", "pb.System.UserUpdate"),
  146. zap.String("params", s),
  147. zap.String("error", err.Error()))
  148. return errors.ErrorTransForm(err)
  149. }
  150. ctx.JSON(http.StatusOK, resp)
  151. logReq := OperationLogRequest{
  152. Module: ModuleUser,
  153. Action: ActionUserUpdate,
  154. Origin: rpcRsp.Origin,
  155. Target: req.UserUpdateBody,
  156. UserName: tokenInfo.UserName,
  157. Uid: tokenInfo.Uid,
  158. Cid: tokenInfo.Cid,
  159. GardenId: tokenInfo.GardenId,
  160. }
  161. go OperationLogAdd(&logReq)
  162. return nil
  163. }
  164. // 执行任务
  165. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  166. }
  167. //
  168. // @Summary 删除用户
  169. // @Description 删除用户
  170. // @Tags 用户管理
  171. // @Accept json
  172. // @Produce json
  173. // @Param token header string true "token"
  174. // @Param id query int true " "
  175. // @Success 200 {object} v1.UserDelResponse
  176. // @Failure 500 {object} base.HTTPError
  177. // @Router /api/v1/system_user [delete]
  178. func (c *Controller) UserDel(ctx *gin.Context) {
  179. // 解析参数
  180. req := &param_v1.UserDelRequest{}
  181. parseParamTask := func() error {
  182. err := util.ShouldBind(ctx, &req.Header, nil, &req.UserDelQuery, nil)
  183. if err != nil {
  184. logger.Error("func",
  185. zap.String("call", "util.ShouldBind"),
  186. zap.String("error", err.Error()))
  187. return errors.ParamsError
  188. }
  189. return nil
  190. }
  191. // 业务处理
  192. handleServiceTask := func() error {
  193. tokenInfo, err := utils.GetSubjectValue(ctx)
  194. if err != nil {
  195. return err
  196. }
  197. // 响应数据
  198. resp := param_v1.UserDelResponse{}
  199. rpcReq := &v1.UserDelRequest{
  200. Cid: tokenInfo.Cid,
  201. GardenId: tokenInfo.GardenId,
  202. Uid: tokenInfo.Uid,
  203. Id: req.Id,
  204. ByCompany: tokenInfo.ByCompany,
  205. }
  206. rpcRsp, err := pb.System.UserDel(ctx, rpcReq)
  207. if err != nil {
  208. s, _ := json.MarshalToString(req)
  209. logger.Error("func",
  210. zap.String("call", "pb.System.UserDel"),
  211. zap.String("params", s),
  212. zap.String("error", err.Error()))
  213. return errors.ErrorTransForm(err)
  214. }
  215. ctx.JSON(http.StatusOK, resp)
  216. logReq := OperationLogRequest{
  217. Module: ModuleUser,
  218. Action: ActionUserDel,
  219. Origin: rpcRsp.Origin,
  220. Target: nil,
  221. UserName: tokenInfo.UserName,
  222. Uid: tokenInfo.Uid,
  223. Cid: tokenInfo.Cid,
  224. GardenId: tokenInfo.GardenId,
  225. }
  226. go OperationLogAdd(&logReq)
  227. return nil
  228. }
  229. // 执行任务
  230. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  231. }
  232. //
  233. // @Summary 用户列表
  234. // @Description 用户列表
  235. // @Tags 用户管理
  236. // @Accept json
  237. // @Produce json
  238. // @Param token header string true "token"
  239. // @Param page query int true " "
  240. // @Param page_size query int false " "
  241. // @Param group_id query int false "权限组id"
  242. // @Param user_name query string false "用户名"
  243. // @Param phone query string false "电话号"
  244. // @Param exclude query string false "需要排除的用户id,以逗号分隔,如11,2,13,46 (不需要则留空)"
  245. // @Success 200 {object} v1.UserListResponse
  246. // @Failure 500 {object} base.HTTPError
  247. // @Router /api/v1/system_user [get]
  248. func (c *Controller) UserList(ctx *gin.Context) {
  249. // 解析参数
  250. req := &param_v1.UserListRequest{}
  251. parseParamTask := func() error {
  252. err := util.ShouldBind(ctx, &req.Header, nil, &req.UserListQuery, nil)
  253. if err != nil {
  254. logger.Error("func",
  255. zap.String("call", "util.ShouldBind"),
  256. zap.String("error", err.Error()))
  257. return errors.ParamsError
  258. }
  259. return nil
  260. }
  261. // 业务处理
  262. handleServiceTask := func() error {
  263. tokenInfo, err := utils.GetSubjectValue(ctx)
  264. if err != nil {
  265. return err
  266. }
  267. // 响应数据
  268. resp := param_v1.UserListResponse{}
  269. rpcReq := &v1.UserListRequest{
  270. UserName: req.UserName,
  271. Phone: req.Phone,
  272. Cid: tokenInfo.Cid,
  273. GardenId: tokenInfo.GardenId,
  274. GroupId: req.GroupId,
  275. }
  276. array := strings.Split(req.Exclude, ",")
  277. rpcReq.Exclude = make([]int64, len(array))
  278. for i, v := range array {
  279. rpcReq.Exclude[i], _ = strconv.ParseInt(v, 10, 64)
  280. }
  281. rpcRsp, err := pb.System.UserList(ctx, rpcReq)
  282. if err != nil {
  283. s, _ := json.MarshalToString(req)
  284. logger.Error("func",
  285. zap.String("call", "pb.System.UserList"),
  286. zap.String("params", s),
  287. zap.String("error", err.Error()))
  288. return errors.ErrorTransForm(err)
  289. }
  290. if rpcRsp.List == nil {
  291. rpcRsp.List = make([]*v1.UserItem, 0)
  292. }
  293. resp.Data = *rpcRsp
  294. ctx.JSON(http.StatusOK, resp)
  295. return nil
  296. }
  297. // 执行任务
  298. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  299. }