PublicMethodController.cs 9.6 KB

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