public LoginResultContract Login(string uid, string pwd, string verCode) { LoginContract loginContract = new LoginContract { Uid = uid, Pwd = pwd, VerCode = verCode }; MessagePacket message = new MessagePacket(uid, GloblParams.CurrentClientID, GloblParams.CurrentClientType, MessageType.Login, 0, loginContract.ToByteArray()); byte[] array = null; try { array = this.BasicOutter.Query(message); } catch (TimeoutException e) { LoginResultContract loginResultContract = new LoginResultContract(); loginResultContract.Result = 1; loginResultContract.Failure = "登录请求超时,检查网络或是服务器"; } catch { LoginResultContract loginResultContract = new LoginResultContract(); loginResultContract.Result = 2; loginResultContract.Failure = "unkonw cause"; } LoginResultContract result; if (array == null) { result = null; } else { LoginResultContract loginResultContract = LoginResultContract.Parser.ParseFrom(array); if (loginResultContract.Result == 0) { GloblParams.CurrentClientID = uid; this.channelClientHandler.CanSendHeartMessage = true; } result = loginResultContract; } return(result); }
public void Process(RequestInfo request) { var message = request.MessagePacket; try { #region Noraml if (message.MessageHeader.MessageType == MessageType.Noraml) { if (message.MessageHeader.DestUserID == GloblParams.DefaultServerID) { customizeHandler.Handle(message.MessageHeader.SourceUserID, message.ContractID, message.BodyContent); } else { basicController.Send(message.MessageHeader.DestUserID, message); } } #endregion #region Ack_Req else if (message.MessageHeader.MessageType == MessageType.Ack_Req) { if (message.MessageHeader.DestUserID == GloblParams.DefaultServerID) { ResponseAck(request.Channel, message); customizeHandler.Handle(message.MessageHeader.SourceUserID, message.ContractID, message.BodyContent); } else { basicController.Send(message.MessageHeader.DestUserID, message); } } #endregion #region Blob else if (message.MessageHeader.MessageType == MessageType.Blob) { if (message.MessageHeader.DestUserID == GloblParams.DefaultServerID) { BlobContract contract = BlobContract.Parser.ParseFrom(message.BodyContent); string keyStr = message.MessageHeader.SourceUserID + contract.BlobID.ToString(); BlobItem item = hotDic.Get(keyStr); if (item == null) { item = new BlobItem(); hotDic.Add(keyStr, item); } item.Add(contract.FragmentIndex, contract.Fragment); if (contract.IsLast) { BlobItem blob = this.hotDic.GetAndRemove(keyStr); MemoryStream stream = new MemoryStream(); foreach (var blobItem in blob) { blobItem.Value.WriteTo(stream); } byte[] bytes = stream.ToArray(); stream.Dispose(); customizeHandler.Handle(message.MessageHeader.SourceUserID, message.ContractID, bytes); } } else { basicController.Send(message.MessageHeader.DestUserID, message); } } #endregion #region Ack_Resp else if (message.MessageHeader.MessageType == MessageType.Ack_Resp) { basicController.Send(message.MessageHeader.DestUserID, message); } #endregion #region Query_Req else if (message.MessageHeader.MessageType == MessageType.Query_Req) { if (message.MessageHeader.DestUserID == GloblParams.DefaultServerID) { byte[] result = customizeHandler.HandleQuery(message.MessageHeader.SourceUserID, message.ContractID, message.BodyContent); ResponseQuery(request.Channel, MessageType.Query_Resp, message, result); } else { basicController.Send(message.MessageHeader.DestUserID, message); } } #endregion #region Query_Resp else if (message.MessageHeader.MessageType == MessageType.Query_Resp) { basicController.Send(message.MessageHeader.DestUserID, message); } #endregion #region Login else if (message.MessageHeader.MessageType == MessageType.Login) { LoginContract loginContract = LoginContract.Parser.ParseFrom(message.BodyContent); string cause; LoginResultContract result = null; bool verResult = basicHandler.VerifyUser(loginContract, request.Channel, out cause); this.basicController.userManager.Add(loginContract.Uid, request.Channel); result = new LoginResultContract() { Result = verResult ? 0 : 1, Failure = cause }; ResponseQuery(request.Channel, MessageType.LoginResp, message, result.ToByteArray()); } #endregion else { //handler.HandleQuery(request); } } catch { } }
public async Task <ResultContract <LoginResultContract> > Login([FromBody] LoginParamContract loginParam) { try { if (string.IsNullOrWhiteSpace(loginParam.Account)) { return(new ResultContract <LoginResultContract> { Code = -1, Msg = "请输入账号" }); } if (string.IsNullOrWhiteSpace(loginParam.Password)) { return(new ResultContract <LoginResultContract> { Code = -1, Msg = "请输入密码" }); } //用户验证 var user = userService.CheckUser(loginParam.Account, loginParam.Password); if (user == null) { return(new ResultContract <LoginResultContract> { Code = -1, Msg = "账号或密码不正确" }); } //平台验证 var application = applicationService.GetApplicationByID(loginParam.AppId); if (application == null) { return(new ResultContract <LoginResultContract> { Code = -1, Msg = "平台未注册" }); } //更改用户登录状态 var userEntity = userService.GetUser(user.UserId); userEntity.IsLogin = true; userService.Update(userEntity); //生成token var model = new TokenDataModel { UserId = user.UserId, Account = user.UserAccount, AppName = application.AppName, FromSystem = "" }; var token = JWT.GenerateToken(model, application.AppSecret); var result = new LoginResultContract() { Token = token, User = new Contracts.DB.UserContract() { UserId = user.UserId, UserName = user.UserName, OrganizationId = user.OrganizationId } }; return(new Contracts.ResultContract <LoginResultContract>() { Code = 0, Msg = "", Data = result }); } catch (Exception) { return(new Contracts.ResultContract <LoginResultContract>() { Code = -1, Msg = "服务异常" });; } }