C# Winform开发,小白接口管理系统 - V1.0(开发及使用说明)
重要的下载地址
C#项目源代码下载(OkayApiAdmin):https://gitee.com/dogstar/okayapi-demo/tree/master/c_sharp
小白接口管理系统 - 绿色版 - V1.0.zip:https://gitee.com/dogstar/okayapi-demo/tree/master/c_sharp
小白接口管理系统 - 安装版 - V1.0.zip:https://gitee.com/dogstar/okayapi-demo/tree/master/c_sharp
软件使用说明
例如绿色版的,打开软件【OkayApiAdmin.exe】,
打开后,就能看到软件的界面。第一次使用时,需要设置一下相关的信息。
其中,接口域名、app_key、app_secrect,可以在登录小白后台后,进入我的套餐页面(http://admin.okayapi.com/?r=App/Mine)查看,找到后填上去,最后点击一下【更新配置】。
更新成功后,就能看到如下提示。
批量导入数据
接下来,讲一下如何批量导入数据。
首先,选择需要导入数据的模型。可以进入小白后台(http://admin.okayapi.com/?r=Data/MyModelsManager)查看自己有哪些模型。例如,这里使用模型aaa。
点击批量导入数据,然后进去后,再下载Excel导入模板。其实不下载这个模板也没问题,以后自己熟悉后,可以不参考这份模板。
第二步,开始填充自己要导入的数据。Excel的第一行是字段名字,第二行起,是数据。如下,可以支持中文、数字、字母等,但要和字段 的类型对应上。
编辑后保存,并另存为CSV 文件。如果还不知道如何把Excel另存为CSV的,可参考:
例如:aaa-TPL-20181104-OKAYAPI.csv
第三步, 做好这些准备工作后,回到小白接口管理系统软件,回到刚才批量导入的窗口。按提示选择和输入。
导入成功后, 就可以在网页上看到刚才成功导入的新数据啦!
小白接口的C#版SDK开发要点
小白接口的SDK包,对于C#代码来说,可以这样使用:
Dictionary<String, String> paramsDict = new Dictionary<String, String>();
paramsDict.Add("name", "dogstar");
PhalApiClientResponse response = OkayApiClient.instance().go("Hello.World", paramsDict);
Console.WriteLine("response ret", response.ret + "");
if (response.ret == 200)
{
MessageBox.Show("配置检测通过!更新成功" , "小白套餐", MessageBoxButtons.OK);
} else
{
MessageBox.Show("配置检测不正确,错误信息:" + response.msg, "小白套餐有误", MessageBoxButtons.OK);
}
里面自带了签名生成,以及JSON的解析。具体完整的源代码,如下:
using PhalApiClientSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace OkayApiAdmin
{
class OkayApiClient
{
protected static OkayApiClient client;
public String host;
public String app_key;
public String app_secrect;
protected OkayApiClient()
{
loadConfig();
}
public static OkayApiClient instance()
{
if (OkayApiClient.client == null)
{
OkayApiClient.client = new OkayApiClient();
}
return OkayApiClient.client;
}
public void loadConfig()
{
// 读取配置
IniFileHelper iniFileHelper = new IniFileHelper();
StringBuilder sb = new StringBuilder(200);
iniFileHelper.GetIniString("Product", "host", "", sb, sb.Capacity);
host = sb.ToString();
if (host.Length == 0)
{
host = "http://api.okayapi.com/";
}
iniFileHelper.GetIniString("Product", "app_key", "", sb, sb.Capacity);
app_key = sb.ToString();
if (app_key.Length == 0)
{
app_key = "16BD4337FB1D355902E0502AFCBFD4DF";
}
iniFileHelper.GetIniString("Product", "app_secrect", "", sb, sb.Capacity);
app_secrect = sb.ToString();
if (app_secrect.Length == 0)
{
app_secrect = "4c1402596e4cd017eeaO670df6f8B6783475b4ac8A32B4900f20abP2159711ad";
}
}
public void updateConfig(String hh, String a_k, String a_s)
{
host = hh;
app_key = a_k;
app_secrect = a_s;
IniFileHelper iniFileHelper = new IniFileHelper();
iniFileHelper.WriteIniString("Product", "host", host);
iniFileHelper.WriteIniString("Product", "app_key", app_key);
iniFileHelper.WriteIniString("Product", "app_secrect", app_secrect);
}
public PhalApiClientResponse go(String service, Dictionary<String, String> paramsDict)
{
// 增加必要的公共参数
paramsDict.Add("app_key", app_key);
paramsDict.Add("service", service);
// 重新生成签名
paramsDict.Remove("sign");
String sign = encryptAppKey(paramsDict);
paramsDict.Add("sign", sign);
PhalApiClient client = PhalApiClient.create()
.withHost(host)
.withTimeout(3000);
// 追加参数
foreach (KeyValuePair<String, String> it in paramsDict)
{
client.withParams(it.Key, Uri.EscapeUriString(it.Value));
}
PhalApiClientResponse response = client.request();
return response;
}
public String encryptAppKey(Dictionary<String, String> dict)
{
List<KeyValuePair<String, String>> lst = new List<KeyValuePair<String, String>>(dict);
//倒叙排列:只需要把变量s2 和 s1 互换就行了 例: return s1.Value.CompareTo(s2.Value);
//进行排序 目前是顺序
lst.Sort(delegate (KeyValuePair<String, String> s1, KeyValuePair<String, String> s2)
{
return s1.Key.CompareTo(s2.Key);
});
String tmpStr = "";
foreach (KeyValuePair < String, String > it in lst)
{
tmpStr += it.Value;
}
String strMd5ForUtf8 = UserMd5(tmpStr + app_secrect);
return strMd5ForUtf8.ToUpper();
}
public static string UserMd5(string str)
{
string cl = str;
string pwd = "";
MD5 md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("x2");
}
return pwd;
}
}
}