api-build
This commit is contained in:
97
src/WMS.Web.Api/Controllers/LoginController.cs
Normal file
97
src/WMS.Web.Api/Controllers/LoginController.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Core.Dto.Login;
|
||||
using WMS.Web.Core.Internal.Results;
|
||||
using WMS.Web.Domain.IService.Public;
|
||||
|
||||
namespace WMS.Web.Api.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 登录接口
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class LoginController : ControllerBase
|
||||
{
|
||||
private readonly ILoginService _loginService;
|
||||
public LoginController(ILoginService loginService)
|
||||
{
|
||||
this._loginService = loginService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Route("LoginIn")]
|
||||
public async Task<Result<LoginInDto>> LoginIn([FromQuery] string code)
|
||||
{
|
||||
var result = await _loginService.GetUserInfoAsync(code);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 退出登录
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Route("LoginOut")]
|
||||
public async Task<Result> LoginOut()
|
||||
{
|
||||
string authorization = this.HttpContext.Request.Headers["Authorization"];
|
||||
if (string.IsNullOrEmpty(authorization))
|
||||
{
|
||||
return Result.ReFailure(BaseResultCodes.UnAuthorized);
|
||||
}
|
||||
var logininfo = _loginService.GetLoginInfo(authorization);
|
||||
if (logininfo == null || logininfo.UserInfo == null)
|
||||
return Result.ReFailure(BaseResultCodes.UnAuthorized);
|
||||
var dto = new LoginOutDto()
|
||||
{
|
||||
UcId = logininfo.UserInfo.UcId.ToString(),
|
||||
SessionId = logininfo.UserInfo.SeesionId,
|
||||
Token = logininfo.TokenInfo.Token,
|
||||
AccessToken = logininfo.TokenInfo.PhpToken,
|
||||
ExpiresIn = logininfo.TokenInfo.Expired
|
||||
};
|
||||
var res = await _loginService.LoginOutAsync(dto);
|
||||
return res;
|
||||
}
|
||||
/// <summary>
|
||||
/// PHP单点退出使用
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Route("LoginOutSingle")]
|
||||
public async Task<Result> LoginOutSingle()
|
||||
{
|
||||
this.HttpContext.Request.Cookies.TryGetValue("PHPSESSID", out string value);
|
||||
if (string.IsNullOrEmpty(value))
|
||||
value = string.Empty;
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
await _loginService.LoginOutSingleAsync(value);
|
||||
return Result.ReSuccess();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新token
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Route("RefreshToken")]
|
||||
public async Task<Result<LoginInDto>> RefreshToken()
|
||||
{
|
||||
var loginInfo = _loginService.GetLoginInfo(this.HttpContext.Request.Headers["Authorization"]);
|
||||
if (loginInfo == null || loginInfo.UserInfo == null)
|
||||
return Result<LoginInDto>.ReFailure(BaseResultCodes.UnAuthorized);
|
||||
var res = await _loginService.RefreshToken(loginInfo.TokenInfo.Token, loginInfo.TokenInfo.RefreshToken);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Web;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -21,6 +22,12 @@ namespace WMS.Web.Api
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}).ConfigureLogging(logging =>
|
||||
{
|
||||
//<2F><><EFBFBD><EFBFBD>ϵͳĬ<CDB3><C4AC><EFBFBD><EFBFBD>־
|
||||
//logging.ClearProviders();
|
||||
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
|
||||
})
|
||||
.UseNLog();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,30 +2,53 @@ using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Domain.Options;
|
||||
using WMS.Web.Repositories.DependencyInjection;
|
||||
|
||||
namespace WMS.Web.Api
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
|
||||
{
|
||||
Configuration = configuration;
|
||||
WebHostEnvironment = webHostEnvironment;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public IWebHostEnvironment WebHostEnvironment;
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||
var option = Configuration.GetSection("AppOptions").Get<AppOptions>();
|
||||
var option_soa = Configuration.GetSection("SoaOptions").Get<SoaOptions>();
|
||||
|
||||
var serverVersion = new MySqlServerVersion(new Version(8, 0, 18));
|
||||
//<2F><><EFBFBD>ݿ<EFBFBD>ע<EFBFBD><D7A2>
|
||||
services.AddApp(Configuration, WebHostEnvironment, builder =>
|
||||
{
|
||||
builder.AddDbContext(
|
||||
opt => { opt.UseMySql(option.DBConnectionString, serverVersion); });
|
||||
});
|
||||
services.AddControllers();
|
||||
//<2F>Ƴ<EFBFBD><C6B3><EFBFBD>http<74><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>ַhttps://docs.microsoft.com/zh-cn/dotnet/core/compatibility/aspnet-core/5.0/http-httpclient-instances-log-integer-status-codes<65><73>
|
||||
services.RemoveAll<IHttpMessageHandlerBuilderFilter>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
@@ -35,13 +58,26 @@ namespace WMS.Web.Api
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
if (WebHostEnvironment.IsDevelopment())
|
||||
{
|
||||
// Swagger<65>м<EFBFBD><D0BC><EFBFBD>
|
||||
app.UseSwagger();
|
||||
// SwaggerUI
|
||||
app.UseSwaggerUI(c =>
|
||||
{
|
||||
c.SwaggerEndpoint($"/swagger/v1/swagger.json", "WMS-Api");
|
||||
c.RoutePrefix = string.Empty;
|
||||
c.DocExpansion(DocExpansion.None);
|
||||
});
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseStaticFiles();
|
||||
app.UseCors("AllowAllOrigin");
|
||||
//ͳһ<CDB3>쳣<EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
||||
app.UseMiddleware<AuthorizationTokenSecurityPolicy>();
|
||||
app.UseMiddleware<ErrorHandlingMiddleware>();
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
|
||||
41
src/WMS.Web.Api/nlog.config
Normal file
41
src/WMS.Web.Api/nlog.config
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<!-- enable asp.net core layout renderers -->
|
||||
<extensions>
|
||||
<add assembly="NLog.Web.AspNetCore"/>
|
||||
</extensions>
|
||||
|
||||
<!-- the targets to write to -->
|
||||
<targets>
|
||||
<!--写入文件-->
|
||||
<target
|
||||
xsi:type="File"
|
||||
name="DebugFile"
|
||||
fileName="Logs\Debug\${shortdate}.log"
|
||||
layout="日志时间:${longdate}${newline}日志来源:${callsite}${newline}日志级别:${uppercase:${level}}${newline}消息内容:${message}${newline}----------------------------------------------------------------${newline}" >
|
||||
</target>
|
||||
<target
|
||||
xsi:type="File"
|
||||
name="InfoFile"
|
||||
fileName="Logs\Info\${shortdate}.log"
|
||||
layout="日志时间:${longdate}${newline}日志来源:${callsite}${newline}日志级别:${uppercase:${level}}${newline}消息内容:${message}${newline}----------------------------------------------------------------${newline}" >
|
||||
</target>
|
||||
<target
|
||||
xsi:type="File"
|
||||
name="ErrorFile"
|
||||
fileName="Logs\Error\${shortdate}.log"
|
||||
layout="日志时间:${longdate}${newline}日志来源:${callsite}${newline}日志级别:${uppercase:${level}}${newline}消息内容:${message}${newline}异常信息:${exception:format=tostring}${newline}----------------------------------------------------------------${newline}" >
|
||||
</target>
|
||||
</targets>
|
||||
|
||||
<!-- rules to map from logger name to target -->
|
||||
<rules>
|
||||
<!--Skip non-critical Microsoft logs and so log only own logs-->
|
||||
<logger name="Microsoft.*" maxlevel="Info" final="true" />
|
||||
<logger name="*" minlevel="Debug" maxLevel="Debug" writeTo="DebugFile" />
|
||||
<logger name="*" minlevel="Info" maxLevel="Info" writeTo="InfoFile" />
|
||||
<logger name="*" minlevel="Error" maxLevel="Error" writeTo="ErrorFile" />
|
||||
</rules>
|
||||
</nlog>
|
||||
@@ -4,5 +4,35 @@
|
||||
<name>WMS.Web.Api</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:WMS.Web.Api.Controllers.LoginController">
|
||||
<summary>
|
||||
登录接口
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:WMS.Web.Api.Controllers.LoginController.LoginIn(System.String)">
|
||||
<summary>
|
||||
登录
|
||||
</summary>
|
||||
<param name="code"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:WMS.Web.Api.Controllers.LoginController.LoginOut">
|
||||
<summary>
|
||||
退出登录
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:WMS.Web.Api.Controllers.LoginController.LoginOutSingle">
|
||||
<summary>
|
||||
PHP单点退出使用
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:WMS.Web.Api.Controllers.LoginController.RefreshToken">
|
||||
<summary>
|
||||
刷新token
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
|
||||
Reference in New Issue
Block a user