Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加“ApiJson.Common”项目,主要的解析和执行移动到此项目 #13

Merged
merged 5 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions APIJSON.NET/APIJSON.NET.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2035
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.202
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIJSON.NET", "APIJSON.NET\APIJSON.NET.csproj", "{FF647576-A104-4D54-954D-3547B4FDCDB2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIJSON.NET.Test", "APIJSON.NET.Test\APIJSON.NET.Test.csproj", "{0828346E-207E-49F8-AD57-E1AB6B6E4077}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiJson.Common", "APIJSONCommon\ApiJson.Common.csproj", "{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.Build.0 = Release|Any CPU
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B79D4FD-0BC7-49FD-A3DD-E514433B4B35}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 9 additions & 2 deletions APIJSON.NET/APIJSON.NET/APIJSON.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Controllers\JsonController - 副本.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="Dockerfile" />
</ItemGroup>
Expand All @@ -16,20 +20,23 @@

<ItemGroup>
<PackageReference Include="AspectCore.Extensions.Reflection" Version="1.2.0" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.4" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="MySql.Data" Version="8.0.15" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.18.6" />
<PackageReference Include="sqlSugarCore" Version="4.9.9.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\APIJSONCommon\ApiJson.Common.csproj" />
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JSONSchema="http://json.schemastore.org/config" /></VisualStudio></ProjectExtensions>

</Project>
191 changes: 57 additions & 134 deletions APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
using APIJSON.NET.Services;
using System.Reflection;
using Microsoft.AspNetCore.Cors;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Net.Http;

[Route("api/[controller]")]
[ApiController]
Expand All @@ -22,153 +26,72 @@ public class JsonController : ControllerBase
private SelectTable selectTable;
private DbContext db;
private readonly IIdentityService _identitySvc;
public JsonController(SelectTable _selectTable, DbContext _db,IIdentityService identityService)
{
private ITableMapper _tableMapper;

selectTable = _selectTable;
public JsonController(IIdentityService identityService, ITableMapper tableMapper, DbContext _db)
{
db = _db;
_tableMapper = tableMapper;
_identitySvc = identityService;
selectTable = new SelectTable(_identitySvc, _tableMapper, _db.Db);
}

/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet("/test")]
public ActionResult Test()
{
string str = "{\"page\":1,\"count\":3,\"query\":2,\"Org\":{\"@column\":\"Id,Name\"}}";
var content = new StringContent(str);
return Ok(content);
}

/// <summary>
/// 查询
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/get")]

public ActionResult Query([FromBody] JObject jobject)
public async Task<ActionResult> Query([FromBody] JObject jobject)
{
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
int page = 0, count = 0, query = 0, total = 0;
foreach (var item in jobject)
{
string key = item.Key.Trim();
JObject jb;
if (key.Equals("[]"))
{
jb = JObject.Parse(item.Value.ToString());
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
var htt = new JArray();
List<string> tables = new List<string>(), where = new List<string>();
foreach (var t in jb)
{
tables.Add(t.Key); where.Add(t.Value.ToString());
}
if (tables.Count > 0)
{
string table = tables[0];
var temp = selectTable.GetTableData(table, page, count, where[0], null);
if (query > 0)
{
total = temp.Item2;
}

foreach (var dd in temp.Item1)
{
var zht = new JObject();
zht.Add(table, JToken.FromObject(dd));
for (int i = 1; i < tables.Count; i++)
{
string subtable = tables[i];
if (subtable.EndsWith("[]"))
{
subtable = subtable.TrimEnd("[]".ToCharArray());
var jbb = JObject.Parse(where[i]);
page = jbb["page"] == null ? 0 : int.Parse(jbb["page"].ToString());
count = jbb["count"] == null ? 0 : int.Parse(jbb["count"].ToString());
JObject resultJobj = new SelectTable(_identitySvc, _tableMapper, db.Db).Query(jobject);
return Ok(resultJobj);
}

var lt = new JArray();
foreach (var d in selectTable.GetTableData(subtable, page, count, jbb[subtable].ToString(), zht).Item1)
{
lt.Add(JToken.FromObject(d));
}
zht.Add(tables[i], lt);
}
else
{
var ddf = selectTable.GetFirstData(subtable, where[i].ToString(), zht);
if (ddf != null)
{
zht.Add(subtable, JToken.FromObject(ddf));
[HttpPost("/{table}")]
public async Task<ActionResult> QueryByTable([FromRoute]string table)
{
string json = string.Empty;
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
json = await reader.ReadToEndAsync();
}

}
}
}
htt.Add(zht);
}
json = HttpUtility.UrlDecode(json);
JObject ht = new JObject();

}
if (query != 1)
{
ht.Add("[]", htt);
}
}
else if (key.EndsWith("[]"))
{
jb = JObject.Parse(item.Value.ToString());
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
var htt = new JArray();
foreach (var t in jb)
{
foreach (var d in selectTable.GetTableData(t.Key, page, count, t.Value.ToString(), null).Item1)
{
htt.Add(JToken.FromObject(d));
}
}
ht.Add(key, htt);
}
else if (key.Equals("func"))
{
jb = JObject.Parse(item.Value.ToString());
Type type = typeof(FuncList);
Object obj = Activator.CreateInstance(type);
var bb = new JObject();
foreach (var f in jb)
{
var types = new List<Type>();
var param = new List<object>();
foreach (var va in JArray.Parse(f.Value.ToString()))
{
types.Add(typeof(object));
param.Add(va);
}
bb.Add(f.Key, JToken.FromObject(selectTable.ExecFunc(f.Key,param.ToArray(), types.ToArray())));
}
ht.Add("func", bb);
}
else if (key.Equals("total@"))
{
ht.Add("total", total);
}
else
{
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
if (template != null)
{
ht.Add(key, JToken.FromObject(template));
}
}
JObject jobject = JObject.Parse(json);
ht.Add(table + "[]", jobject);
ht.Add("total@", "");

bool hasTableKey = false;
foreach (var item in jobject)
{
if (item.Key.Equals(table, StringComparison.CurrentCultureIgnoreCase))
{
hasTableKey = true;
break;
}
}
catch (Exception ex)
if (!hasTableKey)
{
ht["code"] = "500";
ht["msg"] = ex.Message;

jobject.Add(table, new JObject());
}
return Ok(ht);

return await Query(ht);
}
/// <summary>
/// 新增
Expand All @@ -178,14 +101,14 @@ public ActionResult Query([FromBody] JObject jobject)
[HttpPost("/add")]
public ActionResult Add([FromBody]JObject jobject)
{

JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{



foreach (var item in jobject)
{
Expand Down Expand Up @@ -248,12 +171,12 @@ public ActionResult Edit([FromBody]JObject jobject)
var dt = new Dictionary<string, object>();
foreach (var f in value)
{
if (f.Key.ToLower() != "id"&& selectTable.IsCol(key,f.Key) && (role.Update.Column.Contains ("*")||role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
{
dt.Add(f.Key, f.Value.ToString());
}
}
db.Db.Updateable(dt).AS(key).Where("id=@id" ,new { id= value["id"].ToString() }).ExecuteCommand();
db.Db.Updateable(dt).AS(key).Where("id=@id", new { id = value["id"].ToString() }).ExecuteCommand();
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
}
}
Expand Down Expand Up @@ -285,13 +208,13 @@ public ActionResult Remove([FromBody]JObject jobject)
var value = JObject.Parse(item.Value.ToString());
var sb = new System.Text.StringBuilder(100);
sb.Append($"delete FROM {key} where ");
if (role.Delete==null||role.Delete.Table==null)
if (role.Delete == null || role.Delete.Table == null)
{
ht["code"] = "500";
ht["msg"] = "delete权限未配置";
break;
}
if (!role.Delete.Table.Contains(key,StringComparer.CurrentCultureIgnoreCase))
if (!role.Delete.Table.Contains(key, StringComparer.CurrentCultureIgnoreCase))
{
ht["code"] = "500";
ht["msg"] = $"没权限删除{key}";
Expand Down
6 changes: 3 additions & 3 deletions APIJSON.NET/APIJSON.NET/Services/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class IdentityService : IIdentityService
private IHttpContextAccessor _context;
private List<Role> roles;

public IdentityService(IHttpContextAccessor context,IOptions<List<Role>> _roles)
public IdentityService(IHttpContextAccessor context, IOptions<List<Role>> _roles)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
roles = _roles.Value;
Expand All @@ -24,7 +24,7 @@ public string GetUserIdentity()
{
return _context.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
}

public string GetUserRoleName()
{
return _context.HttpContext.User.FindFirstValue(ClaimTypes.Role);
Expand All @@ -50,7 +50,7 @@ public Role GetRole()
{
return (false, $"appsettings.json权限配置不正确!");
}
string tablerole = role.Select.Table.FirstOrDefault(it => it.Equals(table, StringComparison.CurrentCultureIgnoreCase));
string tablerole = role.Select.Table.FirstOrDefault(it => it == "*" || it.Equals(table, StringComparison.CurrentCultureIgnoreCase));

if (string.IsNullOrEmpty(tablerole))
{
Expand Down
8 changes: 4 additions & 4 deletions APIJSON.NET/APIJSON.NET/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ConnectionStrings": {
"DbType": 1, //0:MySql,1:SqlServer,2:Sqlite
"ConnectionString": "Server=LIAOZENGBO\\SQL2012; Database=Testdb; User Id=sa;Password=sa123;"
"DbType": 0, //0:MySql,1:SqlServer,2:Sqlite
"ConnectionString": "Server=192.168.2.25;Database=yunwei;Uid=root;Pwd=xmjk;Port=3306;Character Set=utf8;"
//"ConnectionString": "Server=119.29.9.25;Port=3306;Database=test;Uid=root;Pwd=1q,2w.3e?;CharSet=UTF8;"
},
"Authentication": {
Expand Down Expand Up @@ -42,7 +42,7 @@
],
"tablempper": //������ӳ��
{
"user": "apijson_user"
"user": "apijson_user",
"org": "web_organization"
}

}
3 changes: 2 additions & 1 deletion APIJSON.NET/APIJSON.NET/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<option value="add">add</option>
<option value="edit">edit</option>
<option value="remove">remove</option>

<!--<option value="org">org</option>-->

</select>
<button @click="hpost()">发送请求</button>
</div>
Expand Down
17 changes: 17 additions & 0 deletions APIJSON.NET/APIJSONCommon/ApiJson.Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="SelectTable - 副本.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspectCore.Extensions.Reflection" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.1" />
<PackageReference Include="sqlSugarCore" Version="4.9.9.10" />
</ItemGroup>

</Project>
Loading