fix
wangpengfei
2023-07-17 3fb4ddbe96c663f861efef4df87b5d4bc773d81e
api/v1/user.go
@@ -6,9 +6,7 @@
   "aps_crm/model"
   "aps_crm/model/request"
   "aps_crm/model/response"
   _ "aps_crm/model/response"
   "aps_crm/pkg/contextx"
   "aps_crm/pkg/convertx"
   "aps_crm/pkg/ecode"
   "aps_crm/pkg/encrypt"
   "aps_crm/pkg/logx"
@@ -18,7 +16,6 @@
   "github.com/gin-gonic/gin"
   "github.com/go-redis/redis/v8"
   "github.com/mojocn/base64Captcha"
   "time"
)
// 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码
@@ -26,6 +23,7 @@
var store = base64Captcha.DefaultMemStore
// Login
//
//   @Tags      Base
//   @Summary   用户登录
//   @Produce   application/json
@@ -41,41 +39,41 @@
   // 判断验证码是否开启
   key := c.ClientIP()
   openCaptcha := conf.Conf.Captcha.OpenCaptcha               // 是否开启防爆次数
   openCaptchaTimeOut := conf.Conf.Captcha.OpenCaptchaTimeOut // 缓存超时时间
   v, ok := constvar.BlackCache.Get(key)
   if !ok {
      constvar.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
   }
   //openCaptcha := conf.Conf.Captcha.OpenCaptcha               // 是否开启防暴次数
   //openCaptchaTimeOut := conf.Conf.Captcha.OpenCaptchaTimeOut // 缓存超时时间
   //v, ok := constvar.BlackCache.Get(key)
   //if !ok {
   //   constvar.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
   //}
   var oc bool = openCaptcha == 0 || convertx.InterfaceToInt(v) > openCaptcha // 0 表示每次登录都需要验证码 或者当前次数已超过防爆次数
   //var oc = openCaptcha == 0 || convertx.InterfaceToInt(v) > openCaptcha // 0 表示每次登录都需要验证码 或者当前次数已超过防暴次数
   if !oc || store.Verify(params.CaptchaId, params.Captcha, true) {
      u := &model.User{Username: params.Username, Password: params.Password}
      user, errCode := userService.Login(u)
      if errCode != ecode.OK {
         logx.Errorf("登陆失败! 用户名不存在或者密码错误! errCode:%v", errCode)
         // 验证码次数+1
         _ = constvar.BlackCache.Increment(key, 1)
         ctx.Fail(errCode)
         return
      }
      if !user.Enable {
         logx.Errorf("登陆失败! 用户被禁止登录!")
         // 验证码次数+1
         _ = constvar.BlackCache.Increment(key, 1)
         ctx.Fail(ecode.UserForbidden)
         return
      }
      // 赋值菜单ID列表
      //user.MenuIds, _ = menuService.GetUserMenuIds(user.ID, user.UserType)
      slf.TokenNext(ctx, *user)
   //if !oc || store.Verify(params.CaptchaId, params.Captcha, true) {
   u := &model.User{Username: params.Username, Password: params.Password}
   user, errCode := userService.Login(u)
   if errCode != ecode.OK {
      logx.Errorf("登陆失败! 用户名不存在或者密码错误! errCode:%v", errCode)
      // 验证码次数+1
      _ = constvar.BlackCache.Increment(key, 1)
      ctx.Fail(errCode)
      return
   }
   if !user.Enable {
      logx.Errorf("登陆失败! 用户被禁止登录!")
      // 验证码次数+1
      _ = constvar.BlackCache.Increment(key, 1)
      ctx.Fail(ecode.UserForbidden)
      return
   }
   // 赋值菜单ID列表
   //user.MenuIds, _ = menuService.GetUserMenuIds(user.ID, user.UserType)
   slf.TokenNext(ctx, *user)
   return
   //}
   // 验证码次数+1
   _ = constvar.BlackCache.Increment(key, 1)
   ctx.Fail(ecode.CaptchaErr)
   //_ = constvar.BlackCache.Increment(key, 1)
   //ctx.Fail(ecode.CaptchaErr)
}
// TokenNext 登录以后签发jwt
@@ -83,10 +81,11 @@
   logx.Infof("TokenNext user:%+v", user)
   j := &utils.JWT{SigningKey: []byte(conf.Conf.JWT.SigningKey)} // 唯一签名
   claims := j.CreateClaims(request.BaseClaims{
      UserId:   user.ID,
      Username: user.Username,
      ParentId: user.ParentName,
      UserType: user.UserType,
      UserId:      user.ID,
      Username:    user.Username,
      ParentId:    user.ParentName,
      UserType:    user.UserType,
      AuthorityId: user.AuthorityId,
   })
   token, err := j.CreateToken(claims)
   if err != nil {
@@ -137,6 +136,7 @@
}
// Register
//
//   @Tags      User
//   @Summary   注册账号
//   @Produce   application/json
@@ -204,6 +204,7 @@
}
// ChangePassword
//
//   @Tags      User
//   @Summary   用户修改密码
//   @Produce   application/json
@@ -227,6 +228,7 @@
}
// GetUserList
//
//   @Tags      User
//   @Summary   分页获取用户列表(不传分页参数,获取全部)
//   @Produce   application/json
@@ -253,6 +255,7 @@
}
// DeleteUser
//
//   @Tags      User
//   @Summary   删除用户
//   @Produce   application/json
@@ -287,6 +290,7 @@
}
// SetUserInfo
//
//   @Tags      User
//   @Summary   设置用户信息
//   @Produce   application/json
@@ -317,6 +321,7 @@
}
// SetSelfInfo
//
//   @Tags      User
//   @Summary   设置用户信息
//   @Produce   application/json
@@ -348,6 +353,7 @@
}
// GetUserInfo
//
//   @Tags      User
//   @Summary   获取自身信息
//   @Produce   application/json
@@ -372,6 +378,7 @@
}
// ResetPassword
//
//   @Tags      User
//   @Summary   重置用户密码
//   @Produce   application/json