/****************************** * 说明:系统全局类 * 创建人:龚宇超 * 创建日期:2017-08-09 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Management; using DevExpress.XtraTab; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Net.NetworkInformation; using System.Net.Sockets; namespace Lskj.Model { /// /// 系统全局类 /// public sealed class ERPInfo { private static ERPInfo _instance = null; /// /// ERPInfo静态单例对象 /// /// The instance. public static ERPInfo Instance { get { if (_instance == null) { _instance = new ERPInfo(); } return _instance; } } /// /// 是否为超级管理员 /// /// true if this instance is user manager; otherwise, false. public bool IsUserManager { get { return UserManager.Equals(UserName); } } /// /// 登录帐套 /// public string AccountBook; /// /// 输入的密码 /// public string InPassWord; /// /// 员工ID /// public string UserId; /// /// 员工编号 /// public string LoginAccount; /// /// 员工姓名 /// public string UserName; /// /// 密码 /// public string Password; /// /// 菜单GroupId /// public string GroupId; /// /// 子系统 /// public string SubSysId; /// /// 子系统名称 /// public string SubSysName; /// /// 主界面page界面 /// public XtraTabControl PageControl; private readonly object _modulePageCallbackSync = new object(); private readonly List> _modulePageSelectedCallbacks = new List>(); /// /// 顶层页面生命周期服务。旧 WinForms 主程序保持为空并继续使用 /// PageControl;WPF 主程序注入自己的页面宿主实现。 /// public IModulePageHost ModulePageHost; public XtraTabPage CurrentModulePage { get { if (ModulePageHost != null) return ModulePageHost.SelectedPage; return PageControl == null ? null : PageControl.SelectedTabPage; } set { // Keep the original assignment-based API working for legacy // modules. In the classic WinForms shell this forwards to // the real PageControl; when WPF injects ModulePageHost it // selects the page through that host instead of creating a // second XtraTabControl. SelectModulePage(value); } } public IEnumerable ModulePages { get { if (ModulePageHost != null) return ModulePageHost.Pages; if (PageControl == null) return new XtraTabPage[0]; return PageControl.TabPages.Cast(); } } /// /// Indicates whether a top-level module page surface has been /// initialized. The classic shell exposes its XtraTabControl while /// WPF exposes IModulePageHost; callers can use this without reaching /// into either visual implementation. /// public bool HasModulePageSurface { get { return ModulePageHost != null || PageControl != null; } } public void AddModulePage(XtraTabPage page) { if (page == null) return; if (ModulePageHost != null) { ModulePageHost.AddPage(page); return; } if (PageControl != null && !PageControl.TabPages.Contains(page)) PageControl.TabPages.Add(page); } public bool RemoveModulePage(XtraTabPage page) { if (page == null) return false; if (ModulePageHost != null) return ModulePageHost.RemovePage(page); if (PageControl == null || !PageControl.TabPages.Contains(page)) return false; PageControl.TabPages.Remove(page); return true; } public bool SelectModulePage(XtraTabPage page) { if (page == null) return false; if (ModulePageHost != null) return ModulePageHost.SelectPage(page); if (PageControl == null || !PageControl.TabPages.Contains(page)) return false; PageControl.SelectedTabPage = page; return true; } public bool ContainsModulePage(XtraTabPage page) { if (page == null) return false; if (ModulePageHost != null) return ModulePageHost.ContainsPage(page); return PageControl != null && PageControl.TabPages.Contains(page); } /// /// Returns the stable logical identifier used to bind a legacy page to /// its WPF tab. The existing Tag value is retained whenever the old /// module already assigned one; a generated value is written only for /// an otherwise unidentified page. /// public string GetModulePageId(XtraTabPage page) { if (page == null) return string.Empty; string pageId = Convert.ToString(page.Tag); if (string.IsNullOrWhiteSpace(pageId)) { pageId = "legacy-module-" + Guid.NewGuid().ToString("N"); page.Tag = pageId; } return pageId; } /// /// Gets the logical identifier of the selected top-level page without /// exposing the shell's visual tab-control implementation. /// public string CurrentModulePageId { get { return GetModulePageId(CurrentModulePage); } } /// /// Enumerates top-level page identifiers for modules that only need to /// compare or select pages and should not reach PageControl.TabPages. /// public IEnumerable GetModulePageIds() { return ModulePages.Select(GetModulePageId).ToArray(); } /// /// Selects a top-level page by its logical identifier. Both the WPF /// host and the classic WinForms PageControl use the same fallback /// path as the original XtraTabPage-based API. /// public bool SelectModulePage(string pageId) { XtraTabPage page = FindModulePage(pageId); return page != null && SelectModulePage(page); } /// /// Removes a top-level page by its logical identifier. /// public bool RemoveModulePage(string pageId) { XtraTabPage page = FindModulePage(pageId); return page != null && RemoveModulePage(page); } /// /// Indicates whether the supplied logical identifier belongs to a /// top-level module page. This replaces checks based on /// page.Parent as XtraTabControl in migrated module code. /// public bool IsTopLevelModulePage(string pageId) { return FindModulePage(pageId) != null; } /// /// Registers a logical page-selection callback. This is intentionally /// a page-service callback rather than a subscription to a concrete /// WinForms or WPF tab-control event. Dispose the returned token when /// the module closes so the callback cannot retain the module. /// public IDisposable RegisterModulePageSelected(Action callback) { if (callback == null) throw new ArgumentNullException("callback"); lock (_modulePageCallbackSync) { _modulePageSelectedCallbacks.Add(callback); } return new ModulePageCallbackRegistration(this, callback); } /// /// Registers a callback for one logical module page. This overload is /// the source-compatible replacement for code that used to subscribe /// to the top-level PageControl.SelectedPageChanged event and then /// filter by the selected page. The callback receives no WinForms /// event arguments, so the same module code works when the WPF shell /// owns the visible tab while the direct WinForms shell still uses its /// original XtraTabControl. /// public IDisposable RegisterModulePageSelected( string modulePageId, Action callback) { if (callback == null) throw new ArgumentNullException("callback"); string expectedPageId = (modulePageId ?? string.Empty).Trim(); if (string.IsNullOrWhiteSpace(expectedPageId)) throw new ArgumentException( "模块页面标识不能为空。", "modulePageId"); return RegisterModulePageSelected( selectedPageId => { if (string.Equals( selectedPageId, expectedPageId, StringComparison.Ordinal)) { callback(); } }); } /// /// Notifies logical page-selection subscribers. The WPF page host /// calls this after its own selection state is updated; the classic /// MainPanelControlEx calls it from its existing SelectedPageChanged /// handler. No visual control event is introduced in the WPF path. /// public void NotifyModulePageSelected(XtraTabPage page) { if (page == null) return; string pageId = GetModulePageId(page); Action[] callbacks; lock (_modulePageCallbackSync) { callbacks = _modulePageSelectedCallbacks.ToArray(); } foreach (Action callback in callbacks) { try { callback(pageId); } catch (Exception exception) { System.Diagnostics.Trace.WriteLine( "旧模块页面回调执行失败:" + exception); } } } private XtraTabPage FindModulePage(string pageId) { if (string.IsNullOrWhiteSpace(pageId)) return null; foreach (XtraTabPage page in ModulePages) { if (string.Equals( GetModulePageId(page), pageId, StringComparison.Ordinal)) { return page; } } return null; } private void UnregisterModulePageSelected(Action callback) { if (callback == null) return; lock (_modulePageCallbackSync) { _modulePageSelectedCallbacks.Remove(callback); } } private sealed class ModulePageCallbackRegistration : IDisposable { private ERPInfo _owner; private readonly Action _callback; public ModulePageCallbackRegistration( ERPInfo owner, Action callback) { _owner = owner; _callback = callback; } public void Dispose() { ERPInfo owner = _owner; if (owner == null) return; _owner = null; owner.UnregisterModulePageSelected(_callback); } } /// /// 主界面page界面字典 /// public Dictionary ModuleForms; /// /// 登录人联系电话 /// public string UserLinkPhone; /// /// 主界面page界面 /// public Form MainControl; /// /// 主页面焦点 /// public PictureBox pic_search; /// /// 是否阻止回车事件 /// public bool IsExecute = false; /// /// 是否根据模块id判断是否设置不能同时打开多个相同模块 /// public bool SingleOpenMode = false; /// /// 登录IP /// public string LoginIP { get { try { return Dns.GetHostAddresses(WindowName)[0] + ""; } catch (Exception) { } return string.Empty; } } /// /// 登录IP /// public string LoginIPV4 { get { try { string text = string.Empty; IPAddress[] ip = Dns.GetHostAddresses(WindowName); foreach (IPAddress address in ip) { if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { text = address + ""; } } return text; } catch (Exception) { } return ""; } } /// /// 用户电脑名 /// public string WindowName { get { return Dns.GetHostName(); } } /// /// 子系统数量 /// public int SubMenuCount; /// /// 超级管理员 /// public string UserManager = "管理员"; /// /// 用户皮肤字段 /// /// The name of the skin. public string SkinFieldName = "SkinName"; /// /// 默认新增判断行 /// public string isAddRows = "xxxxx_isAddRows"; /// /// 用户皮肤 /// public string SkinName; /// /// 模块编号 /// public string WorkModid; /// /// 当前选中的方案id /// /// The mac address. public string SeriesId; /// /// 车间 /// public string WorkCJ; /// /// 车间ID /// public string WorkCJId; /// /// 机台 /// public string WorkJT; /// /// 机台ID /// public string WorkJTId; /// /// 班次 /// public string WorkBZ; /// /// 班次ID /// public string WorkBZId; /// /// 班组 /// public string ClassName; /// /// The work no /// public string WorkNo; /// /// 是否显示 /// public static bool ShowMsg = true; /// /// 当前登录Mac地址 /// /// The mac address. public string MacAddress { get { if (!string.IsNullOrWhiteSpace(RecordMAC)) return RecordMAC; return GetMacAddress(); } } /// /// 客户名称 /// public string ClientName = ""; /// /// 如果无法连接则后面不在继续连接了 /// public bool bSmartClientIsOK = true; /// /// 文件升级ip /// public string UpdateIP = ""; /// /// 登录结果 /// public bool LoginResult = false; /// /// Mrp当前切换状态 /// public string MrpTag = ""; /// /// 记录本机MAC地址(只记录初始获取的,每次获取导致速度很慢) /// public string RecordMAC = ""; /// /// 获取本机MAC地址 /// /// 本机MAC地址 private string GetMacAddress() { try { string fallbackMac = string.Empty; NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { if (ni == null) { continue; } if (ni.OperationalStatus != OperationalStatus.Up) { continue; } if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback || ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel) { continue; } PhysicalAddress physicalAddress = ni.GetPhysicalAddress(); if (physicalAddress == null || physicalAddress.GetAddressBytes().Length == 0) { continue; } string mac = FormatMacAddress(physicalAddress); if (string.IsNullOrEmpty(fallbackMac)) { fallbackMac = mac; } IPInterfaceProperties properties = ni.GetIPProperties(); foreach (UnicastIPAddressInformation ipInfo in properties.UnicastAddresses) { if (ipInfo.Address.AddressFamily != AddressFamily.InterNetwork) { continue; } if (!string.IsNullOrEmpty(LoginIPV4) && ipInfo.Address.ToString() == LoginIPV4) { RecordMAC = mac; return mac; } } } RecordMAC = fallbackMac; return fallbackMac; } catch { return ""; } } private string FormatMacAddress(PhysicalAddress physicalAddress) { byte[] bytes = physicalAddress.GetAddressBytes(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { if (i > 0) { sb.Append("-"); } sb.Append(bytes[i].ToString("X2")); } return sb.ToString(); } /// /// 加密狗id /// public int[] keyHandles = new int[8]; /// /// 加密狗编号 /// public int[] keyNumber = new int[8]; /// /// 是否需要U盾密码验证 /// public bool isDogVerifyPassword = true; /// /// U盾密码默认值 /// public string dogVerifyPasswordDefault { get { byte[] bytes = new byte[256]; for (int i = 0; i < bytes.Length; i++) { bytes[i] = 255; } return Encoding.Default.GetString(bytes); } } /// /// 当前选中页签模块编号 /// public string selectFormModleId; #region 判断windows版本 [StructLayout(LayoutKind.Sequential)] private struct OSVERSIONINFOEX { public int dwOSVersionInfoSize; public int dwMajorVersion; public int dwMinorVersion; public int dwBuildNumber; public int dwPlatformId; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szCSDVersion; } [DllImport("ntdll.dll", SetLastError = true)] private static extern int RtlGetVersion(ref OSVERSIONINFOEX versionInfo); private static OSVERSIONINFOEX GetOSVersion() { OSVERSIONINFOEX versionInfo = new OSVERSIONINFOEX(); versionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); int result = RtlGetVersion(ref versionInfo); if (result != 0) { throw new Exception("Failed to get OS version"); } return versionInfo; } public string SystemVersion { get { string systemVersion = ""; OSVERSIONINFOEX osVersionInfo = GetOSVersion(); // 判断具体的Windows版本 if (osVersionInfo.dwPlatformId == 2) // 2 is VER_PLATFORM_WIN32_NT { if (osVersionInfo.dwMajorVersion == 10) { systemVersion = "Windows 10 或 Windows 11"; } else { switch (osVersionInfo.dwMajorVersion) { case 6: switch (osVersionInfo.dwMinorVersion) { case 3: systemVersion = "Windows 8.1"; break; case 2: systemVersion = "Windows 8"; break; case 1: systemVersion = "Windows 7"; break; case 0: systemVersion = "Windows Vista"; break; } break; case 5: switch (osVersionInfo.dwMinorVersion) { case 2: systemVersion = "Windows Server 2003; Windows XP x64 Edition"; break; case 1: systemVersion = "Windows XP"; break; case 0: systemVersion = "Windows 2000"; break; } break; default: systemVersion = "未知版本"; break; } } } else { systemVersion = "非Windows NT系列操作系统"; } return systemVersion; } } #endregion /// /// 使用winfrom自带浏览器 /// public bool PrimitiveBrowser; /// /// 当前程序使用的语言 /// public string LanguageName; /// /// 是否改变存储过程参数类型(越南语要用Nvarchar) /// public bool ChangeParameterType { get { if (!string.IsNullOrWhiteSpace(this.LanguageName)) { return true; } return false; } } /// /// 页面水印层 /// public Form WatermarkForm; /// /// 单据水印 草稿替换值 /// public string DraftReplacementValue=""; /// /// 是否有可翻译的文本 /// public bool Translatable; /// /// 翻译文本存放字典 /// public Dictionary ContrastiveLanguage; /// /// 传入文本,获取对应语言的翻译文本 /// /// /// public string GetTranslatedText(string text) { string value = text; if (this.ContrastiveLanguage.ContainsKey(text)) { value = this.ContrastiveLanguage[text]; } return value; } } }