BaseController.cs 8.7 KB

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