123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package v1
- import (
- "cp-system-management-gateway/consts"
- "cp-system-management-gateway/utils"
- "fmt"
- "github.com/tidwall/gjson"
- "net/http"
- "cp-system-management-gateway/errors"
- param_v1 "cp-system-management-gateway/param/v1"
- "cp-system-management-gateway/pb"
- "cp-system-management-gateway/pb/v1"
- "time"
- "github.com/dgrijalva/jwt-go"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jaryhe/gopkgs/tasker/httptasker"
- "github.com/jaryhe/gopkgs/util"
- "cp-system-management-gateway/parser"
- "github.com/gin-gonic/gin"
- "github.com/jaryhe/gopkgs/jwtwrapper"
- "go.uber.org/zap"
- )
- // 登录
- // @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 := &v1.LoginRequest{
- Username: req.User,
- Password: req.Password,
- }
- rpcRsp, err := pb.System.Login(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Project.Login"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- subject := map[string]interface{}{
- "user_name": req.User,
- }
- 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
- }
- 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
- 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:
- return errors.TokenFailedError
- default:
- return errors.TokenFailedError
- }
- }
- uid := tokenObj.Id
- subject := tokenObj.Subject
- remberPass := gjson.GetBytes([]byte(subject), "rember_password").Bool()
- // 生成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
- }
- refreshTokenTime := time.Duration(24*60*60)*time.Second
- if remberPass {
- refreshTokenTime = time.Duration(7*24*60*60)*time.Second
- }
- refreshToken, err := jwtwrapper.GenToken(uid, parser.Conf.Jwt.Issuer, subject,
- refreshTokenTime)
- 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 = refreshToken
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 修改系统账号密码
- // @Description 修改系统账号密码
- // @Tags 用户
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param body body v1.ResetPasswdBody true " "
- // @Success 200 {object} v1.ResetePasswdResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/user/password [put]
- func (c *Controller) ResetePasswd(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ResetePasswdRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ResetPasswdBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- loginUid, userName, _ := utils.GetJwtIdFromCtx(ctx)
- // 响应数据
- resp := param_v1.ResetePasswdResponse{}
- rpcReq := &v1.ChangePasswordRequest{
- Uid: loginUid,
- Password:req.New,
- Old:req.Old,
- }
- _, err := pb.System.ChangePassword(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.System.ChangePassword"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- req.New = "******"
- req.Old = "******"
- logReq := OperationLogRequest{
- Module:consts.OperationModuleOrganization,
- Action:consts.OperationActionOrganizationUserResetPassword,
- Origin:nil,
- Target:req.ResetPasswdBody,
- UserName:userName,
- Uid:loginUid,
- }
- OperationLogAdd(&logReq)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
|