// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package v1 import ( "fmt" "net/http" "time" "xingjia-management-gateway/apis" "xingjia-management-gateway/errors" "xingjia-management-gateway/impl/v1/user" param_v1 "xingjia-management-gateway/param/v1" "git.getensh.com/common/gopkgs/logger" "git.getensh.com/common/gopkgs/tasker/httptasker" "git.getensh.com/common/gopkgs/util" "github.com/dgrijalva/jwt-go" "xingjia-management-gateway/parser" "git.getensh.com/common/gopkgs/jwtwrapper" "github.com/gin-gonic/gin" "go.uber.org/zap" "xingjia-management-gateway/utils" ) // 登录 // @Summary 登录 // @Description 登录 // @Tags 用户 // @Accept json // @Produce json // @Param body body v1.LoginBody true "登录信息" // @Success 200 {object} v1.LoginResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/user/login [post] func (c *Controller) Login(ctx *gin.Context) { // 解析参数 req := ¶m_v1.LoginRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.LoginBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { // 响应数据 resp := param_v1.LoginResponse{} rpcReq := &apis.LoginRequest{ User: req.User, Password: req.Password, } rpcRsp, err := user.Login(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "user.Login"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } subject := map[string]interface{}{ "user_name": req.User, "user_type": rpcRsp.UserType, "effective_start": rpcRsp.EffectiveStart, "effective_end": rpcRsp.EffectiveEnd, } str, _ := json.MarshalToString(subject) // 生成token token, err := jwtwrapper.GenToken(fmt.Sprintf("%d", rpcRsp.Uid), parser.Conf.Jwt.Issuer, str, time.Duration(parser.Conf.Jwt.Seconds)*time.Second) if err != nil { logger.Error("func", zap.String("call", "util.GenJwtToken"), zap.String("args", fmt.Sprintf("%d", rpcRsp.Uid)), zap.String("error", err.Error())) return errors.SystemError } resp.Data.Uid = rpcRsp.Uid resp.Data.Token = token resp.Data.User = req.User resp.Data.UserType = rpcRsp.UserType ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // token // @Summary 刷新token // @Description 刷新token // @Tags 用户 // @Accept json // @Produce json // @Param token header string true "token" // @Success 200 {object} v1.TokenResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/token_refresh [put] func (c *Controller) TokenRefresh(ctx *gin.Context) { // 解析参数 req := ¶m_v1.TokenRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenObj, err := jwtwrapper.ParseToken(req.Token) if tokenObj == nil { return errors.TokenFailedError } if err != nil { switch err.(*jwt.ValidationError).Errors { case jwt.ValidationErrorExpired: if tokenObj == nil { return errors.TokenFailedError } if time.Now().Unix()-tokenObj.ExpiresAt > 3600 { return errors.TokenFailedError } default: return errors.TokenFailedError } } uid := tokenObj.Id subject := tokenObj.Subject // 生成token token, err := jwtwrapper.GenToken(uid, parser.Conf.Jwt.Issuer, subject, time.Duration(parser.Conf.Jwt.Seconds)*time.Second) if err != nil { logger.Error("func", zap.String("call", "util.GenJwtToken"), zap.String("args", fmt.Sprintf("%s", uid)), zap.String("error", err.Error())) return errors.SystemError } resp := param_v1.TokenResponse{} resp.Data = token resp.RefreshToken = token ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 添加账号 // @Description 添加账号 // @Tags 账号 // @Accept json // @Produce json // @Param token header string true " " // @Param body body v1.UserAddBody true " " // @Success 200 {object} v1.UserAddResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/management_user [post] func (c *Controller) ManagementUserAdd(ctx *gin.Context) { // 解析参数 req := ¶m_v1.UserAddRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserAddBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenFromCtx(ctx) if err != nil { return err } // 响应数据 resp := param_v1.UserAddResponse{} rpcReq := &apis.UserAddRequest{ User: req.User, RealName: req.RealName, Password: req.Password, SelfId: tokenInfo.Uid, SelfName: tokenInfo.User, UserType: req.UserType, EffectiveEnd: req.EffectiveEnd, EffectiveStart: req.EffectiveStart, } _, err = user.UserAdd(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "user.UserAdd"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 修改账号 // @Description 修改账号 // @Tags 账号 // @Accept json // @Produce json // @Param token header string true " " // @Param body body v1.UserUpdateBody true " " // @Success 200 {object} v1.UserUpdateResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/management_user [put] func (c *Controller) ManagementUserUpdate(ctx *gin.Context) { // 解析参数 req := ¶m_v1.UserUpdateRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserUpdateBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenFromCtx(ctx) if err != nil { return err } // 响应数据 resp := param_v1.UserUpdateResponse{} rpcReq := &apis.UserUpdateRequest{ User: req.User, RealName: req.RealName, Password: req.Password, SelfId: tokenInfo.Uid, SelfName: tokenInfo.User, UserType: req.UserType, EffectiveEnd: req.EffectiveEnd, EffectiveStart: req.EffectiveStart, UpdateId: req.Id, } _, err = user.UserUpdate(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "user.UserUpdate"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 删除账号 // @Description 删除账号 // @Tags 账号 // @Accept json // @Produce json // @Param token header string true " " // @Param id query int true " 记录id" // @Success 200 {object} v1.UserDelResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/management_user [delete] func (c *Controller) ManagementUserDel(ctx *gin.Context) { // 解析参数 req := ¶m_v1.UserDelRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.UserDelQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenFromCtx(ctx) if err != nil { return err } // 响应数据 resp := param_v1.UserDelResponse{} rpcReq := &apis.UserDelRequest{ SelfId: tokenInfo.Uid, SelfName: tokenInfo.User, DelId: req.Id, } _, err = user.UserDel(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "user.UserDel"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 账号列表 // @Description 账号列表 // @Tags 账号 // @Accept json // @Produce json // @Param token header string true " " // @Param page query int false " " // @Param page_size query int false " " // @Success 200 {object} v1.UserListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/management_user [get] func (c *Controller) ManagementUserList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.UserListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.UserListQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenFromCtx(ctx) if err != nil { return err } // 响应数据 resp := param_v1.UserListResponse{} rpcReq := &apis.UserListRequest{ PageSize: req.PageSize, Page: req.Page, SelfId: tokenInfo.Uid, User: req.User, RealName: req.RealName, } rpcRsp, err := user.UserList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "user.UserList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if rpcRsp.List == nil { rpcRsp.List = make([]*apis.UserItem, 0) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 重置密码 // @Description 重置密码 // @Tags 账号 // @Accept json // @Produce json // @Param token header string true " " // @Param body body v1.UserResetPasswordBody true " " // @Success 200 {object} v1.UserResetPasswordResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/management_user/password [put] func (c *Controller) ManagementUserResetPassword(ctx *gin.Context) { // 解析参数 req := ¶m_v1.UserResetPasswordRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.UserResetPasswordBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetJwtTokenFromCtx(ctx) if err != nil { return err } // 响应数据 resp := param_v1.UserResetPasswordResponse{} rpcReq := &apis.UserResetPasswordRequest{ Password: req.Password, SelfId: tokenInfo.Uid, SelfName: tokenInfo.User, ResetId: req.Id, } _, err = user.UserResetPassword(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "user.UserResetPassword"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) }