🎨 新增ip检测配置功能
This commit is contained in:
@@ -55,7 +55,7 @@ func (*AppUserApi) Login(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := appUserService.Login(p)
|
||||
user, isNewUser, err := appUserService.Login(p)
|
||||
if err != nil {
|
||||
r.FailWithMessage("登录失败", ctx)
|
||||
return
|
||||
@@ -104,6 +104,7 @@ func (*AppUserApi) Login(ctx *gin.Context) {
|
||||
"User": user,
|
||||
"Token": token,
|
||||
"ExpiresAt": claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
|
||||
"IsNewUser": isNewUser,
|
||||
}, "登录成功", ctx)
|
||||
} else if err != nil {
|
||||
global.GVA_LOG.Error("设置登录状态失败!", zap.Error(err))
|
||||
@@ -127,7 +128,7 @@ func (*AppUserApi) WechatLogin(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := appUserService.WechatLogin(info)
|
||||
user, isNewUser, err := appUserService.WechatLogin(info)
|
||||
if err != nil {
|
||||
r.FailWithMessage("登录失败:"+err.Error(), ctx)
|
||||
return
|
||||
@@ -158,17 +159,29 @@ func (*AppUserApi) WechatLogin(ctx *gin.Context) {
|
||||
}
|
||||
}()
|
||||
|
||||
adcodes := utils.CheckIPInAdcodes(loginLog.Address)
|
||||
if !adcodes {
|
||||
global.GVA_LOG.Warn("异常登录地址", zap.String("address", loginLog.Address), zap.Uint("userId", user.ID))
|
||||
// 判断是否为新用户
|
||||
if isNewUser {
|
||||
ipCheckStatus := false
|
||||
|
||||
user.Status = 0
|
||||
if err := global.GVA_DB.Save(&user).Error; err != nil {
|
||||
global.GVA_LOG.Error("禁用用户失败!", zap.Error(err))
|
||||
err = global.GVA_DB.Model(&user2.IpCheck{}).Select("status").Scan(&ipCheckStatus).Error
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取IP检测状态失败", zap.Error(err))
|
||||
}
|
||||
|
||||
r.Banned("用户已被禁用", ctx)
|
||||
return
|
||||
if ipCheckStatus {
|
||||
adcodes := utils.CheckIPInAdcodes(loginLog.Address)
|
||||
if !adcodes {
|
||||
global.GVA_LOG.Warn("异常登录地址", zap.String("address", loginLog.Address), zap.Uint("userId", user.ID))
|
||||
|
||||
user.Status = 0
|
||||
if err := global.GVA_DB.Save(&user).Error; err != nil {
|
||||
global.GVA_LOG.Error("禁用用户失败!", zap.Error(err))
|
||||
}
|
||||
|
||||
r.Banned("用户已被禁用", ctx)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 生成token
|
||||
@@ -195,6 +208,7 @@ func (*AppUserApi) WechatLogin(ctx *gin.Context) {
|
||||
"User": user,
|
||||
"Token": token,
|
||||
"ExpiresAt": claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
|
||||
"IsNewUser": isNewUser,
|
||||
}, "登录成功", ctx)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"git.echol.cn/loser/lckt/model/app"
|
||||
common "git.echol.cn/loser/lckt/model/common/request"
|
||||
r "git.echol.cn/loser/lckt/model/common/response"
|
||||
"git.echol.cn/loser/lckt/model/user"
|
||||
"git.echol.cn/loser/lckt/model/user/request"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
@@ -272,3 +273,57 @@ func (a *UserApi) SetTeacherExpectRate(context *gin.Context) {
|
||||
r.OkWithMessage("修改讲师信息成功", context)
|
||||
|
||||
}
|
||||
|
||||
// ===========================IP检测配置==========================
|
||||
|
||||
// SetIpCheckConfig 设置IP检测配置
|
||||
func (a *UserApi) SetIpCheckConfig(ctx *gin.Context) {
|
||||
var p user.IpCheck
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数错误,设置IP检测配置失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
config, err := userService.SetIpConfig(p)
|
||||
if err != nil {
|
||||
r.FailWithMessage("设置IP检测配置失败", ctx)
|
||||
return
|
||||
}
|
||||
r.OkWithData(config, ctx)
|
||||
}
|
||||
|
||||
// GetIpCheckConfig 获取IP检测配置
|
||||
func (a *UserApi) GetIpCheckConfig(ctx *gin.Context) {
|
||||
config, err := userService.GetIpConfig()
|
||||
if err != nil {
|
||||
r.FailWithMessage("获取IP检测配置失败", ctx)
|
||||
return
|
||||
}
|
||||
r.OkWithData(config, ctx)
|
||||
}
|
||||
|
||||
// GetIpCheckStatus 获取IP检测状态
|
||||
func (a *UserApi) GetIpCheckStatus(ctx *gin.Context) {
|
||||
status, err := userService.GetIpStatus()
|
||||
if err != nil {
|
||||
r.FailWithMessage("获取IP检测状态失败", ctx)
|
||||
return
|
||||
}
|
||||
r.OkWithDetailed(status, "获取IP检测状态成功", ctx)
|
||||
}
|
||||
|
||||
// SetIpCheckStatus 设置IP检测状态
|
||||
func (a *UserApi) SetIpCheckStatus(ctx *gin.Context) {
|
||||
var p user.IpCheck
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数错误,设置IP检测状态失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
status, err := userService.SetIpStatus(p.Status)
|
||||
if err != nil {
|
||||
r.FailWithMessage("设置IP检测状态失败", ctx)
|
||||
return
|
||||
}
|
||||
r.OkWithDetailed(status, "设置IP检测状态成功", ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user