apichyHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using BizCom;
  2. using Microsoft.SqlServer.Server;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using NHibernate.Cache;
  6. using SiteCore.Handler;
  7. using SiteCore.taoObj;
  8. using SiteCore.taoObj.WebApi;
  9. using SQLData;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.Specialized;
  13. using System.Data;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Net;
  18. using System.Net.Http.Headers;
  19. using System.Net.Http;
  20. using System.Reflection;
  21. using System.Security.Cryptography.Xml;
  22. using System.Security.Policy;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using System.Web;
  26. using static SiteCore.taoObj.Api_trade_info;
  27. using System.Text.RegularExpressions;
  28. using NPOI.OpenXmlFormats.Shared;
  29. namespace SiteCore.Handler
  30. {
  31. public class apichyHelper : BaseHandler, IHttpHandler
  32. {
  33. public void ProcessRequest(HttpContext context)
  34. {
  35. if (UrlParmsCheck("t"))
  36. {
  37. String tname = GetString("t");
  38. MethodInfo method;
  39. Type type = this.GetType();
  40. method = type.GetMethod(tname);
  41. con = context;
  42. if (method == null)
  43. {
  44. conError("找不到对应方法,服务器返回错误");
  45. return;
  46. }
  47. successFlag = false;
  48. context.Response.ContentType = "application/json";
  49. context.Response.Headers.Set("Access-Control-Allow-Origin", "*");
  50. try
  51. {
  52. method.Invoke(this, null);
  53. }
  54. catch (Exception ex)
  55. {
  56. conError("处理接口错误,服务器返回错误");
  57. }
  58. finally
  59. {
  60. //AfterInvoke(methodName);
  61. }
  62. //if (!successFlag) returnErrorMsg("服务器返回错误");
  63. }
  64. }
  65. /* static string pUrl = "http://tst.ds.gw.chenghuiyin.com/ordering";
  66. static string userName = "001008";
  67. static string password = "123234";*/
  68. static string pUrl = "http://ds.gw.chenghuiyin.com/ordering";
  69. static string userName = "001008";
  70. static string password = "lt666888";
  71. public static string getToken()
  72. {
  73. //return "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiYXBpIl0sInNjb3BlIjpbInJlYWQiLCJ3cml0ZSJdLCJleHAiOjE3MzU5NjU0ODAsImF1dGhvcml0aWVzIjpbIm9wZW5hcGkiXSwianRpIjoiYjY4NTRhZjctZjNmMy00OTU0LTk4ZmUtZDRlZTE4ZDllNzQyIiwiY2xpZW50X2lkIjoieGNsaWVudCJ9.E6U0NVMZPcUypl4P7L5718J1X8UxSJg9R7vZX_oMkuEJ-yelNXuYwu7-eAi1JTgIJ0GuuviQu-KEAH-W7sHb7t-JJJc-wLPMsLKmpwQDG2EpiRK5hW5ZmyUgL8rPuTUawKVP5k8SGrN60sVyXA2zKm4ztEQ39EGudXnam1Lld2w";
  74. string token = "";
  75. if (RedisHelper.HasKey("chy_token"))
  76. {
  77. token = RedisHelper.StringGet("chy_token").ToString();
  78. {
  79. return token;
  80. }
  81. }
  82. string post_url = pUrl + "/api/User/GetToken";
  83. string remoteInfo = "";
  84. try
  85. {
  86. remoteInfo = httpGetTokenContent(post_url, JsonConvert.SerializeObject(new { userName, password }));
  87. if (!string.IsNullOrEmpty(remoteInfo))
  88. {
  89. ChyTokenResponse chyToken = JsonConvert.DeserializeObject<ChyTokenResponse>(remoteInfo);
  90. token = chyToken.accessToken;
  91. TimeSpan timeSpan = TimeSpan.FromSeconds(chyToken.expiresIn - 5);
  92. RedisHelper.StringSet("chy_token", token, timeSpan);
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. XLog.SaveLog(0, ex.Message);
  98. }
  99. return token;
  100. }
  101. public static string updateExpressMark(string customerOrderId, string deliveryWayName)
  102. {
  103. string post_url = pUrl + "/api/ExpressAddress/UpdateExpressMark";
  104. string remoteInfo = "";
  105. string result = "修改成功";
  106. try
  107. {
  108. remoteInfo = httpContent(post_url, JsonConvert.SerializeObject(new { customerOrderId, deliveryWayName }));
  109. if (!string.IsNullOrEmpty(remoteInfo))
  110. {
  111. result = "修改失败";
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. result = "修改失败";
  117. }
  118. return result;
  119. }
  120. static HttpClient client = new HttpClient();
  121. private static string httpContent(string post_url, string content)
  122. {
  123. string token = getToken();
  124. var request = new HttpRequestMessage
  125. {
  126. Method = HttpMethod.Post,
  127. RequestUri = new Uri(post_url),
  128. Headers =
  129. {
  130. { "Authorization", "bearer " + token },
  131. { "ContentType", "application/json" },
  132. },
  133. Content = new StringContent(content)
  134. {
  135. Headers =
  136. {
  137. ContentType = new MediaTypeHeaderValue("application/json")
  138. }
  139. }
  140. };
  141. var task = Task.Run(() =>
  142. {
  143. try
  144. {
  145. var response = client.SendAsync(request).Result;
  146. response.EnsureSuccessStatusCode();
  147. return response.Content.ReadAsStringAsync().Result;
  148. }
  149. catch (Exception e)
  150. {
  151. XLog.SaveLog(0, "chyhttpContent:" + e.Message);
  152. return "找不到商品或已发货";
  153. }
  154. });
  155. return task.Result;
  156. }
  157. private static string httpGetTokenContent(string post_url, string content)
  158. {
  159. var request = new HttpRequestMessage
  160. {
  161. Method = HttpMethod.Post,
  162. RequestUri = new Uri(post_url),
  163. Headers =
  164. {
  165. { "ContentType", "application/json" },
  166. },
  167. Content = new StringContent(content)
  168. {
  169. Headers =
  170. {
  171. ContentType = new MediaTypeHeaderValue("application/json")
  172. }
  173. }
  174. };
  175. var task = Task.Run(() =>
  176. {
  177. try
  178. {
  179. var response = client.SendAsync(request).Result;
  180. response.EnsureSuccessStatusCode();
  181. return response.Content.ReadAsStringAsync().Result;
  182. }
  183. catch (Exception e)
  184. {
  185. XLog.SaveLog(0, "chyhttpContent:" + e.Message);
  186. return "";
  187. }
  188. });
  189. return task.Result;
  190. }
  191. }
  192. }