WSGetData.asmx.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using SysBaseLibs;
  2. using SysDataLibs;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. using System.Xml.Serialization;
  9. namespace GSMarketSys.WS
  10. {
  11. /// <summary>
  12. /// WSGetData 的摘要说明
  13. /// </summary>
  14. [WebService(Namespace = "http://tempuri.org/")]
  15. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  16. [System.ComponentModel.ToolboxItem(false)]
  17. // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
  18. // [System.Web.Script.Services.ScriptService]
  19. public class WSGetData : System.Web.Services.WebService
  20. {
  21. public WSGetData()
  22. {
  23. //如果使用设计的组件,请取消注释以下行
  24. //InitializeComponent();
  25. }
  26. // 将一个Array各项用逗号隔开,拼成一个string
  27. protected string ArrayToString(Array paArray)
  28. {
  29. string lcStr = "";
  30. foreach (string lcStr2 in paArray)
  31. {
  32. lcStr = lcStr + ((lcStr == "") ? "" : ",") + lcStr2;
  33. }
  34. return lcStr;
  35. }
  36. protected string GetLocalTableName(DBConnSql loDBConn, string DSName)
  37. {
  38. string strRet = string.Empty;
  39. if (loDBConn != null & loDBConn.Open() && loDBConn.IsOpened)
  40. {
  41. string strSqlCommand = "select TableId from PlatRegDsn where PlatDsName='" + DSName + "'";
  42. rsQuery loQuery = loDBConn.OpenQuery(strSqlCommand);
  43. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  44. {
  45. strRet = loQuery.GetString("TableId");
  46. }
  47. }
  48. return strRet;
  49. }
  50. [XmlInclude(typeof(QueryOperate))]
  51. [XmlInclude(typeof(Condition))]
  52. [WebMethod]
  53. // 子系统在登录时生成guid,以便子系统实现调用查询的权限控制
  54. public string GetSubSysData(Condition[] condition_Query, string dSName, string[] dispField, string guid)
  55. {
  56. string strJson = string.Empty;
  57. string strCondition = string.Empty;
  58. DBConnSql loDBConn = null;
  59. try
  60. {
  61. if (LoginGUID.Instance.GetOne(guid) == null)
  62. {
  63. string strMsg = "GetSubSysData: Invalid guid " + guid;
  64. throw new Exception(strMsg);
  65. }
  66. if (dispField == null || dispField.Length == 0)
  67. {
  68. throw new Exception("GetSubSysData: Param dispField is null! ");
  69. }
  70. if (condition_Query != null)
  71. {
  72. foreach (Condition cnd in condition_Query)
  73. {
  74. string strQuote = string.Empty; // 值比较时是否需要加单引号
  75. // 左括号
  76. if (cnd.BeginLeftTag == 1)
  77. strCondition += " (";
  78. // string或者日期类型
  79. if ((cnd.Operate.FieldType == 0) || (cnd.Operate.FieldType == 1))
  80. strQuote = "'";
  81. // 比较条件
  82. switch (cnd.Operate.OptTag)
  83. {
  84. case 0:
  85. strCondition += cnd.FieldName + " like " + strQuote + "%" + cnd.Operate.FieldValue + "%" + strQuote + " ";
  86. break;
  87. case 1:
  88. strCondition += cnd.FieldName + " like " + strQuote + "%" + cnd.Operate.FieldValue + strQuote + " ";
  89. break;
  90. case 2:
  91. strCondition += cnd.FieldName + " like " + strQuote + cnd.Operate.FieldValue + "%" + strQuote + " ";
  92. break;
  93. case 3:
  94. strCondition += cnd.FieldName + " =" + strQuote + cnd.Operate.FieldValue + strQuote + " ";
  95. break;
  96. case 4:
  97. strCondition += cnd.FieldName + " >=" + strQuote + cnd.Operate.FieldValue + strQuote + " ";
  98. break;
  99. case 5:
  100. strCondition += cnd.FieldName + " <=" + strQuote + cnd.Operate.FieldValue + strQuote + " ";
  101. break;
  102. }
  103. // 右括号
  104. if (cnd.EndRightTag == 1)
  105. strCondition += ") ";
  106. // 逻辑连接在最后,无论是否有右括号
  107. switch (cnd.LogicJoin)
  108. {
  109. case 0:
  110. strCondition += " and ";
  111. break;
  112. case 1:
  113. strCondition += " or ";
  114. break;
  115. case 2:
  116. break;
  117. }
  118. }//foreach
  119. }
  120. // 列名
  121. string strDispFields = string.Empty;
  122. foreach (string strDispField in dispField)
  123. {
  124. string strDispFieldTrim = strDispField.Trim();
  125. if (strDispFieldTrim != "")
  126. {
  127. if (strDispFields == "")
  128. strDispFields += strDispFieldTrim;
  129. else
  130. strDispFields += "," + strDispFieldTrim;
  131. }
  132. }
  133. if (strDispFields == "")
  134. {
  135. //strDispFields = "*";
  136. throw new Exception("GetSubSysData: Param dispField is empty! ");
  137. }
  138. // 进行查询
  139. loDBConn = new DBConnSql();
  140. if (loDBConn.Open() && loDBConn.IsOpened)
  141. {
  142. // 本地数据表(视图)名称
  143. string strDSName = GetLocalTableName(loDBConn, dSName);
  144. if (strDSName == "")
  145. {
  146. throw new Exception("GetSubSysData: invalid DSName " + dSName);
  147. }
  148. // 查询语句
  149. string strSqlCommand = " select " + strDispFields + " from " + strDSName + " where " + strCondition;
  150. SysBaseLibs.ThreadLog.LogInfo("GetSubSysData:" + strSqlCommand);
  151. // sql注入检查
  152. string strTemp = strSqlCommand.ToUpper();
  153. if ((strTemp.IndexOf("DELETE ") >= 0)
  154. || (strTemp.IndexOf("INSERT ") >= 0)
  155. || (strTemp.IndexOf("UPDATE ") >= 0)
  156. || (strTemp.IndexOf("TRUNCATE ") >= 0)
  157. || (strTemp.IndexOf("DROP ") >= 0)
  158. || (strTemp.IndexOf("ALTER ") >= 0))
  159. {
  160. throw new Exception("GetSubSysData: sql injection");
  161. }
  162. rsQuery loQuery = loDBConn.OpenQuery(strSqlCommand);
  163. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  164. {
  165. Array arrayFields = UtilStr.StrToArrayEx(loQuery.AllFields, ",");
  166. int nColumnCount = loQuery.Columns.Count;
  167. Array allRows = Array.CreateInstance(typeof(string), loQuery.RecCount);
  168. // 轮询每一行
  169. for (int i = 0; i < loQuery.RecCount; i++)
  170. {
  171. Array curRow = Array.CreateInstance(typeof(string), nColumnCount);
  172. for (int j = 0; j < arrayFields.Length; j++)
  173. {
  174. // 列名
  175. string strField = arrayFields.GetValue(j).ToString();
  176. // 在数值两边加上单引号
  177. string strVal = loQuery.GetString(strField);
  178. strVal = strVal.Replace("'", "");
  179. strVal = "'" + strVal + "'";
  180. curRow.SetValue(strField + ":" + strVal, j);
  181. }
  182. // 每一行的格式: {column1:value1,column2:value2,column3:value3}
  183. string strCurRow = ArrayToString(curRow);
  184. strCurRow = "{" + strCurRow + "}";
  185. allRows.SetValue(strCurRow, i);
  186. loQuery.MoveNext();
  187. }
  188. // 返回格式:{table:[{row1},{row2}]}
  189. strJson = ArrayToString(allRows);
  190. strJson = "{" + dSName + ":[" + strJson + "]}";
  191. }
  192. }
  193. }
  194. catch (Exception err)
  195. {
  196. strJson = string.Empty;
  197. SysBaseLibs.ThreadLog.LogInfo("GetSubSysData:" + err.Message);
  198. }
  199. finally
  200. {
  201. if (loDBConn != null && loDBConn.IsOpened)
  202. loDBConn.Close();
  203. }
  204. return strJson;
  205. }
  206. }
  207. [Serializable]
  208. public class QueryOperate
  209. {
  210. public int OptTag; // 0:like '%value%' 1:like '%value' 2:like 'value%' 3:= 4:>= 5:<=
  211. public string FieldValue; // 字段值
  212. public int FieldType; // 字段类型:0:string 1:dateTime 2:int 3:decimal
  213. }
  214. [Serializable]
  215. public class Condition
  216. {
  217. public int BeginLeftTag; // 是否加左括号,0:不加 1:加
  218. public string FieldName; // 数据字典项名称,可能是某个数据表或视图的字段名称
  219. public QueryOperate Operate; // 查询操作符对象
  220. public int LogicJoin; // 逻辑连接符,0:and 1:or 2:无逻辑连接
  221. public int EndRightTag; // 是否加右括号,0:不加 1:加
  222. }
  223. }