예제 #1
0
        private void Login()
        {
            userNameValue = edit_userName.Text.ToString();
            passwordValue = edit_userPassword.Text.ToString();
            if (string.IsNullOrEmpty(userNameValue) || string.IsNullOrEmpty(passwordValue))
            {
                Toast.MakeText(this, "用户名或密码不能为空,请填写...", ToastLength.Long).Show();
                return;
            }

            if (!EldYoungUtil.IsValidUserName(userNameValue))
            {
                Toast.MakeText(this, "请输入正确规范用户名4-20位,由中英文、数字、下划线组成", ToastLength.Short).Show();
                return;
            }
            if (!EldYoungUtil.IsPassWord(passwordValue))
            {
                Toast.MakeText(this, "请输入正确规范用户名6-20位,建议由字母、数字和符号两种以上组成", ToastLength.Short).Show();
                return;
            }

            ProgressDialogUtil.StartProgressDialog(this, GetString(Resource.String.loginMsg), true);
            //检测网络连接
            if (!EldYoungUtil.IsConnected(this))
            {
                Toast.MakeText(this, "网络连接超时,请检测网络", ToastLength.Short).Show();
                ProgressDialogUtil.StopProgressDialog();
                return;
            }

            //调用restapi service 登录
            var loginInfoParam = new LoginInfoParam()
            {
                LoginName = userNameValue, LoginPwd = passwordValue
            };

            //初始化请求参数
            SetRestRequestParams(loginInfoParam);

            var restSharpRequestHelp = new RestSharpRequestHelp(loginInfoParam.Url, requestParams);

            restSharpRequestHelp.ExcuteAsync((response) => {
                if (response.ResponseStatus == RestSharp.ResponseStatus.Completed && response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    //获取并解析返回resultJson获取安全码结果值
                    var resultJson = response.Content;
                    var loginJson  = JsonConvert.DeserializeObject <LoginJson>(resultJson);
                    if (loginJson.statuscode == "1")
                    {
                        Global.MyInfo = loginJson.data.Table[0];
                        var Uid       = Global.MyInfo.UId;

                        var guidAsAlias = Uid.Replace("-", "_");                       //使用用户guid作为推送别名
                        _jpushUtil.SetAlias(guidAsAlias);
                        //登录成功,且记住用户密码选中,才记录
                        if (cb_passWord.Checked)
                        {
                            sp_userinfo.Edit().PutString(Global.login_UserName, userNameValue).Commit();
                            sp_userinfo.Edit().PutString(Global.login_Password, passwordValue).Commit();
                            sp_userinfo.Edit().PutBoolean(Global.login_Password_Check, true).Commit();
                        }
                        RunOnUiThread(() =>
                        {
                            //跳转到功能主界面
                            var intent = new Intent(this, typeof(MainFragActivity));
                            intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
                            var bundle = Intent.Extras;
                            if (bundle != null)
                            {
                                intent.PutExtras(bundle);
                            }
                            StartActivity(intent);
                            this.Finish();
                            Toast.MakeText(this, "登录成功", ToastLength.Short).Show();
                            OverridePendingTransition(Android.Resource.Animation.FadeIn, Android.Resource.Animation.FadeOut);
                        });
                    }
                    else
                    {
                        RunOnUiThread(() =>
                        {
                            Toast.MakeText(this, loginJson.message, ToastLength.Short).Show();
                        });
                    }
                }
                else if (response.ResponseStatus == RestSharp.ResponseStatus.TimedOut)
                {
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this, "网络连接超时", ToastLength.Short).Show();
                    });
                }
                else
                {
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this, response.StatusDescription, ToastLength.Short).Show();
                    });
                }
                RunOnUiThread(() =>
                {
                    ProgressDialogUtil.StopProgressDialog();

                    return;
                });
            });
        }
        /// <summary>
        /// Reister this instance.
        /// </summary>
        private void Reister()
        {
            ProgressDialogUtil.StartProgressDialog(this, "正在注册...");
            //检测网络连接
            if (!EldYoungUtil.IsConnected(this))
            {
                Toast.MakeText(this, "网络连接超时,请检测网路", ToastLength.Short).Show();
                ProgressDialogUtil.StopProgressDialog();
                return;
            }


            //todo:调用restapi注册,将用户名、密码信息写
            var registerInfoParam = new RegisterInfoParam()
            {
                PhoneNumber = phoneNum, NickName = nickName, PassWord = passWord
            };

            //初始化参数
            SetRestRequestParams(registerInfoParam);

            var restSharpRequestHelp = new RestSharpRequestHelp(registerInfoParam.Url, requestParams);

            restSharpRequestHelp.ExcuteAsync((response) => {
                if (response.ResponseStatus == ResponseStatus.Completed)
                {
                    //获取并解析返回resultJson获取Guid结果值
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var resultJson   = response.Content;
                        var registerJson = JsonConvert.DeserializeObject <RegisterJson>(resultJson);
                        if (registerJson.statuscode == "1")
                        {
                            Global.MyInfo   = registerJson.data.Table[0];
                            var Uid         = Global.MyInfo.UId;
                            var guidAsAlias = Uid.Replace("-", "_");
                            //注册用户成功,写极光推送别名,进入主界面
                            _jpushUtil.SetAlias(guidAsAlias);
                            sp_userinfo.Edit().PutString(Global.login_UserName, nickName).Commit();
                            sp_userinfo.Edit().PutString(Global.login_Password, passWord).Commit();
                            sp_userinfo.Edit().PutBoolean(Global.login_Password_Check, true).Commit();
                            RunOnUiThread(() =>
                            {
                                //跳转到功能主界面
                                var intent = new Intent(this, typeof(MainFragActivity));
                                intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
                                StartActivity(intent);
                                this.Finish();
                                ProgressDialogUtil.StopProgressDialog();
                                Toast.MakeText(this, "注册成功", ToastLength.Short).Show();
                                OverridePendingTransition(Android.Resource.Animation.SlideInLeft, Android.Resource.Animation.SlideOutRight);
                            });
                        }
                        else
                        {
                            //注册失败
                            RunOnUiThread(() =>
                            {
                                Toast.MakeText(this, registerJson.message, ToastLength.Short).Show();
                                ProgressDialogUtil.StopProgressDialog();
                                return;
                            });
                        }
                    }
                }
                else if (response.ResponseStatus == ResponseStatus.TimedOut)
                {
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this, "网络连接超时", ToastLength.Short).Show();
                        ProgressDialogUtil.StopProgressDialog();
                        return;
                    });
                }
                else
                {
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this, response.StatusDescription, ToastLength.Short).Show();
                        ProgressDialogUtil.StopProgressDialog();
                        return;
                    });
                }
            });
        }