增加后台功能
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Core;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
{
|
||||
[Area("AdminCP")]
|
||||
public class AdminBaseController : Controller
|
||||
{
|
||||
public JsonTip tip = new JsonTip();
|
||||
|
||||
/// <summary>
|
||||
/// 如果没登录
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
bool islogin = Admin.IsAdminLogin();
|
||||
if (!islogin)
|
||||
{
|
||||
context.Result = new RedirectResult("/AdminCP/Login");
|
||||
return;
|
||||
}
|
||||
base.OnActionExecuting(context);
|
||||
}
|
||||
|
||||
#region 获取验证第一条错误
|
||||
/// <summary>
|
||||
/// 获取服务端验证的第一条错误信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetModelStateError()
|
||||
{
|
||||
foreach (var item in ModelState.Values)
|
||||
{
|
||||
if (item.Errors.Count > 0)
|
||||
{
|
||||
return item.Errors[0].ErrorMessage;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 信息提示页面
|
||||
/// <summary>
|
||||
/// 显示提示页面
|
||||
/// </summary>
|
||||
/// <param name="message">提示信息</param>
|
||||
/// <param name="type">类型,0失败,1成功</param>
|
||||
/// <param name="isCloseDialog">是否关闭掉dialog</param>
|
||||
/// <param name="backURL">后退地址,为空则默认上一页</param>
|
||||
/// <returns></returns>
|
||||
public IActionResult EchoTipPage(string message, int type = 0, bool isCloseDialog = false, string backURL = "")
|
||||
{
|
||||
ViewBag.Message = message;
|
||||
ViewBag.Type = type;
|
||||
ViewBag.IsCloseDialog = isCloseDialog;
|
||||
ViewBag.BackURL = backURL;
|
||||
return View("TipPage");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Core;
|
||||
using XCode;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Newtonsoft.Json;
|
||||
using COMCMS.Web.Common;
|
||||
|
||||
namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
{
|
||||
[Area("AdminCP")]
|
||||
public class IndexController : AdminBaseController
|
||||
{
|
||||
private readonly IHostingEnvironment _env;
|
||||
public IndexController(IHostingEnvironment env)
|
||||
{
|
||||
_env = env;
|
||||
}
|
||||
#region 后台首页
|
||||
[AllowAnonymous]
|
||||
public IActionResult Index()
|
||||
{
|
||||
if (!Core.Admin.IsAdminLogin())
|
||||
{
|
||||
return RedirectToAction("Index", "Login");
|
||||
}
|
||||
|
||||
Admin admin = Admin.GetMyInfo();
|
||||
ViewBag.admin = admin;
|
||||
|
||||
//获取菜单
|
||||
List<AdminMenu> list = new List<AdminMenu>();
|
||||
//这里需要获取权限,暂时先所有
|
||||
if (admin.Roles.IsSuperAdmin == 1)
|
||||
{
|
||||
list = AdminMenu.GetListTree(0, -1, false, true).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
list = JsonConvert.DeserializeObject<List<AdminMenu>>(admin.Roles.Menus);
|
||||
}
|
||||
|
||||
return View(list);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 后台主界面
|
||||
public IActionResult Main()
|
||||
{
|
||||
string remoteip = Request.HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
string host = Request.HttpContext.Request.Host.Host;
|
||||
string port = Request.HttpContext.Connection.RemotePort.ToString();
|
||||
|
||||
ViewBag.remoteip = remoteip;
|
||||
ViewBag.port = port;
|
||||
ViewBag.host = host;
|
||||
ViewBag.contentPath = _env.ContentRootPath;
|
||||
ViewBag.rootPath = _env.WebRootPath;
|
||||
|
||||
return View();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Core;
|
||||
using XCode;
|
||||
|
||||
namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
{
|
||||
[Area("AdminCP")]
|
||||
public class LoginController : Controller
|
||||
{
|
||||
public JsonTip tip = new JsonTip();
|
||||
|
||||
#region 登录页面
|
||||
public IActionResult Index()
|
||||
{
|
||||
if (Admin.IsAdminLogin())
|
||||
{
|
||||
return Redirect("/AdminCP");
|
||||
}
|
||||
string key = Utils.GetRandomChar(12);
|
||||
SessionHelper.WriteSession("des_key", key);
|
||||
ViewBag.key = key;
|
||||
return View();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 低版本IE界面
|
||||
public IActionResult Ie()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 执行登录
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public IActionResult Login()
|
||||
{
|
||||
string strUserName = Request.Form["username"];
|
||||
string strPassWord = Request.Form["password"];
|
||||
|
||||
if (strUserName.Length % 8 != 0)
|
||||
{
|
||||
tip.Message = "请输入用户名不合法!";
|
||||
return Json(tip);
|
||||
}
|
||||
if (strPassWord.Length % 8 != 0)
|
||||
{
|
||||
tip.Message = "请输入密码不合法!";
|
||||
return Json(tip);
|
||||
}
|
||||
//判断并解密
|
||||
string key = SessionHelper.GetSession("des_key").ToString();
|
||||
//解密
|
||||
string username = "";
|
||||
string password = "";
|
||||
try
|
||||
{
|
||||
username = MyDES.uncMe(strUserName, key);
|
||||
password = MyDES.uncMe(strPassWord, key);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
NewLife.Log.XTrace.WriteException(exp);
|
||||
tip.Message = "用户名或者密码错误!";
|
||||
return Json(tip);
|
||||
}
|
||||
|
||||
//验证用户
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
tip.Message = "请输入用户名!";
|
||||
return Json(tip);
|
||||
}
|
||||
if (string.IsNullOrEmpty(password) || Utils.GetStringLength(password) < 5)
|
||||
{
|
||||
tip.Message = "登录密码不能为空或者长度小于5!";
|
||||
return Json(tip);
|
||||
}
|
||||
//如果15分钟内有10次失败登录,则提示错误
|
||||
string ip = Utils.GetIP();
|
||||
Expression ex = AdminLog._.IsLoginOK == 0 & AdminLog._.LoginIP == ip & AdminLog._.LoginTime >= DateTime.Now.AddMinutes(-15);
|
||||
|
||||
if (AdminLog.FindCount(ex, null, null, 0, 0) >= 10)
|
||||
{
|
||||
tip.Message = "错误登录次数限制!";
|
||||
return Json(tip);
|
||||
}
|
||||
//执行登录操作
|
||||
if (Admin.AdminLogin(username, password))
|
||||
{
|
||||
tip.Status = JsonTip.SUCCESS;
|
||||
tip.Message = "登录成功";
|
||||
tip.ReturnUrl = "/AdminCP";
|
||||
return Json(tip);
|
||||
}
|
||||
else
|
||||
{
|
||||
tip.Message = "用户名或者密码错误!请重新登录!";
|
||||
return Json(tip);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region 退出登录
|
||||
/// <summary>
|
||||
/// 退出登录
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IActionResult Logout()
|
||||
{
|
||||
Admin.ClearInfo();
|
||||
return Redirect("~/AdminCP/Login");
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Core;
|
||||
using XCode;
|
||||
using Newtonsoft.Json;
|
||||
using COMCMS.Web.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
{
|
||||
[Area("AdminCP")]
|
||||
public class PermissionController : AdminBaseController
|
||||
{
|
||||
#region 事件管理
|
||||
/// <summary>
|
||||
/// 目标事件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[MyAuthorize( "view", "eventkey")]
|
||||
public IActionResult EventKey()
|
||||
{
|
||||
IList<TargetEvent> list = TargetEvent.FindAll(TargetEvent._.Id > 0, TargetEvent._.Id.Asc(), null, 0, 0);
|
||||
return View(list);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <returns></returns>
|
||||
[MyAuthorize( "viewlist", "eventkey", "JSON")]
|
||||
public IActionResult GetEventKey(string keyword, int page = 1, int limit = 20)
|
||||
{
|
||||
int numPerPage, currentPage, startRowIndex;
|
||||
long totalCount = 0;
|
||||
|
||||
numPerPage = limit;
|
||||
currentPage = page;
|
||||
startRowIndex = (currentPage - 1) * numPerPage;
|
||||
Expression ex = TargetEvent._.Id > 0;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(keyword))
|
||||
ex &= TargetEvent._.EventName.Contains(keyword);
|
||||
|
||||
|
||||
IList<TargetEvent> list = TargetEvent.FindAll(ex, TargetEvent._.Id.Asc(), null, startRowIndex, numPerPage);
|
||||
totalCount = TargetEvent.FindCount(ex, TargetEvent._.Id.Asc(), null, startRowIndex, numPerPage);
|
||||
Admin.WriteLogActions($"查看后台事件列表(keyword:{keyword};page:{page};limit:{limit});");
|
||||
return Content(JsonConvert.SerializeObject(new { total = totalCount, rows = list.ToList() }), "text/json");
|
||||
}
|
||||
|
||||
//添加eventkey
|
||||
[MyAuthorize( "add", "eventkey")]
|
||||
public IActionResult AddEventKey()
|
||||
{
|
||||
Admin.WriteLogActions($"查看添加后台事件页面;");
|
||||
return View();
|
||||
}
|
||||
|
||||
[MyAuthorize( "add", "eventkey", "JSON")]
|
||||
[HttpPost]
|
||||
public IActionResult AddEventKey(FormCollection fc)
|
||||
{
|
||||
TargetEvent entity = new TargetEvent();
|
||||
entity.EventKey = fc["EventKey"];
|
||||
entity.EventName = fc["EventName"];
|
||||
string rank = fc["Rank"];
|
||||
if (string.IsNullOrWhiteSpace(rank) || !Utils.IsInt(rank)) rank = "0";
|
||||
entity.Rank = int.Parse(rank);
|
||||
if (string.IsNullOrWhiteSpace(fc["IsDisable"]) || fc["IsDisable"] != "1")
|
||||
entity.IsDisable = 0;
|
||||
else
|
||||
entity.IsDisable = 1;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(entity.EventKey))
|
||||
{
|
||||
tip.Message = "请填写事件KEY";
|
||||
return Json(tip);
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(entity.EventName))
|
||||
{
|
||||
tip.Message = "请填写事件名称";
|
||||
return Json(tip);
|
||||
}
|
||||
entity.EventKey = entity.EventKey.ToLower();
|
||||
|
||||
if (TargetEvent.FindCount(TargetEvent._.EventKey == entity.EventKey.ToLower(), null, null, 0, 0) > 0)
|
||||
{
|
||||
tip.Message = "事件key已经存在,请重新填写一个";
|
||||
return Json(tip);
|
||||
}
|
||||
entity.Insert();
|
||||
tip.Status = JsonTip.SUCCESS;
|
||||
tip.Message = "添加事件成功";
|
||||
tip.ReturnUrl = "close";
|
||||
|
||||
Core.Admin.WriteLogActions("添加事件详情(id:" + entity.Id + ");");
|
||||
|
||||
return Json(tip);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 后台栏目管理
|
||||
//后台菜单列表
|
||||
[MyAuthorize( "viewlist", "adminmenu")]
|
||||
public IActionResult AdminMenuList()
|
||||
{
|
||||
IList<AdminMenu> list = AdminMenu.GetListTree(0, -1, false, true);
|
||||
return View(list);
|
||||
}
|
||||
//添加后台菜单
|
||||
[MyAuthorize( "add", "adminmenu")]
|
||||
public IActionResult AddAdminMenu()
|
||||
{
|
||||
//获取上级栏目
|
||||
IList<AdminMenu> list = AdminMenu.GetListTree(0, -1, true, false);
|
||||
ViewBag.ListTree = list;
|
||||
IList<TargetEvent> listevent = TargetEvent.FindAll(TargetEvent._.IsDisable != 1, TargetEvent._.Rank.Asc(), null, 0, 0);
|
||||
ViewBag.ListEvent = listevent;
|
||||
return View();
|
||||
}
|
||||
//执行添加菜单
|
||||
[HttpPost]
|
||||
[MyAuthorize( "add", "adminmenu", "JSON")]
|
||||
public IActionResult AddAdminMenu(AdminMenu model)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
string messages = string.Join("; ", ModelState.Values
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage));
|
||||
|
||||
tip.Message = messages;
|
||||
return Json(tip);
|
||||
}
|
||||
//判断
|
||||
if (AdminMenu.FindCount(AdminMenu._.MenuKey == model.MenuKey, null, null, 0, 0) > 0)
|
||||
{
|
||||
tip.Message = "菜单KEY已经存在,请填写其他的!";
|
||||
}
|
||||
IList<TargetEvent> listevent = TargetEvent.FindAll(null, TargetEvent._.Rank.Asc(), null, 0, 0);
|
||||
|
||||
string[] eventkeys = Request.Form["eventkey"];
|
||||
List<AdminMenuEvent> listme = new List<AdminMenuEvent>();
|
||||
if (eventkeys != null && eventkeys.Length > 0)
|
||||
{
|
||||
foreach (string s in eventkeys)
|
||||
{
|
||||
if (Utils.IsInt(s))
|
||||
{
|
||||
TargetEvent te = listevent.FirstOrDefault(t => t.Id == int.Parse(s));
|
||||
if (te != null)
|
||||
{
|
||||
AdminMenuEvent tmp = new AdminMenuEvent();
|
||||
tmp.EventId = te.Id;
|
||||
tmp.EventKey = te.EventKey;
|
||||
tmp.EventName = te.EventName;
|
||||
tmp.MenuKey = model.MenuKey;
|
||||
listme.Add(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
model.Insert();
|
||||
if (listme != null && listme.Count > 0)
|
||||
{
|
||||
listme.ForEach(s =>
|
||||
{
|
||||
s.MenuId = model.Id;
|
||||
});
|
||||
listme.Insert();
|
||||
}
|
||||
tip.Status = JsonTip.SUCCESS;
|
||||
tip.Message = "添加后台菜单成功";
|
||||
tip.ReturnUrl = "close";
|
||||
return Json(tip);
|
||||
}
|
||||
|
||||
//修改菜单
|
||||
public IActionResult EditAdminMenu(int id)
|
||||
{
|
||||
AdminMenu entity = AdminMenu.Find(AdminMenu._.Id == id);
|
||||
if (entity == null)
|
||||
{
|
||||
return EchoTipPage("系统找不到本记录!");
|
||||
}
|
||||
//获取上级栏目
|
||||
IList<AdminMenu> list = AdminMenu.GetListTree(0, -1, true, false);
|
||||
ViewBag.ListTree = list;
|
||||
IList<TargetEvent> listevent = TargetEvent.FindAll(TargetEvent._.IsDisable != 1, TargetEvent._.Rank.Asc(), null, 0, 0);
|
||||
ViewBag.ListEvent = listevent;
|
||||
|
||||
return View(entity);
|
||||
}
|
||||
|
||||
//执行添加菜单
|
||||
[HttpPost]
|
||||
[MyAuthorize( "edit", "adminmenu", "JSON")]
|
||||
public IActionResult EditAdminMenu(AdminMenu model)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
string messages = string.Join("; ", ModelState.Values
|
||||
.SelectMany(x => x.Errors)
|
||||
.Select(x => x.ErrorMessage));
|
||||
tip.Message = messages;
|
||||
return Json(tip);
|
||||
}
|
||||
if (model.Id <= 0)
|
||||
{
|
||||
tip.Message = "错误参数传递!";
|
||||
return Json(tip);
|
||||
}
|
||||
AdminMenu entity = AdminMenu.Find(AdminMenu._.Id == model.Id);
|
||||
if (entity == null)
|
||||
{
|
||||
tip.Message = "系统找不到本记录!";
|
||||
return Json(tip);
|
||||
}
|
||||
//赋值
|
||||
entity.MenuName = model.MenuName;
|
||||
//如果key修改了。判断是否有存在
|
||||
if (entity.MenuKey != model.MenuKey)
|
||||
{
|
||||
if (AdminMenu.FindCount(AdminMenu._.MenuKey == model.MenuKey & AdminMenu._.Id != entity.Id, null, null, 0, 0) > 0)
|
||||
{
|
||||
tip.Message = "您修改的菜单KEY已经存在,请填写其他的!";
|
||||
return Json(tip);
|
||||
}
|
||||
entity.MenuKey = model.MenuKey;
|
||||
}
|
||||
entity.Link = model.Link;
|
||||
entity.IsHide = model.IsHide;
|
||||
entity.ClassName = model.ClassName;
|
||||
entity.Rank = model.Rank;
|
||||
if (entity.PId != model.PId)
|
||||
{
|
||||
//重新获取level和location
|
||||
entity.Level = AdminMenu.GetNewLevel(model.PId);
|
||||
entity.Location = AdminMenu.GetNewLocation(model.PId);
|
||||
}
|
||||
entity.PId = model.PId;
|
||||
entity.Update();
|
||||
|
||||
//处理权限
|
||||
IList<AdminMenuEvent> oldevent = AdminMenuEvent.FindAll(AdminMenuEvent._.MenuId == entity.Id, null, null, 0, 0);
|
||||
|
||||
//所有的权限
|
||||
IList<TargetEvent> listevent = TargetEvent.FindAll(TargetEvent._.Id > 0, TargetEvent._.Rank.Asc(), null, 0, 0);
|
||||
//新的权限
|
||||
string[] eventkeys = Request.Form["eventkey"];
|
||||
List<AdminMenuEvent> listme = new List<AdminMenuEvent>();
|
||||
if (eventkeys != null && eventkeys.Length > 0)
|
||||
{
|
||||
foreach (string s in eventkeys)
|
||||
{
|
||||
if (Utils.IsInt(s))
|
||||
{
|
||||
TargetEvent te = listevent.FirstOrDefault(t => t.Id == int.Parse(s));
|
||||
if (te != null)
|
||||
{
|
||||
AdminMenuEvent tmp = new AdminMenuEvent();
|
||||
tmp.EventId = te.Id;
|
||||
tmp.EventKey = te.EventKey;
|
||||
tmp.EventName = te.EventName;
|
||||
tmp.MenuKey = model.MenuKey;
|
||||
tmp.MenuId = entity.Id;
|
||||
listme.Add(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//判断是否在旧的里面
|
||||
listme.ForEach(s =>
|
||||
{
|
||||
var oe = oldevent.FirstOrDefault(o => o.EventId == s.EventId);
|
||||
//如果是旧列表存在,则删掉旧列表里面的,然后更新自己
|
||||
if (oe != null)
|
||||
{
|
||||
s.Id = oe.Id;
|
||||
s.Update();
|
||||
oldevent.Remove(oe);
|
||||
}
|
||||
else
|
||||
{
|
||||
//如果不存在,则是新增的。直接新增处理
|
||||
s.Insert();
|
||||
}
|
||||
});
|
||||
//如果还有,则删除旧列表里面的
|
||||
if (oldevent != null && oldevent.Count > 0)
|
||||
{
|
||||
oldevent.Delete();
|
||||
}
|
||||
|
||||
tip.Status = JsonTip.SUCCESS;
|
||||
tip.Message = "编辑后台菜单成功";
|
||||
tip.ReturnUrl = "close";
|
||||
return Json(tip);
|
||||
}
|
||||
|
||||
//删除菜单
|
||||
[HttpPost]
|
||||
[MyAuthorize( "del", "adminmenu", "JSON")]
|
||||
public IActionResult DelAdminMenu(int id)
|
||||
{
|
||||
AdminMenu entity = AdminMenu.Find(AdminMenu._.Id == id);
|
||||
if (entity == null)
|
||||
{
|
||||
tip.Message = "系统找不到本记录!";
|
||||
return Json(tip);
|
||||
}
|
||||
if (AdminMenu.FindCount(AdminMenu._.PId == entity.Id, null, null, 0, 0) > 0)
|
||||
{
|
||||
tip.Message = "本菜单有下级菜单,不允许删除!";
|
||||
return Json(tip);
|
||||
}
|
||||
entity.Delete();
|
||||
tip.Status = JsonTip.SUCCESS;
|
||||
tip.Message = "删除后台菜单成功";
|
||||
return Json(tip);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Core;
|
||||
using XCode;
|
||||
using Newtonsoft.Json;
|
||||
using COMCMS.Web.Common;
|
||||
|
||||
namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
{
|
||||
[Area("AdminCP")]
|
||||
public class SystemController : AdminBaseController
|
||||
{
|
||||
#region 系统基本设置
|
||||
[MyAuthorize("view", "baseconfig")]
|
||||
public IActionResult BaseConfig()
|
||||
{
|
||||
Config cfg = Config.GetSystemConfig();
|
||||
Core.Admin.WriteLogActions("查看基本配置;");
|
||||
return View(cfg);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[MyAuthorize("view", "baseconfig", "JSON")]
|
||||
public IActionResult BaseConfig(Config model)
|
||||
{
|
||||
Config cfg = Config.GetSystemConfig();
|
||||
if (string.IsNullOrEmpty(model.SiteName))
|
||||
{
|
||||
tip.Message = "请输入站点名称!";
|
||||
return Json(tip);
|
||||
}
|
||||
string IsResetData = Request.Form["IsResetData"];
|
||||
if (IsResetData != "1") IsResetData = "0";
|
||||
|
||||
model.Id = cfg.Id;
|
||||
model.LastCacheTime = DateTime.Now;
|
||||
model.LastUpdateTime = DateTime.Now;
|
||||
model.IsResetData = int.Parse(IsResetData);
|
||||
model.Update();
|
||||
|
||||
tip.Status = JsonTip.SUCCESS;
|
||||
tip.Message = "修改站点基本配置成功";
|
||||
|
||||
Core.Admin.WriteLogActions("修改站点基本配置;");
|
||||
|
||||
return Json(tip);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,766 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Core;
|
||||
using XCode;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using COMCMS.Web.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using COMCMS.Core.Models;
|
||||
using System.DrawingCore;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
|
||||
namespace COMCMS.Web.Areas.AdminCP.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 后台上传文件帮助类
|
||||
/// </summary>
|
||||
[Area("AdminCP")]
|
||||
public class UploadController : AdminBaseController
|
||||
{
|
||||
private readonly SystemSetting _attachsetting;
|
||||
private IHostingEnvironment _env;
|
||||
private AttachConfigEntity attach;
|
||||
public UploadController( IHostingEnvironment env, IOptions<SystemSetting> attachsetting)
|
||||
{
|
||||
attach = Config.GetSystemConfig().AttachConfigEntity;
|
||||
_env = env;
|
||||
_attachsetting = attachsetting.Value;
|
||||
}
|
||||
|
||||
#region CKEditor 上传图片
|
||||
/// <summary>
|
||||
/// ckeditor 上传图片
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<IActionResult> CKUploadImage()
|
||||
{
|
||||
|
||||
string callback = Request.Query["CKEditorFuncNum"];//要求返回值
|
||||
var upload = Request.Form.Files[0];
|
||||
string tpl = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(\"{1}\", \"{0}\", \"{2}\");</script>";
|
||||
if (upload == null)
|
||||
return Content(string.Format(tpl, "", callback, "请选择一张图片!"), "text/html");
|
||||
//判断是否是图片类型
|
||||
List<string> imgtypelist = new List<string> { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp", "image/jpeg" };
|
||||
if (imgtypelist.FindIndex(x => x == upload.ContentType) == -1)
|
||||
{
|
||||
return Content(string.Format(tpl, "", callback, "请上传一张图片!"), "text/html");
|
||||
}
|
||||
var data = Request.Form.Files["upload"];
|
||||
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\images\\";
|
||||
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\images\\";
|
||||
|
||||
//根据附件配置,设置上传图片目录
|
||||
string imgPath = DateTime.Now.Year.ToString();//默认按年
|
||||
switch (attach.SaveType)
|
||||
{
|
||||
case 1://按月份
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
|
||||
break;
|
||||
case 2:
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
|
||||
break;
|
||||
}
|
||||
filepath += imgPath;//存放路径
|
||||
thumbsFilePath += imgPath;//缩略图路径
|
||||
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(upload.FileName);//文件名字,不带扩展名
|
||||
string sFullExtension = Utils.GetFileExtName(upload.FileName);//扩展名
|
||||
//图片名字
|
||||
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upload.FileName);
|
||||
|
||||
switch (attach.IsRandomFileName)
|
||||
{
|
||||
case 0://不随机
|
||||
imgname = upload.FileName;
|
||||
//判断是否存在
|
||||
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
|
||||
{
|
||||
imgname = sFileNameNoExt + "(1)" + sFullExtension;
|
||||
}
|
||||
break;
|
||||
case 1://随机字符串
|
||||
imgname = Utils.GetShortGUId() + sFullExtension;
|
||||
break;
|
||||
case 2://时间
|
||||
imgname = Utils.GetOrderNum() + sFullExtension;
|
||||
break;
|
||||
}
|
||||
string fullpath = Path.Combine(filepath, imgname);//图片
|
||||
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
|
||||
try
|
||||
{
|
||||
//判断路径
|
||||
if (!Directory.Exists(filepath))
|
||||
Directory.CreateDirectory(filepath);
|
||||
//缩略图路径
|
||||
if (!Directory.Exists(thumbsFilePath))
|
||||
Directory.CreateDirectory(thumbsFilePath);
|
||||
if (data != null)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
|
||||
{
|
||||
data.CopyTo(fs);
|
||||
}
|
||||
});
|
||||
//生成缩略图
|
||||
if (attach.IsCreateThum == 1)
|
||||
{
|
||||
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
|
||||
}
|
||||
//添加水印
|
||||
if (attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
|
||||
{
|
||||
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
|
||||
if (System.IO.File.Exists(watermarkimg))
|
||||
{
|
||||
//先复制一张图片出来
|
||||
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
|
||||
System.IO.File.Copy(fullpath, copyfullpath);
|
||||
|
||||
Image waterpic = new Bitmap(watermarkimg);
|
||||
Image srcPic = new Bitmap(copyfullpath);
|
||||
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
|
||||
{
|
||||
waterpic.Dispose();
|
||||
//srcPic.Dispose();
|
||||
try
|
||||
{
|
||||
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
|
||||
srcPic.Dispose();
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (System.IO.File.Exists(copyfullpath))
|
||||
{
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
waterpic.Dispose();
|
||||
srcPic.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Content(string.Format(tpl, "", callback, "图片上传失败:" + ex.Message), "text/html");
|
||||
}
|
||||
return Content(string.Format(tpl, $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname, callback, ""), "text/html");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region CKEditor 上传文件
|
||||
public async Task<IActionResult> CKUploadFile()
|
||||
{
|
||||
string callback = Request.Query["CKEditorFuncNum"];//要求返回值
|
||||
var upload = Request.Form.Files[0];
|
||||
string tpl = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(\"{1}\", \"{0}\", \"{2}\");</script>";
|
||||
if (upload == null)
|
||||
return Content(string.Format(tpl, "", callback, "请选择一个文件!"), "text/html");
|
||||
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(upload.FileName);//文件名字,不带扩展名
|
||||
string sFullExtension = Utils.GetFileExtName(upload.FileName);//扩展名
|
||||
if (string.IsNullOrEmpty(sFullExtension))
|
||||
{
|
||||
return Content(string.Format(tpl, "", callback, $"错误的文件类型!"), "text/html");
|
||||
}
|
||||
//判断是否是允许文件扩展名
|
||||
string sAllowedExtensions = _attachsetting.FileAllowedExtensions;
|
||||
List<string> listAllowedExtensions = new List<string>();
|
||||
string[] arrAllowedExtensions = sAllowedExtensions.Split(new string[] { "," }, StringSplitOptions.None);
|
||||
if (arrAllowedExtensions != null && arrAllowedExtensions.Length > 0)
|
||||
{
|
||||
foreach (var s in arrAllowedExtensions)
|
||||
{
|
||||
listAllowedExtensions.Add(s);
|
||||
}
|
||||
}
|
||||
if (listAllowedExtensions.Find(x => x == sFullExtension.ToLower().Replace(".", "")) == null)
|
||||
{
|
||||
return Content(string.Format(tpl, "", callback, $"{sFullExtension}的文件类型,不允许上传!"), "text/html");
|
||||
}
|
||||
//判断是否是图片,如果是图片,后面需要生成缩略图并可能的话,就加上水印
|
||||
bool isImage = false;
|
||||
//判断是否是图片类型
|
||||
List<string> imgtypelist = new List<string> { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp" };
|
||||
if (imgtypelist.FindIndex(x => x == upload.ContentType) >= 0)
|
||||
{
|
||||
isImage = true;
|
||||
}
|
||||
var data = Request.Form.Files["upload"];
|
||||
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\files\\";
|
||||
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\files\\";
|
||||
//根据附件配置,设置上传图片目录
|
||||
string imgPath = DateTime.Now.Year.ToString();//默认按年
|
||||
switch (attach.SaveType)
|
||||
{
|
||||
case 1://按月份
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
|
||||
break;
|
||||
case 2:
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
|
||||
break;
|
||||
}
|
||||
//文件名字
|
||||
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upload.FileName);
|
||||
switch (attach.IsRandomFileName)
|
||||
{
|
||||
case 0://不随机
|
||||
imgname = upload.FileName;
|
||||
//判断是否存在
|
||||
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
|
||||
{
|
||||
imgname = sFileNameNoExt + "(1)" + sFullExtension;
|
||||
}
|
||||
break;
|
||||
case 1://随机字符串
|
||||
imgname = Utils.GetShortGUId() + sFullExtension;
|
||||
break;
|
||||
case 2://时间
|
||||
imgname = Utils.GetOrderNum() + sFullExtension;
|
||||
break;
|
||||
}
|
||||
string fullpath = Path.Combine(filepath, imgname);//图片
|
||||
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图,可能的话
|
||||
try
|
||||
{
|
||||
//判断路径
|
||||
if (!Directory.Exists(filepath))
|
||||
Directory.CreateDirectory(filepath);
|
||||
//缩略图路径
|
||||
if (isImage && !Directory.Exists(thumbsFilePath))
|
||||
Directory.CreateDirectory(thumbsFilePath);
|
||||
if (data != null)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
|
||||
{
|
||||
data.CopyTo(fs);
|
||||
}
|
||||
});
|
||||
//生成缩略图
|
||||
if (isImage && attach.IsCreateThum == 1)
|
||||
{
|
||||
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
|
||||
}
|
||||
//添加水印
|
||||
if (isImage && attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
|
||||
{
|
||||
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
|
||||
if (System.IO.File.Exists(watermarkimg))
|
||||
{
|
||||
//先复制一张图片出来
|
||||
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
|
||||
System.IO.File.Copy(fullpath, copyfullpath);
|
||||
|
||||
Image waterpic = new Bitmap(watermarkimg);
|
||||
Image srcPic = new Bitmap(copyfullpath);
|
||||
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
|
||||
{
|
||||
waterpic.Dispose();
|
||||
//srcPic.Dispose();
|
||||
try
|
||||
{
|
||||
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
|
||||
srcPic.Dispose();
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (System.IO.File.Exists(copyfullpath))
|
||||
{
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
waterpic.Dispose();
|
||||
srcPic.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Content(string.Format(tpl, "", callback, "文件上传失败:" + ex.Message), "text/html");
|
||||
}
|
||||
return Content(string.Format(tpl, $"/{attach.AttachPatch}/fales/{imgPath.Replace("\\", "/")}/" + imgname, callback, ""), "text/html");
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region CKEditor 上传多媒体
|
||||
|
||||
#endregion
|
||||
|
||||
#region Webuploader 上传图片
|
||||
//public IActionResult WUUploadImage()
|
||||
//{
|
||||
// string isbinary = Request.Query["isbinary"];
|
||||
// switch (isbinary)
|
||||
// {
|
||||
// case "1"://二进制上传
|
||||
// return DoWebuploaderImageByBinary();
|
||||
// default://默认上传
|
||||
// return DoWebuploaderImage();
|
||||
// }
|
||||
//}
|
||||
|
||||
#region 二进制上传图片
|
||||
/// <summary>
|
||||
/// 二进制上传图片(未实现)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IActionResult DoWebuploaderImageByBinary()
|
||||
{
|
||||
var fileinfo = "";
|
||||
var msg = "上传失败!";
|
||||
var status = 0;
|
||||
var name = "";
|
||||
var path = "";
|
||||
var thumb = "";
|
||||
var size = "";
|
||||
var ext = "";
|
||||
try
|
||||
{
|
||||
string upname = Request.Query["name"];
|
||||
//判断是否是允许文件扩展名
|
||||
string sAllowedExtensions = _attachsetting.ImageAllowedExtensions;
|
||||
List<string> listAllowedExtensions = new List<string>();
|
||||
string[] arrAllowedExtensions = sAllowedExtensions.Split(new string[] { "," }, StringSplitOptions.None);
|
||||
|
||||
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(upname);//文件名字,不带扩展名
|
||||
string sFullExtension = Utils.GetFileExtName(upname);//扩展名
|
||||
|
||||
if (arrAllowedExtensions != null && arrAllowedExtensions.Length > 0)
|
||||
{
|
||||
foreach (var s in arrAllowedExtensions)
|
||||
{
|
||||
listAllowedExtensions.Add(s);
|
||||
}
|
||||
}
|
||||
if (listAllowedExtensions.Find(x => x == sFullExtension.ToLower().Replace(".", "")) == null)
|
||||
{
|
||||
return Json(new { status = status, msg = $"{sFullExtension}的文件类型,不允许上传,请上传一张图片!", name = name, path = path, thumb = thumb, size = size, ext = ext });
|
||||
}
|
||||
var req = HttpContext.Request;
|
||||
var f = Request.Form.Files[0];
|
||||
|
||||
Request.EnableRewind();
|
||||
Image image = null;
|
||||
using (StreamReader reader = new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
|
||||
{
|
||||
var bytes = ConvertToBinary(reader.BaseStream);
|
||||
image = ReturnPic(bytes);
|
||||
size = bytes.Length.ToString();
|
||||
}
|
||||
|
||||
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\images\\";
|
||||
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\images\\";
|
||||
//根据附件配置,设置上传图片目录
|
||||
string imgPath = DateTime.Now.Year.ToString();//默认按年
|
||||
switch (attach.SaveType)
|
||||
{
|
||||
case 1://按月份
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
|
||||
break;
|
||||
case 2:
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
|
||||
break;
|
||||
}
|
||||
filepath += imgPath;//存放路径
|
||||
thumbsFilePath += imgPath;//缩略图路径
|
||||
//图片名字
|
||||
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(upname);
|
||||
switch (attach.IsRandomFileName)
|
||||
{
|
||||
case 0://不随机
|
||||
imgname = upname;
|
||||
//判断是否存在
|
||||
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
|
||||
{
|
||||
imgname = sFileNameNoExt + "(1)" + sFullExtension;
|
||||
}
|
||||
break;
|
||||
case 1://随机字符串
|
||||
imgname = Utils.GetShortGUId() + sFullExtension;
|
||||
break;
|
||||
case 2://时间
|
||||
imgname = Utils.GetOrderNum() + sFullExtension;
|
||||
break;
|
||||
}
|
||||
string fullpath = Path.Combine(filepath, imgname);//图片
|
||||
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
|
||||
|
||||
//判断路径
|
||||
if (!Directory.Exists(filepath))
|
||||
Directory.CreateDirectory(filepath);
|
||||
//缩略图路径
|
||||
if (!Directory.Exists(thumbsFilePath))
|
||||
Directory.CreateDirectory(thumbsFilePath);
|
||||
|
||||
//保存图片
|
||||
image.Save(filepath);
|
||||
//生成缩略图
|
||||
if (attach.IsCreateThum == 1)
|
||||
{
|
||||
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
|
||||
}
|
||||
//添加水印
|
||||
if (attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
|
||||
{
|
||||
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
|
||||
if (System.IO.File.Exists(watermarkimg))
|
||||
{
|
||||
//先复制一张图片出来
|
||||
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
|
||||
System.IO.File.Copy(fullpath, copyfullpath);
|
||||
|
||||
Image waterpic = new Bitmap(watermarkimg);
|
||||
Image srcPic = new Bitmap(copyfullpath);
|
||||
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
|
||||
{
|
||||
waterpic.Dispose();
|
||||
//srcPic.Dispose();
|
||||
try
|
||||
{
|
||||
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
|
||||
srcPic.Dispose();
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (System.IO.File.Exists(copyfullpath))
|
||||
{
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
waterpic.Dispose();
|
||||
srcPic.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
image.Dispose();
|
||||
status = 1;
|
||||
name = imgname;
|
||||
path = $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname;
|
||||
thumb = $"/{attach.AttachPatch}/_thumbs/images/{imgPath.Replace("\\", "/")}/" + imgname;
|
||||
ext = sFullExtension;
|
||||
fileinfo = $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname;
|
||||
msg = "上传成功!";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
msg = ex.Message;
|
||||
}
|
||||
|
||||
return Json(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext });
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 上传图片
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<IActionResult> DoWebuploaderImage()
|
||||
{
|
||||
var msg = "上传失败!";
|
||||
var status = 0;
|
||||
var name = "";
|
||||
var path = "";
|
||||
var thumb = "";
|
||||
var size = "";
|
||||
var ext = "";
|
||||
var data = Request.Form.Files[0];
|
||||
if (data != null)
|
||||
{
|
||||
//判断是否是图片类型
|
||||
List<string> imgtypelist = new List<string> { "image/pjpeg", "image/png", "image/x-png", "image/gif", "image/bmp", "image/jpeg" };
|
||||
if (imgtypelist.FindIndex(x => x == data.ContentType) == -1)
|
||||
{
|
||||
msg = "只能上传一张图片格式文件!";
|
||||
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
|
||||
}
|
||||
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\images\\";
|
||||
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\images\\";
|
||||
//根据附件配置,设置上传图片目录
|
||||
string imgPath = DateTime.Now.Year.ToString();//默认按年
|
||||
switch (attach.SaveType)
|
||||
{
|
||||
case 1://按月份
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
|
||||
break;
|
||||
case 2:
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
|
||||
break;
|
||||
}
|
||||
filepath += imgPath;//存放路径
|
||||
thumbsFilePath += imgPath;//缩略图路径
|
||||
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(data.FileName);//文件名字,不带扩展名
|
||||
string sFullExtension = Utils.GetFileExtName(data.FileName);//扩展名
|
||||
//图片名字
|
||||
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(data.FileName);
|
||||
|
||||
switch (attach.IsRandomFileName)
|
||||
{
|
||||
case 0://不随机
|
||||
imgname = data.FileName;
|
||||
//判断是否存在
|
||||
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
|
||||
{
|
||||
imgname = sFileNameNoExt + "(1)" + sFullExtension;
|
||||
}
|
||||
break;
|
||||
case 1://随机字符串
|
||||
imgname = Utils.GetShortGUId() + sFullExtension;
|
||||
break;
|
||||
case 2://时间
|
||||
imgname = Utils.GetOrderNum() + sFullExtension;
|
||||
break;
|
||||
}
|
||||
string fullpath = Path.Combine(filepath, imgname);//图片
|
||||
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
|
||||
try
|
||||
{
|
||||
//判断路径
|
||||
if (!Directory.Exists(filepath))
|
||||
Directory.CreateDirectory(filepath);
|
||||
//缩略图路径
|
||||
if (!Directory.Exists(thumbsFilePath))
|
||||
Directory.CreateDirectory(thumbsFilePath);
|
||||
if (data != null)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
|
||||
{
|
||||
data.CopyTo(fs);
|
||||
}
|
||||
});
|
||||
//生成缩略图
|
||||
if (attach.IsCreateThum == 1)
|
||||
{
|
||||
ThumbnailHelper.MakeThumbnailImage(fullpath, fullThumbPath, attach.ThumMaxWidth, attach.ThumMaxHeight, ThumbnailHelper.CutMode.Cut);
|
||||
}
|
||||
string notwater = Request.Query["notwater"];//是否强制不加水印
|
||||
if (string.IsNullOrEmpty(notwater) || notwater != "1") notwater = "0";
|
||||
//添加水印
|
||||
if (notwater == "0" && attach.IsWaterMark == 1 && !string.IsNullOrEmpty(attach.WaterMarkImg))
|
||||
{
|
||||
string watermarkimg = _env.WebRootPath + attach.WaterMarkImg.Replace("/", "\\");
|
||||
if (System.IO.File.Exists(watermarkimg))
|
||||
{
|
||||
//先复制一张图片出来
|
||||
string copyfullpath = fullpath.Replace(sFullExtension, "_copy" + sFullExtension);
|
||||
System.IO.File.Copy(fullpath, copyfullpath);
|
||||
|
||||
Image waterpic = new Bitmap(watermarkimg);
|
||||
Image srcPic = new Bitmap(copyfullpath);
|
||||
if (waterpic.Width < srcPic.Width && waterpic.Height < srcPic.Height)
|
||||
{
|
||||
waterpic.Dispose();
|
||||
//srcPic.Dispose();
|
||||
try
|
||||
{
|
||||
WatermarkHelper.AddImageSignPic(srcPic, fullpath, watermarkimg, attach.WaterMarkPlace, attach.WaterMarkQty, attach.WaterMarkDiaphaneity);
|
||||
srcPic.Dispose();
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (System.IO.File.Exists(copyfullpath))
|
||||
{
|
||||
System.IO.File.Delete(copyfullpath);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
waterpic.Dispose();
|
||||
srcPic.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
status = 1;
|
||||
name = imgname;
|
||||
path = $"/{attach.AttachPatch}/images/{imgPath.Replace("\\", "/")}/" + imgname;
|
||||
thumb = $"/{attach.AttachPatch}/_thumbs/images/{imgPath.Replace("\\", "/")}/" + imgname;
|
||||
ext = sFullExtension;
|
||||
msg = "上传成功!";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
msg = "图片上传失败:" + ex.Message;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
msg = "请上传一张图片";
|
||||
}
|
||||
|
||||
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Webuploader 上传文件
|
||||
/// <summary>
|
||||
/// 上传图片
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<IActionResult> DoWebuploaderFile()
|
||||
{
|
||||
var msg = "上传失败!";
|
||||
var status = 0;
|
||||
var name = "";
|
||||
var path = "";
|
||||
var thumb = "";
|
||||
var size = "";
|
||||
var ext = "";
|
||||
var data = Request.Form.Files[0];
|
||||
if (data != null)
|
||||
{
|
||||
string filepath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\files\\";
|
||||
string thumbsFilePath = $"{_env.WebRootPath}\\{attach.AttachPatch}\\_thumbs\\files\\";
|
||||
//根据附件配置,设置上传图片目录
|
||||
string imgPath = DateTime.Now.Year.ToString();//默认按年
|
||||
switch (attach.SaveType)
|
||||
{
|
||||
case 1://按月份
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}";
|
||||
break;
|
||||
case 2:
|
||||
imgPath = $"{DateTime.Now.Year.ToString()}\\{DateTime.Now.ToString("MM")}\\{DateTime.Now.ToString("dd")}";
|
||||
break;
|
||||
}
|
||||
filepath += imgPath;//存放路径
|
||||
thumbsFilePath += imgPath;//缩略图路径
|
||||
string sFileNameNoExt = Utils.GetFileNameWithoutExtension(data.FileName);//文件名字,不带扩展名
|
||||
string sFullExtension = Utils.GetFileExtName(data.FileName);//扩展名
|
||||
|
||||
//判断是否是允许文件扩展名
|
||||
string sAllowedExtensions = _attachsetting.FileAllowedExtensions;
|
||||
List<string> listAllowedExtensions = new List<string>();
|
||||
string[] arrAllowedExtensions = sAllowedExtensions.Split(new string[] { "," }, StringSplitOptions.None);
|
||||
if (arrAllowedExtensions != null && arrAllowedExtensions.Length > 0)
|
||||
{
|
||||
foreach (var s in arrAllowedExtensions)
|
||||
{
|
||||
listAllowedExtensions.Add(s);
|
||||
}
|
||||
}
|
||||
if (listAllowedExtensions.Find(x => x == sFullExtension.ToLower().Replace(".", "")) == null)
|
||||
{
|
||||
msg = $"{sFullExtension}的文件类型,不允许上传!";
|
||||
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
|
||||
}
|
||||
|
||||
//图片名字
|
||||
string imgname = Utils.GetOrderNum() + Utils.GetFileExtName(data.FileName);
|
||||
|
||||
switch (attach.IsRandomFileName)
|
||||
{
|
||||
case 0://不随机
|
||||
imgname = data.FileName;
|
||||
//判断是否存在
|
||||
if (System.IO.File.Exists(Path.Combine(filepath, imgname)))
|
||||
{
|
||||
imgname = sFileNameNoExt + "(1)" + sFullExtension;
|
||||
}
|
||||
break;
|
||||
case 1://随机字符串
|
||||
imgname = Utils.GetShortGUId() + sFullExtension;
|
||||
break;
|
||||
case 2://时间
|
||||
imgname = Utils.GetOrderNum() + sFullExtension;
|
||||
break;
|
||||
}
|
||||
string fullpath = Path.Combine(filepath, imgname);//图片
|
||||
string fullThumbPath = Path.Combine(thumbsFilePath, imgname);//缩略图
|
||||
try
|
||||
{
|
||||
//判断路径
|
||||
if (!Directory.Exists(filepath))
|
||||
Directory.CreateDirectory(filepath);
|
||||
//缩略图路径
|
||||
if (!Directory.Exists(thumbsFilePath))
|
||||
Directory.CreateDirectory(thumbsFilePath);
|
||||
if (data != null)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
using (FileStream fs = new FileStream(fullpath, FileMode.Create))
|
||||
{
|
||||
data.CopyTo(fs);
|
||||
}
|
||||
});
|
||||
}
|
||||
status = 1;
|
||||
name = imgname;
|
||||
path = $"/{attach.AttachPatch}/files/{imgPath.Replace("\\", "/")}/" + imgname;
|
||||
thumb = $"/{attach.AttachPatch}/_thumbs/files/{imgPath.Replace("\\", "/")}/" + imgname;
|
||||
ext = sFullExtension;
|
||||
msg = "上传成功!";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
msg = "图片上传失败:" + ex.Message;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
msg = "请上传一个文件";
|
||||
}
|
||||
|
||||
return Content(JsonConvert.SerializeObject(new { status = status, msg = msg, name = name, path = path, thumb = thumb, size = size, ext = ext }), "text/plain");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 帮助方法
|
||||
public byte[] ConvertToBinary(Stream stream)
|
||||
{
|
||||
// 保存为byte[]
|
||||
byte[] byData = new byte[stream.Length];
|
||||
stream.Read(byData, 0, byData.Length);
|
||||
stream.Dispose();
|
||||
return byData;
|
||||
}
|
||||
/// <summary>
|
||||
/// 二进制转图片
|
||||
/// </summary>
|
||||
/// <param name="streamByte"></param>
|
||||
/// <returns></returns>
|
||||
public Image ReturnPic(byte[] streamByte)
|
||||
{
|
||||
MemoryStream ms = new MemoryStream(streamByte);
|
||||
Image img = Image.FromStream(ms);
|
||||
return img;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user