mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-05 09:36:00 +08:00
fix log
This commit is contained in:
@@ -2,6 +2,7 @@ package im_mysql_model
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db"
|
||||
"errors"
|
||||
_ "gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -46,7 +47,11 @@ func AddUserRegisterAddFriendIDList(userIDList ...string) error {
|
||||
for _, v := range userIDList {
|
||||
list = append(list, db.RegisterAddFriend{UserID: v})
|
||||
}
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Create(list).Error
|
||||
result := db.DB.MysqlDB.DefaultGormDB().Create(list)
|
||||
if int(result.RowsAffected) < len(userIDList) {
|
||||
return errors.New("some line insert failed")
|
||||
}
|
||||
err := result.Error
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package im_mysql_model
|
||||
import (
|
||||
"Open_IM/pkg/common/db"
|
||||
"errors"
|
||||
"github.com/jinzhu/gorm"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
@@ -10,15 +11,17 @@ import (
|
||||
/**
|
||||
* 批量生成邀请码
|
||||
*/
|
||||
func BatchCreateInvitationCodes(CodeNums int, CodeLen int) error {
|
||||
func BatchCreateInvitationCodes(CodeNums int, CodeLen int) ([]string, error) {
|
||||
i := CodeNums
|
||||
var codes []string
|
||||
for {
|
||||
if i == 0 {
|
||||
break
|
||||
}
|
||||
code := CreateRandomString(CodeLen)
|
||||
invitation := new(db.Invitation)
|
||||
invitation.CreateTime = time.Now()
|
||||
invitation.InvitationCode = CreateRandomString(CodeLen)
|
||||
invitation.InvitationCode = code
|
||||
invitation.LastTime = time.Now()
|
||||
invitation.Status = 0
|
||||
invitation.UserID = ""
|
||||
@@ -29,8 +32,9 @@ func BatchCreateInvitationCodes(CodeNums int, CodeLen int) error {
|
||||
if result.RowsAffected > 0 {
|
||||
i = i - 1
|
||||
}
|
||||
codes = append(codes, code)
|
||||
}
|
||||
return nil
|
||||
return codes, nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,9 +58,9 @@ func CheckInvitationCode(code string) error {
|
||||
/**
|
||||
* 尝试加锁模式解决邀请码抢占的问题
|
||||
*/
|
||||
func TryLockInvitationCode(Code string, UserId string) bool {
|
||||
func TryLockInvitationCode(Code string, UserID string) bool {
|
||||
Data := make(map[string]interface{}, 0)
|
||||
Data["user_id"] = UserId
|
||||
Data["user_id"] = UserID
|
||||
Data["status"] = 1
|
||||
Data["last_time"] = time.Now()
|
||||
result := db.DB.MysqlDB.DefaultGormDB().Table("invitations").Where("invitation_code=? and user_id=? and status=?", Code, "", 0).Updates(Data)
|
||||
@@ -79,6 +83,17 @@ func FinishInvitationCode(Code string, UserId string) bool {
|
||||
return result.RowsAffected > 0
|
||||
}
|
||||
|
||||
func GetInvitationCode(code string) (*db.Invitation, error) {
|
||||
invitation := &db.Invitation{
|
||||
InvitationCode: code,
|
||||
}
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Model(invitation).Find(invitation).Error
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
return invitation, nil
|
||||
}
|
||||
return invitation, err
|
||||
}
|
||||
|
||||
func CreateRandomString(strlen int) string {
|
||||
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
@@ -89,3 +104,10 @@ func CreateRandomString(strlen int) string {
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
func GetInvitationCodes(pageNumber, showNumber, status int32) ([]db.Invitation, error) {
|
||||
var invitationList []db.Invitation
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Model(db.Invitation{}).Limit(int(showNumber)).Offset(int(showNumber*(pageNumber-1))).Where("status=?", status).
|
||||
Order("create_time desc").Find(&invitationList).Error
|
||||
return invitationList, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package im_mysql_model
|
||||
|
||||
import "Open_IM/pkg/common/db"
|
||||
|
||||
func IsLimitRegisterIp(RegisterIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("ip_limits").Where("ip=? and limit_register=? and limit_time>now()", RegisterIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitLoginIp(LoginIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("ip_limits").Where("ip=? and limit_login=? and limit_time>now()", LoginIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitUserLoginIp(userID string, LoginIp string) (bool, error) {
|
||||
//如果已经存在则放行
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("user_ip_limits").Where("ip=? and user_id=?", LoginIp, userID).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count == 0, nil
|
||||
}
|
||||
|
||||
func QueryUserIPLimits(ip string) ([]db.UserIpLimit, error) {
|
||||
var ips []db.UserIpLimit
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpLimit{}).Where("ip=?", ip).Find(&ips).Error
|
||||
return ips, err
|
||||
}
|
||||
|
||||
func InsertOneIntoIpLimits(ipLimits db.IpLimit) error {
|
||||
return db.DB.MysqlDB.DefaultGormDB().Model(&db.IpLimit{}).Create(ipLimits).Error
|
||||
}
|
||||
@@ -46,20 +46,10 @@ func UserRegister(user db.User) error {
|
||||
user.LastLoginTime = time.Now()
|
||||
user.LoginTimes = 0
|
||||
user.LastLoginIp = user.CreateIp
|
||||
if config.Config.Demo.NeedInvitationCode {
|
||||
//判断一下验证码的使用情况
|
||||
LockSucc := TryLockInvitationCode(user.InvitationCode, user.UserID)
|
||||
if !LockSucc {
|
||||
return constant.InvitationMsg
|
||||
}
|
||||
}
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Table("users").Create(&user).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if config.Config.Demo.NeedInvitationCode {
|
||||
FinishInvitationCode(user.InvitationCode, user.UserID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -288,30 +278,3 @@ func GetBlockUsersNumCount() (int32, error) {
|
||||
}
|
||||
return int32(count), nil
|
||||
}
|
||||
|
||||
func IsLimitRegisterIp(RegisterIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("ip_limits").Where("ip=? and limit_register=? and limit_time>now()", RegisterIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitLoginIp(LoginIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("ip_limits").Where("ip=? and limit_login=? and limit_time>now()", LoginIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitUserLoginIp(userID string, LoginIp string) (bool, error) {
|
||||
//如果已经存在则放行
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("user_ip_limits").Where("ip=? and user_id=?", LoginIp, userID).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count == 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user