using com.hnshituo.core.webapp.vo; using CoreFS.CA06; using System; using System.Collections.Generic; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Windows.Forms; namespace Common { public class DbHelper { private Log lg = Log.GetInstance(); /// /// /// /// /// /// /// /// 0get查询 1post增删改及传sql查询 /// public RESTfulResult doOption(string strServiceName, string strMethodName, object[] objs, int iType) { int iQueryType = 0; string sId = ""; RESTfulResult rm = new RESTfulResult(); //Type t = typeof(T); //t.Name可得List PageList等名称 lg.WriteLog(16, string.Format("开始调用:服务[{0}],接口[{1}]", strServiceName, strMethodName)); try { if (iType == 0) { Dictionary d = ReflexCls.method(objs, out iQueryType, out sId); if (iQueryType == 0) //Id查询 { rm = doOptionId(strServiceName, sId); if (rm.Succeed && rm.Data != null) { PropertyInfo[] PropertyInfos1 = rm.Data.GetType().GetProperties(); foreach (var item in d) { foreach (PropertyInfo p1 in PropertyInfos1) { if (item.Key.ToUpper() == p1.Name.ToUpper()) { object value = p1.GetValue(rm.Data, null); if (value != null && value.ToString() != item.Value.ToString()) { rm.Data = default(T); } } } } } } else //非ID查询 { rm = doOptionOther(strServiceName, strMethodName, d); } } else { rm = doOptionPost(strServiceName, strMethodName, objs); } if (!rm.Succeed) { if (rm.ResultMessage == null && !string.IsNullOrEmpty(rm.Message)) { MessageBox.Show(rm.Message); //System.Environment.Exit(0); } if (rm.ResultMessage != null && rm.ResultMessage.Contains("授权")) { MessageBox.Show(rm.ResultMessage); //System.Environment.Exit(0); } lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, rm.ResultMessage ==null?"服务器访问异常": rm.ResultMessage)); } } catch (Exception ex) { rm = new RESTfulResult(); rm.Succeed = false; rm.ResultMessage = string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim()); lg.WriteLog(16, string.Format("服务[{0}],接口[{1}],错误信息:{2}", strServiceName, strMethodName, ex.Message.Trim())); } lg.WriteLog(16, string.Format("完成调用:服务[{0}],接口[{1}]", strServiceName, strMethodName)); return rm; } private CoreRESTfulService restful = new CoreRESTfulService(AppConfigCache.serviceUrl, "", true); /// /// 操作数据并返回结果其中尖括号里面的可以是class List DataTable string等等 /// /// 服务端的服务名称 /// 服务端方法名称 /// 请求参数 new object[]{ } /// public RESTfulResult doOptionPost(string strServiceName, string strMethodName, object[] objs) { RESTfulResult rm = new RESTfulResult(); try { Dictionary dc = new Dictionary(); dc.Add("verificationCode", VerificationCode.getCode()); ReqParam[] req = new ReqParam[objs.Length + 1]; for (int i = 0; i < objs.Length; i++) { req[i] = new ReqParam(objs[i]); } req[objs.Length] = new ReqParam("verificationCode", VerificationCode.getCode()); //rm = restful.Post>(strServiceName, strMethodName, req); rm = restful.Post(strServiceName, strMethodName, req); } catch (Exception ex) { rm.Succeed = false; rm.ResultMessage = ex.Message.Trim(); } return rm; } //*/ /// /// 非ID的搞法 /// /// /// /// /// /// public RESTfulResult doOptionOther(string strServiceName, string strMethodName, Dictionary dc) { RESTfulResult rm = new RESTfulResult(); try { dc.Add("verificationCode", VerificationCode.getCode()); return restful.Get(strServiceName, strMethodName, dc); } catch (Exception ex) { rm.Succeed = false; rm.Message = ex.Message.Trim(); } return rm; } /// /// ID的搞法 /// /// /// /// id值 /// public RESTfulResult doOptionId(string strServiceName, string strIdValue) { RESTfulResult rm = new RESTfulResult(); try { Dictionary dc = new Dictionary(); dc.Add("verificationCode", VerificationCode.getCode()); return restful.Get(strServiceName, strIdValue, dc); } catch (Exception ex) { rm.Succeed = false; rm.Message = ex.Message.Trim(); } return rm; } public RESTfulResult doOptionPut(string strServiceName, string strMethodName, object[] objs) { RESTfulResult rm = new RESTfulResult(); try { Dictionary dc = new Dictionary(); dc.Add("verificationCode", VerificationCode.getCode()); ReqParam[] req = new ReqParam[objs.Length + 1]; for (int i = 0; i < objs.Length; i++) { req[i] = new ReqParam(objs[i]); } req[objs.Length] = new ReqParam("verificationCode", VerificationCode.getCode()); //Dictionary dc = new Dictionary(); //dc.Add("verificationCode", VerificationCode.getCode()); rm = restful.Put(strServiceName, strMethodName, req); } catch (Exception ex) { rm.Succeed = false; rm.ResultMessage = ex.Message.Trim(); } return rm; } } public static class VerificationCode { public static string getCode() { DateTime dt = DateTime.Now; string sdt = dt.ToString("MM-yyyy-dd"); string newdt = sdt.Substring(0, 2) + "jisco" + sdt.Substring(2); return md5(newdt); } public static string md5(string input) { // Use input string to calculate MD5 hash MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); // Convert the byte array to hexadecimal string StringBuilder sb = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("X2")); // To force the hex string to lower-case letters instead of // upper-case, use he following line instead: // sb.Append(hashBytes[i].ToString("x2")); } return sb.ToString(); } } }