ApiControllerBase.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using CsvHelper;
  2. using CsvHelper.Configuration;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.StaticFiles;
  5. using Microsoft.Net.Http.Headers;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Net;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Linq;
  14. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  15. namespace XYY.Core.Standard.Mvc
  16. {
  17. public class ApiControllerBase : ControllerBase
  18. {
  19. string tempPath = System.AppContext.BaseDirectory;
  20. [NonAction]
  21. public async Task<IActionResult> OutCsv<X, Y>(List<X> list, string fileName) where Y : ClassMap<X>
  22. {
  23. if (!System.IO.Directory.Exists(tempPath))
  24. System.IO.Directory.CreateDirectory(tempPath);
  25. string filePath = tempPath + "\\" + Guid.NewGuid() + ".csv";
  26. using (var writer = new StreamWriter(filePath, false, Encoding.GetEncoding("GB2312")))
  27. {
  28. using (var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture))
  29. {
  30. csv.Context.RegisterClassMap<Y>();
  31. await csv.WriteRecordsAsync(list);
  32. }
  33. }
  34. var data = System.IO.File.ReadAllBytes(filePath);
  35. System.IO.File.Delete(filePath);
  36. return CSVFile(fileName, data);
  37. }
  38. [NonAction]
  39. public IActionResult CSVFile(string fileName, byte[] data)
  40. {
  41. this.HttpContext.Response.Headers.Add("ContentDisposition", "attachment");
  42. return File(data, "text/csv", fileName);
  43. }
  44. [NonAction]
  45. public new IActionResult File(string fileName, byte[] data)
  46. {
  47. this.HttpContext.Response.Headers.Add("ContentDisposition", "attachment");
  48. new FileExtensionContentTypeProvider().TryGetContentType(fileName, out var contentType);
  49. return File(data, contentType, fileName);
  50. }
  51. [NonAction]
  52. public new IActionResult Ok(object o)
  53. {
  54. return new OkObjectResult(new ApiJsonModel
  55. {
  56. code = HttpStatusCode.OK,
  57. data = o,
  58. success = true,
  59. message = "操作成功!"
  60. });
  61. }
  62. [NonAction]
  63. public IActionResult Ok(string message)
  64. {
  65. return new OkObjectResult(new ApiJsonModel
  66. {
  67. code = HttpStatusCode.OK,
  68. data = message,
  69. success = true,
  70. message = "操作成功!"
  71. });
  72. }
  73. [NonAction]
  74. public new IActionResult Ok()
  75. {
  76. return new OkObjectResult(new ApiJsonModel
  77. {
  78. code = HttpStatusCode.OK,
  79. data = string.Empty,
  80. success = true,
  81. message = "操作成功!"
  82. });
  83. }
  84. [NonAction]
  85. public IActionResult Error(string message)
  86. {
  87. return new OkObjectResult(new ApiJsonModel
  88. {
  89. code = HttpStatusCode.InternalServerError,
  90. data = message,
  91. success = false,
  92. message = "操作失败!"
  93. });
  94. }
  95. [NonAction]
  96. public IActionResult GetError(string message, object response)
  97. {
  98. return new OkObjectResult(new ApiJsonModel
  99. {
  100. code = HttpStatusCode.InternalServerError,
  101. data = response,
  102. success = false,
  103. message = message,
  104. error = message
  105. });
  106. }
  107. }
  108. }