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
|
{
|
/// <summary>
|
/// MainControl.xaml 的交互逻辑
|
/// UI 相关
|
/// </summary>
|
public partial class MainControl : UserControl, ISwAppSetter
|
{
|
|
#region 控件
|
#endregion
|
|
#region 检查更新相关
|
/// <summary>
|
/// 检查更新
|
/// </summary>
|
public async void CheckUpdate()
|
{
|
// 主进程ID,自动更新时要Kill
|
int mainProcessId = Process.GetCurrentProcess().Id;
|
try
|
{
|
Dictionary<string, object> datas = new Dictionary<string, object>
|
{
|
{
|
"params", new List<Dictionary<string, string>>
|
{
|
new Dictionary<string, string>
|
{
|
{"appId", PluginConst.AppId},
|
{"currentVersion", PluginConst.Version},
|
}
|
}
|
}
|
};
|
|
Result<List<PluginInfo>> result =
|
await HttpClientCreator.PostAsyncAction<List<PluginInfo>>("plugin/openApi/checkUpdate", datas);
|
List<PluginInfo> 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("检查更新错误!");
|
}
|
}
|
|
/// <summary>
|
/// 非必要更新
|
/// </summary>
|
/// <param name="mainProcessId">SoliWorks进程ID</param>
|
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();
|
}
|
|
/// <summary>
|
/// 当前版本写入注册表
|
/// </summary>
|
private void WriteVersionToReg()
|
{
|
try
|
{
|
// 加载完先写版本注册表,保证版本统一,版本写入失败提示并退出
|
VersionUtil.SetPluginVersion(PluginConst.AppId, PluginConst.Version);
|
}
|
catch (Exception ex)
|
{
|
this.Error("插件版本写入失败!");
|
Logger.Error("Plugin version write to reg failed!", ex);
|
}
|
}
|
|
/// <summary>
|
/// 更新自动更新程序
|
/// </summary>
|
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
|
|
|
/// <summary>
|
/// 自动登录
|
/// </summary>
|
/// <param name="callback"></param>
|
/// <param name="reject"></param>
|
private void AutoLogin(Action<PdmUser> callback, Action<Exception> reject)
|
{
|
MaskAdorner.ShowMask(mainBorder, "登陆中...");
|
Task.Run(() =>
|
{
|
try
|
{
|
if (GlobalConfig.ClassicLogin)
|
{
|
callback(null);
|
return;
|
}
|
Result<PdmUser> res = HttpClientCreator.GetSyncAction<PdmUser>("openApi/wpf/simpleLogin", new PdmUser
|
{
|
username = System.Environment.UserName,
|
appId = PluginConst.AppId,
|
pluginVersion = PluginConst.Version
|
});
|
PdmUser user = res.HandleResult();
|
user.appId = PluginConst.AppId;
|
user.pluginVersion = PluginConst.Version;
|
callback(user);
|
}
|
catch (Exception e)
|
{
|
reject(e);
|
}
|
finally
|
{
|
MaskAdorner.HideMask(mainBorder);
|
}
|
});
|
}
|
|
/// <summary>
|
/// 加载插件主Tab
|
/// </summary>
|
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}");
|
});
|
}
|
|
/// <summary>
|
/// 登陆点击事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
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<PdmUser> res = HttpClientCreator.PostSyncAction<PdmUser>("openApi/wpf/login", param);
|
PdmUser user = res.HandleResult();
|
user.appId = PluginConst.AppId;
|
user.pluginVersion = PluginConst.Version;
|
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;
|
}
|
}
|
}
|