protected override async void Run(Session session, C2G_BindPhone message, Action <G2C_BindPhone> reply)
        {
            Log.Info(JsonHelper.ToJson(message));
            G2C_BindPhone response = new G2C_BindPhone();

            try
            {
                DBProxyComponent   proxyComponent = Game.Scene.GetComponent <DBProxyComponent>();
                List <AccountInfo> accountInfos   = await proxyComponent.QueryJson <AccountInfo>($"{{_id:{message.Uid}}}");

                if (!string.IsNullOrEmpty(accountInfos[0].Phone))
                {
                    response.Message = "您已绑定手机号,请勿重复绑定";
                    response.Error   = ErrorCode.ERR_PhoneCodeError;
                    reply(response);
                    return;
                }

                // 校验验证码
                {
                    string str = HttpUtil.CheckSms("0", message.Phone, message.Code);
                    if (!CommonUtil.checkSmsCode(str))
                    {
                        response.Message = "验证码错误";
                        response.Error   = ErrorCode.ERR_PhoneCodeError;
                        reply(response);
                        return;
                    }
                }

                accountInfos[0].Phone = message.Phone;
                await proxyComponent.Save(accountInfos[0]);

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
        }
Exemple #2
0
        protected override async void Run(Session session, C2R_PhoneLogin message, Action <R2C_PhoneLogin> reply)
        {
            Log.Info(JsonHelper.ToJson(message));
            R2C_PhoneLogin response = new R2C_PhoneLogin();

            try
            {
                DBProxyComponent   proxyComponent = Game.Scene.GetComponent <DBProxyComponent>();
                List <AccountInfo> accountInfos   = await proxyComponent.QueryJson <AccountInfo>($"{{Phone:'{message.Phone}'}}");

                // 用验证码登录
                if (message.Code.CompareTo("") != 0)
                {
                    // 先校验验证码
                    {
                        string str = HttpUtil.CheckSms("0", message.Phone, message.Code);
                        if (!CommonUtil.checkSmsCode(str))
                        {
                            response.Message = "验证码错误";
                            response.Error   = ErrorCode.ERR_PhoneCodeError;
                            reply(response);
                            return;
                        }
                    }
                    // 用户已存在,走登录流程
                    if (accountInfos.Count > 0)
                    {
                        AccountInfo accountInfo = accountInfos[0];

                        // 黑名单检测
                        if (await DBCommonUtil.CheckIsInBlackList(accountInfo.Id, session))
                        {
                            response.Message = "您的账号存在异常,请联系客服处理。";
                            response.Error   = ErrorCode.ERR_PhoneCodeError;
                            reply(response);
                            return;
                        }

                        // 更新Token
                        accountInfo.Token = CommonUtil.getToken(message.Phone);
                        await proxyComponent.Save(accountInfo);

                        // 随机分配一个Gate
                        StartConfig config       = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                        IPEndPoint  innerAddress = config.GetComponent <InnerConfig>().IPEndPoint;
                        Session     gateSession  = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress);

                        // 向gate请求一个key,客户端可以拿着这个key连接gate
                        G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey()
                        {
                            UserId = accountInfo.Id
                        });

                        string outerAddress = config.GetComponent <OuterConfig>().IPEndPoint2.ToString();

                        response.Address = outerAddress;
                        response.Key     = g2RGetLoginKey.Key;
                        response.Token   = accountInfo.Token;
                        reply(response);
                        // 登录日志
                        await DBCommonUtil.Log_Login(accountInfo.Id, session, message.ClientVersion);
                    }
                    // 用户不存在,走注册流程
                    else
                    {
                        AccountInfo accountInfo = ComponentFactory.CreateWithId <AccountInfo>(UidUtil.createUID());
                        accountInfo.Phone         = message.Phone;
                        accountInfo.Token         = CommonUtil.getToken(message.Phone);
                        accountInfo.MachineId     = message.MachineId;
                        accountInfo.ChannelName   = message.ChannelName;
                        accountInfo.ClientVersion = message.ClientVersion;

                        await proxyComponent.Save(accountInfo);

                        // 添加用户信息
                        PlayerBaseInfo playerBaseInfo = await DBCommonUtil.addPlayerBaseInfo(accountInfo.Id, accountInfo.Phone, "", "");

                        // 随机分配一个Gate
                        StartConfig config       = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                        IPEndPoint  innerAddress = config.GetComponent <InnerConfig>().IPEndPoint;
                        Session     gateSession  = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress);

                        // 向gate请求一个key,客户端可以拿着这个key连接gate
                        G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey()
                        {
                            UserId = accountInfo.Id
                        });

                        string outerAddress = config.GetComponent <OuterConfig>().IPEndPoint2.ToString();

                        response.Address = outerAddress;
                        response.Key     = g2RGetLoginKey.Key;
                        response.Token   = accountInfo.Token;
                        reply(response);
                        // 登录日志
                        await DBCommonUtil.Log_Login(accountInfo.Id, session, message.ClientVersion);
                    }
                }
                // 用Token登录
                else if (message.Token.CompareTo("") != 0)
                {
                    if (accountInfos.Count > 0)
                    {
                        AccountInfo accountInfo = accountInfos[0];

                        // 黑名单检测
                        if (await DBCommonUtil.CheckIsInBlackList(accountInfo.Id, session))
                        {
                            response.Message = "您的账号存在异常,请联系客服处理。";
                            response.Error   = ErrorCode.ERR_PhoneCodeError;
                            reply(response);
                            return;
                        }

                        if (accountInfo?.Token?.CompareTo(message.Token) == 0)
                        {
                            // 随机分配一个Gate
                            StartConfig config       = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress();
                            IPEndPoint  innerAddress = config.GetComponent <InnerConfig>().IPEndPoint;
                            Session     gateSession  = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress);

                            // 向gate请求一个key,客户端可以拿着这个key连接gate
                            G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey()
                            {
                                UserId = accountInfo.Id
                            });

                            string outerAddress = config.GetComponent <OuterConfig>().IPEndPoint2.ToString();

                            response.Address = outerAddress;
                            response.Key     = g2RGetLoginKey.Key;
                            response.Token   = accountInfo.Token;
                            reply(response);

                            // 登录日志
                            await DBCommonUtil.Log_Login(accountInfo.Id, session, message.ClientVersion);
                        }
                        else
                        {
                            response.Message = "Token失效,请重新验证登录";
                            response.Error   = ErrorCode.ERR_TokenError;
                            reply(response);
                            return;
                        }
                    }
                    else
                    {
                        response.Message = "用户不存在";
                        response.Error   = ErrorCode.ERR_AccountNoExist;
                        reply(response);
                        return;
                    }
                }
                // 传的数据错误
                else
                {
                    response.Message = "请求参数缺失";
                    response.Error   = ErrorCode.ERR_ParamError;
                    reply(response);
                    return;
                }
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }
            session.Dispose();
        }
        public override string OnResponse(string data)
        {
            CheckSmsReq defaultReqData = null;

            try
            {
                defaultReqData = JsonConvert.DeserializeObject <CheckSmsReq>(data);
            }
            catch (Exception e)
            {
                MySqlService.log.Warn("传入的参数有误:" + e);
                return(null);
            }
            string Tag         = defaultReqData.tag;
            int    connId      = defaultReqData.connId;
            string uid         = defaultReqData.uid;
            string phoneNum    = defaultReqData.phoneNum;
            string verfityCode = defaultReqData.verfityCode;

            if (string.IsNullOrWhiteSpace(Tag) || string.IsNullOrWhiteSpace(uid) ||
                string.IsNullOrWhiteSpace(phoneNum) || string.IsNullOrWhiteSpace(verfityCode))
            {
                MySqlService.log.Warn("字段有空");
                return(null);
            }
            //传给客户端的数据
            JObject responseData = new JObject();

            responseData.Add(MyCommon.TAG, Tag);
            responseData.Add(MyCommon.CONNID, connId);
            responseData.Add(MyCommon.UID, uid);

            try
            {
                uid = uid.Substring(1, uid.Length - 1);
                string checkSms = HttpUtil.CheckSms(uid, phoneNum, verfityCode);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(checkSms);
                XmlNodeList nodeList = xmlDoc.ChildNodes;
                foreach (XmlNode node in nodeList)
                {
                    string nodeValue = node.InnerText;
                    if ("string".Equals(node.Name))
                    {
                        MySqlService.log.Info(nodeValue);

                        JObject result        = JObject.Parse(nodeValue);
                        var     ResultCode    = (int)result.GetValue("ResultCode");
                        var     ResultMessage = (string)result.GetValue("ResultMessageDetails");
                        responseData.Add("msg", ResultMessage);

                        if (ResultCode == 1)
                        {
                            uid = "6" + uid;
                            UserInfo userInfo = NHibernateHelper.userInfoManager.GetByUid(uid);
                            if (userInfo == null)
                            {
                                MySqlService.log.Warn("uid未注册");
                                responseData.Add(MyCommon.CODE, (int)Consts.Code.Code_CommonFail);
                            }
                            else
                            {
                                if (string.IsNullOrWhiteSpace(userInfo.Phone))
                                {
                                    responseData.Add("isFirst", 1);
                                }
                                else
                                {
                                    responseData.Add("isFirst", 0);
                                }
                                userInfo.Phone = phoneNum;
                                if (NHibernateHelper.userInfoManager.Update(userInfo))
                                {
                                    responseData.Add(MyCommon.CODE, (int)Consts.Code.Code_OK);
                                }
                                else
                                {
                                    MySqlService.log.Warn("更新用户数据库失败");
                                    responseData.Add(MyCommon.CODE, (int)Consts.Code.Code_CommonFail);
                                }
                            }
                        }
                        else
                        {
                            responseData.Add(MyCommon.CODE, (int)Consts.Code.Code_CommonFail);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MySqlService.log.Warn("发送信息失败:" + e);
                responseData.Add(MyCommon.CODE, (int)Consts.Code.Code_CommonFail);
            }
            return(responseData.ToString());
        }