mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 14:22:26 +08:00
1.MongoDB、Redis兼容集群模式
2.离线数据接口化访问
This commit is contained in:
@@ -137,7 +137,6 @@ func (c *Controller) registerRouter() {
|
||||
}
|
||||
engine.Use(c.authorize())
|
||||
engine.POST("/gate/token/verify", c.gateTokenVerify)
|
||||
engine.POST("/gate/token/reset", c.gateTokenReset)
|
||||
port := config.GetConfig().HttpPort
|
||||
addr := ":" + strconv.Itoa(int(port))
|
||||
err := engine.Run(addr)
|
||||
|
||||
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
@@ -51,12 +52,7 @@ func (c *Controller) gateTokenVerify(context *gin.Context) {
|
||||
verifyFail(account.PlayerID)
|
||||
return
|
||||
}
|
||||
if account.ComboTokenUsed {
|
||||
verifyFail(account.PlayerID)
|
||||
return
|
||||
}
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "ComboTokenUsed", true)
|
||||
if err != nil {
|
||||
if time.Now().UnixMilli()-int64(account.ComboTokenCreateTime) > time.Minute.Milliseconds()*5 {
|
||||
verifyFail(account.PlayerID)
|
||||
return
|
||||
}
|
||||
@@ -67,32 +63,3 @@ func (c *Controller) gateTokenVerify(context *gin.Context) {
|
||||
PlayerID: account.PlayerID,
|
||||
})
|
||||
}
|
||||
|
||||
type TokenResetReq struct {
|
||||
PlayerId uint32 `json:"playerId"`
|
||||
}
|
||||
|
||||
type TokenResetRsp struct {
|
||||
Result bool `json:"result"`
|
||||
}
|
||||
|
||||
func (c *Controller) gateTokenReset(context *gin.Context) {
|
||||
req := new(TokenResetReq)
|
||||
err := context.ShouldBindJSON(req)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, &TokenResetRsp{
|
||||
Result: false,
|
||||
})
|
||||
return
|
||||
}
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("PlayerID", req.PlayerId, "ComboTokenUsed", false)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, &TokenResetRsp{
|
||||
Result: false,
|
||||
})
|
||||
return
|
||||
}
|
||||
context.JSON(http.StatusOK, &TokenResetRsp{
|
||||
Result: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -101,6 +101,7 @@ func (c *Controller) apiLogin(context *gin.Context) {
|
||||
// 自动注册
|
||||
accountId, err := c.dao.GetNextAccountId()
|
||||
if err != nil {
|
||||
logger.Error("get next account id error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-1"
|
||||
context.JSON(http.StatusOK, responseData)
|
||||
@@ -108,6 +109,7 @@ func (c *Controller) apiLogin(context *gin.Context) {
|
||||
}
|
||||
playerID, err := c.dao.GetNextYuanShenUid()
|
||||
if err != nil {
|
||||
logger.Error("get next player id error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-2"
|
||||
context.JSON(http.StatusOK, responseData)
|
||||
@@ -125,6 +127,7 @@ func (c *Controller) apiLogin(context *gin.Context) {
|
||||
}
|
||||
_, err = c.dao.InsertAccount(regAccount)
|
||||
if err != nil {
|
||||
logger.Error("insert account error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-3"
|
||||
context.JSON(http.StatusOK, responseData)
|
||||
@@ -142,6 +145,7 @@ func (c *Controller) apiLogin(context *gin.Context) {
|
||||
account.Token = base64.StdEncoding.EncodeToString(random.GetRandomByte(24))
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "Token", account.Token)
|
||||
if err != nil {
|
||||
logger.Error("update account token error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-4"
|
||||
context.JSON(http.StatusOK, responseData)
|
||||
@@ -149,6 +153,7 @@ func (c *Controller) apiLogin(context *gin.Context) {
|
||||
}
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "TokenCreateTime", time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
logger.Error("update account token time error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-5"
|
||||
context.JSON(http.StatusOK, responseData)
|
||||
@@ -233,13 +238,15 @@ func (c *Controller) v2Login(context *gin.Context) {
|
||||
account.ComboToken = random.GetRandomByteHexStr(20)
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "ComboToken", account.ComboToken)
|
||||
if err != nil {
|
||||
logger.Error("update combo token error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-1"
|
||||
context.JSON(http.StatusOK, responseData)
|
||||
return
|
||||
}
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "ComboTokenUsed", false)
|
||||
_, err = c.dao.UpdateAccountFieldByFieldName("AccountID", account.AccountID, "ComboTokenCreateTime", time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
logger.Error("update combo token time error: %v", err)
|
||||
responseData.Retcode = -201
|
||||
responseData.Message = "服务器内部错误:-2"
|
||||
context.JSON(http.StatusOK, responseData)
|
||||
|
||||
@@ -27,7 +27,7 @@ func (d *Dao) InsertAccount(account *model.Account) (primitive.ObjectID, error)
|
||||
|
||||
func (d *Dao) UpdateAccountFieldByFieldName(fieldName string, fieldValue any, fieldUpdateName string, fieldUpdateValue any) (int64, error) {
|
||||
db := d.db.Collection("account")
|
||||
updateCount, err := db.UpdateOne(
|
||||
updateCount, err := db.UpdateMany(
|
||||
context.TODO(),
|
||||
bson.D{
|
||||
{fieldName, fieldValue},
|
||||
|
||||
@@ -22,7 +22,13 @@ func (d *Dao) GetNextYuanShenUid() (uint32, error) {
|
||||
}
|
||||
|
||||
func (d *Dao) redisInc(keyName string) (uint32, error) {
|
||||
exist, err := d.redis.Exists(context.TODO(), keyName).Result()
|
||||
var exist int64 = 0
|
||||
var err error = nil
|
||||
if d.redisCluster != nil {
|
||||
exist, err = d.redisCluster.Exists(context.TODO(), keyName).Result()
|
||||
} else {
|
||||
exist, err = d.redis.Exists(context.TODO(), keyName).Result()
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -33,12 +39,22 @@ func (d *Dao) redisInc(keyName string) (uint32, error) {
|
||||
} else if keyName == RedisPlayerKeyPrefix+":"+YuanShenUidRedisKey {
|
||||
value = YuanShenUidBegin
|
||||
}
|
||||
err := d.redis.Set(context.TODO(), keyName, value, 0).Err()
|
||||
var err error = nil
|
||||
if d.redisCluster != nil {
|
||||
err = d.redisCluster.Set(context.TODO(), keyName, value, 0).Err()
|
||||
} else {
|
||||
err = d.redis.Set(context.TODO(), keyName, value, 0).Err()
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
id, err := d.redis.Incr(context.TODO(), keyName).Result()
|
||||
var id int64 = 0
|
||||
if d.redisCluster != nil {
|
||||
id, err = d.redisCluster.Incr(context.TODO(), keyName).Result()
|
||||
} else {
|
||||
id, err = d.redis.Incr(context.TODO(), keyName).Result()
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/pkg/logger"
|
||||
@@ -13,13 +14,15 @@ import (
|
||||
)
|
||||
|
||||
type Dao struct {
|
||||
mongo *mongo.Client
|
||||
db *mongo.Database
|
||||
redis *redis.Client
|
||||
mongo *mongo.Client
|
||||
db *mongo.Database
|
||||
redis *redis.Client
|
||||
redisCluster *redis.ClusterClient
|
||||
}
|
||||
|
||||
func NewDao() (r *Dao) {
|
||||
r = new(Dao)
|
||||
|
||||
clientOptions := options.Client().ApplyURI(config.GetConfig().Database.Url).SetMinPoolSize(10).SetMaxPoolSize(100)
|
||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
@@ -33,18 +36,37 @@ func NewDao() (r *Dao) {
|
||||
}
|
||||
r.mongo = client
|
||||
r.db = client.Database("dispatch_hk4e")
|
||||
r.redis = redis.NewClient(&redis.Options{
|
||||
Addr: config.GetConfig().Redis.Addr,
|
||||
Password: config.GetConfig().Redis.Password,
|
||||
DB: 0,
|
||||
PoolSize: 10,
|
||||
MinIdleConns: 1,
|
||||
})
|
||||
err = r.redis.Ping(context.TODO()).Err()
|
||||
|
||||
r.redis = nil
|
||||
r.redisCluster = nil
|
||||
redisAddr := strings.ReplaceAll(config.GetConfig().Redis.Addr, "redis://", "")
|
||||
if strings.Contains(redisAddr, ",") {
|
||||
redisAddrList := strings.Split(redisAddr, ",")
|
||||
r.redisCluster = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: redisAddrList,
|
||||
Password: config.GetConfig().Redis.Password,
|
||||
PoolSize: 10,
|
||||
MinIdleConns: 1,
|
||||
})
|
||||
} else {
|
||||
r.redis = redis.NewClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
Password: config.GetConfig().Redis.Password,
|
||||
DB: 0,
|
||||
PoolSize: 10,
|
||||
MinIdleConns: 1,
|
||||
})
|
||||
}
|
||||
if r.redisCluster != nil {
|
||||
err = r.redisCluster.Ping(context.TODO()).Err()
|
||||
} else {
|
||||
err = r.redis.Ping(context.TODO()).Err()
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("redis ping error: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -53,7 +75,11 @@ func (d *Dao) CloseDao() {
|
||||
if err != nil {
|
||||
logger.Error("mongo close error: %v", err)
|
||||
}
|
||||
err = d.redis.Close()
|
||||
if d.redisCluster != nil {
|
||||
err = d.redisCluster.Close()
|
||||
} else {
|
||||
err = d.redis.Close()
|
||||
}
|
||||
if err != nil {
|
||||
logger.Error("redis close error: %v", err)
|
||||
}
|
||||
|
||||
@@ -3,15 +3,15 @@ package model
|
||||
import "go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
type Account struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
AccountID uint32 `bson:"AccountID"`
|
||||
PlayerID uint32 `bson:"PlayerID"`
|
||||
Username string `bson:"Username"`
|
||||
Password string `bson:"Password"`
|
||||
Token string `bson:"Token"`
|
||||
TokenCreateTime uint64 `bson:"TokenCreateTime"` // 毫秒时间戳
|
||||
ComboToken string `bson:"ComboToken"`
|
||||
ComboTokenUsed bool `bson:"ComboTokenUsed"`
|
||||
Forbid bool `bson:"Forbid"`
|
||||
ForbidEndTime uint32 `bson:"ForbidEndTime"` // 秒时间戳
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
AccountID uint32 `bson:"AccountID"`
|
||||
PlayerID uint32 `bson:"PlayerID"`
|
||||
Username string `bson:"Username"`
|
||||
Password string `bson:"Password"`
|
||||
Token string `bson:"Token"`
|
||||
TokenCreateTime uint64 `bson:"TokenCreateTime"` // 毫秒时间戳
|
||||
ComboToken string `bson:"ComboToken"`
|
||||
ComboTokenCreateTime uint64 `bson:"ComboTokenCreateTime"` // 毫秒时间戳
|
||||
Forbid bool `bson:"Forbid"`
|
||||
ForbidEndTime uint32 `bson:"ForbidEndTime"` // 秒时间戳
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user