user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "fmt"
  6. "net/http"
  7. "time"
  8. "xingjia-management-gateway/apis"
  9. "xingjia-management-gateway/errors"
  10. "xingjia-management-gateway/impl/v1/user"
  11. param_v1 "xingjia-management-gateway/param/v1"
  12. "git.getensh.com/common/gopkgs/logger"
  13. "git.getensh.com/common/gopkgs/tasker/httptasker"
  14. "git.getensh.com/common/gopkgs/util"
  15. "github.com/dgrijalva/jwt-go"
  16. "xingjia-management-gateway/parser"
  17. "git.getensh.com/common/gopkgs/jwtwrapper"
  18. "github.com/gin-gonic/gin"
  19. "go.uber.org/zap"
  20. "xingjia-management-gateway/utils"
  21. )
  22. // 登录
  23. // @Summary 登录
  24. // @Description 登录
  25. // @Tags 用户
  26. // @Accept json
  27. // @Produce json
  28. // @Param body body v1.LoginBody true "登录信息"
  29. // @Success 200 {object} v1.LoginResponse
  30. // @Failure 500 {object} base.HTTPError
  31. // @Router /api/v1/user/login [post]
  32. func (c *Controller) Login(ctx *gin.Context) {
  33. // 解析参数
  34. req := &param_v1.LoginRequest{}
  35. parseParamTask := func() error {
  36. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.LoginBody)
  37. if err != nil {
  38. logger.Error("func",
  39. zap.String("call", "util.ShouldBind"),
  40. zap.String("error", err.Error()))
  41. return errors.ParamsError
  42. }
  43. return nil
  44. }
  45. // 业务处理
  46. handleServiceTask := func() error {
  47. // 响应数据
  48. resp := param_v1.LoginResponse{}
  49. rpcReq := &apis.LoginRequest{
  50. User: req.User,
  51. Password: req.Password,
  52. }
  53. rpcRsp, err := user.Login(ctx, rpcReq)
  54. if err != nil {
  55. s, _ := json.MarshalToString(req)
  56. logger.Error("func",
  57. zap.String("call", "user.Login"),
  58. zap.String("params", s),
  59. zap.String("error", err.Error()))
  60. return errors.ErrorTransForm(err)
  61. }
  62. subject := map[string]interface{}{
  63. "user_name": req.User,
  64. "user_type": rpcRsp.UserType,
  65. "effective_start": rpcRsp.EffectiveStart,
  66. "effective_end": rpcRsp.EffectiveEnd,
  67. }
  68. str, _ := json.MarshalToString(subject)
  69. // 生成token
  70. token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str,
  71. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  72. if err != nil {
  73. logger.Error("func",
  74. zap.String("call", "util.GenJwtToken"),
  75. zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)),
  76. zap.String("error", err.Error()))
  77. return errors.SystemError
  78. }
  79. resp.Data.Uid = rpcRsp.Uid
  80. resp.Data.Token = token
  81. resp.Data.User = req.User
  82. resp.Data.UserType = rpcRsp.UserType
  83. ctx.JSON(http.StatusOK, resp)
  84. return nil
  85. }
  86. // 执行任务
  87. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  88. }
  89. // token
  90. // @Summary 刷新token
  91. // @Description 刷新token
  92. // @Tags 用户
  93. // @Accept json
  94. // @Produce json
  95. // @Param token header string true "token"
  96. // @Success 200 {object} v1.TokenResponse
  97. // @Failure 500 {object} base.HTTPError
  98. // @Router /api/v1/token_refresh [put]
  99. func (c *Controller) TokenRefresh(ctx *gin.Context) {
  100. // 解析参数
  101. req := &param_v1.TokenRequest{}
  102. parseParamTask := func() error {
  103. err := util.ShouldBind(ctx, &req.Header, nil, nil, nil)
  104. if err != nil {
  105. logger.Error("func",
  106. zap.String("call", "util.ShouldBind"),
  107. zap.String("error", err.Error()))
  108. return errors.ParamsError
  109. }
  110. return nil
  111. }
  112. // 业务处理
  113. handleServiceTask := func() error {
  114. tokenObj, err := jwtwrapper.ParseToken(req.Token)
  115. if tokenObj == nil {
  116. return errors.TokenFailedError
  117. }
  118. if err != nil {
  119. switch err.(*jwt.ValidationError).Errors {
  120. case jwt.ValidationErrorExpired:
  121. if tokenObj == nil {
  122. return errors.TokenFailedError
  123. }
  124. if time.Now().Unix()-tokenObj.ExpiresAt > 3600 {
  125. return errors.TokenFailedError
  126. }
  127. default:
  128. return errors.TokenFailedError
  129. }
  130. }
  131. uid := tokenObj.Id
  132. subject := tokenObj.Subject
  133. // 生成token
  134. token, err := jwtwrapper.GenToken(uid, parser.Conf.Jwt.Issuer, subject,
  135. time.Duration(parser.Conf.Jwt.Seconds)*time.Second)
  136. if err != nil {
  137. logger.Error("func",
  138. zap.String("call", "util.GenJwtToken"),
  139. zap.String("args", fmt.Sprintf("%s", uid)),
  140. zap.String("error", err.Error()))
  141. return errors.SystemError
  142. }
  143. resp := param_v1.TokenResponse{}
  144. resp.Data = token
  145. resp.RefreshToken = token
  146. ctx.JSON(http.StatusOK, resp)
  147. return nil
  148. }
  149. // 执行任务
  150. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  151. }
  152. //
  153. // @Summary 添加账号
  154. // @Description 添加账号
  155. // @Tags 账号
  156. // @Accept json
  157. // @Produce json
  158. // @Param token header string true " "
  159. // @Param body body v1.UserAddBody true " "
  160. // @Success 200 {object} v1.UserAddResponse
  161. // @Failure 500 {object} base.HTTPError
  162. // @Router /api/v1/management_user [post]
  163. func (c *Controller) ManagementUserAdd(ctx *gin.Context) {
  164. // 解析参数
  165. req := &param_v1.UserAddRequest{}
  166. parseParamTask := func() error {
  167. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserAddBody)
  168. if err != nil {
  169. logger.Error("func",
  170. zap.String("call", "util.ShouldBind"),
  171. zap.String("error", err.Error()))
  172. return errors.ParamsError
  173. }
  174. return nil
  175. }
  176. // 业务处理
  177. handleServiceTask := func() error {
  178. tokenInfo, err := utils.GetJwtTokenFromCtx(ctx)
  179. if err != nil {
  180. return err
  181. }
  182. // 响应数据
  183. resp := param_v1.UserAddResponse{}
  184. rpcReq := &apis.UserAddRequest{
  185. User: req.User,
  186. RealName: req.RealName,
  187. Password: req.Password,
  188. SelfId: tokenInfo.Uid,
  189. SelfName: tokenInfo.User,
  190. UserType: req.UserType,
  191. EffectiveEnd: req.EffectiveEnd,
  192. EffectiveStart: req.EffectiveStart,
  193. }
  194. _, err = user.UserAdd(ctx, rpcReq)
  195. if err != nil {
  196. s, _ := json.MarshalToString(req)
  197. logger.Error("func",
  198. zap.String("call", "user.UserAdd"),
  199. zap.String("params", s),
  200. zap.String("error", err.Error()))
  201. return errors.ErrorTransForm(err)
  202. }
  203. ctx.JSON(http.StatusOK, resp)
  204. return nil
  205. }
  206. // 执行任务
  207. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  208. }
  209. //
  210. // @Summary 修改账号
  211. // @Description 修改账号
  212. // @Tags 账号
  213. // @Accept json
  214. // @Produce json
  215. // @Param token header string true " "
  216. // @Param body body v1.UserUpdateBody true " "
  217. // @Success 200 {object} v1.UserUpdateResponse
  218. // @Failure 500 {object} base.HTTPError
  219. // @Router /api/v1/management_user [put]
  220. func (c *Controller) ManagementUserUpdate(ctx *gin.Context) {
  221. // 解析参数
  222. req := &param_v1.UserUpdateRequest{}
  223. parseParamTask := func() error {
  224. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserUpdateBody)
  225. if err != nil {
  226. logger.Error("func",
  227. zap.String("call", "util.ShouldBind"),
  228. zap.String("error", err.Error()))
  229. return errors.ParamsError
  230. }
  231. return nil
  232. }
  233. // 业务处理
  234. handleServiceTask := func() error {
  235. tokenInfo, err := utils.GetJwtTokenFromCtx(ctx)
  236. if err != nil {
  237. return err
  238. }
  239. // 响应数据
  240. resp := param_v1.UserUpdateResponse{}
  241. rpcReq := &apis.UserUpdateRequest{
  242. User: req.User,
  243. RealName: req.RealName,
  244. Password: req.Password,
  245. SelfId: tokenInfo.Uid,
  246. SelfName: tokenInfo.User,
  247. UserType: req.UserType,
  248. EffectiveEnd: req.EffectiveEnd,
  249. EffectiveStart: req.EffectiveStart,
  250. UpdateId: req.Id,
  251. }
  252. _, err = user.UserUpdate(ctx, rpcReq)
  253. if err != nil {
  254. s, _ := json.MarshalToString(req)
  255. logger.Error("func",
  256. zap.String("call", "user.UserUpdate"),
  257. zap.String("params", s),
  258. zap.String("error", err.Error()))
  259. return errors.ErrorTransForm(err)
  260. }
  261. ctx.JSON(http.StatusOK, resp)
  262. return nil
  263. }
  264. // 执行任务
  265. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  266. }
  267. //
  268. // @Summary 删除账号
  269. // @Description 删除账号
  270. // @Tags 账号
  271. // @Accept json
  272. // @Produce json
  273. // @Param token header string true " "
  274. // @Param id query int true " 记录id"
  275. // @Success 200 {object} v1.UserDelResponse
  276. // @Failure 500 {object} base.HTTPError
  277. // @Router /api/v1/management_user [delete]
  278. func (c *Controller) ManagementUserDel(ctx *gin.Context) {
  279. // 解析参数
  280. req := &param_v1.UserDelRequest{}
  281. parseParamTask := func() error {
  282. err := util.ShouldBind(ctx, &req.Header, nil, &req.UserDelQuery, nil)
  283. if err != nil {
  284. logger.Error("func",
  285. zap.String("call", "util.ShouldBind"),
  286. zap.String("error", err.Error()))
  287. return errors.ParamsError
  288. }
  289. return nil
  290. }
  291. // 业务处理
  292. handleServiceTask := func() error {
  293. tokenInfo, err := utils.GetJwtTokenFromCtx(ctx)
  294. if err != nil {
  295. return err
  296. }
  297. // 响应数据
  298. resp := param_v1.UserDelResponse{}
  299. rpcReq := &apis.UserDelRequest{
  300. SelfId: tokenInfo.Uid,
  301. SelfName: tokenInfo.User,
  302. DelId: req.Id,
  303. }
  304. _, err = user.UserDel(ctx, rpcReq)
  305. if err != nil {
  306. s, _ := json.MarshalToString(req)
  307. logger.Error("func",
  308. zap.String("call", "user.UserDel"),
  309. zap.String("params", s),
  310. zap.String("error", err.Error()))
  311. return errors.ErrorTransForm(err)
  312. }
  313. ctx.JSON(http.StatusOK, resp)
  314. return nil
  315. }
  316. // 执行任务
  317. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  318. }
  319. //
  320. // @Summary 账号列表
  321. // @Description 账号列表
  322. // @Tags 账号
  323. // @Accept json
  324. // @Produce json
  325. // @Param token header string true " "
  326. // @Param page query int false " "
  327. // @Param page_size query int false " "
  328. // @Success 200 {object} v1.UserListResponse
  329. // @Failure 500 {object} base.HTTPError
  330. // @Router /api/v1/management_user [get]
  331. func (c *Controller) ManagementUserList(ctx *gin.Context) {
  332. // 解析参数
  333. req := &param_v1.UserListRequest{}
  334. parseParamTask := func() error {
  335. err := util.ShouldBind(ctx, &req.Header, nil, &req.UserListQuery, nil)
  336. if err != nil {
  337. logger.Error("func",
  338. zap.String("call", "util.ShouldBind"),
  339. zap.String("error", err.Error()))
  340. return errors.ParamsError
  341. }
  342. return nil
  343. }
  344. // 业务处理
  345. handleServiceTask := func() error {
  346. tokenInfo, err := utils.GetJwtTokenFromCtx(ctx)
  347. if err != nil {
  348. return err
  349. }
  350. // 响应数据
  351. resp := param_v1.UserListResponse{}
  352. rpcReq := &apis.UserListRequest{
  353. PageSize: req.PageSize,
  354. Page: req.Page,
  355. SelfId: tokenInfo.Uid,
  356. User: req.User,
  357. RealName: req.RealName,
  358. }
  359. rpcRsp, err := user.UserList(ctx, rpcReq)
  360. if err != nil {
  361. s, _ := json.MarshalToString(req)
  362. logger.Error("func",
  363. zap.String("call", "user.UserList"),
  364. zap.String("params", s),
  365. zap.String("error", err.Error()))
  366. return errors.ErrorTransForm(err)
  367. }
  368. if rpcRsp.List == nil {
  369. rpcRsp.List = make([]*apis.UserItem, 0)
  370. }
  371. resp.Data = *rpcRsp
  372. ctx.JSON(http.StatusOK, resp)
  373. return nil
  374. }
  375. // 执行任务
  376. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  377. }
  378. //
  379. // @Summary 重置密码
  380. // @Description 重置密码
  381. // @Tags 账号
  382. // @Accept json
  383. // @Produce json
  384. // @Param token header string true " "
  385. // @Param body body v1.UserResetPasswordBody true " "
  386. // @Success 200 {object} v1.UserResetPasswordResponse
  387. // @Failure 500 {object} base.HTTPError
  388. // @Router /api/v1/management_user/password [put]
  389. func (c *Controller) ManagementUserResetPassword(ctx *gin.Context) {
  390. // 解析参数
  391. req := &param_v1.UserResetPasswordRequest{}
  392. parseParamTask := func() error {
  393. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserResetPasswordBody)
  394. if err != nil {
  395. logger.Error("func",
  396. zap.String("call", "util.ShouldBind"),
  397. zap.String("error", err.Error()))
  398. return errors.ParamsError
  399. }
  400. return nil
  401. }
  402. // 业务处理
  403. handleServiceTask := func() error {
  404. tokenInfo, err := utils.GetJwtTokenFromCtx(ctx)
  405. if err != nil {
  406. return err
  407. }
  408. // 响应数据
  409. resp := param_v1.UserResetPasswordResponse{}
  410. rpcReq := &apis.UserResetPasswordRequest{
  411. Password: req.Password,
  412. SelfId: tokenInfo.Uid,
  413. SelfName: tokenInfo.User,
  414. ResetId: req.Id,
  415. }
  416. _, err = user.UserResetPassword(ctx, rpcReq)
  417. if err != nil {
  418. s, _ := json.MarshalToString(req)
  419. logger.Error("func",
  420. zap.String("call", "user.UserResetPassword"),
  421. zap.String("params", s),
  422. zap.String("error", err.Error()))
  423. return errors.ErrorTransForm(err)
  424. }
  425. ctx.JSON(http.StatusOK, resp)
  426. return nil
  427. }
  428. // 执行任务
  429. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  430. }