PublicMethodController.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using System.DrawingCore.Imaging;
  10. using System.IO;
  11. using Library;
  12. using MySystem.Areas.Admin.Controllers;
  13. using System.Collections;
  14. using LitJson;
  15. using System.Globalization;
  16. using System.Web;
  17. // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  18. namespace MySystem.Areas.Api.Controllers
  19. {
  20. [Area("Api")]
  21. [Route("Api/[controller]/[action]")]
  22. public class PublicMethodController : Admin.Controllers.BaseController
  23. {
  24. public PublicMethodController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  25. {
  26. }
  27. #region 图片验证码
  28. public FileContentResult CheckCode()
  29. {
  30. string code = function.get_Random(4);
  31. var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
  32. MemoryStream stream = new MemoryStream();
  33. bitmap.Save(stream, ImageFormat.Gif);
  34. function.WriteCookie(_accessor.HttpContext, "checkcode", code);
  35. return File(stream.ToArray(), "image/gif");
  36. }
  37. #endregion
  38. #region 图片验证码
  39. public FileContentResult PictureCode(string Tag = "")
  40. {
  41. string code = function.get_Random(4);
  42. var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
  43. MemoryStream stream = new MemoryStream();
  44. bitmap.Save(stream, ImageFormat.Gif);
  45. RedisDbconn.Instance.Set(Tag, code);
  46. RedisDbconn.Instance.SetExpire(Tag, 600);
  47. return File(stream.ToArray(), "image/jpg");
  48. }
  49. #endregion
  50. #region 编辑器上传图片
  51. public object EditorUpload([FromForm] IFormCollection rf)
  52. {
  53. //文件保存目录路径
  54. String savePath = "/up/";
  55. //文件保存目录URL
  56. String saveUrl = "/up/";
  57. //定义允许上传的文件扩展名
  58. Hashtable extTable = new Hashtable();
  59. extTable.Add("image", "gif,jpg,jpeg,png,bmp");
  60. extTable.Add("flash", "swf,flv");
  61. extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4");
  62. extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
  63. //最大文件大小
  64. long maxSize = 1073741824000;
  65. IFormFile imgFile = rf.Files["imgFile"];
  66. if (imgFile == null)
  67. {
  68. showError("请选择文件。");
  69. }
  70. String dirPath = function.getPath(MySystem.OssHelper.Instance.PathName + savePath);
  71. if (!Directory.Exists(dirPath))
  72. {
  73. //showError("上传目录不存在。");
  74. Directory.CreateDirectory(dirPath);
  75. }
  76. String fileName = imgFile.FileName;
  77. String dirName = "";
  78. if (String.IsNullOrEmpty(dirName))
  79. {
  80. if (fileName.ToLower().EndsWith(".mp4") || fileName.ToLower().EndsWith(".mp3"))
  81. {
  82. dirName = "media";
  83. }
  84. else
  85. {
  86. dirName = "image";
  87. }
  88. }
  89. if (!extTable.ContainsKey(dirName))
  90. {
  91. showError("目录名不正确。");
  92. }
  93. String fileExt = Path.GetExtension(fileName).ToLower();
  94. if (imgFile.Length > maxSize)
  95. {
  96. showError("上传文件大小超过限制。");
  97. }
  98. if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
  99. {
  100. showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
  101. }
  102. //创建文件夹
  103. dirPath += dirName + "/";
  104. saveUrl += dirName + "/";
  105. if (!Directory.Exists(dirPath))
  106. {
  107. Directory.CreateDirectory(dirPath);
  108. }
  109. String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
  110. dirPath += ymd + "/";
  111. saveUrl += ymd + "/";
  112. if (!Directory.Exists(dirPath))
  113. {
  114. Directory.CreateDirectory(dirPath);
  115. }
  116. String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
  117. String filePath = dirPath + newFileName;
  118. using var fs = new FileStream(filePath, FileMode.Create);
  119. imgFile.CopyTo(fs);
  120. String fileUrl = saveUrl + newFileName;
  121. if (dirName != "media")
  122. {
  123. if (!function.check_pic(filePath))
  124. {
  125. System.IO.File.Delete(filePath);
  126. return null;
  127. }
  128. if (function.getImgRule(filePath)["width"] > 2000)
  129. {
  130. String newFileName_sl = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "_sl" + fileExt;
  131. function.imgcut(2000, filePath, dirPath + newFileName_sl);
  132. System.IO.File.Delete(filePath);
  133. fileUrl = saveUrl + newFileName_sl;
  134. }
  135. }
  136. Hashtable hash = new Hashtable();
  137. hash["error"] = 0;
  138. hash["url"] = MySystem.OssHelper.Instance.SourceHost + "/" + MySystem.OssHelper.Instance.PathName + fileUrl;
  139. OssHelper.Instance.ScanQueue(fileUrl, "");
  140. return JsonMapper.ToJson(hash);
  141. }
  142. private object showError(string message)
  143. {
  144. Hashtable hash = new Hashtable();
  145. hash["error"] = 1;
  146. hash["message"] = message;
  147. return JsonMapper.ToJson(hash);
  148. }
  149. public bool IsReusable
  150. {
  151. get
  152. {
  153. return false;
  154. }
  155. }
  156. #endregion
  157. #region 系统-上传文件
  158. public JsonResult UploadFile([FromForm] IFormCollection rf)
  159. {
  160. IFormFile imgFile = rf.Files["file"];
  161. string path = MySystemLib.SystemPublicFuction.GetFilePath(imgFile, OssHelper.Instance.PathName + "/uploadfile/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
  162. OssHelper.Instance.ScanQueue(path, "");
  163. Dictionary<string, string> obj = new Dictionary<string, string>();
  164. obj.Add("FileName", path);
  165. return Json(obj);
  166. }
  167. #endregion
  168. #region 系统-上传图片
  169. public JsonResult UploadPhoto([FromForm] IFormCollection rf)
  170. {
  171. IFormFile imgFile = rf.Files["Icon"];
  172. string Icon = MySystemLib.SystemPublicFuction.GetPicPath(imgFile, OssHelper.Instance.PathName + "/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
  173. OssHelper.Instance.ScanQueue(Icon, "");
  174. return Json(new AppResultJson() { Status = "1", Info = "", Data = Icon });
  175. }
  176. #endregion
  177. #region 系统-上传图片
  178. public JsonResult UploadPhotoByBase64(string value)
  179. {
  180. value = value.Replace("data:image/png;base64,", "");
  181. string base64str = HttpUtility.UrlDecode(value).Replace(" ", "+");
  182. string dummyData = base64str.Replace("%", "").Replace(",", "").Replace(" ", "+");
  183. if (dummyData.Length % 4 > 0)
  184. {
  185. dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
  186. }
  187. string Icon = function.base64StringToImage(dummyData, "/" + OssHelper.Instance.PathName + "/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/", "MT" + function.MD5_16(Guid.NewGuid().ToString()) + ".png");
  188. // OssHelper.Instance.ScanQueue(Icon, "");
  189. return Json(new AppResultJson() { Status = "1", Info = "", Data = Icon });
  190. }
  191. #endregion
  192. #region 系统-layui上传文件
  193. public JsonResult LayUIUpload([FromForm] IFormCollection rf, string Path = "", string Resize = "")
  194. {
  195. IFormFile imgFile = rf.Files[0];
  196. string Icon = MySystemLib.SystemPublicFuction.GetFilePath(imgFile, Path + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
  197. string piccut = "";
  198. if (!string.IsNullOrEmpty(Resize) && (imgFile.FileName.ToLower().EndsWith(".png") || imgFile.FileName.EndsWith(".jpg")))
  199. {
  200. JsonData data = JsonMapper.ToObject(Resize);
  201. int width = int.Parse(data["width"].ToString());
  202. int height = int.Parse(data["height"].ToString());
  203. int quality = int.Parse(data["quality"].ToString());
  204. piccut = Icon.Replace(".", "s.");
  205. function.imgcut2(width, height, function.getPath(Icon), function.getPath(piccut));
  206. System.IO.File.Delete(function.getPath(Icon));
  207. }
  208. else
  209. {
  210. piccut = Icon;
  211. }
  212. return Json(new AppResultJson() { Status = "1", Info = "", Data = piccut });
  213. }
  214. #endregion
  215. #region 图片转base64
  216. public string ImgToBase64(string path)
  217. {
  218. path = HttpUtility.UrlDecode(path);
  219. return function.imageToBase64String(function.getPath(path));
  220. }
  221. #endregion
  222. }
  223. }