using KGIS.Framework.Utils; using Kingo.KMapSDK; using Kingo.PluginServiceInterface.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; namespace Kingo.PluginServiceInterface.Helper { public class UserLoginHelper : IUserLogin { /// /// 用户登录 /// /// 用户名 /// 密码 /// public bool UserLogin(string UserName, string Password) { bool b = false; try { var requestParam = new { username = UserName, password = SHA(Password) }; ResultInfo requestData = HttpHelper.PostResponse>(GetAppsetingValueByKey("ApiHost") + "user/UserLoginJK", requestParam); if (requestData != null && requestData.Code == 200) { //获取登录信息后,将用户信息写入配置文件 UpdateAppsettingValueByKey("UserName", requestData.Data.username); if (requestData.Data.userid > 0 && !string.IsNullOrEmpty(requestData.Data.userid.ToString())) { UpdateAppsettingValueByKey("UserId", requestData.Data.userid.ToString()); } UpdateAppsettingValueByKey("YWLX", string.Join(",", requestData.Data.ownedbussiness)); b = true; } return b; } catch (Exception ex) { LogAPI.Debug("UserLogin异常:" + ex.Message); LogAPI.Debug("UserLogin异常:" + ex.StackTrace); } return b; } /// /// 从AppSettings中获取指定key的值 /// /// /// public static string GetAppsetingValueByKey(string key) { try { XmlDocument doc = new XmlDocument(); string strPath = SysAppPath.GetConfigPath() + "UserLoginConfig.xml"; doc.Load(strPath); XmlNode xmlSysNode = doc.SelectSingleNode("configuration/AppSettings"); if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0) { foreach (XmlNode node in xmlSysNode.ChildNodes) { if (node.Attributes["Key"].Value == key) { return node.Attributes["Value"].Value; } } } return ""; } catch (Exception ex) { Console.WriteLine(ex.Message); return ""; } } /// /// 更新配置文件AppSetting值 /// /// /// /// public static void UpdateAppsettingValueByKey(string key, string value) { try { XmlDocument doc = new XmlDocument(); string strPath = SysAppPath.GetConfigPath() + "UserLoginConfig.xml"; doc.Load(strPath); XmlNode xmlSysNode = doc.SelectSingleNode("configuration/AppSettings"); if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0) { foreach (XmlNode node in xmlSysNode.ChildNodes) { if (node.Attributes["Key"].Value == key) { node.Attributes["Value"].Value = value; doc.Save(strPath); break; } } } } catch (Exception ex) { LogAPI.Debug("UpdateAppsettingValueByKey:" + ex.Message); LogAPI.Debug("UpdateAppsettingValueByKey:" + ex.StackTrace); throw ex; } } public static List GetAllYXGroupLayer() { List AllYXGroupLayer = new List(); try { string isShow = SysConfigsOprator.GetAppsetingValueByKey("IsShowSJYX"); if (isShow.Equals("true") || isShow.Equals("True") || isShow.Equals("TRUE")) { XmlDocument doc = new XmlDocument(); string strPath = SysAppPath.GetConfigPath() + "UserLoginConfig.xml"; doc.Load(strPath); XmlNode xmlSysNode = doc.SelectSingleNode("configuration/AppSettings/YXGroupLayer"); if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0) { foreach (XmlNode node in xmlSysNode.ChildNodes) { AllYXGroupLayer.Add(new ImageSlicing() { Name = node.Attributes["Name"].Value, Alias = node.Attributes["AliasName"].Value, Path = node.Attributes["PathUrl"].Value }); } } } return AllYXGroupLayer; } catch (Exception ex) { Console.WriteLine(ex.Message); return AllYXGroupLayer; } } public static string GetYXGroupLayerValueByAliasName(string aliasName) { try { XmlDocument doc = new XmlDocument(); string strPath = SysAppPath.GetConfigPath() + "UserLoginConfig.xml"; doc.Load(strPath); XmlNode xmlSysNode = doc.SelectSingleNode("configuration/AppSettings/YXGroupLayer"); if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0) { foreach (XmlNode node in xmlSysNode.ChildNodes) { if (node.Attributes["AliasName"].Value == aliasName) { return node.Attributes["PathUrl"].Value; } } } return ""; } catch (Exception ex) { LogAPI.Debug("GetYXGroupLayerValueByAliasName 异常:" + ex.Message); LogAPI.Debug("GetYXGroupLayerValueByAliasName 异常:" + ex.StackTrace); return ""; } } public static void UpdateYXGroupLayerValueByName(string name, string aliasName, string value) { bool isExit = false; try { XmlDocument doc = new XmlDocument(); string strPath = SysAppPath.GetConfigPath() + "UserLoginConfig.xml"; doc.Load(strPath); XmlNode xmlSysNode = doc.SelectSingleNode("configuration/AppSettings/YXGroupLayer"); if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0) { if (!string.IsNullOrEmpty(value) && value.Contains("@")) { value = value.Split('@')[0]; } foreach (XmlNode node in xmlSysNode.ChildNodes) { if (node.Attributes["Name"].Value == name) { isExit = true; node.Attributes["AliasName"].Value = aliasName; node.Attributes["PathUrl"].Value = value; doc.Save(strPath); break; } } if (!isExit) { XmlElement newNode = doc.CreateElement("WMTSLayer"); newNode.SetAttribute("Name", name); newNode.SetAttribute("AliasName", aliasName); newNode.SetAttribute("PathUrl", value); xmlSysNode.AppendChild(newNode); doc.Save(strPath); } } } catch (Exception ex) { LogAPI.Debug("UpdateAppsettingValueByKey:" + ex.Message); LogAPI.Debug("UpdateAppsettingValueByKey:" + ex.StackTrace); throw ex; } } public static void DeleteYXGroupLayerValueByName(string name) { try { XmlDocument doc = new XmlDocument(); string strPath = SysAppPath.GetConfigPath() + "UserLoginConfig.xml"; doc.Load(strPath); XmlNode xmlSysNode = doc.SelectSingleNode("configuration/AppSettings/YXGroupLayer"); if (xmlSysNode.ChildNodes != null && xmlSysNode.ChildNodes.Count > 0) { foreach (XmlNode node in xmlSysNode.ChildNodes) { if (node.Attributes["Name"].Value == name) { xmlSysNode.RemoveChild(node); doc.Save(strPath); return; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// SHA1哈希算法数字签字 /// /// /// private string SHA(string str) { byte[] bytes = Encoding.UTF8.GetBytes(str); StringBuilder sb = new StringBuilder(); // 创建MD5哈希算法对象 using (var md5 = System.Security.Cryptography.SHA1.Create()) { // 计算输入数据的哈希值 byte[] hash = md5.ComputeHash(bytes); // 将哈希值转换为十六进制字符串 for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("x2")); } } return sb.ToString(); } } }