banniuApiHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using BizCom;
  2. using Newtonsoft.Json;
  3. using NPOI.Util;
  4. using SiteCore.Handler;
  5. using SiteCore.taoObj;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.Specialized;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Reflection;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Web;
  16. namespace SiteCore
  17. {
  18. public class banniuApiHelper : BaseHandler, IHttpHandler
  19. {
  20. static string API_VERSION = "1.0";
  21. static string LOGISTICS_ACCESS_TOKEN = "";
  22. static string LOGISTICS_APP_KEY = "";
  23. static string LOGISTICS_SECRET_KEY = "";
  24. static string LOGISTICS_APP_ID = "";//物流同步
  25. static string REPRINT_LOGISTICS_PROJECT_ID = "";//重印单物流同步
  26. public void ProcessRequest(HttpContext context)
  27. {
  28. if (UrlParmsCheck("t"))
  29. {
  30. String tname = GetString("t");
  31. MethodInfo method;
  32. Type type = this.GetType();
  33. method = type.GetMethod(tname);
  34. con = context;
  35. if (method == null)
  36. {
  37. conError("找不到对应方法,服务器返回错误");
  38. return;
  39. }
  40. successFlag = false;
  41. context.Response.ContentType = "application/json";
  42. context.Response.Headers.Set("Access-Control-Allow-Origin", "*");
  43. try
  44. {
  45. method.Invoke(this, null);
  46. }
  47. catch (Exception ex)
  48. {
  49. conError("处理接口错误,服务器返回错误");
  50. }
  51. finally
  52. {
  53. }
  54. }
  55. }
  56. //get请求时需要包含所有请求参数 , post请求时只用包含 系统参数
  57. private static string getSign(Dictionary<string, string> map, string secret)
  58. {
  59. List<string> list = map.Keys.ToList();
  60. list.Sort();
  61. StringBuilder sb = new StringBuilder(secret);
  62. foreach (string item in list)
  63. {
  64. sb.Append(item).Append(map[item]);
  65. }
  66. sb.Append(secret);
  67. return CalculateMD5(sb.ToString());
  68. }
  69. private static string CalculateMD5(string source)
  70. {
  71. using (MD5 md5 = MD5.Create())
  72. {
  73. byte[] inputBytes = Encoding.UTF8.GetBytes(source);
  74. byte[] hashBytes = md5.ComputeHash(inputBytes);
  75. StringBuilder sb = new StringBuilder();
  76. foreach (byte b in hashBytes)
  77. {
  78. sb.Append(b.ToString("x2"));
  79. }
  80. return sb.ToString().ToUpperInvariant();
  81. }
  82. }
  83. private static NameValueCollection changeObject(Dictionary<string, string> map)
  84. {
  85. NameValueCollection PostVars = new NameValueCollection();
  86. foreach (string key in map.Keys)
  87. {
  88. PostVars.Add(key, map[key]);
  89. }
  90. return PostVars;
  91. }
  92. private static Dictionary<string, string> getSystemParams(string type)
  93. {
  94. Dictionary<string, string> map = new Dictionary<string, string>();
  95. if ("sendLogistics".Equals(type))
  96. {
  97. map.Add("method", "task.create");
  98. map.Add("access_token", LOGISTICS_ACCESS_TOKEN);
  99. map.Add("app_key", LOGISTICS_APP_KEY);
  100. map.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss"));
  101. map.Add("v", API_VERSION);
  102. }
  103. return map;
  104. }
  105. string base_url = "https://open.bytenew.com/gateway/api/miniAPI";
  106. public static BNApiResponseVo sendLogisticsInfo(CeErpTradeCell ceErpTradeCell)
  107. {
  108. Dictionary<string, string> map = getSystemParams("sendLogistics");
  109. string sign = getSign(map, LOGISTICS_SECRET_KEY);
  110. NameValueCollection PostVars = changeObject(map);
  111. WebClient wc = new WebClient();
  112. wc.Encoding = Encoding.GetEncoding("utf-8");
  113. string result = "";
  114. BNApiResponseVo response = null;
  115. try
  116. {
  117. }
  118. catch (Exception ex)
  119. {
  120. XLog.SaveLog(0, "API_GetPrintData_CreateOrder:" + ex.Message);
  121. }
  122. return null;
  123. }
  124. }
  125. }