You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

580 lines
20 KiB

2 years ago
  1. using EasyBL;
  2. using EasyBL.WebApi.Common;
  3. using EasyBL.WebApi.WebApi;
  4. using EasyNet.DBUtility;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data.SqlClient;
  9. using System.Web;
  10. using System.Web.Script.Services;
  11. using System.Web.Services;
  12. namespace WebApp.WS
  13. {
  14. /// <summary>
  15. /// ComWebService 的摘要说明
  16. /// </summary>
  17. [WebService(Namespace = @"http://tempuri.org/")]
  18. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  19. [System.ComponentModel.ToolboxItem(false)]
  20. // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
  21. [ScriptService]
  22. public class ComWebService : System.Web.Services.WebService
  23. {
  24. public APISoapHeader Header { get; set; }
  25. public ComWebService()
  26. {
  27. //如果使用設計的元件,請取消註解下列一行
  28. //InitializeComponent();
  29. }
  30. #region 資料查詢
  31. #region QueryTableByPrc
  32. /// <summary>
  33. /// 資料查詢DataSet
  34. /// </summary>
  35. /// <param name="Type">todo: describe Type parameter on QueryTableByPrc</param>
  36. /// <param name="Params">todo: describe Params parameter on QueryTableByPrc</param>
  37. [System.Web.Services.Protocols.SoapHeader(@"header")]
  38. [WebMethod]
  39. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  40. public string QueryTableByPrc(string Type, Object Params)
  41. {
  42. var sJson = @"";
  43. try
  44. {
  45. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  46. {
  47. return @"-1";
  48. }
  49. var ds = EntityBL.GetTableByPrc(Type, Params);
  50. sJson = JsonConvert.SerializeObject(ds, Formatting.Indented); //把DataSet轉成Json字串
  51. }
  52. catch (Exception ex)
  53. {
  54. LogService.MailSend(ex.Message + @"Param(Type:" + Type + @"Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(QueryTableByPrc), nameof(QueryTableByPrc), @"", @"", @"");
  55. }
  56. return sJson; //回傳字串
  57. }
  58. #endregion QueryTableByPrc
  59. #region QueryList
  60. /// <summary>
  61. /// 資料查詢List
  62. /// </summary>
  63. /// <param name="Type">todo: describe Type parameter on QueryList</param>
  64. /// <param name="Params">todo: describe Params parameter on QueryList</param>
  65. [System.Web.Services.Protocols.SoapHeader(@"header")]
  66. [WebMethod]
  67. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  68. public string QueryList(string Type, Object Params)
  69. {
  70. var sJson = @"";
  71. try
  72. {
  73. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  74. {
  75. return @"-1";
  76. }
  77. var lst = EntityBL.QueryList(Type, Params);
  78. sJson = JsonConvert.SerializeObject(lst, Formatting.Indented); //把list轉成Json字串
  79. }
  80. catch (Exception ex)
  81. {
  82. LogService.MailSend(ex.Message + @"Param(Type:" + Type + @"Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(QueryList), nameof(QueryList), @"", @"", @"");
  83. }
  84. return sJson; //回傳字串
  85. }
  86. #endregion QueryList
  87. #region QueryOne
  88. /// <summary>
  89. /// 資料查詢單筆資料
  90. /// </summary>
  91. /// <param name="Type">todo: describe Type parameter on QueryOne</param>
  92. /// <param name="Params">todo: describe Params parameter on QueryOne</param>
  93. [System.Web.Services.Protocols.SoapHeader(@"header")]
  94. [WebMethod]
  95. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  96. public string QueryOne(string Type, Object Params)
  97. {
  98. var sJson = @"";
  99. try
  100. {
  101. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  102. {
  103. return @"-1";
  104. }
  105. var eOne = EntityBL.QueryOne(Type, Params);
  106. sJson = JsonConvert.SerializeObject(eOne, Formatting.Indented); //把Model轉成Json字串
  107. }
  108. catch (Exception ex)
  109. {
  110. LogService.MailSend(ex.Message + @"Param(Type:" + Type + @"Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(QueryOne), nameof(QueryOne), @"", @"", @"");
  111. }
  112. return sJson; //回傳字串
  113. }
  114. #endregion QueryOne
  115. #region QueryByPage
  116. /// <summary>
  117. /// 資料查詢單筆資料
  118. /// </summary>
  119. /// <param name="Params">todo: describe Params parameter on QueryPage</param>
  120. [System.Web.Services.Protocols.SoapHeader(@"header")]
  121. [WebMethod]
  122. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  123. public string QueryPage(Object Params)
  124. {
  125. var sJson = @"";
  126. try
  127. {
  128. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  129. {
  130. return @"-1";
  131. }
  132. var pr = EntityBL.QueryPage(Params);
  133. sJson = JsonConvert.SerializeObject(pr, Formatting.Indented); //把Model轉成Json字串
  134. }
  135. catch (Exception ex)
  136. {
  137. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(QueryPage), nameof(QueryPage), @"", @"", @"");
  138. }
  139. return sJson; //回傳字串
  140. }
  141. #endregion QueryByPage
  142. #region QueryPageByPrc
  143. /// <summary>
  144. /// 資料查詢通過預存函數
  145. /// </summary>
  146. /// <param name="Type">todo: describe Type parameter on QueryPageByPrc</param>
  147. /// <param name="Params">todo: describe Params parameter on QueryPageByPrc</param>
  148. [System.Web.Services.Protocols.SoapHeader(@"header")]
  149. [WebMethod]
  150. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  151. public string QueryPageByPrc(string Type, Object Params)
  152. {
  153. var sJson = @"";
  154. try
  155. {
  156. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  157. {
  158. return @"-1";
  159. }
  160. var pr = EntityBL.QueryPageByPrc(Type, Params, true);
  161. sJson = JsonConvert.SerializeObject(pr, Formatting.Indented); //把Model轉成Json字串
  162. }
  163. catch (Exception ex)
  164. {
  165. LogService.MailSend(ex.Message + @"Param(Type:" + Type + @"Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(QueryPageByPrc), nameof(QueryPageByPrc), @"", @"", @"");
  166. }
  167. return sJson; //回傳字串
  168. }
  169. #endregion QueryPageByPrc
  170. #region QueryCount
  171. /// <summary>
  172. /// 查詢資料筆數
  173. /// </summary>
  174. /// <param name="Params">todo: describe Params parameter on QueryCount</param>
  175. [System.Web.Services.Protocols.SoapHeader(@"header")]
  176. [WebMethod]
  177. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  178. public int QueryCount(Object Params)
  179. {
  180. var iCount = 0;
  181. try
  182. {
  183. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  184. {
  185. return -1;
  186. }
  187. iCount = DBHelper.QueryCount(Params);
  188. }
  189. catch (Exception ex)
  190. {
  191. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(QueryCount), nameof(QueryCount), @"", @"", @"");
  192. }
  193. return iCount; //回傳字串
  194. }
  195. #endregion QueryCount
  196. #endregion 資料查詢
  197. #region 資料新增
  198. #region Add
  199. /// <summary>
  200. /// 資料新增Add
  201. /// </summary>
  202. /// <param name="Params">todo: describe Params parameter on Add</param>
  203. [System.Web.Services.Protocols.SoapHeader(@"header")]
  204. [WebMethod]
  205. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  206. public int Add(Object Params)
  207. {
  208. var iRel = 0;
  209. try
  210. {
  211. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  212. {
  213. return -1;
  214. }
  215. iRel = new DBHelper().Insert(Params);
  216. }
  217. catch (Exception ex)
  218. {
  219. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(Add), nameof(Add), @"", @"", @"");
  220. }
  221. return iRel; //回傳字串
  222. }
  223. #endregion Add
  224. #endregion 資料新增
  225. #region 資料修改
  226. #region Update
  227. /// <summary>
  228. /// 資料修改Update
  229. /// </summary>
  230. /// <param name="Params">todo: describe Params parameter on Update</param>
  231. [System.Web.Services.Protocols.SoapHeader(@"header")]
  232. [WebMethod]
  233. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  234. public int Update(Object Params)
  235. {
  236. var iRel = 0;
  237. try
  238. {
  239. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  240. {
  241. return -1;
  242. }
  243. iRel = new DBHelper().Update(Params);
  244. }
  245. catch (Exception ex)
  246. {
  247. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(Update), nameof(Update), @"", @"", @"");
  248. }
  249. return iRel; //回傳字串
  250. }
  251. #endregion Update
  252. #region UpdateTran
  253. /// <summary>
  254. /// 資料修改Update
  255. /// </summary>
  256. /// <param name="Params">todo: describe Params parameter on UpdateTran</param>
  257. [System.Web.Services.Protocols.SoapHeader(@"header")]
  258. [WebMethod]
  259. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  260. public int UpdateTran(Object Params)
  261. {
  262. var iRel = 0;
  263. try
  264. {
  265. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  266. {
  267. return -1;
  268. }
  269. iRel = new DBHelper().UpdateTran(Params);
  270. }
  271. catch (Exception ex)
  272. {
  273. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(UpdateTran), nameof(UpdateTran), @"", @"", @"");
  274. }
  275. return iRel; //回傳字串
  276. }
  277. #endregion UpdateTran
  278. #region UpdatePrc
  279. /// <summary>
  280. /// 資料修改UpdatePrc
  281. /// </summary>
  282. /// <param name="Params">todo: describe Params parameter on UpdatePrc</param>
  283. [System.Web.Services.Protocols.SoapHeader(@"header")]
  284. [WebMethod]
  285. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  286. public int UpdatePrc(Object Params)
  287. {
  288. var iRel = 0;
  289. try
  290. {
  291. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  292. {
  293. return -1;
  294. }
  295. iRel = EntityBL.ExecuteSqlTran(Params);
  296. }
  297. catch (Exception ex)
  298. {
  299. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(UpdatePrc), nameof(UpdatePrc), @"", @"", @"");
  300. }
  301. return iRel; //回傳字串
  302. }
  303. #endregion UpdatePrc
  304. #endregion 資料修改
  305. #region 資料刪除
  306. #region Delete
  307. /// <summary>
  308. /// 資料刪除
  309. /// </summary>
  310. /// <param name="Params">todo: describe Params parameter on Delete</param>
  311. [System.Web.Services.Protocols.SoapHeader(@"header")]
  312. [WebMethod]
  313. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  314. public int Delete(Object Params)
  315. {
  316. var iRel = 0;
  317. try
  318. {
  319. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  320. {
  321. return -1;
  322. }
  323. iRel = new DBHelper().Delete(Params);
  324. var sOrgid = HttpContext.Current.Request.Headers[@"orgid"];
  325. var sUserid = HttpContext.Current.Request.Headers[@"userid"];
  326. LogService.mo_Log.Debug(@"Params:" + ServiceBase.JsonToString(Params) + @" Deleter:" + sOrgid + @"-" + sUserid);
  327. }
  328. catch (Exception ex)
  329. {
  330. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(Delete), nameof(Delete), @"", @"", @"");
  331. }
  332. return iRel; //回傳字串
  333. }
  334. #endregion Delete
  335. #endregion 資料刪除
  336. #region 發送郵件
  337. //<summary>
  338. //發送郵件
  339. //</summary>
  340. [System.Web.Services.Protocols.SoapHeader(@"header")]
  341. [WebMethod]
  342. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  343. public string SendMail(Object Params)
  344. {
  345. var sRes = @"0";
  346. string sError = null;
  347. try
  348. {
  349. do
  350. {
  351. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  352. {
  353. return @"-1";
  354. }
  355. var sOrgid = HttpContext.Current.Request.Headers[@"orgid"];
  356. var ms = new MailService(sOrgid);
  357. var bSend = ms.SendMail(Params, out sError);
  358. if (sError != null)
  359. {
  360. sRes = sError;
  361. break;
  362. }
  363. if (bSend)
  364. {
  365. sRes = @"1";
  366. }
  367. } while (false);
  368. return sRes;
  369. }
  370. catch (Exception ex)
  371. {
  372. LogService.MailSend(ex.Message + @"Param(Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(SendMail), @"SendMailPro", @"", @"", @"");
  373. return sRes;
  374. }
  375. }
  376. #endregion 發送郵件
  377. #region GetHttpClient
  378. /// <summary>
  379. /// 獲取特定Url html
  380. /// </summary>
  381. /// <param name="Url">todo: describe Url parameter on GetHttpClient</param>
  382. /// <param name="Params">todo: describe Params parameter on GetHttpClient</param>
  383. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  384. public string GetHttpClient(string Url, Object Params)
  385. {
  386. var oJson = new Dictionary<string, object>();
  387. var sHtml = @"";
  388. try
  389. {
  390. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  391. {
  392. return @"-1";
  393. }
  394. var client = new HttpWebClient(Url);
  395. if (Params is Dictionary<string, object>)
  396. {
  397. var dicpm = Params as Dictionary<string, object>;
  398. if (dicpm.Keys.Count > 0)
  399. {
  400. foreach (string key in dicpm.Keys)
  401. {
  402. client.PostingData.Add(key, dicpm[key].ToString());
  403. }
  404. }
  405. }
  406. sHtml = client.GetString();
  407. }
  408. catch (Exception ex)
  409. {
  410. LogService.MailSend(ex.Message + @"Param(Url:" + Url + @"Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(GetHttpClient), nameof(GetHttpClient), @"", @"", @"");
  411. return @"";
  412. }
  413. return sHtml;
  414. }
  415. #endregion GetHttpClient
  416. #region GetStringAsync
  417. /// <summary>
  418. /// 獲取特定Url html
  419. /// </summary>
  420. /// <param name="Url">todo: describe Url parameter on GetStringAsync</param>
  421. /// <param name="Params">todo: describe Params parameter on GetStringAsync</param>
  422. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  423. public string GetStringAsync(string Url, Object Params)
  424. {
  425. var oJson = new Dictionary<string, object>();
  426. var sHtml = @"";
  427. try
  428. {
  429. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  430. {
  431. return @"-1";
  432. }
  433. using (var httpClient = new System.Net.Http.HttpClient())
  434. {
  435. var paramList = new List<KeyValuePair<String, String>>();
  436. if (Params is Dictionary<string, object>)
  437. {
  438. var dicpm = Params as Dictionary<string, object>;
  439. if (dicpm.Keys.Count > 0)
  440. {
  441. foreach (string key in dicpm.Keys)
  442. {
  443. paramList.Add(new KeyValuePair<string, string>(key, dicpm[key].ToString()));
  444. }
  445. }
  446. }
  447. var content = new System.Net.Http.FormUrlEncodedContent(paramList);
  448. var response = httpClient.PostAsync(Url, content).Result;
  449. sHtml = response.Content.ReadAsStringAsync().Result;
  450. }
  451. }
  452. catch (Exception ex)
  453. {
  454. LogService.MailSend(ex.Message + @"Param(Url:" + Url + @"Params:" + ServiceBase.JsonToString(Params) + @")", ex, @"", @"", nameof(ComWebService), nameof(GetStringAsync), nameof(GetStringAsync), @"", @"", @"");
  455. return @"";
  456. }
  457. return sHtml;
  458. }
  459. #endregion GetStringAsync
  460. #region 获取web客户端ip
  461. /*获取web客户端ip*/
  462. [System.Web.Services.Protocols.SoapHeader(@"header")]
  463. [WebMethod]
  464. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  465. public string GetWebClientIp()
  466. {
  467. var sUserIP = @"";
  468. try
  469. {
  470. if (!SignExtension.VerifyIdentity(HttpContext.Current, Header))
  471. {
  472. return @"-1";
  473. }
  474. if (HttpContext.Current == null
  475. || HttpContext.Current.Request == null
  476. || HttpContext.Current.Request.ServerVariables == null)
  477. return @"";
  478. var CustomerIP = @"";
  479. //CDN加速后取到的IP simone 090805
  480. CustomerIP = HttpContext.Current.Request.Headers[@"Cdn-Src-Ip"];
  481. if (!string.IsNullOrEmpty(CustomerIP))
  482. {
  483. return CustomerIP;
  484. }
  485. CustomerIP = HttpContext.Current.Request.ServerVariables[@"HTTP_X_FORWARDED_FOR"];
  486. if (!String.IsNullOrEmpty(CustomerIP))
  487. return CustomerIP;
  488. if (HttpContext.Current.Request.ServerVariables[@"HTTP_VIA"] != null)
  489. {
  490. CustomerIP = HttpContext.Current.Request.ServerVariables[@"HTTP_X_FORWARDED_FOR"];
  491. if (CustomerIP == null)
  492. CustomerIP = HttpContext.Current.Request.ServerVariables[@"REMOTE_ADDR"];
  493. }
  494. else
  495. {
  496. CustomerIP = HttpContext.Current.Request.ServerVariables[@"REMOTE_ADDR"];
  497. }
  498. if (string.Compare(CustomerIP, @"unknown", true) == 0)
  499. return System.Web.HttpContext.Current.Request.UserHostAddress;
  500. return CustomerIP;
  501. }
  502. catch (Exception ex)
  503. {
  504. LogService.MailSend(ex.Message, ex, @"", @"", nameof(ComWebService), nameof(GetWebClientIp), nameof(GetWebClientIp), @"", @"", @"");
  505. }
  506. return sUserIP;
  507. }
  508. #endregion 获取web客户端ip
  509. }
  510. }