1. 重命名枚举项与实体字段,修正类型引用 2. 新增IsNull扩展方法与多项字符串处理扩展 3. 新增大量业务DTO、服务接口与枚举定义 4. 重构RabbitMQ服务实现,替换旧版消息队列组件 5. 优化签到服务的宠物喂养事务逻辑 6. 移除冗余的项目引用与旧版消息队列代码 7. 新增Excel导出、导入模板相关工具方法
136 lines
4.3 KiB
C#
136 lines
4.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MiniExcelLibs;
|
|
using QYZH.InteractiveMagazine.Infrastructure.Context;
|
|
using QYZH.InteractiveMagazine.Models.Common;
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
using QYZH.InteractiveMagazine.Models.Enum;
|
|
using System.Security.Claims;
|
|
using System.Web;
|
|
|
|
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
|
|
|
/// <summary>
|
|
/// 基础控制器
|
|
/// </summary>
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
|
|
public abstract class BaseController : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// 获取当前用户ID
|
|
/// </summary>
|
|
/// <returns>用户ID</returns>
|
|
protected long? GetCurrentUserId()
|
|
{
|
|
var userIdClaim = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
|
|
if (userIdClaim != null && long.TryParse(userIdClaim.Value, out var userId))
|
|
{
|
|
return userId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前用户名
|
|
/// </summary>
|
|
/// <returns>用户名</returns>
|
|
protected string? GetCurrentUserName()
|
|
{
|
|
return User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 成功响应
|
|
/// </summary>
|
|
/// <typeparam name="T">数据类型</typeparam>
|
|
/// <param name="data">数据</param>
|
|
/// <param name="message">提示信息</param>
|
|
/// <returns>统一响应对象</returns>
|
|
protected BaseResponse<T> Success<T>(T data, string message = "操作成功")
|
|
{
|
|
return BaseResponse<T>.Success(data, message);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 失败响应
|
|
/// </summary>
|
|
/// <param name="message">提示信息</param>
|
|
/// <param name="code">状态码</param>
|
|
/// <returns>统一响应对象</returns>
|
|
protected BaseResponse<object> Fail(string message, int code = 500)
|
|
{
|
|
return BaseResponse<object>.Fail(ResultCode.GLOBAL_ERROR, message);
|
|
}
|
|
|
|
protected IActionResult ApiResult(bool success, string msg)
|
|
{
|
|
return success ? Success() : Fail(msg);
|
|
}
|
|
|
|
protected IActionResult Success()
|
|
{
|
|
return Ok(BaseResponse.Success());
|
|
}
|
|
|
|
protected IActionResult Fail(string msg)
|
|
{
|
|
return Ok(BaseResponse.Fail(msg));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出Excel
|
|
/// </summary>
|
|
/// <param name="path">完整文件路径</param>
|
|
/// <param name="fileName">带扩展文件名</param>
|
|
/// <returns></returns>
|
|
protected IActionResult ExportExcel(string path, string fileName)
|
|
{
|
|
//var webHostEnvironment = App.WebHostEnvironment;
|
|
if (!Path.Exists(path))
|
|
{
|
|
throw new BusinessException(fileName + "文件不存在");
|
|
}
|
|
var stream = System.IO.File.OpenRead(path); //创建文件流
|
|
|
|
Response.Headers.Append("Access-Control-Expose-Headers", "Content-Disposition");
|
|
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
|
|
}
|
|
|
|
protected (string, string) DownloadImportTemplate<T>(List<T> list, string fileName)
|
|
{
|
|
IWebHostEnvironment webHostEnvironment = ServiceContext.GetService<IWebHostEnvironment>();
|
|
string sFileName = $"{fileName}.xlsx";
|
|
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", sFileName);
|
|
|
|
//不存在模板创建模板
|
|
if (!Directory.Exists(fullPath))
|
|
{
|
|
var directoryPath = Path.GetDirectoryName(fullPath);
|
|
if (directoryPath != null)
|
|
{
|
|
Directory.CreateDirectory(directoryPath);
|
|
}
|
|
}
|
|
if (!Path.Exists(fullPath))
|
|
{
|
|
MiniExcel.SaveAs(fullPath, list, overwriteFile: true);
|
|
}
|
|
return (sFileName, fullPath);
|
|
}
|
|
|
|
protected (string, string) ExportExcelMini<T>(List<T> list, string sheetName, string fileName)
|
|
{
|
|
IWebHostEnvironment webHostEnvironment = ServiceContext.GetService<IWebHostEnvironment>();
|
|
string sFileName = $"{fileName}_{DateTime.Now:MMdd_HHmmss}.xlsx";
|
|
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
|
|
|
|
MiniExcel.SaveAs(fullPath, list, sheetName: sheetName);
|
|
return (sFileName, fullPath);
|
|
}
|
|
}
|