| | |
| | | "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" |
| | |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/go-redis/redis/v8" |
| | | "github.com/mojocn/base64Captcha" |
| | | "time" |
| | | ) |
| | | |
| | | // 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码 |
| | |
| | | var store = base64Captcha.DefaultMemStore |
| | | |
| | | // Login |
| | | // |
| | | // @Tags Base |
| | | // @Summary 用户登录 |
| | | // @Produce application/json |
| | |
| | | |
| | | // 判断验证码是否开启 |
| | | 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 |
| | | } |
| | | // 赋值菜单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 |
| | |
| | | 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, |
| | | UserType: user.UserType, |
| | | AuthorityId: user.AuthorityId, |
| | | }) |
| | | token, err := j.CreateToken(claims) |
| | | if err != nil { |
| | |
| | | } |
| | | |
| | | // Register |
| | | // |
| | | // @Tags User |
| | | // @Summary 注册账号 |
| | | // @Produce application/json |
| | |
| | | return |
| | | } |
| | | |
| | | if len(params.Username) == 0 || len(params.Password) == 0 || len(params.Phone) == 0 || len(params.NickName) == 0 { |
| | | if len(params.Username) == 0 || len(params.Password) == 0 || len(params.NickName) == 0 || params.AuthorityId == 0 || len(params.RePassword) == 0 || params.DepartmentId == 0 { |
| | | ctx.Fail(ecode.ParamsErr) |
| | | return |
| | | } |
| | |
| | | var userId = fmt.Sprintf("u%v", snowflake.GenerateId()) |
| | | var passWord = encrypt.BcryptHash(params.Password) |
| | | var userType constvar.UserType |
| | | var parentId string |
| | | var parentName string |
| | | if userInfo.UserType == constvar.UserTypeSuper { |
| | | userType = constvar.UserTypePrimary // 主账户的父用户ID是自己 |
| | | parentId = userId |
| | | parentName = params.Username |
| | | } else if userInfo.UserType == constvar.UserTypePrimary { |
| | | userType = constvar.UserTypeSub |
| | | parentId = userInfo.UserId |
| | | parentName = userInfo.ParentId |
| | | } else { |
| | | ctx.Fail(ecode.NoPowerErr) |
| | | return |
| | | } |
| | | |
| | | user := &model.User{ID: userId, Username: params.Username, UserType: userType, NickName: params.NickName, Password: passWord, HeaderImg: params.HeaderImg, Enable: true, ParentId: parentId, ParentName: parentName, Phone: params.Phone, Email: params.Email} |
| | | user := &model.User{ID: userId, Username: params.Username, UserType: userType, NickName: params.NickName, Password: passWord, HeaderImg: params.HeaderImg, Phone: params.Phone, Email: params.Email, DepartmentId: params.DepartmentId, AuthorityId: params.AuthorityId} |
| | | userReturn, errCode := userService.Register(user) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | if user.UserType == constvar.UserTypePrimary { // 主账户创建对应的数据库用户和排程数据库 |
| | | err := model.NewMysql().CreateDatabase(user.Username) |
| | | if err != nil { |
| | | ctx.Fail(ecode.CreateDatabaseErr) |
| | | return |
| | | } |
| | | |
| | | defaultPwd := fmt.Sprintf("%v@Basic2023", user.Username) |
| | | err = model.NewMysql().CreateUser(user.Username, defaultPwd, user.Username) |
| | | if err != nil { |
| | | ctx.Fail(ecode.CreateDatabaseUserErr) |
| | | return |
| | | } |
| | | } |
| | | //if user.UserType == constvar.UserTypePrimary { // 主账户创建对应的数据库用户和排程数据库 |
| | | // err := model.NewMysql().CreateDatabase(user.Username) |
| | | // if err != nil { |
| | | // ctx.Fail(ecode.CreateDatabaseErr) |
| | | // return |
| | | // } |
| | | // |
| | | // defaultPwd := fmt.Sprintf("%v@Basic2023", user.Username) |
| | | // err = model.NewMysql().CreateUser(user.Username, defaultPwd, user.Username) |
| | | // if err != nil { |
| | | // ctx.Fail(ecode.CreateDatabaseUserErr) |
| | | // return |
| | | // } |
| | | //} |
| | | |
| | | ctx.OkWithDetailed(response.UserResponse{User: *userReturn}) |
| | | } |
| | | |
| | | // ChangePassword |
| | | // |
| | | // @Tags User |
| | | // @Summary 用户修改密码 |
| | | // @Produce application/json |
| | |
| | | } |
| | | |
| | | // GetUserList |
| | | // |
| | | // @Tags User |
| | | // @Summary 分页获取用户列表(不传分页参数,获取全部) |
| | | // @Produce application/json |
| | |
| | | } |
| | | |
| | | // DeleteUser |
| | | // |
| | | // @Tags User |
| | | // @Summary 删除用户 |
| | | // @Produce application/json |
| | |
| | | } |
| | | |
| | | // SetUserInfo |
| | | // |
| | | // @Tags User |
| | | // @Summary 设置用户信息 |
| | | // @Produce application/json |
| | |
| | | } |
| | | |
| | | // SetSelfInfo |
| | | // |
| | | // @Tags User |
| | | // @Summary 设置用户信息 |
| | | // @Produce application/json |
| | |
| | | } |
| | | |
| | | // GetUserInfo |
| | | // |
| | | // @Tags User |
| | | // @Summary 获取自身信息 |
| | | // @Produce application/json |
| | |
| | | } |
| | | |
| | | // ResetPassword |
| | | // |
| | | // @Tags User |
| | | // @Summary 重置用户密码 |
| | | // @Produce application/json |