/// <summary> /// restful路径方式GET 实体数据获取(同步),同时获取状态码,按状态码识别是否成功。 /// </summary> /// <param name="url">地址</param> /// <param name="statusCode">状态码</param> /// <param name="parms">参数值数组</param> /// <returns></returns> public static T GetWithInfo <T>(String app, String url, out CustomException exInfo, List <KeyValuePair <String, String> > parmList = null) { String result = GetStringWithInfo(app, url, out exInfo, parmList); Nullable <int> i; if (exInfo != null || result.Length == 0) { return(default(T)); } return(JsonConvert.DeserializeObject <T>(result)); }
/// <summary> /// 获取自定义格式缓存 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="cacheName">缓存名</param> /// <param name="url">服务地址</param> /// <param name="parms">服务参数</param> /// <returns></returns> public static List <T> GetFast <T>(String cacheName, String url, List <KeyValuePair <String, String> > parms = null) where T : IFastSerialize, new() { List <T> list = null; string remote_ver = GetRemoteVer(cacheName); // 1. 获取服务器缓存的版本 string file = EnvInfo.RunPath + "Cache\\" + cacheName + "(" + remote_ver + ").cache"; bool ok = false; if (File.Exists(file)) { try { LogHelper.Info(typeof(CacheHelper).FullName, "开始获取" + cacheName + "本地磁盘缓存"); byte[] cache_bytes = System.IO.File.ReadAllBytes(file); list = FastSerializeHelper.DeSerialize <T>(cache_bytes); LogHelper.Info(typeof(CacheHelper).FullName, "成功获取" + cacheName + "本地磁盘缓存"); ok = true; } catch (Exception ex) { LogHelper.Warn(typeof(CacheHelper).FullName, "获取" + cacheName + "本地磁盘缓存失败\r\n" + ex.Message + "\r\n" + ex.StackTrace); } } if (!ok) { try { LogHelper.Info(typeof(CacheHelper).FullName, "开始获取" + cacheName + "服务端数据"); CustomException ce = null; string result = HttpDataHelper.GetStringWithInfo("BASE", url, out ce, parms); if (ce != null) { throw ce; } list = JsonConvert.DeserializeObject <List <T> >(result); LogHelper.Info(typeof(CacheHelper).FullName, "开始序列化" + cacheName); List <byte> list_bytes = FastSerializeHelper.Serialize(list); LogHelper.Info(typeof(CacheHelper).FullName, "删除" + cacheName + "旧的缓存文件"); RemoveFile(cacheName); LogHelper.Info(typeof(CacheHelper).FullName, "写入" + cacheName + "新的磁盘缓存文件"); File.WriteAllBytes(file, list_bytes.ToArray()); } catch (Exception ex) { LogHelper.Error(typeof(CacheHelper).FullName, "获取" + cacheName + "服务端数据失败\r\n" + ex.Message + "\r\n" + ex.StackTrace); } } return(list); }
/// <summary> /// GET 字符串数据获取(同步),同时获取状态码,按状态码识别是否成功。 /// </summary> /// <param name="url">地址</param> /// <param name="statusCode">状态码</param> /// <param name="parms">参数对列表</param> /// <returns></returns> public static String GetStringWithInfo(String app, String url, out CustomException ce, List <KeyValuePair <String, String> > parmList = null) { String result = String.Empty; String urlcomp = GetUrl(app, url); if (parmList != null && parmList.Count > 0) { urlcomp = AppendUrlWithParameter(urlcomp, parmList); } try { SetHeader(); HttpResponseMessage response = _HttpClient.GetAsync(urlcomp).Result; String statusCode = response.StatusCode.ToString(); result = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { GetToken(response); ce = null; } else if (statusCode.Equals("Conflict")) //业务错误 { CustomExceptionInfo info = JsonConvert.DeserializeObject <CustomExceptionInfo>(result); ce = new CustomException(info); } else if (statusCode.Equals("PreconditionFailed")) //jwt过期 { if (DoRelogin()) { return(GetStringWithInfo(app, url, out ce, parmList)); //产生新的jwt,方法重新执行 } else { ce = new CustomException(statusCode, "at " + urlcomp, ""); } } else //其它错误 { ce = new CustomException(statusCode, "at " + urlcomp, ""); } } catch (Exception e) { ce = new CustomException("服务异常", e.Message + " [" + urlcomp + "]", e.StackTrace); RemoveHeader(); return(String.Empty); } RemoveHeader(); return(result); }
/// <summary> /// 系统登录 /// </summary> /// <param name="branch_id">机构ID</param> /// <param name="system_code">子系统代码</param> /// <param name="password">口令</param> /// <returns>返回错误信息,空为成功</returns> public static string Login(int branch_id, string system_code, string password) { // 完成登录,获取员工相关信息 // 1. 验证用户口令并获取员工信息记录中的基本内容。 // 2. 判断子系统使用权限 // 3. 获取普通角色、行为角色 List <KeyValuePair <string, string> > parms = new List <KeyValuePair <string, string> >(); parms.Add(new KeyValuePair <string, string>("branchCode", EnvInfo.BranchCode)); parms.Add(new KeyValuePair <string, string>("empCode", EmpInfo.Code)); parms.Add(new KeyValuePair <string, string>("password", DataCryptoHelper.MD5EncryptString(password))); parms.Add(new KeyValuePair <string, string>("ip", EnvInfo.ComputerIp)); CustomException ce = null; int loginRst = int.Parse(HttpDataHelper.HttpPostFormUrlWithInfo("BASE", "/sys/login", out ce, parms)); switch (loginRst) { case -1: return("人员不存在!"); case -2: return("密码错误!"); case 1: // 4. 设置环境信息对应的子系统代码、名称 SelectSystem(system_code, true); return(string.Empty); default: return("服务异常"); } /* TODO 参考 * EmpInfo.Name = EmpInfo.Id.ToString(); * EmpInfo.BizEmpName = " 业务" + EmpInfo.Id.ToString(); * EmpInfo.InputCode = "1"; * * String loginUrl = HttpDataHelper.getUrl("SYSTEM"); * if (loginUrl != null) * { * * Dictionary<String, String> dic = HttpDataHelper.PathGet<Dictionary<String, String>>(loginUrl + "login", * new String[] { branch_id.ToString(), system_code, EmpInfo.Id.ToString(), password }); * * //workplace subSystem判断 * * } */ }
/// <summary> /// POST方法(解压缩方法),json传入RequestBody /// </summary> /// <param name="url"></param> /// <param name="json"></param> /// <returns></returns> public static String HttpPostGZipWithInfo(String app, String url, out CustomException ce, String json) { String result = String.Empty; var content = new StringContent(json); var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; String urlcomp = GetUrl(app, url); //创建HttpClient(注意传入HttpClientHandler) using (var http = new HttpClient(handler)) { SetHeader(); var response = http.PostAsync(urlcomp, content).Result; //await异步等待回应 //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) String statusCode = response.StatusCode.ToString(); result = response.Content.ReadAsStringAsync().Result;//result就是返回的结果。 if (response.IsSuccessStatusCode) { GetToken(response); ce = null; } else if (statusCode.Equals("Conflict")) //业务错误 { CustomExceptionInfo info = JsonConvert.DeserializeObject <CustomExceptionInfo>(result); ce = new CustomException(info); } else if (statusCode.Equals("PreconditionFailed")) //jwt过期 { if (DoRelogin()) { return(HttpPostGZipWithInfo(app, url, out ce, json)); //产生新的jwt,方法重新执行 } else { ce = new CustomException(statusCode, "at " + urlcomp, ""); } } else //其它错误 { ce = new CustomException(statusCode, "at " + urlcomp, ""); } RemoveHeader(); } return(result); }
/// <summary> /// POST方法,json传入RequestBody /// </summary> /// <param name="url"></param> /// <param name="json"></param> /// <returns></returns> public static String HttpPostWithInfo(String app, String url, out CustomException ce, String json) { String result = String.Empty; String urlcomp = GetUrl(app, url); var content = new StringContent(json, Encoding.UTF8, "application/json"); //Content-Type设置 try { SetHeader(); HttpResponseMessage response = _HttpClient.PostAsync(urlcomp, content).Result; String statusCode = response.StatusCode.ToString(); result = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { GetToken(response); ce = null; } else if (statusCode.Equals("Conflict")) //业务错误 { CustomExceptionInfo info = JsonConvert.DeserializeObject <CustomExceptionInfo>(result); ce = new CustomException(info); } else if (statusCode.Equals("PreconditionFailed")) //jwt过期 { if (DoRelogin()) { return(HttpPostWithInfo(app, url, out ce, json)); //产生新的jwt,方法重新执行 } else { ce = new CustomException(statusCode, "at " + urlcomp, ""); } } else //其它错误 { ce = new CustomException(statusCode, "at " + urlcomp, ""); } } catch (Exception e) { ce = new CustomException("服务异常", e.Message + " [" + urlcomp + "]", e.StackTrace); RemoveHeader(); return(String.Empty); } RemoveHeader(); return(result); }
/// <summary> /// 判别是否有功能点权限 /// </summary> /// <param name="container_code">容器代码</param> /// <param name="code">代码</param> /// <param name="name">名称</param> /// <param name="describe">描述信息</param> /// <returns>是否有权限</returns> public static bool GetFunctionPointAuthority(string container_code, string code, string name, string describe) { if (String.IsNullOrEmpty(code)) { return(false); } // 判别是否有功能点记录,返回记录数(>0有权) var parmList = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("fpCode", container_code + "-" + code), new KeyValuePair <string, string>("empId", EmpInfo.Id.ToString()), new KeyValuePair <string, string>("roles", EmpInfo.Roles) }; CustomException ce = null; CDictFunctionPoint fp = HttpDataHelper.GetWithInfo <CDictFunctionPoint>("BASE", "/setup/funcpoint", out ce, parmList); return(fp != null && fp.Code.Length > 0); }
/// <summary> /// 判别人员是否有职责性质 /// </summary> /// <param name="kind">职责性质</param> /// <returns></returns> //public static bool HasKind(int kind) //{ // if (KindList != null) // { // return KindList.Contains(kind); // } // return false; //} #endregion #region 功能方法 /// <summary> /// 通过代码获取员工ID及可用系统信息 /// </summary> /// <param name="code">员工代码</param> /// <returns>返回错误信息,空为成功</returns> public static string QueryEmpByCode(string code) { //EmpInfo.CanUseSystemList = new List<BDicSystem>(); if (code.IsNullOrEmpty()) { EmpInfo.Id = 0; EmpInfo.Code = string.Empty; return("工号不可为空!"); } // 依据员工代码获取员工ID,再获取可使用的子系统 var parmList = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("empCode", code) }; CustomException ce = null; int emp_id = HttpDataHelper.GetWithInfo <int>("BASE", "/sys/empid", out ce, parmList); if (emp_id < 0) { return("不存在工号为" + code + "的人员!"); } parmList = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("empId", emp_id.ToString()) }; List <BDictSystem> ls = HttpDataHelper.GetWithInfo <List <BDictSystem> >("BASE", "/sys/system", out ce, parmList); if (ls == null || ls.Count == 0) { return("工号" + code + "的人员没有可用的子系统!"); } EmpInfo.CanUseSystemList = ls; EmpInfo.Code = code; EmpInfo.Id = emp_id; return(string.Empty); }
/// <summary> /// 获取上一次登录的系统代码 /// </summary> /// <returns></returns> public static string GetLastSystemCode() { string code = string.Empty; if (EmpInfo.Id > 0) { var parmList = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("empId", EmpInfo.Id.ToString()) }; CustomException ce = null; // 获取上一次登录的系统代码 code = HttpDataHelper.GetStringWithInfo("BASE", "/sys/systemdefault", out ce, parmList); if (ce != null) { MessageHelper.ShowError("获取默认子系统失败!\r\n" + ce.Info + " " + ce.InnerMessage); return(null); } } return(code); }
public static void SetEmpInfo(BDictEmp emp, List <string> roleList, List <CDictAction> actionList) { EmpInfo.Name = emp.Name; EmpInfo.Ceid = emp.Ceid; EmpInfo.DeptId = emp.DeptId; EmpInfo.DeptName = emp.DeptName; EmpInfo.BizDeptId = emp.BizDeptId; EmpInfo.BizDeptName = emp.BizDeptName; EmpInfo.GroupId = emp.GroupId; EmpInfo.GroupName = emp.GroupName; EmpInfo.TitlesId = emp.TitlesId; EmpInfo.TitlesName = emp.TitlesName; EmpInfo.RoleList = roleList; EmpInfo.ActionRoleList = actionList; var parmList = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("empId", EmpInfo.Id.ToString()), new KeyValuePair <string, string>("parmName", "DEF_INPUT") }; CustomException ce = null; String defInput = HttpDataHelper.GetStringWithInfo("BASE", "/sys/parameteremp", out ce, parmList); if (ce != null) { MessageHelper.ShowError("获取默认输入法失败!\r\n" + ce.Info + " " + ce.InnerMessage); } if (String.IsNullOrEmpty(defInput)) { EmpInfo.InputChoice = "1"; } else { EmpInfo.InputChoice = defInput; } }
/// <summary> /// 通过配置文件设置机构信息 /// </summary> private static void SetBranchInfo() { // 获取机构ID,默认为1. string sid = ConfigurationManager.AppSettings.Get("BranchId"); if (!sid.IsNullOrEmpty() && sid.IsInt()) { EnvInfo.BranchId = int.Parse(sid); } else { EnvInfo.BranchId = 1; } string key = ConfigurationManager.AppSettings.Get("UserSerial").Reversal(); // 获取用户名称 string rets = ConfigurationManager.AppSettings.Get("UserName"); if (!string.IsNullOrEmpty(rets)) { rets = DataCryptoHelper.Decrypting(rets, "Wonder.His" + key); EnvInfo.UserName = rets; } else { throw new Exception("配置文件中用户名称有误!"); } //try //{ // 系统时间与数据库时间同步 DateTime d = DateTime.Now; //int timezone = int.Parse(DateTime.Now.ToString("%z").Substring(1)); CustomException ex = null; String systime = HttpDataHelper.GetStringWithInfo("BASE", "/sys/date", out ex); if (ex != null) { throw ex; } char[] spp = new char[] { '-', ':', ' ', '+' }; string[] tp = systime.Split(spp, StringSplitOptions.RemoveEmptyEntries); int[] itp = new int[7]; for (int i = 0; i < 7; i++) { itp[i] = int.Parse(tp[i]); } DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(itp[0], itp[1], itp[2], itp[3], itp[4], itp[5])); long delta = GetTimeStamp(startTime) - GetTimeStamp(d) - itp[6] * 36000; //时区处理 if (Math.Abs(delta) < 60000) { SYSTEMTIME st = new SYSTEMTIME(); st.FromDateTime(DateTime.Now.AddMilliseconds(delta)); bool syn = Win32API.SetLocalTime(ref st); if (!syn) { EnvInfo.ServerTimeDelta = delta; } } // 通过BranchId 查询 名称及代码 List <KeyValuePair <string, string> > parms = new List <KeyValuePair <string, string> >(); parms.Add(new KeyValuePair <string, string>("branchId", EnvInfo.BranchId.ToString())); CustomException ce = null; CDictBranch branch = HttpDataHelper.GetWithInfo <CDictBranch>("BASE", "/sys/branch", out ce, parms); EnvInfo.BranchCode = branch.Code; // "101"; EnvInfo.BranchName = branch.Name; // EnvInfo.UserName; #region MONGODB 测试 //ErrorLogInfo eli = new ErrorLogInfo(); //eli.BranchId = EnvInfo.BranchId; //eli.EmpId = EmpInfo.Id; //eli.ComputerIp = EnvInfo.ComputerIp; //eli.Info = "Try"; //eli.Message = "Demo message"; //String json = StringHelper.SerializeObject<ErrorLogInfo>(eli); //HttpDataHelper.HttpPostWithInfo"BASE", "/mongo/errorlog/save", json); #endregion //} //catch (Exception ex) //{ // MessageHelper.ShowError(ex.Message); //} }