243 lines
7.9 KiB
C#
243 lines
7.9 KiB
C#
using System.Text;
|
|
using YieldChain.Models;
|
|
using YLErp.Modules.ClientModule;
|
|
using YLErp.Modules.TradeModule.OrderModule;
|
|
using System.Net.Http.Json;
|
|
|
|
namespace YLErp.Forms
|
|
{
|
|
public partial class FormMgrApiTest : Form
|
|
{
|
|
public FormMgrApiTest()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
SetPropertyGrid1(null);
|
|
}
|
|
|
|
private async void btnSearch_Click(object sender, EventArgs e)
|
|
{
|
|
await Execute();
|
|
}
|
|
|
|
private async Task<object> Execute()
|
|
{
|
|
string accessToken;
|
|
|
|
var writer = new RichTextBoxWriter(richTextBox1).Clear();
|
|
|
|
if (cmbApiPath.SelectedIndex < 0)
|
|
{
|
|
return writer.Write("请选择API");
|
|
}
|
|
|
|
try
|
|
{
|
|
accessToken = await GetAccessToken(tbApiBaseUrl.Text.Trim(), tbUserName.Text.Trim(), tbUserPwd.Text.Trim());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return writer.Write(ex.ToString());
|
|
}
|
|
|
|
InnerHelper.SaveSetting(nameof(FormMgrApiTest) + ".txt", $"baseUrl={tbApiBaseUrl.Text}&userName={tbUserName.Text}&userPwd={tbUserPwd.Text}");
|
|
|
|
writer.WriteLine("获取accessToken:").WriteLine(accessToken).WriteLine();
|
|
|
|
try
|
|
{
|
|
ApiRequest(tbApiBaseUrl.Text.Trim(), cmbApiPath.Text, accessToken, propertyGrid1.SelectedObject, writer);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return writer.Write(ex.ToString());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static async Task<string> GetAccessToken(string baseUrl, string userName, string userPwd)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(baseUrl))
|
|
{
|
|
throw new Exception("API基网址 必须填写");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(userName))
|
|
{
|
|
throw new Exception("系统用户 必须填写");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(userPwd))
|
|
{
|
|
throw new Exception("用户密码 必须填写");
|
|
}
|
|
|
|
var uri = new Uri(new Uri(baseUrl), "/api/token");
|
|
var auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + userPwd));
|
|
|
|
var httpContent = new StringContent(string.Empty, Encoding.UTF8, "application/json");
|
|
httpContent.Headers.Add("authorization", "Basic " + auth);
|
|
|
|
var httpClient = Program.GetService<IHttpClientFactory>().CreateClient();
|
|
|
|
var response = await httpClient.PostAsync(uri, httpContent);
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ApiTokenResponseModel>();
|
|
|
|
if (result.errcode > 0)
|
|
{
|
|
throw new Exception(result.errmsg ?? "发生错误");
|
|
}
|
|
|
|
return result.accessToken;
|
|
}
|
|
|
|
private static async void ApiRequest(string baseUrl, string apiPath, string accessToken, object postData, RichTextBoxWriter writer)
|
|
{
|
|
var uri = new Uri(new Uri(baseUrl), apiPath.TrimStart('/'));
|
|
|
|
writer.Write("url: ").WriteLine(uri.AbsoluteUri).WriteLine();
|
|
|
|
var content = string.Empty;
|
|
if (postData != null)
|
|
{
|
|
content = JsonHelper.ToJson(postData);
|
|
|
|
writer.WriteLine("postData: ").WriteLine(content);
|
|
}
|
|
else
|
|
{
|
|
writer.Write("postData: null").WriteLine();
|
|
}
|
|
|
|
var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
|
|
httpContent.Headers.Add("Authorization", "Bearer " + accessToken);
|
|
httpContent.Headers.Add("Accept-Encoding", "gzip");
|
|
|
|
writer.WriteLine();
|
|
|
|
var httpClient = Program.GetService<IHttpClientFactory>().CreateClient();
|
|
|
|
var response = await httpClient.PostAsync(uri, httpContent);
|
|
|
|
var result = await response.Content.ReadAsStringAsync();
|
|
|
|
writer.WriteLine("result:").WriteLine(result);
|
|
}
|
|
|
|
private void FormMgrApiTest_Load(object sender, EventArgs e)
|
|
{
|
|
var setting = InnerHelper.ReadSetting(nameof(FormMgrApiTest) + ".txt");
|
|
var nv = YieldChain.Helpers.UrlHelper.ParseQueryString(setting);
|
|
if (nv != null)
|
|
{
|
|
tbApiBaseUrl.Text = nv["baseUrl"];
|
|
tbUserName.Text = nv["userName"];
|
|
tbUserPwd.Text = nv["userPwd"];
|
|
}
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
var frm = new FormDataImport();
|
|
if (DialogResult.OK == frm.ShowDialog())
|
|
{
|
|
SetPropertyGrid1(frm);
|
|
}
|
|
}
|
|
|
|
private void SetPropertyGrid1(IDataImport dataImport)
|
|
{
|
|
object obj = null;
|
|
switch (cmbApiPath.Text)
|
|
{
|
|
case "api/v1/clientPositionList":
|
|
case "api/v2/clientPositionList":
|
|
if (!string.IsNullOrWhiteSpace(dataImport?.Content))
|
|
{
|
|
try
|
|
{
|
|
if (dataImport.ContentType == ContentType.json)
|
|
{
|
|
obj = JsonHelper.Deserialize<ClientPositionQueryModel>(dataImport.Content);
|
|
}
|
|
else
|
|
{
|
|
obj = ParseFormData<ClientPositionQueryModel>(dataImport.Lines);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("解析数据出错:" + ex);
|
|
}
|
|
}
|
|
if (obj == null)
|
|
{
|
|
obj = new ClientPositionQueryModel();
|
|
}
|
|
break;
|
|
case "api/v1/order/option":
|
|
if (!string.IsNullOrWhiteSpace(dataImport?.Content))
|
|
{
|
|
try
|
|
{
|
|
if (dataImport.ContentType == ContentType.json)
|
|
{
|
|
obj = JsonHelper.Deserialize<OtcOptionTradeFullEx>(dataImport.Content);
|
|
}
|
|
else
|
|
{
|
|
obj = ParseFormData<OtcOptionTradeFullEx>(dataImport.Lines);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("解析数据出错:" + ex);
|
|
}
|
|
}
|
|
if (obj == null)
|
|
{
|
|
obj = new OtcOptionTradeFullEx();
|
|
}
|
|
break;
|
|
default:
|
|
obj = new object();
|
|
break;
|
|
}
|
|
propertyGrid1.SelectedObject = obj;
|
|
}
|
|
|
|
private static T ParseFormData<T>(string[] lines)
|
|
{
|
|
var list = new List<KeyValuePair<string, string>>();
|
|
|
|
foreach (var line in lines)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var index = line.IndexOf(':');
|
|
|
|
if (index < 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
list.Add(new KeyValuePair<string, string>(line.Substring(0, index).Trim(), line.Substring(index + 1).Trim()));
|
|
}
|
|
|
|
//var fc = new FormDataCollection(list);
|
|
|
|
//return fc.ReadAs<T>();
|
|
|
|
return default;
|
|
}
|
|
}
|
|
}
|