Skip to content

Commit 5d0d793

Browse files
committed
#增加前端测试#
1 parent 17ea553 commit 5d0d793

33 files changed

+12503
-58
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="RestSharp" Version="106.3.1" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using RestSharp;
2+
using System;
3+
4+
namespace APIJSON.NET.Test
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
var client = new RestClient("http://localhost:5000/");
11+
var request = new RestRequest("get", Method.POST);
12+
request.AddJsonBody(@"{
13+
'User': {
14+
'id': 38710
15+
}
16+
}
17+
");
18+
IRestResponse response = client.Execute(request);
19+
Console.WriteLine(response.Content);
20+
21+
request = new RestRequest("get", Method.POST);
22+
request.AddJsonBody(@"{
23+
'User': {
24+
'id': 38710
25+
},
26+
'[]': {
27+
'page': 0,
28+
'count': 3,
29+
'Moment': {
30+
'userId': 38710
31+
},
32+
'Comment[]': {
33+
'count': 3,
34+
'Comment': {
35+
'momentId@': '[]/Moment/id'
36+
}
37+
}
38+
}
39+
}
40+
");
41+
IRestResponse response2 = client.Execute(request);
42+
Console.WriteLine(response2.Content);
43+
44+
45+
Console.ReadLine();
46+
}
47+
}
48+
}

APIJSON.NET/APIJSON.NET.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27703.2035
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
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APIJSON.NET.Test", "APIJSON.NET.Test\APIJSON.NET.Test.csproj", "{0828346E-207E-49F8-AD57-E1AB6B6E4077}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{FF647576-A104-4D54-954D-3547B4FDCDB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{FF647576-A104-4D54-954D-3547B4FDCDB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{FF647576-A104-4D54-954D-3547B4FDCDB2}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

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

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

7-
<ItemGroup>
8-
<Folder Include="wwwroot\" />
9-
</ItemGroup>
10-
117
<ItemGroup>
128
<PackageReference Include="Microsoft.AspNetCore.App" />
139
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.0" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class HomeController : Controller
1010
{
1111
public IActionResult Index()
1212
{
13-
return Redirect("/swagger");
13+
return Redirect("index.html");
1414
}
1515
}
1616
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ public ActionResult Query([FromBody]string json)
9292
}
9393
else
9494
{
95-
var ddf = selectTable.GetTableData(subtable, 0, 0, where[i].ToString(), zht);
96-
if (ddf.Item1 != null)
95+
var ddf = selectTable.GetFirstData(subtable, where[i].ToString(), zht);
96+
if (ddf != null)
9797
{
98-
zht.Add(subtable, JToken.FromObject(ddf.Item1));
98+
zht.Add(subtable, JToken.FromObject(ddf));
9999
}
100100
}
101101
}
@@ -127,7 +127,7 @@ public ActionResult Query([FromBody]string json)
127127
}
128128
else if (key.IsTable())
129129
{
130-
var template = selectTable.GetTableData(key, 0, 0, item.Value.ToString(), ht).Item1;
130+
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
131131
if (template != null)
132132
{
133133
ht.Add(key, JToken.FromObject(template));

APIJSON.NET/APIJSON.NET/Controllers/TokenController.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IdentityModel.Tokens.Jwt;
55
using System.Linq;
66
using System.Security.Claims;
7+
using System.Text;
78
using System.Threading.Tasks;
89
using Microsoft.AspNetCore.Http;
910
using Microsoft.AspNetCore.Mvc;
@@ -16,29 +17,37 @@ namespace APIJSON.NET.Controllers
1617
[ApiController]
1718
public class TokenController : ControllerBase
1819
{
19-
private DbOptions _options;
20+
private DbContext db;
2021
private readonly IOptions<TokenAuthConfiguration> _configuration;
21-
public TokenController(IOptions<DbOptions> options, IOptions<TokenAuthConfiguration> configuration)
22+
public TokenController(DbContext _db, IOptions<TokenAuthConfiguration> configuration)
2223
{
23-
this._options = options.Value;
2424
_configuration = configuration;
25+
db = _db;
2526
}
26-
[HttpPost("/token")]
27-
public IActionResult Create(string username, string password)
27+
[HttpGet("/token")]
28+
public IActionResult Create(TokenInput input)
2829
{
2930
JObject ht = new JObject();
3031
ht.Add("code", "200");
3132
ht.Add("msg", "success");
32-
if (username!=password)
33+
var us = db.LoginDb.GetSingle(it => it.userName == input.username);
34+
if (us==null)
3335
{
34-
36+
ht["code"] = "201";
37+
ht["msg"] = "用户名或者密码错误!";
38+
return Ok(ht);
39+
}
40+
string str = SimpleStringCipher.Instance.Encrypt(input.password,null, Encoding.ASCII.GetBytes(us.passWordSalt));
41+
if (!us.passWord.Equals(str))
42+
{
43+
ht["code"]="201";
44+
ht["msg"]= "用户名或者密码错误!";
45+
return Ok(ht);
3546
}
36-
3747
var identity = new ClaimsIdentity();
38-
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "1"));
39-
identity.AddClaim(new Claim(ClaimTypes.Name, "1"));
40-
identity.AddClaim(new Claim(ClaimTypes.Role, ""));
41-
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Sub, username));
48+
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, us.userId.ToString()));
49+
identity.AddClaim(new Claim(ClaimTypes.Role, us.roleCode));
50+
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Sub, input.username));
4251
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
4352
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.Now.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64));
4453

