基础服务api
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

220 líneas
7.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http.Headers;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Furion;
  9. using BPA.SAAS.Manage.Comm.Model;
  10. using Newtonsoft.Json;
  11. namespace BPA.SAAS.Manage.Comm.Util
  12. {
  13. public class Utiily
  14. {
  15. public static string OrderUrl = App.Configuration["order_url"];
  16. public static string WechatUrl = App.Configuration["wechat_url"];
  17. //public static string OrderUrl = "http://order.dev1.com";//
  18. //public static string WechatUrl = "http://wechat.dev1.com";//
  19. /// <summary>
  20. /// 设备订阅
  21. /// </summary>
  22. public const string newmqttsub = "/Mqtt/newmqttsub";
  23. /// <summary>
  24. /// 打印小票
  25. /// </summary>
  26. public const string PrintTicket = "/order/print/printtickets";
  27. /// <summary>
  28. /// 新增要货单
  29. /// </summary>
  30. public const string Addrequiregoods = "/api/requiregoodsapplication";
  31. /// <summary>
  32. /// 获取配送信息
  33. /// </summary>
  34. public const string GetDeliveryentry = "/api/deliveryapplication/deliveryentry/";
  35. /// <summary>
  36. /// 新增差异单
  37. /// </summary>
  38. public const string AddCostpriceadjust = "/api/costpriceadjustapplication";
  39. /// <summary>
  40. /// 获取组织
  41. /// </summary>
  42. public const string GetOrg = "/api/baseapplication/orglist/";
  43. /// <summary>
  44. /// 获取供应商
  45. /// </summary>
  46. public const string GetSupplier = "/api/purchaseapplyapplication/supplier/";
  47. /// <summary>
  48. ///订单售后
  49. /// </summary>
  50. public const string agreerefundUrl = "/order/agreerefund";
  51. /// <summary>
  52. /// 微信退款
  53. /// </summary>
  54. public const string wechatrefundurl = "/api/wechat/wechatrefund";
  55. /// <summary>
  56. /// 加盟商
  57. /// </summary>
  58. public static string Supplier { get; set; }
  59. /// <summary>
  60. /// 代理服务
  61. /// </summary>
  62. public static string Proxy { get; set; }
  63. public static string HttpPost(string url, string postData, Encoding encoding, string authorize = "")
  64. {
  65. try
  66. {
  67. HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
  68. myReq.Method = "POST";
  69. myReq.Timeout = 600 * 60;
  70. if (!string.IsNullOrWhiteSpace(authorize))
  71. {
  72. myReq.Headers.Add("Authorize", authorize);
  73. }
  74. byte[] byteArray = encoding.GetBytes(postData);
  75. myReq.ContentType = "application/json-patch+json";
  76. Stream dataStream = myReq.GetRequestStream();
  77. dataStream.Write(byteArray, 0, byteArray.Length);
  78. dataStream.Close();
  79. HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
  80. dataStream = HttpWResp.GetResponseStream();
  81. string result = "";
  82. using (StreamReader myStreamReader = new StreamReader(dataStream, encoding))
  83. {
  84. result = myStreamReader.ReadToEnd();
  85. myStreamReader.Close();
  86. }
  87. dataStream.Close();
  88. HttpWResp.Close();
  89. // BPALog.WriteLog("post请求:" + $"【url:{url}】【data:{postData}】", LogEnum.Info, null);
  90. return result;
  91. }
  92. catch (System.Exception ex)
  93. {
  94. //BPALog.WriteLog("post请求:" + $"【url:{url}】【data:{postData}】", LogEnum.Error, null, ex);
  95. throw;
  96. }
  97. }
  98. public static string HttpGet(string url, string postDataStr)
  99. {
  100. try
  101. {
  102. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + postDataStr);
  103. request.Method = "GET";
  104. request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  105. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  106. Stream myResponseStream = response.GetResponseStream();
  107. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  108. string retString = myStreamReader.ReadToEnd();
  109. myStreamReader.Close();
  110. myResponseStream.Close();
  111. // BPALog.WriteLog("post请求:" + $"【url:{url}】【data:{postDataStr}】", LogEnum.Info, null);
  112. return retString;
  113. }
  114. catch (Exception ex)
  115. {
  116. //BPALog.WriteLog("post请求:" + $"【url:{url}】【data:{postDataStr}】", LogEnum.Error, null, ex);
  117. throw;
  118. }
  119. }
  120. public static ResultEntity<T> Post<T>(string url, object param, bool UseUnify = true, bool HasResult = true)
  121. {
  122. try
  123. {
  124. HttpClientHandler handler = new HttpClientHandler();
  125. handler.ServerCertificateCustomValidationCallback = (a, b, c, d) => { return true; };
  126. HttpClient client = new HttpClient(handler);
  127. HttpContent content = new StringContent(JsonConvert.SerializeObject(param));
  128. content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  129. if (HasResult)
  130. {
  131. var res = client.PostAsync(Supplier + url, content).Result.Content.ReadAsStringAsync().Result;
  132. if (UseUnify == false)
  133. {
  134. return new ResultEntity<T>
  135. {
  136. Data = default,
  137. IsSuccess = bool.Parse(res),
  138. Msg = string.Empty
  139. };
  140. }
  141. return JsonConvert.DeserializeObject<ResultEntity<T>>(res);// res.FromJSON<ResultEntity<T>>();
  142. }
  143. else
  144. {
  145. client.PostAsync(url, content);
  146. return default;
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. return new ResultEntity<T>
  152. {
  153. Data = default,
  154. IsSuccess = false,
  155. Msg = ex.Message
  156. };
  157. }
  158. }
  159. public static ResultEntity<T> Get<T>(string url, string param, bool HasResult = true)
  160. {
  161. try
  162. {
  163. HttpClient client = new HttpClient();
  164. if (HasResult)
  165. {
  166. var res = client.GetAsync(Supplier + url + param).Result.Content.ReadAsStringAsync().Result;
  167. return JsonConvert.DeserializeObject<ResultEntity<T>>(res);// res.FromJSON<ResultEntity<T>>();
  168. }
  169. else
  170. {
  171. client.GetAsync(Proxy + url + param);
  172. return default;
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. return new ResultEntity<T>
  178. {
  179. Data = default,
  180. IsSuccess = false,
  181. Msg = ex.Message
  182. };
  183. }
  184. }
  185. }
  186. }