BaseController.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Extensions;
  2. using Infrastructure;
  3. using Infrastructure.Model;
  4. using Microsoft.AspNetCore.Mvc;
  5. using MiniExcelLibs;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Serialization;
  8. using System.Web;
  9. namespace Controllers
  10. {
  11. /// <summary>
  12. /// web层通用数据处理
  13. /// </summary>
  14. //[ApiController]
  15. public class BaseController : ControllerBase
  16. {
  17. public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
  18. /// <summary>
  19. /// 返回成功封装
  20. /// </summary>
  21. /// <param name="data"></param>
  22. /// <param name="timeFormatStr"></param>
  23. /// <returns></returns>
  24. protected IActionResult SUCCESS(object data, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
  25. {
  26. string jsonStr = GetJsonStr(GetApiResult(data != null ? ResultCode.SUCCESS : ResultCode.NO_DATA, data), timeFormatStr);
  27. return Content(jsonStr, "application/json");
  28. }
  29. /// <summary>
  30. /// json输出带时间格式的
  31. /// </summary>
  32. /// <param name="apiResult"></param>
  33. /// <returns></returns>
  34. protected IActionResult ToResponse(ApiResult apiResult)
  35. {
  36. string jsonStr = GetJsonStr(apiResult, TIME_FORMAT_FULL);
  37. return Content(jsonStr, "application/json");
  38. }
  39. protected IActionResult ToResponse(long rows, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
  40. {
  41. string jsonStr = GetJsonStr(ToJson(rows), timeFormatStr);
  42. return Content(jsonStr, "application/json");
  43. }
  44. protected IActionResult ToResponse(ResultCode resultCode, string msg = "")
  45. {
  46. return ToResponse(new ApiResult((int)resultCode, msg));
  47. }
  48. /// <summary>
  49. /// 导出Excel
  50. /// </summary>
  51. /// <param name="path">完整文件路径</param>
  52. /// <param name="fileName">带扩展文件名</param>
  53. /// <returns></returns>
  54. protected IActionResult ExportExcel(string path, string fileName)
  55. {
  56. //var webHostEnvironment = App.WebHostEnvironment;
  57. if (!Path.Exists(path))
  58. {
  59. throw new CustomException(fileName + "文件不存在");
  60. }
  61. var stream = System.IO.File.OpenRead(path); //创建文件流
  62. Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
  63. return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
  64. }
  65. /// <summary>
  66. /// 下载文件
  67. /// </summary>
  68. /// <param name="path"></param>
  69. /// <param name="fileName">文件名,一定要带扩展名</param>
  70. /// <returns></returns>
  71. protected IActionResult DownFile(string path, string fileName)
  72. {
  73. if (!System.IO.File.Exists(path))
  74. {
  75. return NotFound();
  76. }
  77. var stream = System.IO.File.OpenRead(path); //创建文件流
  78. Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
  79. return File(stream, "application/octet-stream", HttpUtility.UrlEncode(fileName));
  80. }
  81. #region 方法
  82. /// <summary>
  83. /// 响应返回结果
  84. /// </summary>
  85. /// <param name="rows">受影响行数</param>
  86. /// <param name="data"></param>
  87. /// <returns></returns>
  88. protected ApiResult ToJson(long rows, object? data = null)
  89. {
  90. return rows > 0 ? ApiResult.Success("success", data) : GetApiResult(ResultCode.FAIL);
  91. }
  92. /// <summary>
  93. /// 全局Code使用
  94. /// </summary>
  95. /// <param name="resultCode"></param>
  96. /// <param name="data"></param>
  97. /// <returns></returns>
  98. protected ApiResult GetApiResult(ResultCode resultCode, object? data = null)
  99. {
  100. var msg = resultCode.GetDescription();
  101. return new ApiResult((int)resultCode, msg, data);
  102. }
  103. protected ApiResult Success()
  104. {
  105. return GetApiResult(ResultCode.SUCCESS);
  106. }
  107. /// <summary>
  108. ///
  109. /// </summary>
  110. /// <param name="apiResult"></param>
  111. /// <param name="timeFormatStr"></param>
  112. /// <returns></returns>
  113. private static string GetJsonStr(ApiResult apiResult, string timeFormatStr)
  114. {
  115. if (string.IsNullOrEmpty(timeFormatStr))
  116. {
  117. timeFormatStr = TIME_FORMAT_FULL;
  118. }
  119. var serializerSettings = new JsonSerializerSettings
  120. {
  121. // 设置为驼峰命名
  122. ContractResolver = new CamelCasePropertyNamesContractResolver(),
  123. DateFormatString = timeFormatStr
  124. };
  125. return JsonConvert.SerializeObject(apiResult, Formatting.Indented, serializerSettings);
  126. }
  127. #endregion
  128. /// <summary>
  129. /// 导出Excel
  130. /// </summary>
  131. /// <typeparam name="T"></typeparam>
  132. /// <param name="list"></param>
  133. /// <param name="sheetName"></param>
  134. /// <param name="fileName"></param>
  135. protected string ExportExcel<T>(List<T> list, string sheetName, string fileName)
  136. {
  137. return ExportExcelMini(list, sheetName, fileName).Item1;
  138. }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. /// <typeparam name="T"></typeparam>
  143. /// <param name="list"></param>
  144. /// <param name="sheetName"></param>
  145. /// <param name="fileName"></param>
  146. /// <returns></returns>
  147. protected (string, string) ExportExcelMini<T>(List<T> list, string sheetName, string fileName)
  148. {
  149. IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
  150. string sFileName = $"{fileName}{DateTime.Now:MM-dd-HHmmss}.xlsx";
  151. string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
  152. Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
  153. MiniExcel.SaveAs(fullPath, list, sheetName: sheetName);
  154. return (sFileName, fullPath);
  155. }
  156. /// <summary>
  157. /// 导出多个工作表(Sheet)
  158. /// </summary>
  159. /// <param name="sheets"></param>
  160. /// <param name="fileName"></param>
  161. /// <returns></returns>
  162. protected (string, string) ExportExcelMini(Dictionary<string, object> sheets, string fileName)
  163. {
  164. IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
  165. string sFileName = $"{fileName}{DateTime.Now:MM-dd-HHmmss}.xlsx";
  166. string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
  167. Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
  168. MiniExcel.SaveAs(fullPath, sheets);
  169. return (sFileName, fullPath);
  170. }
  171. /// <summary>
  172. /// 下载导入模板
  173. /// </summary>
  174. /// <typeparam name="T">数据类型</typeparam>
  175. /// <param name="list">空数据类型集合</param>
  176. /// <param name="fileName">下载文件名</param>
  177. /// <returns></returns>
  178. protected (string, string) DownloadImportTemplate<T>(List<T> list, string fileName)
  179. {
  180. IWebHostEnvironment webHostEnvironment = App.WebHostEnvironment;
  181. string sFileName = $"{fileName}.xlsx";
  182. string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", sFileName);
  183. //不存在模板创建模板
  184. if (!Directory.Exists(fullPath))
  185. {
  186. Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
  187. }
  188. if (!Path.Exists(fullPath))
  189. {
  190. MiniExcel.SaveAs(fullPath, list, overwriteFile: true);
  191. }
  192. return (sFileName, fullPath);
  193. }
  194. /// <summary>
  195. /// 下载指定文件模板
  196. /// </summary>
  197. /// <param name="fileName">下载文件名</param>
  198. /// <returns></returns>
  199. protected (string, string) DownloadImportTemplate(string fileName)
  200. {
  201. IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
  202. string sFileName = $"{fileName}.xlsx";
  203. string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", sFileName);
  204. return (sFileName, fullPath);
  205. }
  206. }
  207. }