using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Controls;
using System.Windows;
using PdmSwPlugin.Common.Entity;
using System.Threading.Tasks;
using VersionControl;
using PdmSwPlugin.Common.Util.Http;
using PdmSwPlugin.Common.Entity.System;
using PdmSwPlugin.Commmon.Util.UI;
using PdmSwPlugin.Common.Util;
using PdmSwPlugin.Common.Interface;
using SolidWorks.Interop.sldworks;
using System.Windows.Threading;
namespace PdmSwPlugin.Main
{
///
/// MainControl.xaml 的交互逻辑
/// UI 相关
///
public partial class MainControl : UserControl, ISwAppSetter
{
#region 控件
#endregion
#region 检查更新相关
///
/// 检查更新
///
public async void CheckUpdate()
{
// 主进程ID,自动更新时要Kill
int mainProcessId = Process.GetCurrentProcess().Id;
try
{
Dictionary datas = new Dictionary
{
{
"params", new List>
{
new Dictionary
{
{"appId", PluginConst.AppId},
{"currentVersion", PluginConst.Version},
}
}
}
};
Result> result =
await HttpClientCreator.PostAsyncAction>("plugin/openApi/checkUpdate", datas);
List updates = result.HandleResult();
if (updates != null && updates.Count > 0)
{
PluginInfo update = updates[0];
if (update.required)
{
DoUpdateNotRequired(mainProcessId);
}
else
{
DoUpdateNotRequired(mainProcessId);
}
}
}
catch (Exception e)
{
Logger.Error("检查更新错误!", e);
this.Error("检查更新错误!");
}
}
///
/// 非必要更新
///
/// SoliWorks进程ID
private void DoUpdateNotRequired(int mainProcessId)
{
string updaterPath = "AutoUpdater\\AutoUpdater.exe";
string exePath = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName;
string exeFileName = $"{exePath}\\{updaterPath}";
// 非必要更新
new Task(() =>
{
MessageBoxResult dr = MessageBox.Show("检测到插件更新,是否立刻更新?(请注意保存当前工作)", "插件更新", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (dr == MessageBoxResult.OK)
{
Process updaterProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = exeFileName,
Arguments = mainProcessId + ""
}
};
updaterProcess.Start();
}
}).Start();
}
///
/// 当前版本写入注册表
///
private void WriteVersionToReg()
{
try
{
// 加载完先写版本注册表,保证版本统一,版本写入失败提示并退出
VersionUtil.SetPluginVersion(PluginConst.AppId, PluginConst.Version);
}
catch (Exception ex)
{
this.Error("插件版本写入失败!");
Logger.Error("Plugin version write to reg failed!", ex);
}
}
///
/// 更新自动更新程序
///
public void UpdateAutoUpdater()
{
try
{
string dllPath = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName;
string waitingPath = Path.Combine(dllPath, "WaitingUpdate");
if (Directory.Exists(waitingPath))
{
// 如果存在WaitingUpdate文件夹,把这个文件夹重命名为AutoUpdater
string targetPath = Path.Combine(dllPath, "AutoUpdater");
string delPath = Path.Combine(dllPath, "AutoUpdaterBak");
Directory.Move(targetPath, delPath);
Directory.Move(waitingPath, targetPath);
Directory.Delete(delPath, true);
}
}
catch (Exception ex)
{
this.Error($"更新自动更新程序失败!{ex.StackTrace}");
Logger.Error("Update AutoUpdater failed.", ex);
}
}
#endregion
///
/// 自动登录
///
///
///
private void AutoLogin(Action callback, Action reject)
{
MaskAdorner.ShowMask(mainBorder, "登陆中...");
Task.Run(() =>
{
try
{
if (GlobalConfig.ClassicLogin)
{
callback(null);
return;
}
Result res = HttpClientCreator.GetSyncAction("openApi/wpf/simpleLogin", new PdmUser
{
username = System.Environment.UserName,
appId = PluginConst.AppId,
pluginVersion = PluginConst.Version
});
PdmUser user = res.HandleResult();
callback(user);
}
catch (Exception e)
{
reject(e);
}
finally
{
MaskAdorner.HideMask(mainBorder);
}
});
}
///
/// 加载插件主Tab
///
public void LoginSuccess()
{
// 自动登录成功
clientCreator = new HttpClientCreator();
tabControl = new TabControl();
AddPlugin();
mainBorder.Child = tabControl;
if (tabControl.Items.Count > 0)
{
TabItem tabItem = tabControl.Items[0] as TabItem;
tabItem.Focus();
(tabItem.Content as UIElement).Focus();
}
}
public void MainControl_Loaded(object sender, RoutedEventArgs e)
{
// 加载完先把版本写入注册表
WriteVersionToReg();
// 然后更新AutoUpdater
UpdateAutoUpdater();
// 最后检查更新
CheckUpdate();
AutoLogin(
(user) =>
{
if (user != null)
{
Dispatcher.Invoke(() =>
{
PdmUser.SetLoginUser(user);
if (PdmUser.HasLogin())
{
LoginSuccess();
}
});
}
},
(ex) =>
{
Logger.Error("登陆失败!", ex);
this.Error($"登陆失败! {ex.Message}");
});
}
///
/// 登陆点击事件
///
///
///
private void Button_Click(object sender, RoutedEventArgs e)
{
MaskAdorner.ShowMask(mainBorder, "登陆中...");
PdmUser param = new PdmUser
{
username = usernameTextBlock.Text,
password = passwordTextBlock.Password,
appId = PluginConst.AppId,
pluginVersion = PluginConst.Version
};
Task.Run(() =>
{
try
{
Result res = HttpClientCreator.PostSyncAction("openApi/wpf/login", param);
PdmUser user = res.HandleResult();
Dispatcher.Invoke(() =>
{
PdmUser.SetLoginUser(user);
if (PdmUser.HasLogin())
{
LoginSuccess();
}
});
}
catch (Exception ex)
{
Logger.Error("登陆失败!", ex);
this.Error($"登陆失败! {ex.Message}");
}
finally
{
MaskAdorner.HideMask(mainBorder);
}
});
}
public void SetSwApp(SldWorks SwApp)
{
this.SwApp = SwApp;
}
}
}