132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
package user
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/utils"
|
|
"time"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
//解码
|
|
func decodeUserData(base64Str string) *pb.DB_UserData {
|
|
dec, err := base64.StdEncoding.DecodeString(base64Str[35:])
|
|
if err != nil {
|
|
log.Errorf("base64 decode err %v", err)
|
|
return nil
|
|
}
|
|
now := time.Now().Unix()
|
|
jsonRet := gjson.Parse(string(dec))
|
|
serverId := jsonRet.Get("serverId").Int()
|
|
timestamp := jsonRet.Get("timestamp").Int()
|
|
if now-time.Unix(timestamp, 0).Unix() > 100 {
|
|
return nil
|
|
}
|
|
account := jsonRet.Get("account").String()
|
|
return &pb.DB_UserData{
|
|
Sid: int32(serverId),
|
|
Binduid: account,
|
|
}
|
|
}
|
|
|
|
//参数校验
|
|
func (this *Api_Comp) Login_Check(session comm.IUserSession, req *pb.UserLoginReq) (result map[string]interface{}, code pb.ErrorCode) {
|
|
result = map[string]interface{}{}
|
|
return
|
|
}
|
|
|
|
//登录
|
|
func (this *Api_Comp) Login(session comm.IUserSession, result map[string]interface{}, req *pb.UserLoginReq) (err error) {
|
|
var (
|
|
code pb.ErrorCode
|
|
db_user *pb.DB_UserData
|
|
)
|
|
|
|
defer func() {
|
|
if db_user != nil {
|
|
session.SendMsg(string(this.module.GetType()), User_SubType_Login, code, &pb.UserLoginResp{
|
|
Data: db_user,
|
|
})
|
|
event.TriggerEvent(comm.Event_UserLogin, db_user.Uid)
|
|
}
|
|
}()
|
|
|
|
if !utils.ValidSecretKey(req.Sec) {
|
|
code = pb.ErrorCode_SecKeyInvalid
|
|
return
|
|
}
|
|
|
|
user := decodeUserData(req.Sec)
|
|
|
|
db_user, err = this.module.modelUser.User_FindByAccount(user)
|
|
if err != nil {
|
|
if err != mongo.ErrNoDocuments {
|
|
return err
|
|
}
|
|
}
|
|
|
|
cache_user := &pb.Cache_UserData{
|
|
SessionId: session.GetSessionId(),
|
|
GatewayServiceId: session.GetGatewayServiceId(),
|
|
}
|
|
|
|
//如果是新玩家,创建一条基础的数据,页面会引导进入创角页面
|
|
if db_user == nil {
|
|
//新玩家
|
|
err = this.module.modelUser.User_Create(user)
|
|
if err != nil {
|
|
log.Errorf("User_CreateUser err %v", err)
|
|
return
|
|
}
|
|
session.Bind(user.Uid, this.service.GetId())
|
|
// data := map[string]interface{}{
|
|
// "sessionId": cache_user.SessionId,
|
|
// "gatewayServiceId": cache_user.GatewayServiceId,
|
|
// }
|
|
data := &pb.Cache_UserData{
|
|
Uid: user.Uid,
|
|
SessionId: cache_user.SessionId,
|
|
GatewayServiceId: cache_user.GatewayServiceId,
|
|
}
|
|
err = this.module.modelSession.SetObj(cache_user.Uid, data, true, true)
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
err = this.module.modelUser.SetObj(cache_user.Uid, user, true, true)
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
} else {
|
|
session.Bind(db_user.Uid, this.service.GetId())
|
|
data := &pb.Cache_UserData{
|
|
Uid: db_user.Uid,
|
|
SessionId: cache_user.SessionId,
|
|
GatewayServiceId: cache_user.GatewayServiceId,
|
|
}
|
|
// data := map[string]interface{}{
|
|
// "sessionId": cache_user.SessionId,
|
|
// "gatewayServiceId": cache_user.GatewayServiceId,
|
|
// }
|
|
err = this.module.modelSession.SetObj(db_user.Uid, data, false, false)
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
|
|
err = this.module.modelUser.SetObj(db_user.Uid, db_user, false, true)
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|