using BizCom; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NHibernate.Cfg.MappingSchema; using NPOI.OpenXmlFormats.Shared; using NPOI.SS.Formula.Functions; using SiteCore.taoObj; using SQLData; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Reflection.Emit; using System.Security.Cryptography; using System.Text; using System.Web; using Utils; using static SiteCore.taoObj.Api_waybill_code_response_Obj; using static SiteCore.taoObj.work_core_vo; namespace SiteCore.Handler { public class apiCaiYingTong : BaseHandler, IHttpHandler { public void ProcessRequest(HttpContext context) { if (UrlParmsCheck("t")) { String tname = GetString("t"); MethodInfo method; Type type = this.GetType(); method = type.GetMethod(tname); con = context; if (method == null) { conError("找不到对应方法,服务器返回错误"); return; } successFlag = false; context.Response.ContentType = "application/json"; context.Response.Headers.Set("Access-Control-Allow-Origin", "*"); try { method.Invoke(this, null); } catch (Exception ex) { conError("处理接口错误,服务器返回错误"); } finally { //AfterInvoke(methodName); } //if (!successFlag) returnErrorMsg("服务器返回错误"); } } static string appID = "1823193185001"; //*************************************************** //static string signkey = "fed5be35cf5bd04b14dcfff984f861dd"; static HttpHelper tb_http = new HttpHelper(); static string merSecret = "D8FF9498A5A2E6A6B4CB6EF88DB1DC35"; static string pUrl = "https://front.lingtao8.com/designRest.htm"; //static string pUrl = "https://localhost:8999/designRest.htm"; //static string pUrl = "http://yi.yhmwacg.cn/designRest.htm"; public static int DateTimeToUnixTime(DateTime dateTime) { return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds; } //public static string ToUrlParams(object jsonObject) //{ // var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty); // 遍历JSON对象的属性,并添加到查询字符串中 // foreach (var property in jsonObject.GetType().GetProperties()) // { // queryString[property.Name] = property.GetValue(jsonObject)?.ToString() ?? string.Empty; // } // return queryString.ToString(); //} public static Dictionary ToDictionary(object obj) { var type = obj.GetType(); var properties = type.GetProperties(); var dictionary = new Dictionary(); foreach (var property in properties) { var value = property.GetValue(obj); dictionary.Add(property.Name, value); } return dictionary; } public static string ToUrlParams(Dictionary jsonObject) { var queryString = new System.Collections.Specialized.NameValueCollection(); // 遍历JSON对象的属性,并添加到查询字符串中 foreach (var property in jsonObject) { var value = property.Value; if (value != null) { // 如果属性值不为null,则添加到查询字符串中 if (value is System.Collections.IEnumerable && !(value is string)) { // 如果是数组或集合类型,需要进一步处理 var index = 0; foreach (var item in (System.Collections.IEnumerable)value) { queryString.Add(property.Key + "[" + index + "]", item != null ? item.ToString() : string.Empty); index++; } } else { // 普通属性 queryString.Add(property.Key, value.ToString()); } } } // 获取最终的查询字符串 return string.Join("&", queryString.AllKeys.Select(key => key + "=" + queryString[key])); } public static string createSignCode(string method, string timeStr, string design_app_id, string design_secret) { byte[] use_bytes; //query.Append(secret); //预留看一下到时候签名怎么用md5加密;*************************************************** string encryStr = design_secret + method + timeStr + design_app_id + design_secret; MD5 md5 = MD5.Create(); use_bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(encryStr)); StringBuilder result = new StringBuilder(); for (int i = 0; i < use_bytes.Length; i++) { result.Append(use_bytes[i].ToString("x2")); } string singResult = result.ToString(); return singResult.Replace("-", ""); //return bytes.ToString(); } //http://47.114.150.226:88/Handler/apiDesign.ashx?t=DesignSys_AddLog post请求参数(参数放body中):string tid(淘宝订单号)string userName(操作用户)string con(日志内容)int orderState(订单状态) public void DesignSys_AddLog() { if (UrlPostParmsCheck("tid")) { try { //HttpContext.Current.Request.BinaryRead() string tid = GetPostString("tid"); string userName = GetPostString("userName"); int orderState = GetPostInt("orderState"); string con = GetPostString("con"); //returnErrorMsg(tid + "," + userName + "," + orderState + "," + con); //return; try { CeErpTradeCell ceErpTradeCell = CeErpTradeCell.GetByCtid(tid); if (ceErpTradeCell != null) { if (userName != "") { CeErpUser ceErpUser = CeErpUser.GetByUserName(userName); if (ceErpUser != null) { LogHelper.addLog(tid, ceErpUser.ID, con, orderState); return; } else { returnErrorMsg("找不到对应用户"); return; } } returnErrorMsg("找不到对应用户"); } else { returnErrorMsg("找不到对应订单记录,核对淘宝订单号"); } } catch (Exception ex) { XLog.SaveLog(0, "xxxxx," + ex.Message); } } catch (Exception ex) { XLog.SaveLog(0, ex.Message); } return; } returnErrorMsg("缺少必要的参数"); } public void shipments_Api() { string tid = GetString("tid"); string comCode = GetString("comCode"); string out_Sid = GetString("out_Sid"); string curUseWayBillCode = out_Sid; string cpCode = comCode; CeErpTradeCell entity = CeErpTradeCell.GetByCtid(tid); if (entity == null) { entity = CeErpTradeCell.GetByCode(tid); } if (entity != null) { CeErpTrade mainEn = CeErpTrade.Get(entity.tid); string posCode = mainEn.posCode; string apires = API_LogisticsOnlineSend(tid, posCode, comCode, out_Sid); CeErpMessageAPI ceErpMessageAPI = new CeErpMessageAPI(); JObject jsonObject = new JObject { { "tid", tid}, { "pCode", posCode}, { "comCode", comCode }, { "out_Sid", out_Sid } }; ceErpMessageAPI.ctid = tid; ceErpMessageAPI.tid = tid; ceErpMessageAPI.creationtime = DateTime.Now; ceErpMessageAPI.content = apires; ceErpMessageAPI.Create(); if (mainEn != null && entity.OrderState == 6 && apires.IndexOf("发货成功") != -1) { try { entity.OutSid = curUseWayBillCode; entity.OrderState = 7; entity.IsUrgency = false; entity.LastBillCpCode = ""; entity.LastBillWaybillCode = ""; entity.FinishDeliveryTime = DateTime.Now; entity.IsReturn = 0; entity.MemoOpt = 0; entity.Update(); //还要插入快递信息到 快递信息表 CeErpExpressInfo exinfo = new CeErpExpressInfo(); exinfo.tid = entity.ctid; exinfo.out_sid = entity.OutSid; exinfo.company_code = cpCode; exinfo.company_name = cpCode; exinfo.supplierUserName = commonHelper.getSupplierNameById(entity.SupplierId); exinfo.deliveryType = "发货成功"; exinfo.print_time = DateTime.Now; exinfo.printUser = "彩印通接口"; exinfo.Create(); commonHelper.UpdateRelationOrder(entity.ctid); CeErpSukuraData.createInfo(entity.ctid, 4); commonHelper.insertToBuchaForDelivery(mainEn.tid, mainEn.posCode, cpCode, curUseWayBillCode); } catch (Exception ex) { XLog.SaveLog(0, "发货成功后更新数据失败," + ex.Message); } } else if (mainEn.status == "SHIPPED" || mainEn.status == "PART_SHIPPED" || entity.OrderState >= 7) //已发货的不处理直接返回面单 { entity.OutSid = (entity.OutSid + "," + curUseWayBillCode); if (entity.OrderState == 6) { entity.OrderState = 7; } entity.FinishDeliveryTime = DateTime.Now; entity.Update(); CeErpExpressInfo exinfo = new CeErpExpressInfo(); exinfo.tid = entity.ctid; exinfo.out_sid = entity.OutSid; exinfo.company_code = cpCode; exinfo.company_name = cpCode; exinfo.supplierUserName = commonHelper.getSupplierNameById(entity.SupplierId); exinfo.deliveryType = "发货成功"; exinfo.print_time = DateTime.Now; exinfo.printUser = "彩印通接口"; exinfo.Create(); commonHelper.UpdateRelationOrder(entity.ctid); //不处理 } else { entity.LastBillCpCode = cpCode; entity.LastBillWaybillCode = curUseWayBillCode; entity.Update(); string errmsg = commonHelper.KeepChinese(apires); //returnErrorMsg("同步淘宝发货失败!"+ errmsg); XLog.SaveLog(0, "打单后发货失败," + entity.tid + "," + curUseWayBillCode + "," + apires); } returnSuccessMsg(apires); LogHelper.addLog(entity.ctid, 0, "彩印通发货-" + comCode + "-" + out_Sid, entity.OrderState); return; } else { returnErrorMsg("找不到对应订单记录,核对淘宝订单号"); return; } return; } public void waybill_Api() { string tid = GetString("tid"); string cpCode = GetString("cpCode"); CeErpTradeCell entity = CeErpTradeCell.GetByCtid(tid); if (entity != null) { bool isUseLastWayBillCode = true; if ((entity.OrderState == 7 || entity.OrderState == 6)) { isUseLastWayBillCode = false; } else { returnErrorMsg("订单状态错误无法获取面单"); return; } if (entity.IsSF > 0 && cpCode.IndexOf("SF") == -1) { returnErrorMsg("此单标记使用顺丰发货,不能使用其他快递"); return; } string res = ""; Api_waybill_code_response_Obj fullObj = null; string pData_str = ""; string curUseWayBillCode = ""; if (isUseLastWayBillCode == false) { res = apiHelper.API_GetWaybill(cpCode, tid); try { if (res.IndexOf("failure") != -1) { //int idx = res.IndexOf("sub_message"); //int idx2 = res.IndexOf("flag"); //string emsg = res.Substring(idx + 14, idx2 - idx - 17); string errMsgNeed = commonHelper.KeepChinese(res); var res_objd = new { restype = 0, data = "获取面单失败" + errMsgNeed }; string ro_jsond = JsonConvert.SerializeObject(res_objd); returnSuccess(ro_jsond); if (res.IndexOf("停发") != -1) { entity.UnusualCon = "物流停发"; entity.Update(); } //returnErrorMsg("获取面单失败,"+emsg); XLog.SaveLog(0, "获取面单失败" + res); return; } else if (res.IndexOf("errorMsg") != -1) { //int idx = res.IndexOf("errorMsg"); //int idx2 = res.IndexOf("}]}"); //string emsg = res.Substring(idx + 11, idx2 - idx - 12); string errMsgNeed = commonHelper.KeepChinese(res); var res_objf = new { restype = 0, data = "获取面单失败2" + errMsgNeed }; string ro_jsonf = JsonConvert.SerializeObject(res_objf); if (res.IndexOf("停发") != -1) { entity.UnusualCon = "物流停发"; entity.Update(); CeErpTrade trade = CeErpTrade.Get(entity.tid); if (trade != null) { //string apires = apiHelper.API_TradeMemoUpdate(trade.tid, trade.posCode, "", trade.seller_memo + "-" + entity.UnusualCon); } } returnSuccess(ro_jsonf); //returnErrorMsg("获取面单失败," + emsg); XLog.SaveLog(0, "获取面单失败2" + res); return; } res = tmcHelper.GetUTF8String(Encoding.UTF8.GetBytes(res)); //res = res.Replace(" ", ""); fullObj = JsonConvert.DeserializeObject(res); } catch (Exception ex) { XLog.SaveLog(0, "生成fullObj" + res); var res_objf = new { restype = 0, data = "生成fullObj发生错误" + ex.Message }; string ro_jsonf = JsonConvert.SerializeObject(res_objf); returnSuccess(ro_jsonf); return; } if (fullObj.response.data.content.Count > 0) { ContentItem codeObj = fullObj.response.data.content[0]; if (cpCode.IndexOf("SFFQ-") != -1) { pData_str = apiHelper.API_GetPrintData_SF(codeObj.waybillCode); } else pData_str = apiHelper.API_GetPrintData(codeObj.waybillCode); curUseWayBillCode = codeObj.waybillCode; LogHelper.addLog(entity.ctid, 0, "彩印通获取面单成功-" + cpCode + "-" + curUseWayBillCode, entity.OrderState); returnSuccess(pData_str); return; } else { XLog.SaveLog(0, "生成fullObj找不到快递单号waybillCode" + res); var res_objf = new { restype = 0, data = "生成fullObj找不到快递单号waybillCode" }; string ro_jsonf = JsonConvert.SerializeObject(res_objf); returnSuccess(ro_jsonf); return; } } else { //使用上次打印的快递单号 if (cpCode == "SFFQ-LY") { pData_str = apiHelper.API_GetPrintData_SF(entity.LastBillWaybillCode); } else pData_str = apiHelper.API_GetPrintData(entity.LastBillWaybillCode); curUseWayBillCode = entity.LastBillWaybillCode; } if (pData_str.IndexOf("props") == -1 && pData_str.IndexOf("printData") == -1) { var res_obje = new { restype = 0, data = "获取加密打印数据失败" }; string ro_jsone = JsonConvert.SerializeObject(res_obje); returnSuccess(ro_jsone); entity.LastBillCpCode = cpCode; entity.LastBillWaybillCode = curUseWayBillCode; entity.Update(); XLog.SaveLog(0, "获取加密打印数据失败" + curUseWayBillCode + pData_str); //returnErrorMsg("获取加密打印数据失败"); return; } } else { returnErrorMsg("找不到对应订单记录,核对淘宝订单号"); return; } return; } //http://47.114.150.226:88/Handler/apiCaiYingTong.ashx?t=afterCytSale ctid status supmemo public void afterCytSale() { string ctid = GetString("ctid"); int st = GetInt("status"); string afmemo = GetString("supmemo"); string id = "0"; CeErpTradeCell entity = CeErpTradeCell.GetByCtid(ctid); if (entity == null) { entity = CeErpTradeCell.GetByCode(ctid); if (entity == null) { returnErrorMsg("找不到订单记录"); return; } } CeErpTradeResponsible ceErpTradeResponsible = CeErpTradeResponsible.GetByUserId(entity.ctid, 64, 1, 0); if (ceErpTradeResponsible == null) { ceErpTradeResponsible = CeErpTradeResponsible.GetByUserId(entity.ctid, 80, 1, 0); } if (ceErpTradeResponsible != null) { id = ceErpTradeResponsible.ID.ToString(); if (ceErpTradeResponsible.VerifyState == 1) { returnErrorMsg("已超时自动认可"); return; } } if (entity != null && ceErpTradeResponsible != null) { if (entity.AfterSaleState != 3 || ceErpTradeResponsible.VerifyState != 0) { returnErrorMsg("此单售后当前状态不可操作!"); return; } entity.AfterSaleSupplierState = st; entity.AfterSaleSupplierMemo = afmemo; if (st == 1) { ceErpTradeResponsible.VerifyState = 1; ceErpTradeResponsible.VerifyTime = DateTime.Now; ceErpTradeResponsible.Update(); bool isAll = commonHelper.tradeResponsibleAll(entity); if (isAll) { entity.AfterSaleState = 4; entity.AfterSaleSupplierState = 1; } } else { CeErpTradeResponsible.dateByTid(entity.ctid); string numSql = string.Format("SELECT COUNT ( * ) AS BackNum FROM dbo.Ce_ErpTradeAfterSaleLog WHERE tid = '{0}' AND Con in ('彩印通接口供应商不认可')", ceErpTradeResponsible.tid); DataTable dt = DbHelper.DbConn.ExecuteDataset(numSql).Tables[0]; int num = Convert.ToInt32(dt.Rows[0]["BackNum"]); if (num <= 1) { entity.AfterSaleState = 1; } else { entity.AfterSaleState = 5; } CeErpTradeAfterSaleExtend ceErpTradeAfterSaleExtend = CeErpTradeAfterSaleExtend.getByTid(entity.ctid); if (ceErpTradeAfterSaleExtend != null) { } else { ceErpTradeAfterSaleExtend = new CeErpTradeAfterSaleExtend(); ceErpTradeAfterSaleExtend.tid = entity.ctid; ceErpTradeAfterSaleExtend.Create(); } } if (entity.AfterSaleState == 4) { LogHelper.addLog(entity.tid, 0, "彩印通接口完成售后", 0, 3); if (entity.AfterSaleReason.IndexOf("设计") > 0 || entity.AfterSalePayment > 0) { LogHelper.addDesignerBillLog(entity.tid, 0, "彩印通接口售后取消设计费", "彩印通接口", 1); } ApiVo apiVo = new ApiVo(); apiVo.orderNumber = entity.ctid; apiVo.actionName = "afterOver"; designHelper.API_WorkCore(apiVo); //afterOver } entity.Update(); returnSuccessMsg("操作成功!"); CeErpSukuraData.createInfo(entity.ctid, 8); LogHelper.addLog(entity.ctid, 0, "彩印通接口供应商" + (st == 1 ? "认可" : ("不认可," + afmemo)), 0, 3); LogHelper.AddAfterSaleLog(entity.ctid, Convert.ToInt32(id), "彩印通接口供应商" + (st == 1 ? "认可" : "不认可"), 0, st == 1 ? 0 : 1, afmemo); return; } returnErrorMsg("找不到订单记录"); } //http://47.114.150.226:88/Handler/apiCaiYingTong.ashx?t=getMergeOrders orderSn public void getMergeOrders() { string orderSn = GetString("orderSn"); CeErpTradeCell ceErpTradeCell = CeErpTradeCell.GetByCode(orderSn); if (ceErpTradeCell == null) { ceErpTradeCell = CeErpTradeCell.GetByCtid(orderSn); } if (ceErpTradeCell == null) { returnErrorMsg("查无订单!"); return; } if (ceErpTradeCell.SupplierId != 64) { returnErrorMsg("查无订单!"); return; } CeErpTrade ceErpTrade = CeErpTrade.Get(ceErpTradeCell.tid); DataTable dt = commonHelper.getSameOrderList(ceErpTradeCell, ceErpTrade); if (dt != null && dt.Rows.Count > 1) { ReturnSuccess("{" + string.Format("\"data\":{0}", Utils.Serialization.JsonString.DataTable2MiniAjaxJson(dt)) + "}"); return; } ReturnSuccess("{" + string.Format("\"data\":{0}", Utils.Serialization.JsonString.DataTable2MiniAjaxJson(new DataTable())) + "}"); return; } public static string API_LogisticsOnlineSend(string orderid, string pCode, string comCode, string out_Sid) { if (string.IsNullOrWhiteSpace(orderid) || string.IsNullOrWhiteSpace(pCode) || string.IsNullOrWhiteSpace(comCode) || string.IsNullOrWhiteSpace(out_Sid)) { return "参数不完整"; } List postLst = new List(); var res_obj = new { refOid = orderid, posCode = pCode, packages = new[] { new{ outSid=out_Sid,companyCode=comCode} } }; string ro_json = JsonConvert.SerializeObject(res_obj); string res = apiHelper.Base_Request(ro_json, "ds.omni.erp.third.order.send"); return res; } } }