123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- using PMS.BusinessModels.Account;
- using PMS.Interface;
- using PMS.Interface.SysManager;
- using QWPlatform.SystemLibrary.Security;
- using QWPlatform.SystemLibrary.Web;
- using PMS.BusinessModels.SysManager;
- using QWPlatform.SystemLibrary.Utils;
- using PMS.BusinessModels;
- using System.Drawing;
- using System.Collections;
- using System.IO;
- namespace PMS.WebUI.Controllers
- {
- /// <summary>
- /// 创 建 人:王海洋
- /// 创建日期:2018-12-10
- /// 功能描述:基类控制器
- /// </summary>
- public class BaseController : Controller
- {
- public IAccount account_obj = InterfaceFactory.CreateBusinessInstance<IAccount>();
- /// <summary>
- /// 返回一个标准请求的JSON结构对象
- /// </summary>
- /// <param name="code">状态码,成功OK</param>
- /// <param name="msg">说明消息</param>
- /// <param name="json">需要进行序列化的json对象</param>
- /// <returns></returns>
- protected ActionResult ResponseJson(HttpStatusCode code, string msg = "", object obj = null)
- {
- //var jobj = new JsonResult();
- //jobj.Data = new PmsJsonResoult(code, msg, json).ToString();
- //jobj.ContentEncoding = Encoding.UTF8;
- //jobj.ContentType = "application/json";
- //jobj.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许外部访问
- //return jobj;
- return Content(new PmsJsonResoult(code, msg, obj).ToString(), "application/json");
- }
- /// <summary>
- /// 获取当前的登录用户
- /// </summary>
- /// <returns></returns>
- public UserInfo GetCurrentUser()
- {//获取登录信息
- return SysCom.Instance.GetCurrentAccount();
- }
- /// <summary>
- /// 获取数据权限(渠道,项目)
- /// </summary>
- /// <returns></returns>
- public AuthDatsInfo GetAuthDats()
- {
- var UserInfo = GetCurrentUser();
- var model = UserInfo.AuthDats;
- var Channel = new List<string>();
- var Item = new List<string>();
- ///判断是否存在数据权限
- if (model.Count > 0)
- {
- //便利渠道
- foreach (account_authdata_model m in model)
- {
- if (!String.IsNullOrEmpty(m.orgid))
- {
- Channel.Add(m.orgid);
- }
- //便利项目
- if (m.items != null && m.items.Count > 0)
- {
- Item.AddRange(m.items);
- }
- }
- }
- var AuthInfo = new AuthDatsInfo();
- AuthInfo.Channel = string.Join(",", Channel);
- AuthInfo.Project = string.Join(",", Item);
- return AuthInfo;
- }
- //获取当前页面的工具栏
- [HttpGet]
- public string GetToolbar(string groupName)
- {
- var user = GetCurrentUser();
- if (user != null)
- {//用户不为空,生成工作栏(获取当前请求action及controller),
- /* 1.确定当前访问模块
- * 2.确定当前模块的按钮列表
- * 3.确定当前模块的按钮分组(分组名称由开发人员在外填写div class="分组名")
- */
- var obj = this.ControllerContext.RouteData.Values;
- var controller = "";
- var view = "";
- if (obj.ContainsKey("controller"))
- {//获取控制器
- controller = obj["controller"].ToStringEx();
- }
- if (obj.ContainsKey("action"))
- {//获取方法
- view = obj["action"].ToStringEx();
- }
- var path = string.Format("{0}/{1}", controller, view);
- //查询出该模块的按钮列表(路径,人员角色,分组名称)
- var json = account_obj.GetButtionsForUserRole(user.Roles, path, groupName, user.IsSuperAdmin);
- return json;
- }
- return string.Empty;
- }
- #region 附件处理
- /// <summary>
- /// 上传文件附件
- /// </summary>
- /// <param name="file">文件内容</param>
- /// <param name="fileName">文件名称</param>
- /// <param name="filetype">文件类型</param>
- /// <returns>返回</returns>
- public FtpUploadResult UploadFile(byte[] file, string fileName, string filetype)
- {
- //调用公共方法的上传
- return SysCom.Instance.UploadFileToFtp(file, fileName, filetype);
- }
- /// <summary>
- /// 获取FTP文件的url
- /// </summary>
- /// <param name="fileId">多个文件逗号隔开</param>
- /// <param name="filetype"></param>
- /// <returns></returns>
- public FtpUrlResult GetFtpUrl(string fileId, string filetype )
- {
- return SysCom.Instance.GetFtpUrl(fileId, filetype);
- }
- /// <summary>
- /// 下载文件
- /// </summary>
- /// <param name="fileId">文件ID</param>
- /// <param name="imgType">图像类型:o原图,s缩略图,m大图</param>
- /// <returns></returns>
- public byte[] DownloadFileBytes(string fileId, string imgType)
- {
- return SysCom.Instance.DownloadFileBytesFromFtp(fileId, imgType);
- }
- /// <summary>
- /// 下载文件
- /// </summary>
- /// <param name="fileId">文件ID</param>
- /// <param name="imgType">图像类型:o原图,s缩略图,m大图</param>
- /// <returns>返回下载结构,包括文件名,文件类型</returns>
- public FtpDownloadResult DownloadFileBase64(string fileId, string imgType)
- {
- return SysCom.Instance.DownloadFileBase64FromFtp(fileId, imgType);
- }
- #endregion
- #region 消息推送处理
- //向客户端推送消息
- public void PushMessage(string persionId, string message)
- {
- App_Start.ClientManager.SendMessage(persionId, message);
- }
- //推送问题处理信息
- public void SendDealMessage(string ProblemId,string persionId, string message, int Type)
- {
- App_Start.ClientManager.SendDealMessage(ProblemId,persionId, message, Type);
- }
- ///刷新列表并推送消息
- public void RefreshProblemAndSendMessage(string persionId, string Message,string ProblemId)
- {
- App_Start.ClientManager.RefreshProblemAndSendMessage(persionId, Message, ProblemId);
- }
- /// <summary>
- /// 返回调用短信接口的webService授权码
- /// </summary>
- /// <param name="url">请的的webservice地址</param>
- /// <returns></returns>
- public string GetSMSAuthkey(string url)
- {
- return SysCom.Instance.ZlsoftAppServiceAuthkey(url);
- }
- /// <summary>
- /// 短信发送接口,返回是否成功
- /// </summary>
- /// <param name="telephone">电话号码</param>
- /// <param name="code">验证码</param>
- /// <returns></returns>
- public bool SMSCodeSend(string telephone, string code)
- {
- return SysCom.Instance.ZLsoftSMSCodeSend(telephone, code);
- }
- /// <summary>
- /// 短信验证码
- /// </summary>
- /// <returns></returns>
- public string ZlsoftAppSms(string phone)
- {
- return SysCom.Instance.ZlsoftAppSms(phone);
- }
- #endregion
- /// <summary>
- /// 获取邮件服务器的配置信息(服务器参数)
- /// </summary>
- /// <returns></returns>
- public emailConfig GetEmailConfig()
- {
- return SysCom.Instance.GetSysEmailConfig();
- }
- /// <summary>
- /// 获取当前登录用户的配置信息
- /// </summary>
- /// <returns></returns>
- public NotefiyConfigInfo GetMyConfigInfo()
- {
- var user = this.GetCurrentUser();
- return account_obj.GetConfigInfo(user.ID);
- }
- //图像上传
- public ActionResult ImageUpload(string upfile)
- {
- string pathbase = "/upload/"; //保存路径
- int size = 10; //文件大小限制,单位mb
- string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //文件允许格式
- string callback = this.Request["callback"];
- string editorId = this.Request["editorid"];
- //上传图片
- Hashtable info;
- Uploader up = new Uploader();
- info = up.upFileFTP(this.HttpContext, size); //获取上传状态
- return Json(info);
- }
- }
- /// <summary>
- /// UEditor编辑器通用上传类
- /// </summary>
- public class Uploader
- {
- string state = "SUCCESS";
- string URL = null;
- string currentType = null;
- string uploadpath = null;
- string filename = null;
- string originalName = null;
- HttpPostedFileBase uploadFile = null;
- int fileLen = 0;
- /**
- * 上传文件的主处理方法
- * @param HttpContext
- * @param string
- * @param string[]
- *@param int
- * @return Hashtable
- */
- public Hashtable upFile(HttpContextBase context, string pathbase, string[] filetype, int size)
- {
- pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
- uploadpath = context.Server.MapPath(pathbase);//获取文件上传路径
- try
- {
- uploadFile = context.Request.Files[0];
- originalName = uploadFile.FileName;
- //目录创建
- createFolder();
- //格式验证
- if (checkType(filetype))
- {
- state = "不允许的文件类型";
- }
- //大小验证
- if (checkSize(size))
- {
- state = "文件大小超出网站限制";
- }
- //保存图片
- if (state == "SUCCESS")
- {
- filename = reName();
- uploadFile.SaveAs(uploadpath + filename);
- URL = pathbase + filename;
- }
- }
- catch (Exception e)
- {
- state = "未知错误";
- URL = "";
- }
- return getUploadInfo();
- }
- /// <summary>
- /// 上传图片至FTP
- /// </summary>
- /// <param name="context"></param>
- /// <param name="pathbase"></param>
- /// <param name="filetype"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- public Hashtable upFileFTP(HttpContextBase context, int size)
- {
-
- try
- {
- uploadFile = context.Request.Files[0];
- originalName = uploadFile.FileName;
- fileLen = uploadFile.ContentLength;
- currentType = System.IO.Path.GetExtension(originalName);
- //格式验证
- if (currentType != ".jpg" && currentType != ".png" && currentType != ".jpeg" && currentType != ".bmp")
- {
- state = "不允许的文件类型";
- }
- //大小验证
- if (fileLen >= (size * 1024 * 1024))
- {
- state = "文件大小超出网站限制";
- }
- //保存图片
- if (state == "SUCCESS")
- {
- using (BinaryReader br = new BinaryReader(uploadFile.InputStream))
- {
- byte[] byteData = br.ReadBytes((int)uploadFile.InputStream.Length);
- var UplodResult = SysCom.Instance.UploadFileToFtp(byteData, originalName, currentType);
- if (UplodResult.code == 100)
- {
- URL = "/MobileProblem/ViewProblemImage/" + UplodResult.data + "?type=o";
- }
- else
- {
- state = "上传FTP失败";
- }
- }
- }
-
- }
- catch (Exception ex)
- {
- state = "未知错误";
- URL = "";
- QWPlatform.SystemLibrary.LogManager.Logger.Instance.Error("调用方法upFileFTP上传截图至FTP失败!", ex);
- }
- return getUploadInfoFTP();
- }
- private Hashtable getUploadInfoFTP()
- {
- Hashtable infoList = new Hashtable();
- infoList.Add("state", state);
- infoList.Add("url", URL);
- infoList.Add("originalName", originalName);
- infoList.Add("name", originalName);
- infoList.Add("size", fileLen);
- infoList.Add("type", currentType);
- return infoList;
- }
- /**
- * 上传涂鸦的主处理方法
- * @param HttpContext
- * @param string
- * @param string[]
- *@param string
- * @return Hashtable
- */
- public Hashtable upScrawl(HttpContext cxt, string pathbase, string tmppath, string base64Data)
- {
- pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
- uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径
- FileStream fs = null;
- try
- {
- //创建目录
- createFolder();
- //生成图片
- filename = System.Guid.NewGuid() + ".png";
- fs = File.Create(uploadpath + filename);
- byte[] bytes = Convert.FromBase64String(base64Data);
- fs.Write(bytes, 0, bytes.Length);
- URL = pathbase + filename;
- }
- catch (Exception e)
- {
- state = "未知错误";
- URL = "";
- }
- finally
- {
- fs.Close();
- deleteFolder(cxt.Server.MapPath(tmppath));
- }
- return getUploadInfo();
- }
- /**
- * 获取文件信息
- * @param context
- * @param string
- * @return string
- */
- public string getOtherInfo(HttpContext cxt, string field)
- {
- string info = null;
- if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field]))
- {
- info = field == "fileName" ? cxt.Request.Form[field].Split(',')[1] : cxt.Request.Form[field];
- }
- return info;
- }
- /**
- * 获取上传信息
- * @return Hashtable
- */
- private Hashtable getUploadInfo()
- {
- Hashtable infoList = new Hashtable();
- infoList.Add("state", state);
- infoList.Add("url", URL);
- infoList.Add("originalName", originalName);
- infoList.Add("name", Path.GetFileName(URL));
- infoList.Add("size", uploadFile.ContentLength);
- infoList.Add("type", Path.GetExtension(originalName));
- return infoList;
- }
- /**
- * 重命名文件
- * @return string
- */
- private string reName()
- {
- return System.Guid.NewGuid() + getFileExt();
- }
- /**
- * 文件类型检测
- * @return bool
- */
- private bool checkType(string[] filetype)
- {
- currentType = getFileExt();
- return Array.IndexOf(filetype, currentType) == -1;
- }
- /**
- * 文件大小检测
- * @param int
- * @return bool
- */
- private bool checkSize(int size)
- {
- return uploadFile.ContentLength >= (size * 1024 * 1024);
- }
- /**
- * 获取文件扩展名
- * @return string
- */
- private string getFileExt()
- {
- string[] temp = uploadFile.FileName.Split('.');
- return "." + temp[temp.Length - 1].ToLower();
- }
- /**
- * 按照日期自动创建存储文件夹
- */
- private void createFolder()
- {
- if (!Directory.Exists(uploadpath))
- {
- Directory.CreateDirectory(uploadpath);
- }
- }
- /**
- * 删除存储文件夹
- * @param string
- */
- public void deleteFolder(string path)
- {
- //if (Directory.Exists(path))
- //{
- // Directory.Delete(path, true);
- //}
- }
- }
- }
|