private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            ChangePassword_VM vm = DataContext as ChangePassword_VM;

            if (vm != null)
            {
                if (vm.IsModelValid())
                {
                    Password_API_Put objectToPut = new Password_API_Put();
                    objectToPut.Password = WebApiClientHelper.MakeConfidentialMessage(vm.ConfirmPwd);
                    try
                    {
                        WebApiClientHelper.DoJsonRequest <Password_API_Put>(
                            GlobalData.GetResUri(string.Format("password/{0}", GlobalData.CurrentUserName)),
                            EnuHttpMethod.Put,
                            objToSend: objectToPut);
                    }
                    catch (ClientException ex)
                    {
                        vm.SetExtraError(ex.Message);
                        return;
                    }

                    WebApiClientHelper.Password = vm.ConfirmPwd;
                    DialogResult = true;
                    Close();
                }
            }
        }
        private bool LoadMyInfo(bool bShowSuccessfulPopup = false)
        {
            //加载数据
            UserInfo_API_Get uiToGet;

            try
            {
                uiToGet = WebApiClientHelper.DoJsonRequest <UserInfo_API_Get>(GlobalData.GetResUri(string.Format("usersinfo/{0}", GlobalData.CurrentUserName)), EnuHttpMethod.Get);
            }
            catch (ClientException ex)
            {
                Commands.ShowPopupAlert.Execute(new ShowPopupAlertParam {
                    AlertMessage = ex.Message, AlertType = EnuPopupAlertType.Error
                }, this);
                return(false);
            }

            UserInfo_VM ui_vm = Mapper.Map <UserInfo_VM>(uiToGet);

            ui_vm.ModelType           = EnuModelType.View;
            gridMyInfoTab.DataContext = ui_vm;

            if (bShowSuccessfulPopup)
            {
                Commands.ShowPopupAlert.Execute(new ShowPopupAlertParam {
                    AlertMessage = "刷新我的信息成功.", AlertType = EnuPopupAlertType.Info
                }, this);
            }

            return(true);
        }
        private void btnUploadAvatar_Click(object sender, RoutedEventArgs e)
        {
            string avatar = _OpenFileDialog("选择头像", "jpg文件(*.jpg)|*.jpg");

            if (!string.IsNullOrEmpty(avatar))
            {
                using (FileStream fs = File.Open(avatar, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    try
                    {
                        WebApiClientHelper.DoStreamRequest(
                            GlobalData.GetResUri(string.Format("avatars/{0}", GlobalData.CurrentUserName)),
                            EnuHttpMethod.Put, fs);
                    }
                    catch (ClientException ex)
                    {
                        Commands.ShowPopupAlert.Execute(new ShowPopupAlertParam {
                            AlertMessage = ex.Message, AlertType = EnuPopupAlertType.Error
                        }, this);
                        return;
                    }
                }

                //设置头像(需要重新打开文件流并将它转为MemoryStream)
                using (FileStream fs = File.Open(avatar, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    if (fs.Length > 0)
                    {
                        byte[] memory = new byte[fs.Length];
                        fs.Read(memory, 0, (int)fs.Length);
                        SetAvatarImage(new MemoryStream(memory));
                    }
                }
            }
        }
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            UserInfo_VM ui_vm = gridMyInfoTab.DataContext as UserInfo_VM;

            if (ui_vm != null)
            {
                if (ui_vm.IsModelValid())
                {
                    try
                    {
                        UserInfo_API_Put userput = Mapper.Map <UserInfo_VM, UserInfo_API_Put>(ui_vm);
                        WebApiClientHelper.DoJsonRequest <UserInfo_API_Put>(GlobalData.GetResUri(string.Format("usersinfo/{0}", ui_vm.UserName)), EnuHttpMethod.Put, objToSend: userput, tick: ui_vm.UpdateTicks);
                    }
                    catch (ClientException ex)
                    {
                        ui_vm.SetExtraError(ex.Message); //这是Popup以外的另一种异常显示方式
                        return;
                    }

                    LoadMyInfo();

                    Commands.ShowPopupAlert.Execute(new ShowPopupAlertParam {
                        AlertMessage = "修改信息成功.", AlertType = EnuPopupAlertType.Info
                    }, this);

                    Commands.GoToViewMode.Execute(null, this);
                }
            }
        }
 private void btnClearAvatar_Click(object sender, RoutedEventArgs e)
 {
     if (MessageBoxResult.OK == MessageBox.Show("要删除头像吗?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question))
     {
         try
         {
             WebApiClientHelper.DoJsonRequest <int>(GlobalData.GetResUri(string.Format("avatars/{0}", GlobalData.CurrentUserName)),
                                                    EnuHttpMethod.Delete);
         }
         catch (ClientException ex)
         {
             Commands.ShowPopupAlert.Execute(new ShowPopupAlertParam {
                 AlertMessage = ex.Message, AlertType = EnuPopupAlertType.Error
             }, this);
             return;
         }
         SetAvatarImage(null);
     }
 }
        private void RetrieveMyAvatar()
        {
            Stream stm;

            try
            {
                stm = WebApiClientHelper.DoStreamRequest(GlobalData.GetResUri(string.Format("avatars/{0}", GlobalData.CurrentUserName)), EnuHttpMethod.Get);
            }
            catch (ClientException) //忽略获取失败的错误
            {
                return;
            }

            if (stm == null)
            {
                imgAvatar.Source = null;
                return;
            }

            SetAvatarImage(stm);
        }
Esempio n. 7
0
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string   pwd     = Md5.MD5TwiceEncode("123456");
            Login_VM vmLogin = DataContext as Login_VM;

            if (vmLogin == null)
            {
                return;
            }

            if (vmLogin.IsModelValid())
            {
                WebApiClientHelper.UserName = vmLogin.UserName;
                WebApiClientHelper.Password = vmLogin.Password;
                UserInfo_API_Get userInfo;
                try
                {
                    userInfo = WebApiClientHelper.DoJsonRequest <UserInfo_API_Get>(GlobalData.GetResUri("entrance"), EnuHttpMethod.Get);
                }
                catch (ClientException ex)
                {
                    vmLogin.SetExtraError(ex.Message);
                    return;
                }

                //验证成功,记录用户信息
                GlobalData.CurrentUserName = userInfo.UserName;
                GlobalData.CurrentDispName = userInfo.RealName;
                GlobalData.CurrentRole     = userInfo.Role;

                //转入主窗口
                MainWindow main = new MainWindow();
                Application.Current.MainWindow = main;
                Close();
                main.Show();
            }
        }
        private bool LoadAllUsersInfo(bool bShowSuccessfulPopup = false)
        {
            //加载数据
            IEnumerable <UserInfo_API_Get> uisToGet;

            try
            {
                uisToGet =
                    WebApiClientHelper.DoJsonRequest <IEnumerable <UserInfo_API_Get> >(GlobalData.GetResUri("usersinfo"),
                                                                                       EnuHttpMethod.Get);
            }
            catch (ClientException ex)
            {
                Commands.ShowPopupAlert.Execute(new ShowPopupAlertParam {
                    AlertMessage = ex.Message, AlertType = EnuPopupAlertType.Error
                }, this);
                return(false);
            }

            IEnumerable <UserInfo_VM> uis_vm = Mapper.Map <IEnumerable <UserInfo_VM> >(uisToGet);

            foreach (var ui in uis_vm)
            {
                ui.ModelType = EnuModelType.View;
            }
            gridAllUsersInfoTab.DataContext = uis_vm;

            if (bShowSuccessfulPopup)
            {
                Commands.ShowPopupAlert.Execute(new ShowPopupAlertParam {
                    AlertMessage = "刷新所有用户信息成功.", AlertType = EnuPopupAlertType.Info
                }, this);
            }

            return(true);
        }