Skip to content

Commit e40782e

Browse files
authored
Merge pull request liaozb#13 from haptear/master
增加“ApiJson.Common”项目,主要的解析和执行移动到此项目
2 parents 655a6ef + 0f0ffa7 commit e40782e

File tree

15 files changed

+311
-165
lines changed

15 files changed

+311
-165
lines changed

APIJSON.NET/APIJSON.NET.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27703.2035
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28803.202
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIJSON.NET", "APIJSON.NET\APIJSON.NET.csproj", "{FF647576-A104-4D54-954D-3547B4FDCDB2}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIJSON.NET.Test", "APIJSON.NET.Test\APIJSON.NET.Test.csproj", "{0828346E-207E-49F8-AD57-E1AB6B6E4077}"
99
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiJson.Common", "APIJSONCommon\ApiJson.Common.csproj", "{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

APIJSON.NET/APIJSON.NET/APIJSON.NET.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<TargetFramework>netcoreapp2.2</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<Compile Remove="Controllers\JsonController - 副本.cs" />
9+
</ItemGroup>
10+
711
<ItemGroup>
812
<None Remove="Dockerfile" />
913
</ItemGroup>
@@ -16,20 +20,23 @@
1620

1721
<ItemGroup>
1822
<PackageReference Include="AspectCore.Extensions.Reflection" Version="1.2.0" />
19-
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.4" />
23+
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
2024
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
2125
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
2226
<PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.2.0" />
2327
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" />
2428
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
2529
<PackageReference Include="MySql.Data" Version="8.0.15" />
2630
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.18.6" />
27-
<PackageReference Include="sqlSugarCore" Version="4.9.9.6" />
2831
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
2932
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
3033
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="4.0.1" />
3134
</ItemGroup>
3235

36+
<ItemGroup>
37+
<ProjectReference Include="..\APIJSONCommon\ApiJson.Common.csproj" />
38+
</ItemGroup>
39+
3340
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JSONSchema="http://json.schemastore.org/config" /></VisualStudio></ProjectExtensions>
3441

3542
</Project>

APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs

Lines changed: 57 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
using APIJSON.NET.Services;
1313
using System.Reflection;
1414
using Microsoft.AspNetCore.Cors;
15+
using System.Threading.Tasks;
16+
using System.IO;
17+
using System.Text;
18+
using System.Net.Http;
1519

1620
[Route("api/[controller]")]
1721
[ApiController]
@@ -22,153 +26,72 @@ public class JsonController : ControllerBase
2226
private SelectTable selectTable;
2327
private DbContext db;
2428
private readonly IIdentityService _identitySvc;
25-
public JsonController(SelectTable _selectTable, DbContext _db,IIdentityService identityService)
26-
{
29+
private ITableMapper _tableMapper;
2730

28-
selectTable = _selectTable;
31+
public JsonController(IIdentityService identityService, ITableMapper tableMapper, DbContext _db)
32+
{
2933
db = _db;
34+
_tableMapper = tableMapper;
3035
_identitySvc = identityService;
36+
selectTable = new SelectTable(_identitySvc, _tableMapper, _db.Db);
37+
}
38+
39+
/// <summary>
40+
///
41+
/// </summary>
42+
/// <returns></returns>
43+
[HttpGet("/test")]
44+
public ActionResult Test()
45+
{
46+
string str = "{\"page\":1,\"count\":3,\"query\":2,\"Org\":{\"@column\":\"Id,Name\"}}";
47+
var content = new StringContent(str);
48+
return Ok(content);
3149
}
32-
50+
3351
/// <summary>
3452
/// 查询
3553
/// </summary>
3654
/// <param name="json"></param>
3755
/// <returns></returns>
3856
[HttpPost("/get")]
3957

40-
public ActionResult Query([FromBody] JObject jobject)
58+
public async Task<ActionResult> Query([FromBody] JObject jobject)
4159
{
42-
JObject ht = new JObject();
43-
ht.Add("code", "200");
44-
ht.Add("msg", "success");
45-
try
46-
{
47-
int page = 0, count = 0, query = 0, total = 0;
48-
foreach (var item in jobject)
49-
{
50-
string key = item.Key.Trim();
51-
JObject jb;
52-
if (key.Equals("[]"))
53-
{
54-
jb = JObject.Parse(item.Value.ToString());
55-
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
56-
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
57-
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
58-
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
59-
var htt = new JArray();
60-
List<string> tables = new List<string>(), where = new List<string>();
61-
foreach (var t in jb)
62-
{
63-
tables.Add(t.Key); where.Add(t.Value.ToString());
64-
}
65-
if (tables.Count > 0)
66-
{
67-
string table = tables[0];
68-
var temp = selectTable.GetTableData(table, page, count, where[0], null);
69-
if (query > 0)
70-
{
71-
total = temp.Item2;
72-
}
73-
74-
foreach (var dd in temp.Item1)
75-
{
76-
var zht = new JObject();
77-
zht.Add(table, JToken.FromObject(dd));
78-
for (int i = 1; i < tables.Count; i++)
79-
{
80-
string subtable = tables[i];
81-
if (subtable.EndsWith("[]"))
82-
{
83-
subtable = subtable.TrimEnd("[]".ToCharArray());
84-
var jbb = JObject.Parse(where[i]);
85-
page = jbb["page"] == null ? 0 : int.Parse(jbb["page"].ToString());
86-
count = jbb["count"] == null ? 0 : int.Parse(jbb["count"].ToString());
60+
JObject resultJobj = new SelectTable(_identitySvc, _tableMapper, db.Db).Query(jobject);
61+
return Ok(resultJobj);
62+
}
8763

88-
var lt = new JArray();
89-
foreach (var d in selectTable.GetTableData(subtable, page, count, jbb[subtable].ToString(), zht).Item1)
90-
{
91-
lt.Add(JToken.FromObject(d));
92-
}
93-
zht.Add(tables[i], lt);
94-
}
95-
else
96-
{
97-
var ddf = selectTable.GetFirstData(subtable, where[i].ToString(), zht);
98-
if (ddf != null)
99-
{
100-
zht.Add(subtable, JToken.FromObject(ddf));
64+
[HttpPost("/{table}")]
65+
public async Task<ActionResult> QueryByTable([FromRoute]string table)
66+
{
67+
string json = string.Empty;
68+
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
69+
{
70+
json = await reader.ReadToEndAsync();
71+
}
10172

102-
}
103-
}
104-
}
105-
htt.Add(zht);
106-
}
73+
json = HttpUtility.UrlDecode(json);
74+
JObject ht = new JObject();
10775

108-
}
109-
if (query != 1)
110-
{
111-
ht.Add("[]", htt);
112-
}
113-
}
114-
else if (key.EndsWith("[]"))
115-
{
116-
jb = JObject.Parse(item.Value.ToString());
117-
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
118-
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
119-
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
120-
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
121-
var htt = new JArray();
122-
foreach (var t in jb)
123-
{
124-
foreach (var d in selectTable.GetTableData(t.Key, page, count, t.Value.ToString(), null).Item1)
125-
{
126-
htt.Add(JToken.FromObject(d));
127-
}
128-
}
129-
ht.Add(key, htt);
130-
}
131-
else if (key.Equals("func"))
132-
{
133-
jb = JObject.Parse(item.Value.ToString());
134-
Type type = typeof(FuncList);
135-
Object obj = Activator.CreateInstance(type);
136-
var bb = new JObject();
137-
foreach (var f in jb)
138-
{
139-
var types = new List<Type>();
140-
var param = new List<object>();
141-
foreach (var va in JArray.Parse(f.Value.ToString()))
142-
{
143-
types.Add(typeof(object));
144-
param.Add(va);
145-
}
146-
bb.Add(f.Key, JToken.FromObject(selectTable.ExecFunc(f.Key,param.ToArray(), types.ToArray())));
147-
}
148-
ht.Add("func", bb);
149-
}
150-
else if (key.Equals("total@"))
151-
{
152-
ht.Add("total", total);
153-
}
154-
else
155-
{
156-
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
157-
if (template != null)
158-
{
159-
ht.Add(key, JToken.FromObject(template));
160-
}
161-
}
76+
JObject jobject = JObject.Parse(json);
77+
ht.Add(table + "[]", jobject);
78+
ht.Add("total@", "");
16279

80+
bool hasTableKey = false;
81+
foreach (var item in jobject)
82+
{
83+
if (item.Key.Equals(table, StringComparison.CurrentCultureIgnoreCase))
84+
{
85+
hasTableKey = true;
86+
break;
16387
}
16488
}
165-
catch (Exception ex)
89+
if (!hasTableKey)
16690
{
167-
ht["code"] = "500";
168-
ht["msg"] = ex.Message;
169-
91+
jobject.Add(table, new JObject());
17092
}
171-
return Ok(ht);
93+
94+
return await Query(ht);
17295
}
17396
/// <summary>
17497
/// 新增
@@ -178,14 +101,14 @@ public ActionResult Query([FromBody] JObject jobject)
178101
[HttpPost("/add")]
179102
public ActionResult Add([FromBody]JObject jobject)
180103
{
181-
104+
182105
JObject ht = new JObject();
183106
ht.Add("code", "200");
184107
ht.Add("msg", "success");
185108
try
186109
{
187-
188-
110+
111+
189112

190113
foreach (var item in jobject)
191114
{
@@ -248,12 +171,12 @@ public ActionResult Edit([FromBody]JObject jobject)
248171
var dt = new Dictionary<string, object>();
249172
foreach (var f in value)
250173
{
251-
if (f.Key.ToLower() != "id"&& selectTable.IsCol(key,f.Key) && (role.Update.Column.Contains ("*")||role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
174+
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
252175
{
253176
dt.Add(f.Key, f.Value.ToString());
254177
}
255178
}
256-
db.Db.Updateable(dt).AS(key).Where("id=@id" ,new { id= value["id"].ToString() }).ExecuteCommand();
179+
db.Db.Updateable(dt).AS(key).Where("id=@id", new { id = value["id"].ToString() }).ExecuteCommand();
257180
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
258181
}
259182
}
@@ -285,13 +208,13 @@ public ActionResult Remove([FromBody]JObject jobject)
285208
var value = JObject.Parse(item.Value.ToString());
286209
var sb = new System.Text.StringBuilder(100);
287210
sb.Append($"delete FROM {key} where ");
288-
if (role.Delete==null||role.Delete.Table==null)
211+
if (role.Delete == null || role.Delete.Table == null)
289212
{
290213
ht["code"] = "500";
291214
ht["msg"] = "delete权限未配置";
292215
break;
293216
}
294-
if (!role.Delete.Table.Contains(key,StringComparer.CurrentCultureIgnoreCase))
217+
if (!role.Delete.Table.Contains(key, StringComparer.CurrentCultureIgnoreCase))
295218
{
296219
ht["code"] = "500";
297220
ht["msg"] = $"没权限删除{key}";

APIJSON.NET/APIJSON.NET/Services/IdentityService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class IdentityService : IIdentityService
1515
private IHttpContextAccessor _context;
1616
private List<Role> roles;
1717

18-
public IdentityService(IHttpContextAccessor context,IOptions<List<Role>> _roles)
18+
public IdentityService(IHttpContextAccessor context, IOptions<List<Role>> _roles)
1919
{
2020
_context = context ?? throw new ArgumentNullException(nameof(context));
2121
roles = _roles.Value;
@@ -24,7 +24,7 @@ public string GetUserIdentity()
2424
{
2525
return _context.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
2626
}
27-
27+
2828
public string GetUserRoleName()
2929
{
3030
return _context.HttpContext.User.FindFirstValue(ClaimTypes.Role);
@@ -50,7 +50,7 @@ public Role GetRole()
5050
{
5151
return (false, $"appsettings.json权限配置不正确!");
5252
}
53-
string tablerole = role.Select.Table.FirstOrDefault(it => it.Equals(table, StringComparison.CurrentCultureIgnoreCase));
53+
string tablerole = role.Select.Table.FirstOrDefault(it => it == "*" || it.Equals(table, StringComparison.CurrentCultureIgnoreCase));
5454

5555
if (string.IsNullOrEmpty(tablerole))
5656
{

APIJSON.NET/APIJSON.NET/appsettings.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"ConnectionStrings": {
3-
"DbType": 1, //0:MySql,1:SqlServer,2:Sqlite
4-
"ConnectionString": "Server=LIAOZENGBO\\SQL2012; Database=Testdb; User Id=sa;Password=sa123;"
3+
"DbType": 0, //0:MySql,1:SqlServer,2:Sqlite
4+
"ConnectionString": "Server=192.168.2.25;Database=yunwei;Uid=root;Pwd=xmjk;Port=3306;Character Set=utf8;"
55
//"ConnectionString": "Server=119.29.9.25;Port=3306;Database=test;Uid=root;Pwd=1q,2w.3e?;CharSet=UTF8;"
66
},
77
"Authentication": {
@@ -42,7 +42,7 @@
4242
],
4343
"tablempper": //别名表映射
4444
{
45-
"user": "apijson_user"
45+
"user": "apijson_user",
46+
"org": "web_organization"
4647
}
47-
4848
}

APIJSON.NET/APIJSON.NET/wwwroot/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
<option value="add">add</option>
3636
<option value="edit">edit</option>
3737
<option value="remove">remove</option>
38-
38+
<!--<option value="org">org</option>-->
39+
3940
</select>
4041
<button @click="hpost()">发送请求</button>
4142
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Remove="SelectTable - 副本.cs" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="AspectCore.Extensions.Reflection" Version="1.1.0" />
13+
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" />
14+
<PackageReference Include="sqlSugarCore" Version="4.9.9.10" />
15+
</ItemGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy