Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3991593

Browse files
authored
Merge branch 'liaozb:master' into master
2 parents87da3c8 +1a65cf3 commit3991593

File tree

14 files changed

+334
-206
lines changed

14 files changed

+334
-206
lines changed

‎APIJSON.NET/APIJSON.NET.Test/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
usingRestSharp;
22
usingSystem;
3+
usingSystem.Text.RegularExpressions;
34

45
namespaceAPIJSON.NET.Test
56
{

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<ProjectSdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
@@ -19,19 +19,17 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReferenceInclude="Microsoft.AspNetCore.Authentication.JwtBearer"Version="3.1.9" />
23-
<PackageReferenceInclude="Microsoft.AspNetCore.Mvc.NewtonsoftJson"Version="3.1.9" />
24-
<PackageReferenceInclude="Microsoft.VisualStudio.Web.CodeGeneration.Design"Version="3.1.4" />
22+
<PackageReferenceInclude="Microsoft.AspNetCore.Authentication.JwtBearer"Version="6.0.6" />
23+
<PackageReferenceInclude="Microsoft.AspNetCore.Mvc.NewtonsoftJson"Version="6.0.6" />
24+
<PackageReferenceInclude="Microsoft.VisualStudio.Web.CodeGeneration.Design"Version="6.0.6" />
2525

26-
<PackageReferenceInclude="Swashbuckle.AspNetCore"Version="5.6.3" />
27-
<PackageReferenceInclude="Swashbuckle.AspNetCore.Annotations"Version="5.6.3" />
28-
<PackageReferenceInclude="Swashbuckle.AspNetCore.Swagger"Version="5.6.3" />
26+
<PackageReferenceInclude="Swashbuckle.AspNetCore"Version="6.2.3" />
2927
</ItemGroup>
3028

3129
<ItemGroup>
3230
<ProjectReferenceInclude="..\APIJSONCommon\ApiJson.Common.csproj" />
3331
</ItemGroup>
3432

35-
<ProjectExtensions><VisualStudio><UserPropertiesappsettings_1json__JSONSchema="http://json.schemastore.org/config" /></VisualStudio></ProjectExtensions>
33+
3634

3735
</Project>

‎APIJSON.NET/APIJSON.NET/Controllers/HomeController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class HomeController : Controller
1010
{
1111
publicIActionResultIndex()
1212
{
13-
returnRedirect("index.html");
13+
returnFile("./index.html","text/html");
14+
//return Redirect("index.html");
1415
}
1516
}
1617
}

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public ActionResult Test()
5757

5858
publicasyncTask<ActionResult>Query([FromBody]JObjectjobject)
5959
{
60-
JObjectresultJobj=newSelectTable(_identitySvc,_tableMapper,db.Db).Query(jobject);
60+
varst=newSelectTable(_identitySvc,_tableMapper,db.Db);
61+
JObjectresultJobj=st.Query(jobject);
6162
returnOk(resultJobj);
6263
}
6364

@@ -75,20 +76,46 @@ public async Task<ActionResult> QueryByTable([FromRoute]string table)
7576

7677
JObjectjobject=JObject.Parse(json);
7778
ht.Add(table+"[]",jobject);
78-
ht.Add("total@","");
79+
80+
if(jobject["query"]!=null&&jobject["query"].ToString()!="0"&&jobject["total@"]==null)
81+
{
82+
//自动添加总计数量
83+
ht.Add("total@","");
84+
}
85+
86+
//每页最大1000条数据
87+
if(jobject["count"]!=null&&int.Parse(jobject["count"].ToString())>1000)
88+
{
89+
thrownewException("count分页数量最大不能超过1000");
90+
}
91+
92+
boolisDebug=(jobject["@debug"]!=null&&jobject["@debug"].ToString()!="0");
93+
jobject.Remove("@debug");
7994

8095
boolhasTableKey=false;
96+
List<string>ignoreConditions=newList<string>{"page","count","query"};
97+
JObjecttableConditions=newJObject();//表的其它查询条件,比如过滤,字段等
8198
foreach(variteminjobject)
8299
{
83100
if(item.Key.Equals(table,StringComparison.CurrentCultureIgnoreCase))
84101
{
85102
hasTableKey=true;
86103
break;
87104
}
105+
if(!ignoreConditions.Contains(item.Key.ToLower()))
106+
{
107+
tableConditions.Add(item.Key,item.Value);
108+
}
109+
}
110+
111+
foreach(varremoveKeyintableConditions)
112+
{
113+
jobject.Remove(removeKey.Key);
88114
}
115+
89116
if(!hasTableKey)
90117
{
91-
jobject.Add(table,newJObject());
118+
jobject.Add(table,tableConditions);
92119
}
93120

94121
returnawaitQuery(ht);

‎APIJSON.NET/APIJSON.NET/Program.cs

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,83 @@
1-
usingMicrosoft.AspNetCore;
1+
usingAPIJSON.NET;
2+
usingAPIJSON.NET.Models;
3+
usingAPIJSON.NET.Services;
4+
usingMicrosoft.AspNetCore;
5+
usingMicrosoft.AspNetCore.Builder;
26
usingMicrosoft.AspNetCore.Hosting;
7+
usingMicrosoft.AspNetCore.Http;
8+
usingMicrosoft.Extensions.DependencyInjection;
9+
usingMicrosoft.Extensions.Hosting;
10+
usingMicrosoft.IdentityModel.Tokens;
11+
usingMicrosoft.OpenApi.Models;
12+
usingSystem;
13+
usingSystem.Collections.Generic;
314
usingSystem.Net;
15+
usingSystem.Text;
416

5-
namespaceAPIJSON.NET
17+
conststring_defaultCorsPolicyName="localhost";
18+
varbuilder=WebApplication.CreateBuilder(args);
19+
20+
// Add services to the container.
21+
22+
builder.Services.AddControllers();
23+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
24+
builder.Services.AddEndpointsApiExplorer();
25+
26+
builder.Services.Configure<List<Role>>(builder.Configuration.GetSection("RoleList"));
27+
builder.Services.Configure<Dictionary<string,string>>(builder.Configuration.GetSection("tablempper"));
28+
builder.Services.Configure<TokenAuthConfiguration>(tokenAuthConfig=>
629
{
7-
publicclassProgram
30+
tokenAuthConfig.SecurityKey=newSymmetricSecurityKey(Encoding.ASCII.GetBytes(builder.Configuration["Authentication:JwtBearer:SecurityKey"]));
31+
tokenAuthConfig.Issuer=builder.Configuration["Authentication:JwtBearer:Issuer"];
32+
tokenAuthConfig.Audience=builder.Configuration["Authentication:JwtBearer:Audience"];
33+
tokenAuthConfig.SigningCredentials=newSigningCredentials(tokenAuthConfig.SecurityKey,SecurityAlgorithms.HmacSha256);
34+
tokenAuthConfig.Expiration=TimeSpan.FromDays(1);
35+
});
36+
AuthConfigurer.Configure(builder.Services,builder.Configuration);
37+
38+
varorigins=builder.Configuration.GetSection("CorsUrls").Value.Split(",");
39+
builder.Services.AddCors(options=>options.AddPolicy(_defaultCorsPolicyName,
40+
builder=>
41+
builder.WithOrigins(origins)
42+
.AllowAnyHeader()
43+
.AllowAnyMethod().AllowCredentials()
44+
));
45+
builder.Services.AddControllers()
46+
.AddNewtonsoftJson(options=>
847
{
9-
publicstaticvoidMain(string[]args)
10-
{
11-
CreateWebHostBuilder(args).Build().Run();
12-
}
13-
14-
publicstaticIWebHostBuilderCreateWebHostBuilder(string[]args)
15-
{
16-
returnWebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
17-
}
18-
}
48+
options.SerializerSettings.ReferenceLoopHandling=Newtonsoft.Json.ReferenceLoopHandling.Ignore;
49+
options.SerializerSettings.DateFormatString="yyyy-MM-dd HH:mm:ss";
50+
});;
51+
builder.Services.AddSwaggerGen(c=>
52+
{
53+
c.SwaggerDoc("v1",newOpenApiInfo{Title="APIJSON.NET",Version="v1"});
54+
});
55+
builder.Services.AddSingleton<DbContext>();
56+
builder.Services.AddSingleton<SelectTable>();
57+
builder.Services.AddSingleton<TokenAuthConfiguration>();
58+
builder.Services.AddSingleton<IHttpContextAccessor,HttpContextAccessor>();
59+
builder.Services.AddTransient<IIdentityService,IdentityService>();
60+
builder.Services.AddTransient<ITableMapper,TableMapper>();
61+
62+
63+
varapp=builder.Build();
64+
65+
// Configure the HTTP request pipeline.
66+
if(app.Environment.IsDevelopment())
67+
{
68+
app.UseSwagger();
69+
app.UseSwaggerUI(c=>
70+
{
71+
c.SwaggerEndpoint("/swagger/v1/swagger.json","My API V1");
72+
73+
});
1974
}
75+
76+
app.UseHttpsRedirection();
77+
app.UseStaticFiles();
78+
app.UseAuthorization();
79+
app.UseCors(_defaultCorsPolicyName);
80+
app.MapControllers();
81+
app.UseJwtTokenMiddleware();
82+
DbInit.Initialize(app);
83+
app.Run();

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,47 @@
1010

