478 lines
15 KiB
Plaintext
Executable File
478 lines
15 KiB
Plaintext
Executable File
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Sockets;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GuangFu.StringTracing.Common
|
|
{
|
|
|
|
public class CtripApi
|
|
{
|
|
#region 获取携程Ticket
|
|
public string GetCtripTicket()
|
|
{
|
|
string path = "https://ct.ctrip.com/SwitchAPI/Order/Ticket";
|
|
string result = string.Empty;
|
|
var body = @"{
|
|
" + "\n" +
|
|
@" ""appKey"": ""obk_GXHJ"",
|
|
" + "\n" +
|
|
@" ""appSecurity"": ""mD2!^w7CCGq)U%c*An*um%^T""
|
|
" + "\n" +
|
|
@"}";
|
|
HttpContent httpContent = new StringContent(body);
|
|
|
|
//设置Http的内容标头
|
|
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|
//设置Http的内容标头的字符
|
|
httpContent.Headers.ContentType.CharSet = "utf-8";
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
//异步Post
|
|
HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result;
|
|
//确保Http响应成功
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
//异步读取json
|
|
result = response.Content.ReadAsStringAsync().Result;
|
|
CtripRoot tokenData = JsonConvert.DeserializeObject<CtripRoot>(result);
|
|
return tokenData.Ticket;
|
|
}
|
|
else
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 获取携程全量国家数据
|
|
public string GetXCCountry(string Ticket)
|
|
{
|
|
string path = "https://ct.ctrip.com/switchAPI/basedata/v2/getcountry";
|
|
string result = string.Empty;
|
|
|
|
HttpContent httpContent = new StringContent("{\r\n \"Auth\": {\r\n \"AppKey\": \"obk_GXHJ\",\r\n \"Ticket\": \""+ Ticket + "\"\r\n },\r\n \"locale\": \"zh-CN\"\r\n}");
|
|
//设置Http的内容标头
|
|
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|
//设置Http的内容标头的字符
|
|
httpContent.Headers.ContentType.CharSet = "utf-8";
|
|
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
//异步Post
|
|
HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result;
|
|
//确保Http响应成功
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
//异步读取json
|
|
result = response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region 获取携程国内城市数据
|
|
public string GetXCChinaCity(string Ticket)
|
|
{
|
|
string path = "https://ct.ctrip.com/switchapi/basedata/v2/queryAllPOIInfo";
|
|
string result = string.Empty;
|
|
|
|
HttpContent httpContent = new StringContent("{\r\n \"auth\": {\r\n \"AppKey\": \"obk_GXHJ\",\r\n \"Ticket\": \""+ Ticket + "\"\r\n },\r\n \"provinceConditions\": {\r\n \"prefectureLevelCityConditions\": {\r\n \"returnDistrict\": false\r\n }\r\n },\r\n \"countryId\": 1,\r\n \"poiConditions\": {\r\n \"returnAirport\": false,\r\n \"returnTrainStation\": false,\r\n \"returnBusStation\": false\r\n }\r\n}");
|
|
//设置Http的内容标头
|
|
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|
//设置Http的内容标头的字符
|
|
httpContent.Headers.ContentType.CharSet = "utf-8";
|
|
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
//异步Post
|
|
HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result;
|
|
//确保Http响应成功
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
//异步读取json
|
|
result = response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region 提前审批接口
|
|
public static string PostData(string postString)
|
|
{
|
|
byte[] postData = Encoding.UTF8.GetBytes(postString);
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://ct.ctrip.com/switchapi/approval/save"));
|
|
|
|
request.Method = "POST";
|
|
|
|
request.ServicePoint.Expect100Continue = false;
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
request.ContentLength = postData.Length;
|
|
|
|
using (Stream requestStream = request.GetRequestStream())
|
|
{
|
|
requestStream.Write(postData, 0, postData.Length);
|
|
}
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
|
|
string srcString = stream.ReadToEnd();
|
|
|
|
response.Close();
|
|
|
|
stream.Close();
|
|
|
|
return srcString;
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region 预算数据落地
|
|
public static string SaveBudget(string postString)
|
|
{
|
|
byte[] postData = Encoding.UTF8.GetBytes(postString);
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://corpsz.ctrip.com/PreApproval/saveBudget"));
|
|
|
|
request.Method = "POST";
|
|
|
|
request.ServicePoint.Expect100Continue = false;
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
request.ContentLength = postData.Length;
|
|
|
|
using (Stream requestStream = request.GetRequestStream())
|
|
{
|
|
requestStream.Write(postData, 0, postData.Length);
|
|
}
|
|
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
|
|
|
string srcString = stream.ReadToEnd();
|
|
|
|
response.Close();
|
|
|
|
stream.Close();
|
|
|
|
return srcString;
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region 预算落地接口加密方法
|
|
/// <summary>
|
|
/// 预算落地接口加密方法
|
|
/// </summary>
|
|
/// <param name="approvalNumber">审批单号</param>
|
|
/// <param name="appSecurity">加密密钥</param>
|
|
/// <param name="budgetInfoList">预算信息列表</param>
|
|
/// <returns>加密结果</returns>
|
|
public string CreateSign(string approvalNumber, string appSecurity, List<BudgetInfo> budgetInfoList)
|
|
{
|
|
if (approvalNumber == null || appSecurity == null || budgetInfoList == null || budgetInfoList.Count == 0)
|
|
{
|
|
throw new ArgumentException("加密参数异常");
|
|
}
|
|
|
|
// 按 Products 排序
|
|
budgetInfoList.Sort((o1, o2) => o1.Products.CompareTo(o2.Products));
|
|
|
|
// 拼接预算字符串
|
|
var budgetStr = new StringBuilder();
|
|
foreach (var budgetInfo in budgetInfoList)
|
|
{
|
|
budgetStr.Append(budgetInfo.Products).Append(budgetInfo.BudgetAmount);
|
|
}
|
|
|
|
// 加密
|
|
string plainText = approvalNumber + budgetStr + Sha1Hex(appSecurity);
|
|
return Sha1Hex(plainText);
|
|
}
|
|
|
|
private static string Sha1Hex(string input)
|
|
{
|
|
using (var sha1 = SHA1.Create())
|
|
{
|
|
byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
|
|
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
#region 自定义变量
|
|
public class Status
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public bool Success { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int ErrorCode { get; set; }
|
|
/// <summary>
|
|
/// 调用成功。
|
|
/// </summary>
|
|
public string Message { get; set; }
|
|
}
|
|
|
|
public class CtripRoot
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Status Status { get; set; }
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string Ticket { get; set; }
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 携程接口请求模型
|
|
/// </summary>
|
|
public class CtripApprovalRequest
|
|
{
|
|
/// <summary>
|
|
/// 申请单号
|
|
/// </summary>
|
|
public string ApprovalNumber { get; set; }
|
|
|
|
/// <summary>
|
|
/// 状态:固定值1(有效)
|
|
/// </summary>
|
|
public int Status { get; set; } = 1;
|
|
|
|
/// <summary>
|
|
/// 员工工号
|
|
/// </summary>
|
|
public string EmployeeID { get; set; }
|
|
|
|
/// <summary>
|
|
/// 过期时间
|
|
/// </summary>
|
|
public DateTime ExpiredTime { get; set; }
|
|
|
|
/// <summary>
|
|
/// 备注
|
|
/// </summary>
|
|
public string Remark { get; set; }
|
|
|
|
/// <summary>
|
|
/// 鉴权对象
|
|
/// </summary>
|
|
public CtripAuth Auth { get; set; }
|
|
|
|
/// <summary>
|
|
/// 扩展字段列表
|
|
/// </summary>
|
|
public List<CtripExtendField> ExtendFieldList { get; set; }
|
|
/// <summary>
|
|
/// 机票行程明细列表
|
|
/// </summary>
|
|
public List<FlightEndorsementDetail> FlightEndorsementDetails { get; set; }
|
|
|
|
/// <summary>
|
|
/// 酒店行程明细列表
|
|
/// </summary>
|
|
public List<HotelEndorsementDetail> HotelEndorsementDetails { get; set; }
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 机票行程明细
|
|
/// </summary>
|
|
public class FlightEndorsementDetail
|
|
{
|
|
/// <summary>
|
|
/// 产品类型:固定FLIGHT
|
|
/// </summary>
|
|
public string ProductType { get; set; } = "FLIGHT";
|
|
|
|
///// <summary>
|
|
///// 行程类型:1-单程,2-往返
|
|
///// </summary>
|
|
//public int FlightWay { get; set; }
|
|
|
|
///// <summary>
|
|
///// 出发开始日期
|
|
///// </summary>
|
|
//public DateTime DepartDateBegin { get; set; }
|
|
|
|
///// <summary>
|
|
///// 出发结束日期
|
|
///// </summary>
|
|
//public DateTime DepartDateEnd { get; set; }
|
|
|
|
///// <summary>
|
|
///// 返程开始日期
|
|
///// </summary>
|
|
//public DateTime? ReturnDateBegin { get; set; }
|
|
|
|
///// <summary>
|
|
///// 返程结束日期
|
|
///// </summary>
|
|
//public DateTime? ReturnDateEnd { get; set; }
|
|
|
|
///// <summary>
|
|
///// 出发城市编码列表
|
|
///// </summary>
|
|
//public List<string> DepartCityIds { get; set; }
|
|
|
|
///// <summary>
|
|
///// 到达城市编码列表
|
|
///// </summary>
|
|
//public List<string> ArrivalCityIds { get; set; }
|
|
|
|
///// <summary>
|
|
///// 舱位等级:3-经济舱
|
|
///// </summary>
|
|
//public int SeatClass { get; set; } = 3;
|
|
|
|
/// <summary>
|
|
/// 出行人列表
|
|
/// </summary>
|
|
public List<PassengerInfo> PassengerList { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 酒店行程明细
|
|
/// </summary>
|
|
public class HotelEndorsementDetail
|
|
{
|
|
/// <summary>
|
|
/// 产品类型:固定HOTEL
|
|
/// </summary>
|
|
public string ProductType { get; set; } = "HOTEL";
|
|
|
|
///// <summary>
|
|
///// 入住开始日期
|
|
///// </summary>
|
|
//public DateTime CheckInDateBegin { get; set; }
|
|
|
|
///// <summary>
|
|
///// 入住结束日期
|
|
///// </summary>
|
|
//public DateTime CheckInDateEnd { get; set; }
|
|
|
|
///// <summary>
|
|
///// 离店开始日期
|
|
///// </summary>
|
|
//public DateTime CheckOutDateBegin { get; set; }
|
|
|
|
///// <summary>
|
|
///// 离店结束日期
|
|
///// </summary>
|
|
//public DateTime CheckOutDateEnd { get; set; }
|
|
|
|
///// <summary>
|
|
///// 入住城市编码列表
|
|
///// </summary>
|
|
//public List<string> CheckInCityCodes { get; set; }
|
|
|
|
/// <summary>
|
|
/// 出行人列表
|
|
/// </summary>
|
|
public List<PassengerInfo> PassengerList { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 出行人信息
|
|
/// </summary>
|
|
public class PassengerInfo
|
|
{
|
|
/// <summary>
|
|
/// 员工工号
|
|
/// </summary>
|
|
public string EID { get; set; }
|
|
|
|
/// <summary>
|
|
/// 英文姓名(暂不传)
|
|
/// </summary>
|
|
public string NameEn { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 携程鉴权对象
|
|
/// </summary>
|
|
public class CtripAuth
|
|
{
|
|
/// <summary>
|
|
/// 应用Key
|
|
/// </summary>
|
|
public string AppKey { get; set; } = "obk_GXHJ";
|
|
|
|
/// <summary>
|
|
/// 动态票据
|
|
/// </summary>
|
|
public string Ticket { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 携程扩展字段
|
|
/// </summary>
|
|
public class CtripExtendField
|
|
{
|
|
/// <summary>
|
|
/// 字段名
|
|
/// </summary>
|
|
public string FieldName { get; set; }
|
|
|
|
/// <summary>
|
|
/// 字段值
|
|
/// </summary>
|
|
public string FieldValue { get; set; }
|
|
}
|
|
public class CtripBudgetRequest
|
|
{
|
|
/// <summary>
|
|
/// 申请单号
|
|
/// </summary>
|
|
public string ApprovalNumber { get; set; }
|
|
|
|
/// <summary>
|
|
/// 签名
|
|
/// </summary>
|
|
public string Signature { get; set; }
|
|
|
|
/// <summary>
|
|
/// 鉴权对象
|
|
/// </summary>
|
|
public CtripAuth Auth { get; set; }
|
|
|
|
/// <summary>
|
|
/// 扩展字段列表
|
|
/// </summary>
|
|
public List<BudgetInfo> BudgetInfoList { get; set; }
|
|
|
|
}
|
|
|
|
public class BudgetInfo
|
|
{
|
|
public decimal BudgetAmount { get; set; }
|
|
public long Products { get; set; } = 12;
|
|
}
|
|
#endregion
|
|
} |