------ #### SuperDesign V3.0.2412.2001 - *.[新增]新增程序更新日志设置和自动发布功能。 - *.[修复]修复Post数据格式不正确时双击文本框会导致软件闪退的BUG。
490 lines
19 KiB
C#
490 lines
19 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using ryCommon;
|
|
using ryCommonDb;
|
|
using 开发辅助工具.Controls;
|
|
using 开发辅助工具.Tools.API;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using 开发辅助工具.Manager;
|
|
using 开发辅助工具.Tools;
|
|
using SuperDesign.Manager;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace 开发辅助工具.Tools
|
|
{
|
|
public partial class APIEditor : Form
|
|
{
|
|
public APIEditor()
|
|
{
|
|
InitializeComponent();
|
|
OlvTitle.AspectGetter = delegate (object x) { return ((PostInfo)x).Title; };
|
|
OlvParamName.AspectGetter = delegate (object x) { return ((PostInfo)x).ParamName; };
|
|
OlvParamValue.AspectGetter = delegate (object x) { return ((PostInfo)x).ParamValue; };
|
|
OlvParamValue.AspectToStringConverter = delegate (object rowdata,object x) {
|
|
var row = (PostInfo)rowdata;
|
|
if (row.ParamValue.IsInt())
|
|
{
|
|
long time = row.ParamValue.ToInt64();
|
|
if (time <= 100000) { return row.ParamValue; }
|
|
var dt = DateTime.MinValue;
|
|
if (row.ParamValue.Length == 13)
|
|
{
|
|
dt =RyDate.JSTimeToDateTime(time);
|
|
}
|
|
else
|
|
{
|
|
dt = time.ToDateTime();
|
|
}
|
|
if (dt == DateTime.MinValue || dt<=new DateTime(2000,1,1) || dt >= new DateTime(2100, 1, 1)) { return row.ParamValue; }
|
|
if (dt == dt.Date) {return dt.ToString("yyyy-MM-dd"); }
|
|
return dt.ToString("yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
return row.ParamValue;
|
|
};
|
|
OlvDesc.AspectGetter = delegate (object x) { return ((PostInfo)x).Desc; };
|
|
LvPostData.FormatCell += LvPostData_FormatCell;
|
|
}
|
|
Font bold_font = new Font("Consolas",9,FontStyle.Bold);
|
|
private void LvPostData_FormatCell(object sender, BrightIdeasSoftware.FormatCellEventArgs e)
|
|
{
|
|
if(e.Column ==OlvParamName)
|
|
{
|
|
e.SubItem.Font = bold_font;
|
|
e.SubItem.BackColor = Color.FromArgb(255, 255, 192);
|
|
}
|
|
else if (e.Column == OlvTitle || e.Column==OlvDesc)
|
|
{
|
|
e.SubItem.ForeColor = Color.Gray;
|
|
}
|
|
}
|
|
|
|
public void LoadData(Uri uri,string postdata,bool isGetMode=false)
|
|
{
|
|
var host = uri.Host.ToLower();
|
|
if (host.IndexOfEx("www.87g") == 0 || host.IndexOfEx("www.3h3") == 0
|
|
|| host.IndexOfEx("www.downxia") == 0 || host.IndexOfEx("www.downbank") == 0)
|
|
{
|
|
if (isGetMode)
|
|
{
|
|
TxtUrl.Text = "<site>" + uri.AbsolutePath;
|
|
}
|
|
else
|
|
{
|
|
TxtUrl.Text = "<site>" + uri.PathAndQuery;
|
|
}
|
|
var urlparam = RyWeb.WebDecode.UrlToData(uri.OriginalString);
|
|
TxtAPIName.Text = urlparam.Get("a");
|
|
if (TxtAPIName.Text.Length==0)
|
|
{
|
|
TxtAPIName.Text = urlparam.Get("api");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (isGetMode)
|
|
{
|
|
TxtUrl.Text = uri.Scheme+"://" +uri.Host+(uri.Port==80?"":(":"+uri.Port))+ uri.AbsolutePath;
|
|
}
|
|
else
|
|
{
|
|
TxtUrl.Text = uri.OriginalString;
|
|
}
|
|
}
|
|
LvPostData.ClearObjects();
|
|
IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
|
|
if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1)
|
|
{
|
|
RyQuickSQL mySQL = new RyQuickSQL("api");
|
|
mySQL.AddField("Name", TxtName.Text);
|
|
mySQL.AddField("Url", TxtUrl.Text);
|
|
DataSet ds = db.ReadData("select * from api where Url=@Url", mySQL);
|
|
var json_post = "";
|
|
if (ds.HaveData())
|
|
{
|
|
var row = ds.GetFirstRowData();
|
|
TxtName.Text = row["Name"].ToString();
|
|
TxtAPIName.Text = row["APIName"].ToString();
|
|
TxtDesc.Text = row["desc"].ToString();
|
|
json_post = row["Postdata"].ToString();
|
|
}
|
|
JObject jo;
|
|
if (json_post.Length == 0) { jo = new JObject(); }
|
|
else { jo = JObject.Parse(json_post); }
|
|
Dictionary<string, PostInfo> dict_post = new Dictionary<string, PostInfo>();
|
|
if (jo.ContainsKey("list"))
|
|
{
|
|
JArray jarr = JArray.Parse(jo["list"].ToString());
|
|
for (int i = 0; i < jarr.Count; i++)
|
|
{
|
|
var item = jarr[i];
|
|
var param_name = item["paramname"].ToString();
|
|
if (!dict_post.ContainsKey(param_name))
|
|
{
|
|
dict_post.Add(param_name, new PostInfo()
|
|
{
|
|
Title = item["title"].ToString(),
|
|
ParamName = param_name,
|
|
Desc = item["desc"].ToString(),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
var post = RyWeb.WebDecode.UrlToData(isGetMode?uri.OriginalString:("f?" + postdata));
|
|
List<KeyValuePair<string, string>> post_list;
|
|
if (post.Item2 == null)
|
|
{
|
|
post_list = new List<KeyValuePair<string, string>>();
|
|
}
|
|
else
|
|
{
|
|
post_list = post.Item2.ToList();
|
|
}
|
|
List<PostInfo> list = new List<PostInfo>();
|
|
if (post_list.Count > 0)
|
|
{
|
|
for (int i = 0; i < post_list.Count; i++)
|
|
{
|
|
var item = post_list[i];
|
|
var title = "";
|
|
var desc = "";
|
|
if (dict_post.ContainsKey(item.Key))
|
|
{
|
|
var value = dict_post[item.Key];
|
|
title = value.Title;
|
|
desc= value.Desc;
|
|
}
|
|
else
|
|
{
|
|
title= APIManager.GetParamTitle(item.Key);
|
|
}
|
|
list.Add(new PostInfo() { ParamName = item.Key, ParamValue = item.Value,Title=title,Desc=desc });
|
|
}
|
|
}
|
|
LvPostData.AddObjects(list);
|
|
}
|
|
db.Free();
|
|
}
|
|
private void BtnCancel_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult= DialogResult.Cancel;
|
|
}
|
|
|
|
public string SelectedPostDatas { get; set; } = "";
|
|
private void BtnOK_Click(object sender, EventArgs e)
|
|
{
|
|
if(LvPostData.GetItemCount()==0)
|
|
{
|
|
MessageBox.Show("POST内容为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
return;
|
|
}
|
|
IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
|
|
if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1)
|
|
{
|
|
RyQuickSQL mySQL = new RyQuickSQL("api");
|
|
mySQL.AddField("Name", TxtName.Text);
|
|
mySQL.AddField("Url", TxtUrl.Text);
|
|
mySQL.AddField("desc", TxtDesc.Text);
|
|
DataSet ds = db.ReadData("select * from api where Url=@Url", mySQL);
|
|
var json_post = "";
|
|
if(ds.HaveData())
|
|
{
|
|
json_post = ds.GetFirstRowData()["Postdata"].ToString();
|
|
}
|
|
mySQL.AddField("APIName",TxtAPIName.Text);
|
|
JObject jo;
|
|
if (json_post.Length == 0) { jo = new JObject(); }
|
|
else { jo = JObject.Parse(json_post); }
|
|
Dictionary<string,PostInfo> dict_post = new Dictionary<string, PostInfo>();
|
|
if(jo.ContainsKey("list"))
|
|
{
|
|
JArray jarr = JArray.Parse(jo["list"].ToString());
|
|
for (int i = 0; i < jarr.Count; i++)
|
|
{
|
|
var item = jarr[i];
|
|
var param_name = item["paramname"].ToString("");
|
|
if (!dict_post.ContainsKey(param_name))
|
|
{
|
|
dict_post.Add(param_name, new PostInfo()
|
|
{
|
|
Title = item["title"].ToString(""),
|
|
ParamName = param_name,
|
|
Desc = item["desc"].ToString(""),
|
|
ParamValue = item["paramvalue"].ToString(""),
|
|
IsAdd = false
|
|
}) ;
|
|
}
|
|
}
|
|
}
|
|
var list = LvPostData.ObjectsList;
|
|
var post = "";
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
var item = (PostInfo)list[i];
|
|
if (post.Length > 0) { post += "&"; }
|
|
if (item.ParamValue.IsEngOrNum())
|
|
{
|
|
post += item.ParamName + "=" + item.ParamValue;
|
|
}
|
|
else
|
|
{
|
|
post += item.ParamName + "=" +RyWeb.WebDecode.UrlEncode(item.ParamValue,Encoding.UTF8);
|
|
}
|
|
if(dict_post.ContainsKey(item.ParamName))
|
|
{
|
|
item.ParamValue = dict_post[item.ParamName].ParamValue;
|
|
dict_post[item.ParamName] = item;
|
|
}
|
|
else
|
|
{
|
|
dict_post.Add(item.ParamName,item);
|
|
}
|
|
}
|
|
SelectedPostDatas = post;
|
|
JObject jo_new=new JObject();
|
|
JArray jarr_new=new JArray();
|
|
foreach (var item in dict_post)
|
|
{
|
|
JObject jo_item = new JObject
|
|
{
|
|
{ "paramname", item.Value.ParamName },
|
|
{ "title", item.Value.Title },
|
|
{ "paramvalue", item.Value.ParamValue },
|
|
{ "desc", item.Value.Desc }
|
|
};
|
|
jarr_new.Add(jo_item);
|
|
}
|
|
jo_new.Add("list",jarr_new);
|
|
mySQL.AddField("Postdata", jo_new.ToString());
|
|
if (db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where Url=@Url", mySQL) == 0)
|
|
{
|
|
mySQL.AddField("ClickCount", 0);//点击次数
|
|
mySQL.AddField("editTime", DateTime.Now);
|
|
mySQL.AddField("addTime", DateTime.Now);
|
|
db.Insert(mySQL);
|
|
}
|
|
}
|
|
db.Free();
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
|
|
private void LvPostData_CellEditFinishing(object sender, BrightIdeasSoftware.CellEditEventArgs e)
|
|
{
|
|
if (e.Cancel) { return; }
|
|
var row = (PostInfo)e.RowObject;
|
|
if (e.Column==OlvTitle)
|
|
{
|
|
row.Title = e.NewValue.ToString();
|
|
}
|
|
else if (e.Column == OlvParamName)
|
|
{
|
|
row.ParamName = e.NewValue.ToString();
|
|
}
|
|
else if (e.Column == OlvParamValue)
|
|
{
|
|
row.ParamValue = e.NewValue.ToString();
|
|
}
|
|
else if (e.Column == OlvDesc)
|
|
{
|
|
row.Desc = e.NewValue.ToString();
|
|
}
|
|
}
|
|
|
|
private void BtnDel_Click(object sender, EventArgs e)
|
|
{
|
|
if (LvPostData.SelectedObject == null) { return; }
|
|
switch (MessageBox.Show("是否要删除选定项", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
|
|
{
|
|
case DialogResult.Yes:
|
|
LvPostData.RemoveObjects(LvPostData.SelectedObjects);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void APIEditor_Shown(object sender, EventArgs e)
|
|
{
|
|
TxtName.SelectionLength = 0;
|
|
}
|
|
|
|
private void BtnAddPost_Click(object sender, EventArgs e)
|
|
{
|
|
IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
|
|
if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1)
|
|
{
|
|
RyQuickSQL mySQL = new RyQuickSQL("api");
|
|
mySQL.AddField("Url", TxtUrl.Text);
|
|
DataSet ds = db.ReadData("select * from api where Url=@Url", mySQL);
|
|
var json_post = "";
|
|
if (ds.HaveData())
|
|
{
|
|
var row = ds.GetFirstRowData();
|
|
json_post = row["Postdata"].ToString();
|
|
}
|
|
JObject jo;
|
|
if (json_post.Length == 0) { jo = new JObject(); }
|
|
else { jo = JObject.Parse(json_post); }
|
|
Dictionary<string, PostInfo> dict_post = new Dictionary<string, PostInfo>();
|
|
List<PostInfo> list = new List<PostInfo>();
|
|
var list_lv = LvPostData.ObjectsList;
|
|
for (int i = 0; i < list_lv.Count; i++)
|
|
{
|
|
var item = (PostInfo)list_lv[i];
|
|
dict_post[item.ParamName] = item;
|
|
}
|
|
if (jo.ContainsKey("list"))
|
|
{
|
|
JArray jarr = JArray.Parse(jo["list"].ToString());
|
|
for (int i = 0; i < jarr.Count; i++)
|
|
{
|
|
var item = jarr[i];
|
|
var param_name = item["paramname"].ToString();
|
|
if (!dict_post.ContainsKey(param_name))
|
|
{
|
|
list.Add(new PostInfo()
|
|
{
|
|
ParamName = param_name,
|
|
Title = item["title"].ToString(),
|
|
Desc = item["desc"].ToString(),
|
|
ParamValue = item["paramvalue"].ToString()
|
|
});
|
|
}
|
|
}
|
|
}
|
|
FrmAddPostParam frm = new FrmAddPostParam();
|
|
frm.Url = TxtUrl.Text;
|
|
frm.LvPostData.AddObjects(list);
|
|
if (frm.ShowDialog() == DialogResult.OK)
|
|
{
|
|
var item = frm.SelectedItem;
|
|
item.IsAdd = false;
|
|
LvPostData.AddObject(item);
|
|
}
|
|
frm.Dispose();
|
|
}
|
|
db.Free();
|
|
}
|
|
|
|
private void APIEditor_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void 添加参数ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
BtnAddPost.PerformClick();
|
|
}
|
|
|
|
private void 删除参数ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
BtnDelPost.PerformClick();
|
|
}
|
|
|
|
private void 设置时间戳ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
if (LvPostData.SelectedObject == null) { return; }
|
|
var item = (PostInfo)LvPostData.SelectedObject;
|
|
FrmInsertUnixTime frm = new FrmInsertUnixTime();
|
|
frm.Icon = Icon;
|
|
var text = item.ParamValue;
|
|
if (text.Length > 4 && text.IsInt())
|
|
{
|
|
if (text.Length == 13)
|
|
{
|
|
frm.IsJsTime = true;
|
|
frm.dateTimePickerEX1.Value = RyDate.JSTimeToDateTime(text);
|
|
}
|
|
else
|
|
{
|
|
frm.dateTimePickerEX1.Value = RyDate.UnixTimeToDateTime(text);
|
|
}
|
|
}
|
|
if (frm.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (frm.IsJsTime)
|
|
{
|
|
item.ParamValue = RyDate.DateTimeToJSTime(frm.dateTimePickerEX1.Value).ToString();
|
|
}
|
|
else
|
|
{
|
|
item.ParamValue = RyDate.DateTimeToUnixTime(frm.dateTimePickerEX1.Value).ToString();
|
|
}
|
|
LvPostData.RefreshObject(item);
|
|
}
|
|
frm.Dispose();
|
|
}
|
|
|
|
private void LvPostData_MouseDoubleClick(object sender, MouseEventArgs e)
|
|
{
|
|
设置jsonToolStripMenuItem.PerformClick();
|
|
}
|
|
|
|
private void 设置jsonToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
if (LvPostData.SelectedObject == null) { return; }
|
|
var item = (PostInfo)LvPostData.SelectedObject;
|
|
try
|
|
{
|
|
if (item.ParamValue.StartsWith("{") || item.ParamValue.StartsWith("["))
|
|
{
|
|
JObject jo2 = JObject.Parse(item.ParamValue);
|
|
开发辅助工具.Controls.FrmText frm = new Controls.FrmText
|
|
{
|
|
Icon = Icon
|
|
};
|
|
frm.highlightEditor1.Visible = true;
|
|
frm.richTextBox1.Visible = false;
|
|
try
|
|
{
|
|
frm.highlightEditor1.SetHightlightText(Manager.Json.ConvertJsonString(item.ParamValue), "json");
|
|
}
|
|
catch
|
|
{
|
|
frm.highlightEditor1.SetHightlightText(item.ParamValue, "txt");
|
|
}
|
|
if (frm.ShowDialog() == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
JObject jo = (JObject)JsonConvert.DeserializeObject(frm.highlightEditor1.Text);
|
|
var aa = Newtonsoft.Json.JsonConvert.SerializeObject(jo, new Newtonsoft.Json.JsonSerializerSettings() { StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.EscapeNonAscii });
|
|
item.ParamValue = aa;
|
|
LvPostData.RefreshObject(item);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "出错", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void 复制参数名ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
if (LvPostData.SelectedObject == null) { return; }
|
|
var row = (PostInfo)LvPostData.SelectedObject;
|
|
RyFiles.CopyToClip(row.ParamName);
|
|
}
|
|
}
|
|
public class PostInfo
|
|
{
|
|
public string Title { get; set; } = "";
|
|
public string ParamName { get; set; } = "";
|
|
public string ParamValue { get; set; } = "";
|
|
public string Desc { get; set; } = "";
|
|
/// <summary>
|
|
/// 是否是json新增的内容
|
|
/// </summary>
|
|
public bool IsAdd { get; set; } = true;
|
|
}
|
|
}
|