You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/Service/DjyApiAuth/DjyApiAuthService.cs

147 lines
5.1 KiB
C#

2 years ago
using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Myshipping.Core.Entity;
using Microsoft.Extensions.Logging;
using System;
using Furion.DataEncryption;
2 years ago
namespace Myshipping.Core.Service
{
/// <summary>
/// 接口授权服务
/// </summary>
2 years ago
[ApiDescriptionSettings(Name = "DjyApiAuth", Order = 1)]
2 years ago
public class DjyApiAuthService : IDjyApiAuthService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<DjyApiAuth> _rep;
private readonly ILogger<DjyApiAuth> _logger;
public DjyApiAuthService(SqlSugarRepository<DjyApiAuth> rep, ILogger<DjyApiAuth> logger)
{
_rep = rep;
_logger = logger;
}
/// <summary>
/// 分页查询接口授权
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyApiAuth/page")]
public async Task<dynamic> Page([FromQuery] QueryDjyApiAuthInput input)
{
var entities = await _rep.AsQueryable()
2 years ago
.WhereIF(!string.IsNullOrWhiteSpace(input.ApiCode), u => u.ApiCode.Contains(input.ApiCode))
.WhereIF(!string.IsNullOrWhiteSpace(input.ApiName), u => u.ApiName.Contains(input.ApiName))
2 years ago
.WhereIF(!string.IsNullOrWhiteSpace(input.TenantName), u => u.TenantName == input.TenantName)
.WhereIF(!string.IsNullOrWhiteSpace(input.UserName), u => u.UserName == input.UserName)
2 years ago
.WhereIF(input.IsDisable.HasValue, u => u.IsDisable == input.IsDisable.Value)
2 years ago
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
2 years ago
/// 保存授权
2 years ago
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyApiAuth/save")]
2 years ago
public async Task<long> Save(SaveDjyApiAuthInput input)
2 years ago
{
2 years ago
DjyApiAuth entity = null;
if (input.Id == 0)
2 years ago
{
entity = input.Adapt<DjyApiAuth>();
entity.ApiKey = Guid.NewGuid().ToString().Replace("-", "").ToLower();
2 years ago
entity.ApiSecret = GenSecret();
2 years ago
await _rep.InsertAsync(entity);
}
else
{
entity = _rep.AsQueryable().Filter(null, true).First(x => x.Id == input.Id);
input.Adapt(entity);
await _rep.UpdateAsync(entity);
}
return entity.Id;
2 years ago
}
/// <summary>
/// 删除接口授权
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyApiAuth/delete")]
public async Task Delete(GetDjyApiAuthInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
/// <summary>
/// 获取接口授权
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyApiAuth/detail")]
public async Task<DjyApiAuth> Get([FromQuery] GetDjyApiAuthInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
2 years ago
/// <summary>
/// 重置秘钥
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("/DjyApiAuth/SecretReset")]
2 years ago
public async Task<string> SecretReset(long id)
{
var entity = _rep.AsQueryable().Filter(null, true).First(x => x.Id == id);
entity.ApiSecret = GenSecret();
await _rep.UpdateAsync(entity);
return entity.ApiSecret;
}
/// <summary>
/// 启用禁用
/// </summary>
/// <param name="id"></param>
/// <param name="disable">可用false禁用true</param>
/// <returns></returns>
[HttpPost("/DjyApiAuth/SetDisable")]
public async Task SetDisable(long id, bool disable)
{
var entity = _rep.AsQueryable().Filter(null, true).First(x => x.Id == id);
entity.IsDisable = disable;
await _rep.UpdateAsync(entity);
}
2 years ago
/// <summary>
/// 生成新秘钥
/// </summary>
/// <returns></returns>
[NonAction]
private string GenSecret()
{
return DESCEncryption.Encrypt(Guid.NewGuid().ToString(), "13245687").ToLower();
}
2 years ago
///// <summary>
///// 获取接口授权列表
///// </summary>
///// <param name="input"></param>
///// <returns></returns>
//[HttpGet("/DjyApiAuth/list")]
//public async Task<dynamic> List([FromQuery] QueryDjyApiAuthInput input)
//{
// return await _rep.ToListAsync();
//}
}
}