init
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
using ClockSnowFlake;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using dy.net.service;
|
||||
using dy.net.utils;
|
||||
using dy.net.dto;
|
||||
|
||||
namespace dy.net.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IWebHostEnvironment webHostEnvironment;
|
||||
|
||||
private readonly UserService _userService;
|
||||
public AuthController(UserService userService, IWebHostEnvironment webHostEnvironment )
|
||||
{
|
||||
_userService=userService;
|
||||
this.webHostEnvironment = webHostEnvironment;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 修改密码
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> UpdatePwd(UpdatePwdRequest user)
|
||||
{
|
||||
var (code, erro) = await _userService.UpdatePwd(user);
|
||||
return Ok(new { code, erro });
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetUserAvatar()
|
||||
{
|
||||
var user = await _userService.GetUser();
|
||||
return Ok(new { code = 0, error = "", data = new { user?.Avatar, user?.Id,user?.UserName } });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改用户头像
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> UpdateUserAvatar(IFormFile file)
|
||||
{
|
||||
if (file != null && file.Length > 0)
|
||||
{
|
||||
long maxFileSize = 5 * 1024 * 1024; // 限制文件大小为5MB
|
||||
if (file.Length > maxFileSize)
|
||||
{
|
||||
return Ok(new { code = -1, erro = "文件最大只能上传5M" });
|
||||
}
|
||||
var fileName = $"{IdGener.GetGuid()}_{file.FileName}";
|
||||
var filePath = webHostEnvironment.IsProduction() ?
|
||||
Path.Combine(Md5Util.UPLOAD_PATH_PRO, fileName) : Path.Combine(Md5Util.UPLOAD_PATH_DEV, fileName);
|
||||
using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
|
||||
try
|
||||
{
|
||||
// 问了节约空间,删除文件夹下的所有文件
|
||||
string[] files = Directory.GetFiles(webHostEnvironment.IsProduction() ? Md5Util.UPLOAD_PATH_PRO : Md5Util.UPLOAD_PATH_DEV);
|
||||
foreach (string mfile in files)
|
||||
{
|
||||
if (!mfile.Contains(fileName))
|
||||
System.IO.File.Delete(mfile);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Serilog.Log.Error($"delete file error ,{ex.Message}");
|
||||
}
|
||||
var update = await _userService.UpdateAvatar( fileName);
|
||||
|
||||
return Ok(new { code = update ? 0 : -1, erro = update ? "" : "上传失败", data = update ? fileName : "" });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Ok(new { code = -1, erro = "空文件" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 登录获取token
|
||||
/// </summary>
|
||||
/// <param name="loginUserInfo"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Login(LoginRequest loginUserInfo)
|
||||
{
|
||||
if (loginUserInfo == null)
|
||||
{
|
||||
return Ok(new { code = -1, erro = "参数不能为空" });
|
||||
}
|
||||
else
|
||||
{
|
||||
var user = await _userService.GetUser();
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return Ok(new { code = -1, erro = "用户名或密码不正确" });
|
||||
}
|
||||
else
|
||||
{
|
||||
if (user.Password == Md5Util.Md5(loginUserInfo.Password))
|
||||
{
|
||||
|
||||
var tokenString = GenerateJwtToken(user.UserName);
|
||||
|
||||
return Ok(new { code = 0, erro = "", token = tokenString, expires = 24 * 60 * 60 * 1000 });
|
||||
}
|
||||
else
|
||||
{
|
||||
return Ok(new { code = -1, erro = "用户名或密码不正确" });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private string GenerateJwtToken(string username)
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, username),
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
||||
};
|
||||
var k = Md5Util.JWT_TOKEN_KEY;
|
||||
var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(k));
|
||||
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
var expires = DateTime.Now.AddDays(1);
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: IdGener.GetLong().ToString(),
|
||||
audience: IdGener.GetLong().ToString(),
|
||||
claims: claims,
|
||||
expires: expires,
|
||||
signingCredentials: credentials
|
||||
);
|
||||
|
||||
var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
return jwtToken;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
using ClockSnowFlake;
|
||||
using dy.net.dto;
|
||||
using dy.net.model;
|
||||
using dy.net.service;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using static Dm.net.buffer.ByteArrayBuffer;
|
||||
|
||||
namespace dy.net.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ConfigController : ControllerBase
|
||||
{
|
||||
private readonly DyCookieService dyCookieService;
|
||||
|
||||
private readonly CommonService commonService;
|
||||
private readonly QuartzJobService quartzJobService;
|
||||
|
||||
public ConfigController(DyCookieService dyCookieService, CommonService commonService,QuartzJobService quartzJobService)
|
||||
{
|
||||
this.dyCookieService = dyCookieService;
|
||||
this.commonService = commonService;
|
||||
this.quartzJobService = quartzJobService;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
/// <returns>分页结果(视频列表和总数)</returns>
|
||||
[HttpPost("paged")]
|
||||
public async Task<IActionResult> GetPagedAsync(
|
||||
PageRequestDto dto)
|
||||
{
|
||||
var (list, totalCount) = await dyCookieService.GetPagedAsync(dto.PageIndex, dto.PageSize);
|
||||
return Ok(new
|
||||
{
|
||||
code = 0,
|
||||
data = new
|
||||
{
|
||||
data = list,
|
||||
total=totalCount,
|
||||
pageIndex= dto.PageIndex,
|
||||
pageSize= dto.PageSize
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
/// <summary>
|
||||
/// 新增用户Cookie
|
||||
/// </summary>
|
||||
[HttpPost("add")]
|
||||
public async Task<IActionResult> AddAsync([FromBody] DyUserCookies dyUserCookies)
|
||||
{
|
||||
var result = await dyCookieService.Add(dyUserCookies);
|
||||
if (result)
|
||||
{
|
||||
await ReStartJob();
|
||||
return Ok(new { code = 0 });
|
||||
}
|
||||
return BadRequest(new { code=-1, message = "添加失败" });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新用户Cookie
|
||||
/// </summary>
|
||||
[HttpPost("update")]
|
||||
public async Task<IActionResult> UpdateAsync([FromBody] DyUserCookies dyUserCookies)
|
||||
{
|
||||
if (dyUserCookies.Id == "0")
|
||||
{
|
||||
dyUserCookies.Id=IdGener.GetLong().ToString();
|
||||
var result = await dyCookieService.Add(dyUserCookies);
|
||||
if (result)
|
||||
{
|
||||
await ReStartJob();
|
||||
return Ok(new { code = 0 });
|
||||
}
|
||||
return BadRequest(new { code = -1, message = "添加失败" });
|
||||
}
|
||||
else {
|
||||
var result = await dyCookieService.UpdateAsync(dyUserCookies);
|
||||
if (result)
|
||||
{
|
||||
await ReStartJob();
|
||||
return Ok(new { code = 0 });
|
||||
}
|
||||
return BadRequest(new { code = -1, message = "更新失败" });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除用户Cookie
|
||||
/// </summary>
|
||||
[HttpGet("delete")]
|
||||
public async Task<IActionResult> DeleteAsync(string id)
|
||||
{
|
||||
var count = await dyCookieService.DeleteByIdsAsync(new List<string> { id});
|
||||
if (count > 0) {
|
||||
await ReStartJob();
|
||||
}
|
||||
return Ok(new { code = 0, deletedCount = count });
|
||||
}
|
||||
|
||||
[HttpGet("GetConfig")]
|
||||
public IActionResult GetConfig()
|
||||
{
|
||||
var data = commonService.GetConfig();
|
||||
return Ok(new { code = 0, data = data });
|
||||
}
|
||||
|
||||
[HttpPost("UpdateConfig")]
|
||||
public async Task<IActionResult> UpdateConfig(AppConfig config)
|
||||
{
|
||||
var data = await commonService.UpdateConfig(config);
|
||||
if (data) {
|
||||
await ReStartJob();
|
||||
}
|
||||
return Ok(new { code = 0, data = data });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("ExecuteJobNow")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> ExecuteJobNow()
|
||||
{
|
||||
var config = commonService.GetConfig();
|
||||
await quartzJobService.StartJob(config.Cron);
|
||||
return Ok(new { code = 0 , error = "" });
|
||||
}
|
||||
|
||||
|
||||
private async Task ReStartJob() {
|
||||
var config= commonService.GetConfig();
|
||||
if(config!=null)
|
||||
await quartzJobService.StartJob(config.Cron);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace dy.net.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class LogsController : ControllerBase
|
||||
{
|
||||
private readonly IWebHostEnvironment webHostEnvironment;
|
||||
|
||||
public LogsController(IWebHostEnvironment webHostEnvironment)
|
||||
{
|
||||
this.webHostEnvironment = webHostEnvironment;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetLog(string type, string date)
|
||||
{
|
||||
var filePath = Path.Combine(webHostEnvironment.IsDevelopment() ? Directory.GetCurrentDirectory() : AppDomain.CurrentDomain.BaseDirectory, "logs", $"log-{type}-{date}.txt");
|
||||
if (!System.IO.File.Exists(filePath))
|
||||
{
|
||||
var msg = $"Log file log-{type}-{date}.txt not found.";
|
||||
//Serilog.Log.Error(msg);
|
||||
return Ok(msg);
|
||||
}
|
||||
return PhysicalFile(filePath, "text/plain; charset=utf-8");
|
||||
|
||||
//下面的方案提示文件被占用
|
||||
//var encoding = Encoding.GetEncoding("UTF-8"); // 指定文本文件的编码
|
||||
//var fileBytes = await System.IO.File.ReadAllBytesAsync(filePath);
|
||||
//var fileContent = encoding.GetString(fileBytes);
|
||||
//return Content (fileContent, "text/plain", encoding);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using dy.net.dto;
|
||||
using dy.net.service;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Drawing.Printing;
|
||||
|
||||
namespace dy.net.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class VideoController : ControllerBase
|
||||
{
|
||||
private readonly DyCollectVideoService dyCollectVideoService;
|
||||
|
||||
public VideoController(DyCollectVideoService dyCollectVideoService)
|
||||
{
|
||||
this.dyCollectVideoService = dyCollectVideoService;
|
||||
}
|
||||
/// <summary>
|
||||
/// 分页查询收藏视频
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
[HttpPost("paged")]
|
||||
public async Task<IActionResult> GetPagedAsync(VideoPageRequestDTO dto)
|
||||
{
|
||||
var (list, totalCount) = await dyCollectVideoService.GetPagedAsync(dto.PageIndex, dto.PageSize, dto.Tag, dto.Author,dto.ViedoType,dto.Dates);
|
||||
return Ok(new
|
||||
{
|
||||
code = 0,
|
||||
data = new
|
||||
{
|
||||
data = list,
|
||||
total=totalCount,
|
||||
pageIndex=dto.PageIndex,
|
||||
pageSize=dto.PageSize
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询统计数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("statics")]
|
||||
public async Task<IActionResult> GetStaticsAsync()
|
||||
{
|
||||
var data = await dyCollectVideoService.GetStatics();
|
||||
return Ok(new
|
||||
{
|
||||
code = 0,
|
||||
data
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user