@@ -69,6 +78,11 @@ private string CreateAccessToken(IEnumerable<Claim> claims, TimeSpan? expiration
6978
return new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
7079
}
7180
}
81+
public class TokenInput
82+
{
83+
public string username { get; set; }
84+
public string password { get; set; }
85+
}
7286
public class AuthenticateResultModel
7387
{
7488
public string AccessToken { get; set; }
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
1-
using Microsoft.Extensions.Options;
1+
using APIJSON.NET.Models;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Options;
24
using SqlSugar;
5+
using System;
6+
using System.Collections.Generic;
37

48
namespace APIJSON.NET
59
{
610
public class DbContext
711
{
8-
public DbContext(IOptions<DbOptions> options)
12+
public DbContext(IConfiguration options)
913
{
1014
Db = new SqlSugarClient(new ConnectionConfig()
1115
{
12-
ConnectionString = options.Value.ConnectionString,
13-
DbType = options.Value.DbType,
14-
IsAutoCloseConnection = true
16+
ConnectionString = options.GetConnectionString("ConnectionString"),
17+
DbType = (DbType)Enum.Parse(typeof(SqlSugar.DbType), options.GetConnectionString("DbType")),
18+
IsAutoCloseConnection = true,
19+
InitKeyType= InitKeyType.Attribute
1520
});
1621
}
1722
public SqlSugarClient Db;
23+
public DbSet<Login> LoginDb { get { return new DbSet<Login>(Db); } }
1824
}
25+
public class DbSet<T> : SimpleClient<T> where T : class, new()
26+
{
27+
public DbSet(SqlSugarClient context) : base(context)
28+
{
29+
30+
}
31+
public List<T> GetByIds(dynamic[] ids)
32+
{
33+
return Context.Queryable<T>().In(ids).ToList(); ;
34+
}
35+
}
36+
1937
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using APIJSON.NET.Models;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using SqlSugar;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
namespace APIJSON.NET
9+
{
10+
public static class DbInit
11+
{
12+
public static void Initialize(IApplicationBuilder app)
13+
{
14+
var db = app.ApplicationServices.GetRequiredService<DbContext>();
15+
16+
db.Db.CodeFirst.InitTables(typeof(Login));
17+
if (!db.LoginDb.IsAny(it=>it.userId>0))
18+
{
19+
var ds = new List<Login>();
20+
21+
for (int i = 1; i < 10; i++)
22+
{
23+
var d = new Login();
24+
d.userId = i;
25+
d.userName = "admin"+i.ToString();
26+
d.passWordSalt = Guid.NewGuid().ToString();
27+
d.passWord = SimpleStringCipher.Instance.Encrypt("123456", null, Encoding.ASCII.GetBytes(d.passWordSalt));
28+
d.roleCode = "role1";
29+
d.CreateTime = DateTime.Now;
30+
ds.Add(d);
31+
}
32+
db.LoginDb.InsertRange(ds.ToArray());
33+
34+
35+
}
36+
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)