apiCaiYingTong.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. using BizCom;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using NHibernate.Cfg.MappingSchema;
  5. using NPOI.OpenXmlFormats.Shared;
  6. using NPOI.SS.Formula.Functions;
  7. using SiteCore.taoObj;
  8. using SQLData;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Reflection;
  16. using System.Reflection.Emit;
  17. using System.Security.Cryptography;
  18. using System.Text;
  19. using System.Web;
  20. using Utils;
  21. using static SiteCore.taoObj.Api_waybill_code_response_Obj;
  22. using static SiteCore.taoObj.work_core_vo;
  23. namespace SiteCore.Handler
  24. {
  25. public class apiCaiYingTong : BaseHandler, IHttpHandler
  26. {
  27. public void ProcessRequest(HttpContext context)
  28. {
  29. if (UrlParmsCheck("t"))
  30. {
  31. String tname = GetString("t");
  32. MethodInfo method;
  33. Type type = this.GetType();
  34. method = type.GetMethod(tname);
  35. con = context;
  36. if (method == null)
  37. {
  38. conError("找不到对应方法,服务器返回错误");
  39. return;
  40. }
  41. successFlag = false;
  42. context.Response.ContentType = "application/json";
  43. context.Response.Headers.Set("Access-Control-Allow-Origin", "*");
  44. try
  45. {
  46. method.Invoke(this, null);
  47. }
  48. catch (Exception ex)
  49. {
  50. conError("处理接口错误,服务器返回错误");
  51. }
  52. finally
  53. {
  54. //AfterInvoke(methodName);
  55. }
  56. //if (!successFlag) returnErrorMsg("服务器返回错误");
  57. }
  58. }
  59. static string appID = "1823193185001";
  60. //***************************************************
  61. //static string signkey = "fed5be35cf5bd04b14dcfff984f861dd";
  62. static HttpHelper tb_http = new HttpHelper();
  63. static string merSecret = "D8FF9498A5A2E6A6B4CB6EF88DB1DC35";
  64. static string pUrl = "https://front.lingtao8.com/designRest.htm";
  65. //static string pUrl = "https://localhost:8999/designRest.htm";
  66. //static string pUrl = "http://yi.yhmwacg.cn/designRest.htm";
  67. public static int DateTimeToUnixTime(DateTime dateTime)
  68. {
  69. return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds;
  70. }
  71. //public static string ToUrlParams(object jsonObject)
  72. //{
  73. // var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty); // 遍历JSON对象的属性,并添加到查询字符串中
  74. // foreach (var property in jsonObject.GetType().GetProperties())
  75. // {
  76. // queryString[property.Name] = property.GetValue(jsonObject)?.ToString() ?? string.Empty;
  77. // }
  78. // return queryString.ToString();
  79. //}
  80. public static Dictionary<string, object> ToDictionary(object obj)
  81. {
  82. var type = obj.GetType();
  83. var properties = type.GetProperties();
  84. var dictionary = new Dictionary<string, object>();
  85. foreach (var property in properties)
  86. {
  87. var value = property.GetValue(obj);
  88. dictionary.Add(property.Name, value);
  89. }
  90. return dictionary;
  91. }
  92. public static string ToUrlParams(Dictionary<string, object> jsonObject)
  93. {
  94. var queryString = new System.Collections.Specialized.NameValueCollection();
  95. // 遍历JSON对象的属性,并添加到查询字符串中
  96. foreach (var property in jsonObject)
  97. {
  98. var value = property.Value;
  99. if (value != null)
  100. {
  101. // 如果属性值不为null,则添加到查询字符串中
  102. if (value is System.Collections.IEnumerable && !(value is string))
  103. {
  104. // 如果是数组或集合类型,需要进一步处理
  105. var index = 0;
  106. foreach (var item in (System.Collections.IEnumerable)value)
  107. {
  108. queryString.Add(property.Key + "[" + index + "]", item != null ? item.ToString() : string.Empty);
  109. index++;
  110. }
  111. }
  112. else
  113. {
  114. // 普通属性
  115. queryString.Add(property.Key, value.ToString());
  116. }
  117. }
  118. }
  119. // 获取最终的查询字符串
  120. return string.Join("&", queryString.AllKeys.Select(key => key + "=" + queryString[key]));
  121. }
  122. public static string createSignCode(string method, string timeStr, string design_app_id, string design_secret)
  123. {
  124. byte[] use_bytes;
  125. //query.Append(secret);
  126. //预留看一下到时候签名怎么用md5加密;***************************************************
  127. string encryStr = design_secret + method + timeStr + design_app_id + design_secret;
  128. MD5 md5 = MD5.Create();
  129. use_bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(encryStr));
  130. StringBuilder result = new StringBuilder();
  131. for (int i = 0; i < use_bytes.Length; i++)
  132. {
  133. result.Append(use_bytes[i].ToString("x2"));
  134. }
  135. string singResult = result.ToString();
  136. return singResult.Replace("-", "");
  137. //return bytes.ToString();
  138. }
  139. //http://47.114.150.226:88/Handler/apiDesign.ashx?t=DesignSys_AddLog post请求参数(参数放body中):string tid(淘宝订单号)string userName(操作用户)string con(日志内容)int orderState(订单状态)
  140. public void DesignSys_AddLog()
  141. {
  142. if (UrlPostParmsCheck("tid"))
  143. {
  144. try
  145. {
  146. //HttpContext.Current.Request.BinaryRead()
  147. string tid = GetPostString("tid");
  148. string userName = GetPostString("userName");
  149. int orderState = GetPostInt("orderState");
  150. string con = GetPostString("con");
  151. //returnErrorMsg(tid + "," + userName + "," + orderState + "," + con);
  152. //return;
  153. try
  154. {
  155. CeErpTradeCell ceErpTradeCell = CeErpTradeCell.GetByCtid(tid);
  156. if (ceErpTradeCell != null)
  157. {
  158. if (userName != "")
  159. {
  160. CeErpUser ceErpUser = CeErpUser.GetByUserName(userName);
  161. if (ceErpUser != null)
  162. {
  163. LogHelper.addLog(tid, ceErpUser.ID, con, orderState);
  164. return;
  165. }
  166. else
  167. {
  168. returnErrorMsg("找不到对应用户");
  169. return;
  170. }
  171. }
  172. returnErrorMsg("找不到对应用户");
  173. }
  174. else
  175. {
  176. returnErrorMsg("找不到对应订单记录,核对淘宝订单号");
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. XLog.SaveLog(0, "xxxxx," + ex.Message);
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. XLog.SaveLog(0, ex.Message);
  187. }
  188. return;
  189. }
  190. returnErrorMsg("缺少必要的参数");
  191. }
  192. public void shipments_Api()
  193. {
  194. string tid = GetString("tid");
  195. string comCode = GetString("comCode");
  196. string out_Sid = GetString("out_Sid");
  197. string curUseWayBillCode = out_Sid;
  198. string cpCode = comCode;
  199. CeErpTradeCell entity = CeErpTradeCell.GetByCtid(tid);
  200. if (entity != null)
  201. {
  202. CeErpTrade mainEn = CeErpTrade.Get(entity.tid);
  203. string sql = "select top 1 * from view_erptradecell where tid='" + tid + "';";
  204. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql.ToString()).Tables[0];
  205. if (dt.Rows.Count <= 0) return;
  206. string posCode = dt.Rows[0]["posCode"].ToString();
  207. string apires = API_LogisticsOnlineSend(tid, posCode, comCode, out_Sid);
  208. CeErpMessageAPI ceErpMessageAPI = new CeErpMessageAPI();
  209. JObject jsonObject = new JObject
  210. {
  211. { "tid", tid},
  212. { "pCode", posCode},
  213. { "comCode", comCode },
  214. { "out_Sid", out_Sid }
  215. };
  216. ceErpMessageAPI.ctid = tid;
  217. ceErpMessageAPI.tid = tid;
  218. ceErpMessageAPI.creationtime = DateTime.Now;
  219. ceErpMessageAPI.content = apires;
  220. ceErpMessageAPI.Create();
  221. if (mainEn != null && entity.OrderState == 6 && apires.IndexOf("发货成功") != -1)
  222. {
  223. try
  224. {
  225. entity.OutSid = curUseWayBillCode;
  226. entity.OrderState = 7;
  227. entity.IsUrgency = false;
  228. entity.LastBillCpCode = "";
  229. entity.LastBillWaybillCode = "";
  230. entity.FinishDeliveryTime = DateTime.Now;
  231. entity.IsReturn = 0;
  232. entity.MemoOpt = 0;
  233. entity.Update();
  234. //还要插入快递信息到 快递信息表
  235. CeErpExpressInfo exinfo = new CeErpExpressInfo();
  236. exinfo.tid = entity.ctid;
  237. exinfo.out_sid = entity.OutSid;
  238. exinfo.company_code = cpCode;
  239. exinfo.company_name = cpCode;
  240. exinfo.supplierUserName = commonHelper.getSupplierNameById(entity.SupplierId);
  241. exinfo.deliveryType = "发货成功";
  242. exinfo.print_time = DateTime.Now;
  243. exinfo.printUser = "彩印通接口";
  244. exinfo.Create();
  245. commonHelper.UpdateRelationOrder(entity.ctid);
  246. CeErpSukuraData.createInfo(entity.ctid, 4);
  247. commonHelper.insertToBuchaForDelivery(mainEn.tid, mainEn.posCode, cpCode, curUseWayBillCode);
  248. }
  249. catch (Exception ex)
  250. {
  251. XLog.SaveLog(0, "发货成功后更新数据失败," + ex.Message);
  252. }
  253. }
  254. else if (mainEn.status == "SHIPPED" || mainEn.status == "PART_SHIPPED" || entity.OrderState >= 7) //已发货的不处理直接返回面单
  255. {
  256. entity.OutSid = (entity.OutSid + "," + curUseWayBillCode);
  257. if (entity.OrderState == 6)
  258. {
  259. entity.OrderState = 7;
  260. }
  261. entity.FinishDeliveryTime = DateTime.Now;
  262. entity.Update();
  263. CeErpExpressInfo exinfo = new CeErpExpressInfo();
  264. exinfo.tid = entity.ctid;
  265. exinfo.out_sid = entity.OutSid;
  266. exinfo.company_code = cpCode;
  267. exinfo.company_name = cpCode;
  268. exinfo.supplierUserName = commonHelper.getSupplierNameById(entity.SupplierId);
  269. exinfo.deliveryType = "发货成功";
  270. exinfo.print_time = DateTime.Now;
  271. exinfo.printUser = "彩印通接口";
  272. exinfo.Create();
  273. commonHelper.UpdateRelationOrder(entity.ctid);
  274. //不处理
  275. }
  276. else
  277. {
  278. entity.LastBillCpCode = cpCode;
  279. entity.LastBillWaybillCode = curUseWayBillCode;
  280. entity.Update();
  281. string errmsg = commonHelper.KeepChinese(apires);
  282. //returnErrorMsg("同步淘宝发货失败!"+ errmsg);
  283. XLog.SaveLog(0, "打单后发货失败," + entity.tid + "," + curUseWayBillCode + "," + apires);
  284. }
  285. returnSuccessMsg(apires);
  286. LogHelper.addLog(entity.ctid, 0, "彩印通发货-" + comCode + "-" + out_Sid, entity.OrderState);
  287. return;
  288. }
  289. else
  290. {
  291. returnErrorMsg("找不到对应订单记录,核对淘宝订单号");
  292. return;
  293. }
  294. return;
  295. }
  296. public void waybill_Api()
  297. {
  298. string tid = GetString("tid");
  299. string cpCode = GetString("cpCode");
  300. CeErpTradeCell entity = CeErpTradeCell.GetByCtid(tid);
  301. if (entity != null)
  302. {
  303. bool isUseLastWayBillCode = true;
  304. if ((entity.OrderState == 7 || entity.OrderState == 6))
  305. {
  306. isUseLastWayBillCode = false;
  307. }
  308. else
  309. {
  310. returnErrorMsg("订单状态错误无法获取面单");
  311. return;
  312. }
  313. if (entity.IsSF > 0 && cpCode.IndexOf("SF") == -1)
  314. {
  315. returnErrorMsg("此单标记使用顺丰发货,不能使用其他快递");
  316. return;
  317. }
  318. string res = "";
  319. Api_waybill_code_response_Obj fullObj = null;
  320. string pData_str = "";
  321. string curUseWayBillCode = "";
  322. if (isUseLastWayBillCode == false)
  323. {
  324. res = apiHelper.API_GetWaybill(cpCode, tid);
  325. try
  326. {
  327. if (res.IndexOf("failure") != -1)
  328. {
  329. //int idx = res.IndexOf("sub_message");
  330. //int idx2 = res.IndexOf("flag");
  331. //string emsg = res.Substring(idx + 14, idx2 - idx - 17);
  332. string errMsgNeed = commonHelper.KeepChinese(res);
  333. var res_objd = new
  334. {
  335. restype = 0,
  336. data = "获取面单失败" + errMsgNeed
  337. };
  338. string ro_jsond = JsonConvert.SerializeObject(res_objd);
  339. returnSuccess(ro_jsond);
  340. if (res.IndexOf("停发") != -1)
  341. {
  342. entity.UnusualCon = "物流停发";
  343. entity.Update();
  344. }
  345. //returnErrorMsg("获取面单失败,"+emsg);
  346. XLog.SaveLog(0, "获取面单失败" + res);
  347. return;
  348. }
  349. else if (res.IndexOf("errorMsg") != -1)
  350. {
  351. //int idx = res.IndexOf("errorMsg");
  352. //int idx2 = res.IndexOf("}]}");
  353. //string emsg = res.Substring(idx + 11, idx2 - idx - 12);
  354. string errMsgNeed = commonHelper.KeepChinese(res);
  355. var res_objf = new
  356. {
  357. restype = 0,
  358. data = "获取面单失败2" + errMsgNeed
  359. };
  360. string ro_jsonf = JsonConvert.SerializeObject(res_objf);
  361. if (res.IndexOf("停发") != -1)
  362. {
  363. entity.UnusualCon = "物流停发";
  364. entity.Update();
  365. CeErpTrade trade = CeErpTrade.Get(entity.tid);
  366. if (trade != null)
  367. {
  368. string apires = apiHelper.API_TradeMemoUpdate(trade.tid, trade.posCode, "", trade.seller_memo + "-" + entity.UnusualCon);
  369. }
  370. }
  371. returnSuccess(ro_jsonf);
  372. //returnErrorMsg("获取面单失败," + emsg);
  373. XLog.SaveLog(0, "获取面单失败2" + res);
  374. return;
  375. }
  376. res = tmcHelper.GetUTF8String(Encoding.UTF8.GetBytes(res));
  377. //res = res.Replace(" ", "");
  378. fullObj = JsonConvert.DeserializeObject<Api_waybill_code_response_Obj>(res);
  379. }
  380. catch (Exception ex)
  381. {
  382. XLog.SaveLog(0, "生成fullObj" + res);
  383. var res_objf = new
  384. {
  385. restype = 0,
  386. data = "生成fullObj发生错误" + ex.Message
  387. };
  388. string ro_jsonf = JsonConvert.SerializeObject(res_objf);
  389. returnSuccess(ro_jsonf);
  390. return;
  391. }
  392. if (fullObj.response.data.content.Count > 0)
  393. {
  394. ContentItem codeObj = fullObj.response.data.content[0];
  395. if (cpCode.IndexOf("SFFQ-") != -1)
  396. {
  397. pData_str = apiHelper.API_GetPrintData_SF(codeObj.waybillCode);
  398. }
  399. else
  400. pData_str = apiHelper.API_GetPrintData(codeObj.waybillCode);
  401. curUseWayBillCode = codeObj.waybillCode;
  402. LogHelper.addLog(entity.ctid, 0, "彩印通获取面单成功-" + cpCode + "-" + curUseWayBillCode, entity.OrderState);
  403. returnSuccess(pData_str);
  404. return;
  405. }
  406. else
  407. {
  408. XLog.SaveLog(0, "生成fullObj找不到快递单号waybillCode" + res);
  409. var res_objf = new
  410. {
  411. restype = 0,
  412. data = "生成fullObj找不到快递单号waybillCode"
  413. };
  414. string ro_jsonf = JsonConvert.SerializeObject(res_objf);
  415. returnSuccess(ro_jsonf);
  416. return;
  417. }
  418. }
  419. else
  420. {
  421. //使用上次打印的快递单号
  422. if (cpCode == "SFFQ-LY")
  423. {
  424. pData_str = apiHelper.API_GetPrintData_SF(entity.LastBillWaybillCode);
  425. }
  426. else
  427. pData_str = apiHelper.API_GetPrintData(entity.LastBillWaybillCode);
  428. curUseWayBillCode = entity.LastBillWaybillCode;
  429. }
  430. if (pData_str.IndexOf("props") == -1 && pData_str.IndexOf("printData") == -1)
  431. {
  432. var res_obje = new
  433. {
  434. restype = 0,
  435. data = "获取加密打印数据失败"
  436. };
  437. string ro_jsone = JsonConvert.SerializeObject(res_obje);
  438. returnSuccess(ro_jsone);
  439. entity.LastBillCpCode = cpCode;
  440. entity.LastBillWaybillCode = curUseWayBillCode;
  441. entity.Update();
  442. XLog.SaveLog(0, "获取加密打印数据失败" + curUseWayBillCode + pData_str);
  443. //returnErrorMsg("获取加密打印数据失败");
  444. return;
  445. }
  446. }
  447. else
  448. {
  449. returnErrorMsg("找不到对应订单记录,核对淘宝订单号");
  450. return;
  451. }
  452. return;
  453. }
  454. //http://47.114.150.226:88/Handler/apiCaiYingTong.ashx?t=afterCytSale ctid status supmemo
  455. public void afterCytSale()
  456. {
  457. string ctid = GetString("ctid");
  458. int st = GetInt("status");
  459. string afmemo = GetString("supmemo");
  460. string id = "0";
  461. CeErpTradeResponsible ceErpTradeResponsible = CeErpTradeResponsible.GetByUserId(ctid, 64, 1, 0);
  462. CeErpTradeCell entity = null;
  463. if (ceErpTradeResponsible != null)
  464. {
  465. id = ceErpTradeResponsible.ID.ToString();
  466. entity = CeErpTradeCell.GetByCtid(ceErpTradeResponsible.tid);
  467. if (ceErpTradeResponsible.VerifyState == 1)
  468. {
  469. returnErrorMsg("已超时自动认可");
  470. return;
  471. }
  472. }
  473. if (entity != null)
  474. {
  475. if (entity.AfterSaleState != 3 || ceErpTradeResponsible.VerifyState != 0)
  476. {
  477. returnErrorMsg("此单售后当前状态不可操作!");
  478. return;
  479. }
  480. entity.AfterSaleSupplierState = st;
  481. entity.AfterSaleSupplierMemo = afmemo;
  482. if (st == 1)
  483. {
  484. ceErpTradeResponsible.VerifyState = 1;
  485. ceErpTradeResponsible.VerifyTime = DateTime.Now;
  486. ceErpTradeResponsible.Update();
  487. bool isAll = commonHelper.tradeResponsibleAll(entity);
  488. if (isAll)
  489. {
  490. entity.AfterSaleState = 4;
  491. entity.FinishAfterSaleTime = DateTime.Now;
  492. if (entity.AfterSaleResSupId > 0)
  493. {
  494. entity.AfterSaleSupplierState = 1;
  495. }
  496. }
  497. }
  498. else
  499. {
  500. CeErpTradeResponsible.dateByTid(entity.ctid);
  501. CeErpTradeAfterSaleExtend ceErpTradeAfterSaleExtend = CeErpTradeAfterSaleExtend.getByTid(entity.ctid);
  502. if (ceErpTradeAfterSaleExtend != null)
  503. {
  504. }
  505. else
  506. {
  507. ceErpTradeAfterSaleExtend = new CeErpTradeAfterSaleExtend();
  508. ceErpTradeAfterSaleExtend.tid = entity.ctid;
  509. ceErpTradeAfterSaleExtend.Create();
  510. }
  511. entity.AfterSaleState = 2;
  512. }
  513. if (entity.AfterSaleState == 4)
  514. {
  515. LogHelper.addLog(entity.tid, 0, "彩印通接口完成售后", 0, 3);
  516. if (entity.AfterSaleReason.IndexOf("设计") > 0 || entity.AfterSalePayment > 0)
  517. {
  518. LogHelper.addDesignerBillLog(entity.tid, 0, "彩印通接口售后取消设计费", "彩印通接口", 1);
  519. }
  520. ApiVo apiVo = new ApiVo();
  521. apiVo.orderNumber = entity.ctid;
  522. apiVo.actionName = "afterOver";
  523. designHelper.API_WorkCore(apiVo); //afterOver
  524. }
  525. entity.Update();
  526. returnSuccessMsg("操作成功!");
  527. CeErpSukuraData.createInfo(entity.ctid, 8);
  528. LogHelper.addLog(entity.ctid, 0, "彩印通接口供应商" + (st == 1 ? "认可" : ("不认可," + afmemo)), 0, 3);
  529. LogHelper.AddAfterSaleLog(entity.ctid, Convert.ToInt32(id), "彩印通接口供应商" + (st == 1 ? "认可" : "不认可"), 0, st == 1 ? 0 : 1, afmemo);
  530. return;
  531. }
  532. returnErrorMsg("找不到订单记录");
  533. }
  534. public static string API_LogisticsOnlineSend(string orderid, string pCode, string comCode, string out_Sid)
  535. {
  536. if (string.IsNullOrWhiteSpace(orderid) || string.IsNullOrWhiteSpace(pCode) || string.IsNullOrWhiteSpace(comCode) || string.IsNullOrWhiteSpace(out_Sid))
  537. {
  538. return "参数不完整";
  539. }
  540. List<string> postLst = new List<string>();
  541. var res_obj = new
  542. {
  543. refOid = orderid,
  544. posCode = pCode,
  545. packages = new[]
  546. {
  547. new{ outSid=out_Sid,companyCode=comCode}
  548. }
  549. };
  550. string ro_json = JsonConvert.SerializeObject(res_obj);
  551. string res = apiHelper.Base_Request(ro_json, "ds.omni.erp.third.order.send");
  552. return res;
  553. }
  554. }
  555. }