| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- 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();
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strServiceName"></param>
- /// <param name="strMethodName"></param>
- /// <param name="objs"></param>
- /// <param name="iType">0get查询 1post增删改及传sql查询</param>
- /// <returns></returns>
- public RESTfulResult<T> doOption<T>(string strServiceName, string strMethodName, object[] objs, int iType)
- {
- int iQueryType = 0;
- string sId = "";
- RESTfulResult<T> rm = new RESTfulResult<T>();
- //Type t = typeof(T);
- //t.Name可得List PageList等名称
- //lg.WriteLog(16, string.Format("开始调用:服务[{0}],接口[{1}]", strServiceName, strMethodName));
- try
- {
- if (iType == 0)
- {
- Dictionary<string, object> d = ReflexCls.method(objs, out iQueryType, out sId);
- if (iQueryType == 0) //Id查询
- {
- rm = doOptionId<T>(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<T>(strServiceName, strMethodName, d);
- }
- }
- else
- {
- rm = doOptionPost<T>(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<T>();
- 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);
- /// <summary>
- /// 操作数据并返回结果其中尖括号里面的可以是class List<class> DataTable string等等
- /// </summary>
- /// <param name="strServiceName">服务端的服务名称</param>
- /// <param name="strMethodName">服务端方法名称</param>
- /// <param name="objs">请求参数 new object[]{ }</param>
- /// <returns></returns>
- public RESTfulResult<T> doOptionPost<T>(string strServiceName, string strMethodName, object[] objs)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- try
- {
- Dictionary<string, object> dc = new Dictionary<string, object>();
- 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<RESTfulResult<T>>(strServiceName, strMethodName, req);
- rm = restful.Post<T>(strServiceName, strMethodName, req);
- }
- catch (Exception ex)
- {
- rm.Succeed = false;
- rm.ResultMessage = ex.Message.Trim();
- }
- return rm;
- }
- //*/
- /// <summary>
- /// 非ID的搞法
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strServiceName"></param>
- /// <param name="strMethodName"></param>
- /// <param name="objs"></param>
- /// <returns></returns>
- public RESTfulResult<T> doOptionOther<T>(string strServiceName, string strMethodName, Dictionary<string, object> dc)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- try
- {
- dc.Add("verificationCode", VerificationCode.getCode());
- return restful.Get<T>(strServiceName, strMethodName, dc);
- }
- catch (Exception ex)
- {
- rm.Succeed = false;
- rm.Message = ex.Message.Trim();
- }
- return rm;
- }
- /// <summary>
- /// ID的搞法
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strServiceName"></param>
- /// <param name="strIdValue">id值</param>
- /// <returns></returns>
- public RESTfulResult<T> doOptionId<T>(string strServiceName, string strIdValue)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- try
- {
- Dictionary<string, object> dc = new Dictionary<string, object>();
- dc.Add("verificationCode", VerificationCode.getCode());
- return restful.Get<T>(strServiceName, strIdValue, dc);
- }
- catch (Exception ex)
- {
- rm.Succeed = false;
- rm.Message = ex.Message.Trim();
- }
- return rm;
- }
- public RESTfulResult<T> doOptionPut<T>(string strServiceName, string strMethodName, object[] objs)
- {
- RESTfulResult<T> rm = new RESTfulResult<T>();
- try
- {
- Dictionary<string, object> dc = new Dictionary<string, object>();
- 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<string, object> dc = new Dictionary<string, object>();
- //dc.Add("verificationCode", VerificationCode.getCode());
- rm = restful.Put<T>(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();
- }
- }
- }
|