1111
namespaceAPIJSON.NET.Services
1212
{
13+
/// <summary>
14+
///
15+
/// </summary>
1316
publicclassIdentityService:IIdentityService
1417
{
1518
privateIHttpContextAccessor_context;
1619
privateList<Role>roles;
1720

21+
/// <summary>
22+
///
23+
/// </summary>
24+
/// <param name="context"></param>
25+
/// <param name="_roles"></param>
1826
publicIdentityService(IHttpContextAccessorcontext,IOptions<List<Role>>_roles)
1927
{
2028
_context=context??thrownewArgumentNullException(nameof(context));
2129
roles=_roles.Value;
2230
}
31+
32+
/// <summary>
33+
///
34+
/// </summary>
35+
/// <returns></returns>
2336
publicstringGetUserIdentity()
2437
{
2538
return_context.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
2639
}
2740

41+
/// <summary>
42+
///
43+
/// </summary>
44+
/// <returns></returns>
2845
publicstringGetUserRoleName()
2946
{
3047
return_context.HttpContext.User.FindFirstValue(ClaimTypes.Role);
3148
}
49+
50+
/// <summary>
51+
///
52+
/// </summary>
53+
/// <returns></returns>
3254
publicRoleGetRole()
3355
{
3456
varrole=newRole();
@@ -43,23 +65,37 @@ public Role GetRole()
4365
}
4466
returnrole;
4567
}
46-
public(bool,string)GetSelectRole(stringtable)
68+
69+
/// <summary>
70+
///
71+
/// </summary>
72+
/// <param name="table"></param>
73+
/// <returns></returns>
74+
publicTuple<bool,string>GetSelectRole(stringtable)
4775
{
4876
varrole=GetRole();
4977
if(role==null||role.Select==null||role.Select.Table==null)
5078
{
51-
return(false,$"appsettings.json权限配置不正确!");
79+
returnTuple.Create(false,$"appsettings.json权限配置不正确!");
5280
}
5381
stringtablerole=role.Select.Table.FirstOrDefault(it=>it=="*"||it.Equals(table,StringComparison.CurrentCultureIgnoreCase));
5482

5583
if(string.IsNullOrEmpty(tablerole))
5684
{
57-
return(false,$"表名{table}没权限查询!");
85+
returnTuple.Create(false,$"表名{table}没权限查询!");
5886
}
5987
intindex=Array.IndexOf(role.Select.Table,tablerole);
6088
stringselectrole=role.Select.Column[index];
61-
return(true,selectrole);
89+
returnTuple.Create(true,selectrole);
6290
}
91+
92+
93+
/// <summary>
94+
///
95+
/// </summary>
96+
/// <param name="col"></param>
97+
/// <param name="selectrole"></param>
98+
/// <returns></returns>
6399
publicboolColIsRole(stringcol,string[]selectrole)
64100
{
65101
if(selectrole.Contains("*"))

‎APIJSON.NET/APIJSON.NET/Startup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ public void ConfigureServices(IServiceCollection services)
7070
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
7171
publicvoidConfigure(IApplicationBuilderapp,IWebHostEnvironmentenv)
7272
{
73-
74-
app.UseAuthentication();
75-
7673
app.UseRouting();
74+
app.UseAuthentication();
75+
app.UseDefaultFiles();
7776
app.UseStaticFiles();
7877
app.UseCors(_defaultCorsPolicyName);
7978
app.UseSwagger();
@@ -84,7 +83,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
8483
});
8584
app.UseEndpoints(endpoints=>
8685
{
87-
endpoints.MapControllers();
86+
endpoints.MapDefaultControllerRoute();
87+
//endpoints.MapControllers();
8888
});
8989
app.UseJwtTokenMiddleware();
9090
DbInit.Initialize(app);

‎APIJSON.NET/APIJSON.NET/appsettings.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"ConnectionStrings": {
33
"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;"
4+
"ConnectionString":"Server=192.168.2.25;Database=yunwei1.8;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
"CorsUrls":"http://localhost:5000,http://localhost5001",
@@ -15,21 +15,21 @@
1515
},
1616
"RoleList": [
1717
{
18-
"name":"role1",//权限名称 唯一
19-
"select": {//查询权限
20-
"table": ["moment","User","Comment"],//可操作的表
21-
"column": ["*","*","*"],//可操作的字段
18+
"name":"role1",//Ȩ������ Ψһ
19+
"select": {//��ѯȨ��
20+
"table": ["*"],//�ɲ����ı�
21+
"column": ["*"],//�ɲ������ֶ�
2222
"where": []
2323
},
24-
"update": {//修改权限
24+
"update": {//�޸�Ȩ��
2525
"table": ["moment","User","Comment" ],
2626
"column": ["*","*","*" ]
2727
},
28-
"insert": {//添加权限
28+
"insert": {//���Ȩ��
2929
"table": ["moment","User","Comment" ],
3030
"column": ["*","*","*" ]
3131
},
32-
"delete": {//删除权限
32+
"delete": {//ɾ��Ȩ��
3333
"table": ["moment","User","Comment" ]
3434
}
3535
},
@@ -41,7 +41,7 @@
4141
}
4242
}
4343
],
44-
"tablempper"://别名表映射
44+
"tablempper"://������ӳ��
4545
{
4646
"user":"apijson_user",
4747
"org":"web_organization"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<optionvalue="add">add</option>
3636
<optionvalue="edit">edit</option>
3737
<optionvalue="remove">remove</option>
38-
<!--<option value="org">org</option>-->
38+
<optionvalue="org">org</option>
3939

4040
</select>
4141
<button@click="hpost()">发送请求</button>

‎APIJSON.NET/APIJSON.NET/wwwroot/js/main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,11 @@
142142
},
143143
methods:{
144144
hpost:function(){
145-
146145
$.ajax({
147146
url:$('#rest-url').val(),
148147
type:"POST",dataType:"json",
149148
contentType:"application/json;charset=utf-8",
150-
data:$('#vInput').val(),
149+
data:$('#vInput').val(),//JSON.stringify($('#vInput').val()),
151150
success:function(data){
152151

153152
App.jsonhtml=data;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp