BaseController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using PMS.BusinessModels.Account;
  9. using PMS.Interface;
  10. using PMS.Interface.SysManager;
  11. using QWPlatform.SystemLibrary.Security;
  12. using QWPlatform.SystemLibrary.Web;
  13. using PMS.BusinessModels.SysManager;
  14. using QWPlatform.SystemLibrary.Utils;
  15. using PMS.BusinessModels;
  16. using System.Drawing;
  17. using System.Collections;
  18. using System.IO;
  19. namespace PMS.WebUI.Controllers
  20. {
  21. /// <summary>
  22. /// 创 建 人:王海洋
  23. /// 创建日期:2018-12-10
  24. /// 功能描述:基类控制器
  25. /// </summary>
  26. public class BaseController : Controller
  27. {
  28. public IAccount account_obj = InterfaceFactory.CreateBusinessInstance<IAccount>();
  29. /// <summary>
  30. /// 返回一个标准请求的JSON结构对象
  31. /// </summary>
  32. /// <param name="code">状态码,成功OK</param>
  33. /// <param name="msg">说明消息</param>
  34. /// <param name="json">需要进行序列化的json对象</param>
  35. /// <returns></returns>
  36. protected ActionResult ResponseJson(HttpStatusCode code, string msg = "", object obj = null)
  37. {
  38. //var jobj = new JsonResult();
  39. //jobj.Data = new PmsJsonResoult(code, msg, json).ToString();
  40. //jobj.ContentEncoding = Encoding.UTF8;
  41. //jobj.ContentType = "application/json";
  42. //jobj.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许外部访问
  43. //return jobj;
  44. return Content(new PmsJsonResoult(code, msg, obj).ToString(), "application/json");
  45. }
  46. /// <summary>
  47. /// 获取当前的登录用户
  48. /// </summary>
  49. /// <returns></returns>
  50. public UserInfo GetCurrentUser()
  51. {//获取登录信息
  52. return SysCom.Instance.GetCurrentAccount();
  53. }
  54. /// <summary>
  55. /// 获取数据权限(渠道,项目)
  56. /// </summary>
  57. /// <returns></returns>
  58. public AuthDatsInfo GetAuthDats()
  59. {
  60. var UserInfo = GetCurrentUser();
  61. var model = UserInfo.AuthDats;
  62. var Channel = new List<string>();
  63. var Item = new List<string>();
  64. ///判断是否存在数据权限
  65. if (model.Count > 0)
  66. {
  67. //便利渠道
  68. foreach (account_authdata_model m in model)
  69. {
  70. if (!String.IsNullOrEmpty(m.orgid))
  71. {
  72. Channel.Add(m.orgid);
  73. }
  74. //便利项目
  75. if (m.items != null && m.items.Count > 0)
  76. {
  77. Item.AddRange(m.items);
  78. }
  79. }
  80. }
  81. var AuthInfo = new AuthDatsInfo();
  82. AuthInfo.Channel = string.Join(",", Channel);
  83. AuthInfo.Project = string.Join(",", Item);
  84. return AuthInfo;
  85. }
  86. //获取当前页面的工具栏
  87. [HttpGet]
  88. public string GetToolbar(string groupName)
  89. {
  90. var user = GetCurrentUser();
  91. if (user != null)
  92. {//用户不为空,生成工作栏(获取当前请求action及controller),
  93. /* 1.确定当前访问模块
  94. * 2.确定当前模块的按钮列表
  95. * 3.确定当前模块的按钮分组(分组名称由开发人员在外填写div class="分组名")
  96. */
  97. var obj = this.ControllerContext.RouteData.Values;
  98. var controller = "";
  99. var view = "";
  100. if (obj.ContainsKey("controller"))
  101. {//获取控制器
  102. controller = obj["controller"].ToStringEx();
  103. }
  104. if (obj.ContainsKey("action"))
  105. {//获取方法
  106. view = obj["action"].ToStringEx();
  107. }
  108. var path = string.Format("{0}/{1}", controller, view);
  109. //查询出该模块的按钮列表(路径,人员角色,分组名称)
  110. var json = account_obj.GetButtionsForUserRole(user.Roles, path, groupName, user.IsSuperAdmin);
  111. return json;
  112. }
  113. return string.Empty;
  114. }
  115. #region 附件处理
  116. /// <summary>
  117. /// 上传文件附件
  118. /// </summary>
  119. /// <param name="file">文件内容</param>
  120. /// <param name="fileName">文件名称</param>
  121. /// <param name="filetype">文件类型</param>
  122. /// <returns>返回</returns>
  123. public FtpUploadResult UploadFile(byte[] file, string fileName, string filetype)
  124. {
  125. //调用公共方法的上传
  126. return SysCom.Instance.UploadFileToFtp(file, fileName, filetype);
  127. }
  128. /// <summary>
  129. /// 获取FTP文件的url
  130. /// </summary>
  131. /// <param name="fileId">多个文件逗号隔开</param>
  132. /// <param name="filetype"></param>
  133. /// <returns></returns>
  134. public FtpUrlResult GetFtpUrl(string fileId, string filetype )
  135. {
  136. return SysCom.Instance.GetFtpUrl(fileId, filetype);
  137. }
  138. /// <summary>
  139. /// 下载文件
  140. /// </summary>
  141. /// <param name="fileId">文件ID</param>
  142. /// <param name="imgType">图像类型:o原图,s缩略图,m大图</param>
  143. /// <returns></returns>
  144. public byte[] DownloadFileBytes(string fileId, string imgType)
  145. {
  146. return SysCom.Instance.DownloadFileBytesFromFtp(fileId, imgType);
  147. }
  148. /// <summary>
  149. /// 下载文件
  150. /// </summary>
  151. /// <param name="fileId">文件ID</param>
  152. /// <param name="imgType">图像类型:o原图,s缩略图,m大图</param>
  153. /// <returns>返回下载结构,包括文件名,文件类型</returns>
  154. public FtpDownloadResult DownloadFileBase64(string fileId, string imgType)
  155. {
  156. return SysCom.Instance.DownloadFileBase64FromFtp(fileId, imgType);
  157. }
  158. #endregion
  159. #region 消息推送处理
  160. //向客户端推送消息
  161. public void PushMessage(string persionId, string message)
  162. {
  163. App_Start.ClientManager.SendMessage(persionId, message);
  164. }
  165. //推送问题处理信息
  166. public void SendDealMessage(string ProblemId,string persionId, string message, int Type)
  167. {
  168. App_Start.ClientManager.SendDealMessage(ProblemId,persionId, message, Type);
  169. }
  170. ///刷新列表并推送消息
  171. public void RefreshProblemAndSendMessage(string persionId, string Message,string ProblemId)
  172. {
  173. App_Start.ClientManager.RefreshProblemAndSendMessage(persionId, Message, ProblemId);
  174. }
  175. /// <summary>
  176. /// 返回调用短信接口的webService授权码
  177. /// </summary>
  178. /// <param name="url">请的的webservice地址</param>
  179. /// <returns></returns>
  180. public string GetSMSAuthkey(string url)
  181. {
  182. return SysCom.Instance.ZlsoftAppServiceAuthkey(url);
  183. }
  184. /// <summary>
  185. /// 短信发送接口,返回是否成功
  186. /// </summary>
  187. /// <param name="telephone">电话号码</param>
  188. /// <param name="code">验证码</param>
  189. /// <returns></returns>
  190. public bool SMSCodeSend(string telephone, string code)
  191. {
  192. return SysCom.Instance.ZLsoftSMSCodeSend(telephone, code);
  193. }
  194. /// <summary>
  195. /// 短信验证码
  196. /// </summary>
  197. /// <returns></returns>
  198. public string ZlsoftAppSms(string phone)
  199. {
  200. return SysCom.Instance.ZlsoftAppSms(phone);
  201. }
  202. #endregion
  203. /// <summary>
  204. /// 获取邮件服务器的配置信息(服务器参数)
  205. /// </summary>
  206. /// <returns></returns>
  207. public emailConfig GetEmailConfig()
  208. {
  209. return SysCom.Instance.GetSysEmailConfig();
  210. }
  211. /// <summary>
  212. /// 获取当前登录用户的配置信息
  213. /// </summary>
  214. /// <returns></returns>
  215. public NotefiyConfigInfo GetMyConfigInfo()
  216. {
  217. var user = this.GetCurrentUser();
  218. return account_obj.GetConfigInfo(user.ID);
  219. }
  220. //图像上传
  221. public ActionResult ImageUpload(string upfile)
  222. {
  223. string pathbase = "/upload/"; //保存路径
  224. int size = 10; //文件大小限制,单位mb
  225. string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //文件允许格式
  226. string callback = this.Request["callback"];
  227. string editorId = this.Request["editorid"];
  228. //上传图片
  229. Hashtable info;
  230. Uploader up = new Uploader();
  231. info = up.upFileFTP(this.HttpContext, size); //获取上传状态
  232. return Json(info);
  233. }
  234. }
  235. /// <summary>
  236. /// UEditor编辑器通用上传类
  237. /// </summary>
  238. public class Uploader
  239. {
  240. string state = "SUCCESS";
  241. string URL = null;
  242. string currentType = null;
  243. string uploadpath = null;
  244. string filename = null;
  245. string originalName = null;
  246. HttpPostedFileBase uploadFile = null;
  247. int fileLen = 0;
  248. /**
  249. * 上传文件的主处理方法
  250. * @param HttpContext
  251. * @param string
  252. * @param string[]
  253. *@param int
  254. * @return Hashtable
  255. */
  256. public Hashtable upFile(HttpContextBase context, string pathbase, string[] filetype, int size)
  257. {
  258. pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
  259. uploadpath = context.Server.MapPath(pathbase);//获取文件上传路径
  260. try
  261. {
  262. uploadFile = context.Request.Files[0];
  263. originalName = uploadFile.FileName;
  264. //目录创建
  265. createFolder();
  266. //格式验证
  267. if (checkType(filetype))
  268. {
  269. state = "不允许的文件类型";
  270. }
  271. //大小验证
  272. if (checkSize(size))
  273. {
  274. state = "文件大小超出网站限制";
  275. }
  276. //保存图片
  277. if (state == "SUCCESS")
  278. {
  279. filename = reName();
  280. uploadFile.SaveAs(uploadpath + filename);
  281. URL = pathbase + filename;
  282. }
  283. }
  284. catch (Exception e)
  285. {
  286. state = "未知错误";
  287. URL = "";
  288. }
  289. return getUploadInfo();
  290. }
  291. /// <summary>
  292. /// 上传图片至FTP
  293. /// </summary>
  294. /// <param name="context"></param>
  295. /// <param name="pathbase"></param>
  296. /// <param name="filetype"></param>
  297. /// <param name="size"></param>
  298. /// <returns></returns>
  299. public Hashtable upFileFTP(HttpContextBase context, int size)
  300. {
  301. try
  302. {
  303. uploadFile = context.Request.Files[0];
  304. originalName = uploadFile.FileName;
  305. fileLen = uploadFile.ContentLength;
  306. currentType = System.IO.Path.GetExtension(originalName);
  307. //格式验证
  308. if (currentType != ".jpg" && currentType != ".png" && currentType != ".jpeg" && currentType != ".bmp")
  309. {
  310. state = "不允许的文件类型";
  311. }
  312. //大小验证
  313. if (fileLen >= (size * 1024 * 1024))
  314. {
  315. state = "文件大小超出网站限制";
  316. }
  317. //保存图片
  318. if (state == "SUCCESS")
  319. {
  320. using (BinaryReader br = new BinaryReader(uploadFile.InputStream))
  321. {
  322. byte[] byteData = br.ReadBytes((int)uploadFile.InputStream.Length);
  323. var UplodResult = SysCom.Instance.UploadFileToFtp(byteData, originalName, currentType);
  324. if (UplodResult.code == 100)
  325. {
  326. URL = "/MobileProblem/ViewProblemImage/" + UplodResult.data + "?type=o";
  327. }
  328. else
  329. {
  330. state = "上传FTP失败";
  331. }
  332. }
  333. }
  334. }
  335. catch (Exception ex)
  336. {
  337. state = "未知错误";
  338. URL = "";
  339. QWPlatform.SystemLibrary.LogManager.Logger.Instance.Error("调用方法upFileFTP上传截图至FTP失败!", ex);
  340. }
  341. return getUploadInfoFTP();
  342. }
  343. private Hashtable getUploadInfoFTP()
  344. {
  345. Hashtable infoList = new Hashtable();
  346. infoList.Add("state", state);
  347. infoList.Add("url", URL);
  348. infoList.Add("originalName", originalName);
  349. infoList.Add("name", originalName);
  350. infoList.Add("size", fileLen);
  351. infoList.Add("type", currentType);
  352. return infoList;
  353. }
  354. /**
  355. * 上传涂鸦的主处理方法
  356. * @param HttpContext
  357. * @param string
  358. * @param string[]
  359. *@param string
  360. * @return Hashtable
  361. */
  362. public Hashtable upScrawl(HttpContext cxt, string pathbase, string tmppath, string base64Data)
  363. {
  364. pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
  365. uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径
  366. FileStream fs = null;
  367. try
  368. {
  369. //创建目录
  370. createFolder();
  371. //生成图片
  372. filename = System.Guid.NewGuid() + ".png";
  373. fs = File.Create(uploadpath + filename);
  374. byte[] bytes = Convert.FromBase64String(base64Data);
  375. fs.Write(bytes, 0, bytes.Length);
  376. URL = pathbase + filename;
  377. }
  378. catch (Exception e)
  379. {
  380. state = "未知错误";
  381. URL = "";
  382. }
  383. finally
  384. {
  385. fs.Close();
  386. deleteFolder(cxt.Server.MapPath(tmppath));
  387. }
  388. return getUploadInfo();
  389. }
  390. /**
  391. * 获取文件信息
  392. * @param context
  393. * @param string
  394. * @return string
  395. */
  396. public string getOtherInfo(HttpContext cxt, string field)
  397. {
  398. string info = null;
  399. if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field]))
  400. {
  401. info = field == "fileName" ? cxt.Request.Form[field].Split(',')[1] : cxt.Request.Form[field];
  402. }
  403. return info;
  404. }
  405. /**
  406. * 获取上传信息
  407. * @return Hashtable
  408. */
  409. private Hashtable getUploadInfo()
  410. {
  411. Hashtable infoList = new Hashtable();
  412. infoList.Add("state", state);
  413. infoList.Add("url", URL);
  414. infoList.Add("originalName", originalName);
  415. infoList.Add("name", Path.GetFileName(URL));
  416. infoList.Add("size", uploadFile.ContentLength);
  417. infoList.Add("type", Path.GetExtension(originalName));
  418. return infoList;
  419. }
  420. /**
  421. * 重命名文件
  422. * @return string
  423. */
  424. private string reName()
  425. {
  426. return System.Guid.NewGuid() + getFileExt();
  427. }
  428. /**
  429. * 文件类型检测
  430. * @return bool
  431. */
  432. private bool checkType(string[] filetype)
  433. {
  434. currentType = getFileExt();
  435. return Array.IndexOf(filetype, currentType) == -1;
  436. }
  437. /**
  438. * 文件大小检测
  439. * @param int
  440. * @return bool
  441. */
  442. private bool checkSize(int size)
  443. {
  444. return uploadFile.ContentLength >= (size * 1024 * 1024);
  445. }
  446. /**
  447. * 获取文件扩展名
  448. * @return string
  449. */
  450. private string getFileExt()
  451. {
  452. string[] temp = uploadFile.FileName.Split('.');
  453. return "." + temp[temp.Length - 1].ToLower();
  454. }
  455. /**
  456. * 按照日期自动创建存储文件夹
  457. */
  458. private void createFolder()
  459. {
  460. if (!Directory.Exists(uploadpath))
  461. {
  462. Directory.CreateDirectory(uploadpath);
  463. }
  464. }
  465. /**
  466. * 删除存储文件夹
  467. * @param string
  468. */
  469. public void deleteFolder(string path)
  470. {
  471. //if (Directory.Exists(path))
  472. //{
  473. // Directory.Delete(path, true);
  474. //}
  475. }
  476. }
  477. }