CommonController.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Localization;
  3. using Microsoft.Extensions.Options;
  4. using MiniExcelLibs;
  5. using ZR.Infrastructure.IPTools;
  6. using ZR.Model.Dto;
  7. using ZR.Model.System;
  8. using ZR.ServiceCore.Resources;
  9. namespace ZR.Admin.WebApi.Controllers
  10. {
  11. /// <summary>
  12. /// 公共模块
  13. /// </summary>
  14. [Route("[controller]/[action]")]
  15. [ApiExplorerSettings(GroupName = "sys")]
  16. public class CommonController : BaseController
  17. {
  18. private OptionsSetting OptionsSetting;
  19. private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  20. private IWebHostEnvironment WebHostEnvironment;
  21. private ISysFileService SysFileService;
  22. private readonly IStringLocalizer<SharedResource> _localizer;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="stringLocalizer"></param>
  27. /// <param name="options"></param>
  28. /// <param name="webHostEnvironment"></param>
  29. /// <param name="fileService"></param>
  30. public CommonController(
  31. IStringLocalizer<SharedResource> stringLocalizer,
  32. IOptions<OptionsSetting> options,
  33. IWebHostEnvironment webHostEnvironment,
  34. ISysFileService fileService)
  35. {
  36. WebHostEnvironment = webHostEnvironment;
  37. SysFileService = fileService;
  38. OptionsSetting = options.Value;
  39. _localizer = stringLocalizer;
  40. }
  41. /// <summary>
  42. /// home
  43. /// </summary>
  44. /// <returns></returns>
  45. [Route("/")]
  46. [HttpGet]
  47. [AllowAnonymous]
  48. public IActionResult Index()
  49. {
  50. var hello = _localizer["hello"].Value;
  51. return Ok($"{hello}看到这里页面说明你已经成功启动了本项目:)\n\n" +
  52. "如果觉得项目有用,打赏作者喝杯咖啡作为奖励\n☛☛http://www.izhaorui.cn/vip\n");
  53. }
  54. /// <summary>
  55. /// 查询IP信息
  56. /// </summary>
  57. /// <returns></returns>
  58. [Route("/ip")]
  59. [HttpGet]
  60. [AllowAnonymous]
  61. public IActionResult IPInfo(string ip)
  62. {
  63. if (ip.IsEmpty()) return ToResponse(ResultCode.CUSTOM_ERROR, "IP异常");
  64. var region = IpTool.GetRegion(ip);
  65. return SUCCESS(region);
  66. }
  67. /// <summary>
  68. /// 企业消息测试
  69. /// </summary>
  70. /// <param name="msg">要发送的消息</param>
  71. /// <param name="toUser">要发送的人@all所有,xxx单独发送对个人</param>
  72. /// <returns></returns>
  73. [Route("/sendMsg")]
  74. [HttpGet]
  75. [Log(Title = "企业消息测试")]
  76. public IActionResult SendMsg(string msg, string toUser = "")
  77. {
  78. WxNoticeHelper.SendMsg("消息测试", msg, toUser, WxNoticeHelper.MsgType.markdown);
  79. return SUCCESS(msg);
  80. }
  81. #region 上传
  82. /// <summary>
  83. /// 存储文件
  84. /// </summary>
  85. /// <param name="uploadDto">自定义文件名</param>
  86. /// <param name="file"></param>
  87. /// <param name="storeType">上传类型1、保存到本地 2、保存到阿里云</param>
  88. /// <returns></returns>
  89. [HttpPost]
  90. [ActionPermissionFilter(Permission = "common")]
  91. public async Task<IActionResult> UploadFile([FromForm] UploadDto uploadDto, IFormFile file, StoreType storeType = StoreType.LOCAL)
  92. {
  93. if (file == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
  94. SysFile sysfile = new();
  95. IFormFile formFile = file;
  96. string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
  97. double fileSize = Math.Round(formFile.Length / 1024.0, 2);//文件大小KB
  98. if (OptionsSetting.Upload.NotAllowedExt.Contains(fileExt))
  99. {
  100. return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
  101. }
  102. if (uploadDto.FileNameType == 1)
  103. {
  104. uploadDto.FileName = Path.GetFileNameWithoutExtension(formFile.FileName);
  105. }
  106. else if (uploadDto.FileNameType == 3)
  107. {
  108. uploadDto.FileName = SysFileService.HashFileName();
  109. }
  110. switch (storeType)
  111. {
  112. case StoreType.LOCAL:
  113. string savePath = Path.Combine(WebHostEnvironment.WebRootPath);
  114. if (uploadDto.FileDir.IsEmpty())
  115. {
  116. uploadDto.FileDir = OptionsSetting.Upload.LocalSavePath;
  117. }
  118. sysfile = await SysFileService.SaveFileToLocal(savePath, uploadDto, HttpContext.GetName(), formFile);
  119. break;
  120. case StoreType.REMOTE:
  121. break;
  122. case StoreType.ALIYUN:
  123. int AlimaxContentLength = OptionsSetting.ALIYUN_OSS.MaxSize;
  124. if (OptionsSetting.ALIYUN_OSS.REGIONID.IsEmpty())
  125. {
  126. return ToResponse(ResultCode.CUSTOM_ERROR, "配置文件缺失");
  127. }
  128. if ((fileSize / 1024) > AlimaxContentLength)
  129. {
  130. return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + AlimaxContentLength + " MB");
  131. }
  132. sysfile = new(formFile.FileName, uploadDto.FileName, fileExt, fileSize + "kb", uploadDto.FileDir, HttpContext.GetName())
  133. {
  134. StoreType = (int)StoreType.ALIYUN,
  135. FileType = formFile.ContentType,
  136. ClassifyType = uploadDto.ClassifyType,
  137. CategoryId = uploadDto.CategoryId,
  138. };
  139. sysfile = await SysFileService.SaveFileToAliyun(sysfile, uploadDto, formFile);
  140. if (sysfile.Id <= 0) { return ToResponse(ApiResult.Error("阿里云连接失败")); }
  141. break;
  142. case StoreType.TENCENT:
  143. break;
  144. case StoreType.QINIU:
  145. break;
  146. default:
  147. break;
  148. }
  149. return SUCCESS(new
  150. {
  151. url = sysfile.AccessUrl,
  152. fileName = sysfile.FileName,
  153. fileId = sysfile.Id.ToString()
  154. });
  155. }
  156. #endregion
  157. /// <summary>
  158. /// 下载文件
  159. /// </summary>
  160. /// <param name="path"></param>
  161. /// <param name="fileId"></param>
  162. /// <returns></returns>
  163. [HttpGet]
  164. [ActionPermissionFilter(Permission = "common")]
  165. [Log(Title = "下载文件", IsSaveResponseData = false)]
  166. public IActionResult DownloadFile(string? path, long fileId = 0)
  167. {
  168. var tempPath = path;
  169. if (fileId > 0)
  170. {
  171. var fileInfo = SysFileService.GetById(fileId);
  172. if (fileInfo != null)
  173. {
  174. tempPath = fileInfo.FileUrl;
  175. }
  176. }
  177. string fullPath = tempPath;
  178. //if (tempPath.StartsWith("/"))
  179. //{
  180. // fullPath = Path.Combine(WebHostEnvironment.WebRootPath, tempPath.ReplaceFirst("/", ""));
  181. //}
  182. string fileName = Path.GetFileName(fullPath);
  183. return DownFile(fullPath, fileName);
  184. }
  185. /// <summary>
  186. /// 初始化种子数据
  187. /// </summary>
  188. /// <param name="clean">是否清空数据</param>
  189. /// <returns></returns>
  190. [HttpGet]
  191. [AllowAnonymous]
  192. [ActionPermissionFilter(Permission = "common")]
  193. [Log(BusinessType = BusinessType.INSERT, Title = "初始化数据")]
  194. public IActionResult InitSeedData(bool clean = false)
  195. {
  196. if (!WebHostEnvironment.IsDevelopment())
  197. {
  198. return ToResponse(ResultCode.CUSTOM_ERROR, "导入数据失败,请在开发模式下初始化");
  199. }
  200. var path = Path.Combine(WebHostEnvironment.WebRootPath, "data.xlsx");
  201. SeedDataService seedDataService = new();
  202. var result = seedDataService.InitSeedData(path, clean);
  203. Console.ForegroundColor = ConsoleColor.Red;
  204. foreach (var item in result)
  205. {
  206. Console.WriteLine(item);
  207. }
  208. Console.ForegroundColor = ConsoleColor.White;
  209. return SUCCESS(new
  210. {
  211. result
  212. });
  213. }
  214. /// <summary>
  215. ///
  216. /// </summary>
  217. /// <returns></returns>
  218. [HttpGet]
  219. [AllowAnonymous]
  220. [ActionPermissionFilter(Permission = "common")]
  221. [Log(BusinessType = BusinessType.INSERT, Title = "初始化数据")]
  222. public IActionResult UpdateSeedData()
  223. {
  224. if (!WebHostEnvironment.IsDevelopment())
  225. {
  226. return ToResponse(ResultCode.CUSTOM_ERROR, "导入数据失败,请在开发模式下初始化");
  227. }
  228. var path = Path.Combine(WebHostEnvironment.WebRootPath, "data.xlsx");
  229. SeedDataService seedDataService = new();
  230. var sysNotice = MiniExcel.Query<SysNotice>(path, sheetName: "notice").ToList();
  231. var result = seedDataService.InitNoticeData(sysNotice);
  232. var sysMenu = MiniExcel.Query<SysMenu>(path, sheetName: "menu").Where(f => f.MenuId >= 1104).ToList();
  233. var result5 = seedDataService.InitMenuData(sysMenu);
  234. return SUCCESS(new
  235. {
  236. result,
  237. result5
  238. });
  239. }
  240. }
  241. }