Skip to content
Prev Previous commit
Next Next commit
#增加函数调用#
  • Loading branch information
liaozb committed Jul 25, 2018
commit 25fe6553a5c5a50d7c04e5cd7cce6453e79b8be0
1 change: 1 addition & 0 deletions APIJSON.NET/APIJSON.NET.Test/APIJSON.NET.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonApiFramework.Server" Version="1.7.0" />
<PackageReference Include="RestSharp" Version="106.3.1" />
</ItemGroup>

Expand Down
5 changes: 3 additions & 2 deletions APIJSON.NET/APIJSON.NET.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RestSharp;
using JsonApiFramework.Server;
using RestSharp;
using System;

namespace APIJSON.NET.Test
Expand Down Expand Up @@ -41,7 +42,7 @@ static void Main(string[] args)
IRestResponse response2 = client.Execute(request);
Console.WriteLine(response2.Content);


Console.ReadLine();
}
}
Expand Down
36 changes: 33 additions & 3 deletions APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using SqlSugar;
using System.Linq;
using APIJSON.NET.Services;

using System.Reflection;

[Route("api/[controller]")]
[ApiController]
Expand All @@ -27,6 +27,7 @@ public JsonController(SelectTable _selectTable, DbContext _db,IIdentityService i
db = _db;
_identitySvc = identityService;
}

/// <summary>
/// 查询
/// </summary>
Expand Down Expand Up @@ -64,11 +65,11 @@ public ActionResult Query([FromBody]string json)
{
string table = tables[0];
var temp = selectTable.GetTableData(table, page, count, where[0], null);
if (query >0)
if (query > 0)
{
total = temp.Item2;
}

foreach (var dd in temp.Item1)
{
var zht = new JObject();
Expand Down Expand Up @@ -125,6 +126,34 @@ public ActionResult Query([FromBody]string json)
}
ht.Add(key, htt);
}
else if (key.Equals("func"))
{
jb = JObject.Parse(item.Value.ToString());
Type type = typeof(MethodList);
Object obj = Activator.CreateInstance(type);
var bb = new JObject();
foreach (var f in jb)
{
var types = new List<Type>();
var param = new List<string>();
foreach (var va in JArray.Parse(f.Value.ToString()))
{
types.Add(typeof(string));
param.Add(va.ToString());
}
MethodInfo mt = type.GetMethod(f.Key, types.ToArray());
if (mt == null)
{
bb.Add(f.Key, "没有获取到相应的函数!");
}
else
{
bb.Add(f.Key, JToken.FromObject(mt.Invoke(obj, param.ToArray())));
}

}
ht.Add("func", bb);
}
else if (key.IsTable())
{
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
Expand All @@ -137,6 +166,7 @@ public ActionResult Query([FromBody]string json)
{
ht.Add("total", total);
}

}
}
catch (Exception ex)
Expand Down
19 changes: 19 additions & 0 deletions APIJSON.NET/APIJSON.NET/MethodList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIJSON.NET
{
public class MethodList
{
public string Merge(string a, string b)
{
return a + b;
}
public object MergeObj(string a, string b)
{
return new { a, b };
}
}
}
5 changes: 3 additions & 2 deletions APIJSON.NET/APIJSON.NET/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"ConnectionStrings": {
"DbType": 1, //0:MySql,1:SqlServer,2:Sqlite
"ConnectionString": "Server=liaozengbo\\sql2018; Database=test; User Id=sa;Password=sa123;"
"DbType": 0, //0:MySql,1:SqlServer,2:Sqlite
//"ConnectionString": "Server=liaozengbo\\sql2018; Database=test; User Id=sa;Password=sa123;",
"ConnectionString": "Server=localhost; Database=test; User Id=root;Password=password;charset=UTF8;"
},
"Authentication": {
"JwtBearer": {
Expand Down
1 change: 1 addition & 0 deletions APIJSON.NET/APIJSON.NET/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<option value="add">add</option>
<option value="edit">edit</option>
<option value="remove">remove</option>
<option value="getmethod">method</option>
</select>
<button @click="hpost()">发送请求</button>
</div>
Expand Down