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; /// /// 基础控制器 /// [Authorize] [ApiController] [Route("api/[controller]")] [ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))] public abstract class BaseController : ControllerBase { /// /// 获取当前用户ID /// /// 用户ID 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; } /// /// 获取当前用户名 /// /// 用户名 protected string? GetCurrentUserName() { return User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; } /// /// 成功响应 /// /// 数据类型 /// 数据 /// 提示信息 /// 统一响应对象 protected BaseResponse Success(T data, string message = "操作成功") { return BaseResponse.Success(data, message); } /// /// 失败响应 /// /// 提示信息 /// 状态码 /// 统一响应对象 protected BaseResponse Fail(string message, int code = 500) { return BaseResponse.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)); } /// /// 导出Excel /// /// 完整文件路径 /// 带扩展文件名 /// 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(List list, string fileName) { IWebHostEnvironment webHostEnvironment = ServiceContext.GetService(); var webRoot = webHostEnvironment.WebRootPath ?? Path.Combine(webHostEnvironment.ContentRootPath, "wwwroot"); if (!Directory.Exists(webRoot)) Directory.CreateDirectory(webRoot); string sFileName = $"{fileName}.xlsx"; string fullPath = Path.Combine(webRoot, "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(List list, string sheetName, string fileName) { IWebHostEnvironment webHostEnvironment = ServiceContext.GetService(); var webRoot = webHostEnvironment.WebRootPath ?? Path.Combine(webHostEnvironment.ContentRootPath, "wwwroot"); if (!Directory.Exists(webRoot)) Directory.CreateDirectory(webRoot); string sFileName = $"{fileName}_{DateTime.Now:MMdd_HHmmss}.xlsx"; string fullPath = Path.Combine(webRoot, "export", sFileName); Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); MiniExcel.SaveAs(fullPath, list, sheetName: sheetName); return (sFileName, fullPath); } }