Parcourir la source

增加PublicMethodController文件
修改端口5144

lichunlei il y a 4 ans
Parent
commit
2751b83505
3 fichiers modifiés avec 259 ajouts et 2 suppressions
  1. 257 0
      Areas/Api/Controllers/PublicMethodController.cs
  2. 1 1
      Program.cs
  3. 1 1
      Properties/launchSettings.json

+ 257 - 0
Areas/Api/Controllers/PublicMethodController.cs

@@ -0,0 +1,257 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+using System.DrawingCore.Imaging;
+using System.IO;
+using Library;
+using MySystem.Areas.Admin.Controllers;
+using System.Collections;
+using LitJson;
+using System.Globalization;
+using System.Web;
+
+// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
+
+namespace MySystem.Areas.Api.Controllers
+{
+    [Area("Api")]
+    [Route("Api/[controller]/[action]")]
+    public class PublicMethodController : Admin.Controllers.BaseController
+    {
+        public PublicMethodController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
+        {
+        }
+        #region 图片验证码
+        public FileContentResult CheckCode()
+        {
+            string code = function.get_Random(4);
+            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
+            MemoryStream stream = new MemoryStream();
+            bitmap.Save(stream, ImageFormat.Gif);
+            function.WriteCookie(_accessor.HttpContext, "checkcode", code);
+            return File(stream.ToArray(), "image/gif");
+        }
+        #endregion
+        
+        #region 图片验证码
+        public FileContentResult PictureCode(string Tag = "")
+        {
+            string code = function.get_Random(4);
+            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
+            MemoryStream stream = new MemoryStream();
+            bitmap.Save(stream, ImageFormat.Gif);
+            RedisDbconn.Instance.Set(Tag, code);
+            RedisDbconn.Instance.SetExpire(Tag, 600);
+            return File(stream.ToArray(), "image/jpg");
+        }
+        #endregion
+
+
+
+        #region 编辑器上传图片
+
+        public object EditorUpload([FromForm] IFormCollection rf)
+        {
+            //文件保存目录路径
+            String savePath = "/up/";
+
+            //文件保存目录URL
+            String saveUrl = "/up/";
+
+            //定义允许上传的文件扩展名
+            Hashtable extTable = new Hashtable();
+            extTable.Add("image", "gif,jpg,jpeg,png,bmp");
+            extTable.Add("flash", "swf,flv");
+            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4");
+            extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
+
+            //最大文件大小
+            long maxSize = 1073741824000;
+
+            IFormFile imgFile = rf.Files["imgFile"];
+            if (imgFile == null)
+            {
+                showError("请选择文件。");
+            }
+
+            String dirPath = function.getPath(MySystem.OssHelper.Instance.PathName + savePath);
+            if (!Directory.Exists(dirPath))
+            {
+                //showError("上传目录不存在。");
+                Directory.CreateDirectory(dirPath);
+            }
+
+            String fileName = imgFile.FileName;
+            String dirName = "";
+            if (String.IsNullOrEmpty(dirName))
+            {
+                if (fileName.ToLower().EndsWith(".mp4") || fileName.ToLower().EndsWith(".mp3"))
+                {
+                    dirName = "media";
+                }
+                else
+                {
+                    dirName = "image";
+                }
+            }
+            if (!extTable.ContainsKey(dirName))
+            {
+                showError("目录名不正确。");
+            }
+
+            String fileExt = Path.GetExtension(fileName).ToLower();
+
+            if (imgFile.Length > maxSize)
+            {
+                showError("上传文件大小超过限制。");
+            }
+
+            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
+            {
+                showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
+            }
+
+            //创建文件夹
+            dirPath += dirName + "/";
+            saveUrl += dirName + "/";
+            if (!Directory.Exists(dirPath))
+            {
+                Directory.CreateDirectory(dirPath);
+            }
+            String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
+            dirPath += ymd + "/";
+            saveUrl += ymd + "/";
+            if (!Directory.Exists(dirPath))
+            {
+                Directory.CreateDirectory(dirPath);
+            }
+            String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
+            String filePath = dirPath + newFileName;
+            using var fs = new FileStream(filePath, FileMode.Create);
+            imgFile.CopyTo(fs);
+            String fileUrl = saveUrl + newFileName;
+            if (dirName != "media")
+            {
+                if (!function.check_pic(filePath))
+                {
+                    System.IO.File.Delete(filePath);
+                    return null;
+                }
+                if (function.getImgRule(filePath)["width"] > 2000)
+                {
+                    String newFileName_sl = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "_sl" + fileExt;
+                    function.imgcut(2000, filePath, dirPath + newFileName_sl);
+                    System.IO.File.Delete(filePath);
+                    fileUrl = saveUrl + newFileName_sl;
+                }
+            }
+
+            Hashtable hash = new Hashtable();
+            hash["error"] = 0;
+            hash["url"] = MySystem.OssHelper.Instance.SourceHost + "/" + MySystem.OssHelper.Instance.PathName + fileUrl;
+            OssHelper.Instance.ScanQueue(fileUrl, "");
+            return JsonMapper.ToJson(hash);
+        }
+
+        private object showError(string message)
+        {
+            Hashtable hash = new Hashtable();
+            hash["error"] = 1;
+            hash["message"] = message;
+            return JsonMapper.ToJson(hash);
+        }
+
+        public bool IsReusable
+        {
+            get
+            {
+                return false;
+            }
+        }
+
+
+
+        #endregion
+
+        #region 系统-上传文件
+
+        public JsonResult UploadFile([FromForm] IFormCollection rf)
+        {
+            IFormFile imgFile = rf.Files["file"];
+            string path = MySystemLib.SystemPublicFuction.GetFilePath(imgFile, OssHelper.Instance.PathName + "/uploadfile/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
+            OssHelper.Instance.ScanQueue(path, "");
+            Dictionary<string, string> obj = new Dictionary<string, string>();
+            obj.Add("FileName", path);
+            return Json(obj);
+        }
+
+        #endregion
+
+        #region 系统-上传图片
+        public JsonResult UploadPhoto([FromForm] IFormCollection rf)
+        {
+            IFormFile imgFile = rf.Files["Icon"];
+            string Icon = MySystemLib.SystemPublicFuction.GetPicPath(imgFile, OssHelper.Instance.PathName + "/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
+            OssHelper.Instance.ScanQueue(Icon, "");
+            return Json(new AppResultJson() { Status = "1", Info = "", Data = Icon });
+        }
+
+        #endregion
+
+        #region 系统-上传图片
+        public JsonResult UploadPhotoByBase64(string value)
+        {
+            value = value.Replace("data:image/png;base64,", "");
+            string base64str = HttpUtility.UrlDecode(value).Replace(" ", "+");
+            string dummyData = base64str.Replace("%", "").Replace(",", "").Replace(" ", "+");
+            if (dummyData.Length % 4 > 0)
+            {
+                dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
+            }
+            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");
+            // OssHelper.Instance.ScanQueue(Icon, "");
+            return Json(new AppResultJson() { Status = "1", Info = "", Data = Icon });
+        }
+        #endregion
+
+        #region 系统-layui上传文件
+        public JsonResult LayUIUpload([FromForm] IFormCollection rf, string Path = "", string Resize = "")
+        {
+            IFormFile imgFile = rf.Files[0];
+            string Icon = MySystemLib.SystemPublicFuction.GetFilePath(imgFile, Path + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
+            string piccut = "";
+            if (!string.IsNullOrEmpty(Resize) && (imgFile.FileName.ToLower().EndsWith(".png") || imgFile.FileName.EndsWith(".jpg")))
+            {
+                JsonData data = JsonMapper.ToObject(Resize);
+                int width = int.Parse(data["width"].ToString());
+                int height = int.Parse(data["height"].ToString());
+                int quality = int.Parse(data["quality"].ToString());
+                piccut = Icon.Replace(".", "s.");
+                function.imgcut2(width, height, function.getPath(Icon), function.getPath(piccut));
+                System.IO.File.Delete(function.getPath(Icon));
+            }
+            else
+            { 
+                piccut = Icon;
+            }
+            return Json(new AppResultJson() { Status = "1", Info = "", Data = piccut });
+        }
+
+        #endregion
+
+        #region 图片转base64
+
+        public string ImgToBase64(string path)
+        {
+            path = HttpUtility.UrlDecode(path);
+            return function.imageToBase64String(function.getPath(path));
+        }
+
+        #endregion
+    }
+}

+ 1 - 1
Program.cs

@@ -22,7 +22,7 @@ namespace MySystem
                 .ConfigureWebHostDefaults(webBuilder =>
                 {
                     webBuilder
-                    .UseUrls("http://*:5044")
+                    .UseUrls("http://*:5144")
                     .UseKestrel()
                     .UseContentRoot(Directory.GetCurrentDirectory())
                     .UseIISIntegration()

+ 1 - 1
Properties/launchSettings.json

@@ -21,7 +21,7 @@
       "environmentVariables": {
         "ASPNETCORE_ENVIRONMENT": "Development"
       },
-      "applicationUrl": "http://127.0.0.1:5044"
+      "applicationUrl": "http://127.0.0.1:5144"
     }
   }
 }