選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

633 行
22 KiB

  1. using Learun.Util;
  2. using Learun.Util.Ueditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Web.Mvc;
  9. namespace Learun.Application.Web.Controllers
  10. {
  11. /// <summary>
  12. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  13. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  14. /// 创建人:陈彬彬
  15. /// 日 期:2017.03.07
  16. /// 描 述:通用控制器,处理通用的接口
  17. /// </summary>
  18. [HandlerLogin(FilterMode.Ignore)]
  19. public class UtilityController : MvcControllerBase
  20. {
  21. #region 选择图标
  22. /// <summary>
  23. /// 图标的选择
  24. /// </summary>
  25. /// <returns></returns>
  26. [HttpGet]
  27. [HandlerLogin(FilterMode.Enforce)]
  28. public ActionResult Icon()
  29. {
  30. return View();
  31. }
  32. /// <summary>
  33. /// 移动图标的选择
  34. /// </summary>
  35. /// <returns></returns>
  36. [HttpGet]
  37. [HandlerLogin(FilterMode.Enforce)]
  38. public ActionResult AppIcon()
  39. {
  40. return View();
  41. }
  42. #endregion
  43. #region 百度编辑器的后端接口
  44. /// <summary>
  45. /// 百度编辑器的后端接口
  46. /// </summary>
  47. /// <param name="action">执行动作</param>
  48. /// <returns></returns>
  49. public ActionResult UEditor()
  50. {
  51. string action = Request["action"];
  52. switch (action)
  53. {
  54. case "config":
  55. return Content(UeditorConfig.Items.ToJson());
  56. case "uploadimage":
  57. return UEditorUpload(new UeditorUploadConfig()
  58. {
  59. AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"),
  60. PathFormat = UeditorConfig.GetString("imagePathFormat"),
  61. SizeLimit = UeditorConfig.GetInt("imageMaxSize"),
  62. UploadFieldName = UeditorConfig.GetString("imageFieldName")
  63. });
  64. case "uploadscrawl":
  65. return UEditorUpload(new UeditorUploadConfig()
  66. {
  67. AllowExtensions = new string[] { ".png" },
  68. PathFormat = UeditorConfig.GetString("scrawlPathFormat"),
  69. SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"),
  70. UploadFieldName = UeditorConfig.GetString("scrawlFieldName"),
  71. Base64 = true,
  72. Base64Filename = "scrawl.png"
  73. });
  74. case "uploadvideo":
  75. return UEditorUpload(new UeditorUploadConfig()
  76. {
  77. AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"),
  78. PathFormat = UeditorConfig.GetString("videoPathFormat"),
  79. SizeLimit = UeditorConfig.GetInt("videoMaxSize"),
  80. UploadFieldName = UeditorConfig.GetString("videoFieldName")
  81. });
  82. case "uploadfile":
  83. return UEditorUpload(new UeditorUploadConfig()
  84. {
  85. AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"),
  86. PathFormat = UeditorConfig.GetString("filePathFormat"),
  87. SizeLimit = UeditorConfig.GetInt("fileMaxSize"),
  88. UploadFieldName = UeditorConfig.GetString("fileFieldName")
  89. });
  90. case "listimage":
  91. return ListFileManager(UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles"));
  92. case "listfile":
  93. return ListFileManager(UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles"));
  94. case "catchimage":
  95. return CrawlerHandler();
  96. default:
  97. return Content(new
  98. {
  99. state = "action 参数为空或者 action 不被支持。"
  100. }.ToJson());
  101. }
  102. }
  103. /// <summary>
  104. /// 百度编辑器的文件上传
  105. /// </summary>
  106. /// <param name="uploadConfig">上传配置信息</param>
  107. /// <returns></returns>
  108. public ActionResult UEditorUpload(UeditorUploadConfig uploadConfig)
  109. {
  110. UeditorUploadResult result = new UeditorUploadResult() { State = UeditorUploadState.Unknown };
  111. byte[] uploadFileBytes = null;
  112. string uploadFileName = null;
  113. if (uploadConfig.Base64)
  114. {
  115. uploadFileName = uploadConfig.Base64Filename;
  116. uploadFileBytes = Convert.FromBase64String(Request[uploadConfig.UploadFieldName]);
  117. }
  118. else
  119. {
  120. var file = Request.Files[uploadConfig.UploadFieldName];
  121. uploadFileName = file.FileName;
  122. if (!CheckFileType(uploadConfig, uploadFileName))
  123. {
  124. return Content(new
  125. {
  126. state = GetStateMessage(UeditorUploadState.TypeNotAllow)
  127. }.ToJson());
  128. }
  129. if (!CheckFileSize(uploadConfig, file.ContentLength))
  130. {
  131. return Content(new
  132. {
  133. state = GetStateMessage(UeditorUploadState.SizeLimitExceed)
  134. }.ToJson());
  135. }
  136. uploadFileBytes = new byte[file.ContentLength];
  137. try
  138. {
  139. file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
  140. }
  141. catch (Exception)
  142. {
  143. return Content(new
  144. {
  145. state = GetStateMessage(UeditorUploadState.NetworkError)
  146. }.ToJson());
  147. }
  148. }
  149. result.OriginFileName = uploadFileName;
  150. var savePath = UeditorPathFormatter.Format(uploadFileName, uploadConfig.PathFormat);
  151. var localPath = Server.MapPath(savePath).Replace("\\Utility\\", "\\ueditor\\");// +"/ueditor/net";
  152. try
  153. {
  154. if (!Directory.Exists(Path.GetDirectoryName(localPath)))
  155. {
  156. Directory.CreateDirectory(Path.GetDirectoryName(localPath));
  157. }
  158. System.IO.File.WriteAllBytes(localPath, uploadFileBytes);
  159. result.Url = savePath;
  160. result.State = UeditorUploadState.Success;
  161. }
  162. catch (Exception e)
  163. {
  164. result.State = UeditorUploadState.FileAccessError;
  165. result.ErrorMessage = e.Message;
  166. }
  167. return Content(new
  168. {
  169. state = GetStateMessage(result.State),
  170. url = result.Url,
  171. title = result.OriginFileName,
  172. original = result.OriginFileName,
  173. error = result.ErrorMessage
  174. }.ToJson());
  175. }
  176. /// <summary>
  177. /// 百度编辑器的文件列表管理
  178. /// </summary>
  179. /// <param name="pathToList">文件列表目录</param>
  180. /// <param name="searchExtensions">扩展名</param>
  181. /// <returns></returns>
  182. public ActionResult ListFileManager(string pathToList, string[] searchExtensions)
  183. {
  184. int Start;
  185. int Size;
  186. int Total;
  187. String[] FileList;
  188. String[] SearchExtensions;
  189. SearchExtensions = searchExtensions.Select(x => x.ToLower()).ToArray();
  190. try
  191. {
  192. Start = String.IsNullOrEmpty(Request["start"]) ? 0 : Convert.ToInt32(Request["start"]);
  193. Size = String.IsNullOrEmpty(Request["size"]) ? UeditorConfig.GetInt("imageManagerListSize") : Convert.ToInt32(Request["size"]);
  194. }
  195. catch (FormatException)
  196. {
  197. return Content(new
  198. {
  199. state = "参数不正确",
  200. start = 0,
  201. size = 0,
  202. total = 0
  203. }.ToJson());
  204. }
  205. var buildingList = new List<String>();
  206. try
  207. {
  208. var localPath = Server.MapPath(pathToList).Replace("\\Utility\\", "\\ueditor\\");
  209. buildingList.AddRange(Directory.GetFiles(localPath, "*", SearchOption.AllDirectories)
  210. .Where(x => SearchExtensions.Contains(Path.GetExtension(x).ToLower()))
  211. .Select(x => pathToList + x.Substring(localPath.Length).Replace("\\", "/")));
  212. Total = buildingList.Count;
  213. FileList = buildingList.OrderBy(x => x).Skip(Start).Take(Size).ToArray();
  214. }
  215. catch (UnauthorizedAccessException)
  216. {
  217. return Content(new
  218. {
  219. state = "文件系统权限不足",
  220. start = 0,
  221. size = 0,
  222. total = 0
  223. }.ToJson());
  224. }
  225. catch (DirectoryNotFoundException)
  226. {
  227. return Content(new
  228. {
  229. state = "路径不存在",
  230. start = 0,
  231. size = 0,
  232. total = 0
  233. }.ToJson());
  234. }
  235. catch (IOException)
  236. {
  237. return Content(new
  238. {
  239. state = "文件系统读取错误",
  240. start = 0,
  241. size = 0,
  242. total = 0
  243. }.ToJson());
  244. }
  245. return Content(new
  246. {
  247. state = "SUCCESS",
  248. list = FileList == null ? null : FileList.Select(x => new { url = x }),
  249. start = Start,
  250. size = Size,
  251. total = Total
  252. }.ToJson());
  253. }
  254. public ActionResult CrawlerHandler()
  255. {
  256. string[] sources = Request.Form.GetValues("source[]");
  257. if (sources == null || sources.Length == 0)
  258. {
  259. return Content(new
  260. {
  261. state = "参数错误:没有指定抓取源"
  262. }.ToJson());
  263. }
  264. UeditorCrawler[] crawlers = sources.Select(x => new UeditorCrawler(x).Fetch()).ToArray();
  265. return Content(new
  266. {
  267. state = "SUCCESS",
  268. list = crawlers.Select(x => new
  269. {
  270. state = x.State,
  271. source = x.SourceUrl,
  272. url = x.ServerUrl
  273. })
  274. }.ToJson());
  275. }
  276. private string GetStateMessage(UeditorUploadState state)
  277. {
  278. switch (state)
  279. {
  280. case UeditorUploadState.Success:
  281. return "SUCCESS";
  282. case UeditorUploadState.FileAccessError:
  283. return "文件访问出错,请检查写入权限";
  284. case UeditorUploadState.SizeLimitExceed:
  285. return "文件大小超出服务器限制";
  286. case UeditorUploadState.TypeNotAllow:
  287. return "不允许的文件格式";
  288. case UeditorUploadState.NetworkError:
  289. return "网络错误";
  290. }
  291. return "未知错误";
  292. }
  293. /// <summary>
  294. /// 检测是否符合上传文件格式
  295. /// </summary>
  296. /// <param name="uploadConfig">配置信息</param>
  297. /// <param name="filename">文件名字</param>
  298. /// <returns></returns>
  299. private bool CheckFileType(UeditorUploadConfig uploadConfig, string filename)
  300. {
  301. var fileExtension = Path.GetExtension(filename).ToLower();
  302. var res = false;
  303. foreach (var item in uploadConfig.AllowExtensions)
  304. {
  305. if (item == fileExtension)
  306. {
  307. res = true;
  308. break;
  309. }
  310. }
  311. return res;
  312. }
  313. /// <summary>
  314. /// 检测是否符合上传文件大小
  315. /// </summary>
  316. /// <param name="uploadConfig">配置信息</param>
  317. /// <param name="size">文件大小</param>
  318. /// <returns></returns>
  319. private bool CheckFileSize(UeditorUploadConfig uploadConfig, int size)
  320. {
  321. return size < uploadConfig.SizeLimit;
  322. }
  323. #endregion
  324. #region 导出Excel
  325. /// <summary>
  326. /// 请选择要导出的字段页面
  327. /// </summary>
  328. /// <returns></returns>
  329. [HttpGet]
  330. [HandlerLogin(FilterMode.Enforce)]
  331. public ActionResult ExcelExportForm()
  332. {
  333. return View();
  334. }
  335. [HttpPost, ValidateInput(false)]
  336. public void ExportExcel(string fileName, string columnJson, string dataJson, string exportField)
  337. {
  338. //设置导出格式
  339. ExcelConfig excelconfig = new ExcelConfig();
  340. excelconfig.Title = Server.UrlDecode(fileName);
  341. excelconfig.TitleFont = "微软雅黑";
  342. excelconfig.TitlePoint = 15;
  343. excelconfig.FileName = Server.UrlDecode(fileName) + ".xls";
  344. excelconfig.IsAllSizeColumn = true;
  345. excelconfig.ColumnEntity = new List<ColumnModel>();
  346. //表头
  347. List<jfGridModel> columnList = columnJson.ToList<jfGridModel>();
  348. //行数据
  349. DataTable rowData = dataJson.ToTable();
  350. //写入Excel表头
  351. Dictionary<string, string> exportFieldMap = new Dictionary<string, string>();
  352. if (!string.IsNullOrEmpty(exportField))
  353. {
  354. string[] exportFields = exportField.Split(',');
  355. foreach (var field in exportFields)
  356. {
  357. if (!exportFieldMap.ContainsKey(field))
  358. {
  359. exportFieldMap.Add(field, "1");
  360. }
  361. }
  362. }
  363. foreach (jfGridModel columnModel in columnList)
  364. {
  365. if (exportFieldMap.ContainsKey(columnModel.name) || string.IsNullOrEmpty(exportField))
  366. {
  367. excelconfig.ColumnEntity.Add(new ColumnModel()
  368. {
  369. Column = columnModel.name,
  370. ExcelColumn = columnModel.label,
  371. Alignment = columnModel.align,
  372. });
  373. }
  374. }
  375. ExcelHelper.ExcelDownload(rowData, excelconfig);
  376. }
  377. #endregion
  378. #region 列表选择弹层
  379. /// <summary>
  380. /// 列表选择弹层
  381. /// </summary>
  382. /// <returns></returns>
  383. [HttpGet]
  384. [HandlerLogin(FilterMode.Enforce)]
  385. public ActionResult GirdSelectIndex()
  386. {
  387. return View();
  388. }
  389. #endregion
  390. #region 树形选择弹层
  391. /// <summary>
  392. /// 列表选择弹层
  393. /// </summary>
  394. /// <returns></returns>
  395. [HttpGet]
  396. [HandlerLogin(FilterMode.Enforce)]
  397. public ActionResult TreeSelectIndex()
  398. {
  399. return View();
  400. }
  401. #endregion
  402. #region 加载js和css文件
  403. /// <summary>
  404. /// 列表选择弹层
  405. /// </summary>
  406. /// <returns></returns>
  407. [HttpGet]
  408. public ActionResult JsCss(string plugins)
  409. {
  410. Dictionary<string,JssCssModel> list = new Dictionary<string,JssCssModel>();
  411. string[] pluginArray = plugins.Split(',');
  412. foreach (var item in pluginArray) {
  413. GetJssCss(item,list);
  414. }
  415. return JsonResult(list);
  416. }
  417. /// <summary>
  418. /// 获取js和css文件值
  419. /// </summary>
  420. /// <param name="name"></param>
  421. /// <param name="list"></param>
  422. private void GetJssCss(string name, Dictionary<string, JssCssModel> list)
  423. {
  424. JssCssModel model = new JssCssModel();
  425. switch (name) {
  426. case "jquery":
  427. model.js = JsCssHelper.Read("/Content/jquery/jquery-1.10.2.min.js");
  428. break;
  429. case "cookie":
  430. model.js = JsCssHelper.Read("/Content/jquery/plugin/jquery.cookie.min.js");
  431. break;
  432. case "md5":
  433. model.js = JsCssHelper.Read("/Content/jquery/jquery.md5.min.js");
  434. break;
  435. case "scrollbar":
  436. model.css = JsCssHelper.Read("/Content/jquery/plugin/scrollbar/jquery.mCustomScrollbar.min.css");
  437. model.js = JsCssHelper.Read("/Content/jquery/plugin/scrollbar/jquery.mCustomScrollbar.concat.min.js");
  438. break;
  439. case "toastr":
  440. model.css = JsCssHelper.Read("/Content/jquery/plugin/toastr/toastr.css");
  441. model.js = JsCssHelper.Read("/Content/jquery/plugin/toastr/toastr.min.js");
  442. break;
  443. case "bootstrap":
  444. model.css = JsCssHelper.Read("/Content/bootstrap/bootstrap.min.css");
  445. model.js = JsCssHelper.Read("/Content/bootstrap/bootstrap.min.js");
  446. break;
  447. case "layer":
  448. model.css = JsCssHelper.Read("/Content/bootstrap/bootstrap.min.css");
  449. model.js = JsCssHelper.Read("/Content/bootstrap/bootstrap.min.js");
  450. break;
  451. case "jqprint":
  452. break;
  453. case "wdatePicker":
  454. break;
  455. case "syntaxhighlighter":
  456. break;
  457. case "fontAwesome":
  458. break;
  459. case "iconfont":
  460. break;
  461. case "common":
  462. break;
  463. case "base":
  464. break;
  465. case "tabs":
  466. break;
  467. case "date":
  468. break;
  469. case "validator-helper":
  470. break;
  471. case "lrlayer":
  472. break;
  473. case "ajax":
  474. break;
  475. case "clientdata":
  476. break;
  477. case "iframe":
  478. break;
  479. case "validator":
  480. break;
  481. case "layout":
  482. break;
  483. case "tree":
  484. break;
  485. case "select":
  486. break;
  487. case "formselect":
  488. break;
  489. case "layerselect":
  490. break;
  491. case "jfgrid":
  492. break;
  493. case "wizard":
  494. break;
  495. case "timeline":
  496. break;
  497. case "datepicker":
  498. break;
  499. case "uploader":
  500. break;
  501. case "excel":
  502. break;
  503. case "authorize":
  504. break;
  505. case "custmerform":
  506. break;
  507. case "workflow":
  508. break;
  509. case "form":
  510. break;
  511. }
  512. }
  513. private class JssCssModel {
  514. /// <summary>
  515. /// js 代码
  516. /// </summary>
  517. public string js { get; set; }
  518. /// <summary>
  519. /// css 代码
  520. /// </summary>
  521. public string css { get; set; }
  522. /// <summary>
  523. /// 版本号
  524. /// </summary>
  525. public string ver { get; set; }
  526. }
  527. #endregion
  528. #region 自定义表单
  529. /// <summary>
  530. /// 表单预览
  531. /// </summary>
  532. /// <returns></returns>
  533. [HttpGet]
  534. public ActionResult PreviewForm()
  535. {
  536. return View();
  537. }
  538. /// <summary>
  539. /// 编辑表格
  540. /// </summary>
  541. /// <returns></returns>
  542. [HttpGet]
  543. public ActionResult EditGridForm()
  544. {
  545. return View();
  546. }
  547. #endregion
  548. #region jfgrid弹层选择
  549. /// <summary>
  550. /// 列表选择弹层
  551. /// </summary>
  552. /// <returns></returns>
  553. [HttpGet]
  554. [HandlerLogin(FilterMode.Enforce)]
  555. public ActionResult JfGirdLayerForm()
  556. {
  557. return View();
  558. }
  559. #endregion
  560. #region 桌面消息列表详情查看
  561. /// <summary>
  562. /// 桌面消息列表详情查看
  563. /// </summary>
  564. /// <returns></returns>
  565. [HttpGet]
  566. public ActionResult ListContentIndex()
  567. {
  568. return View();
  569. }
  570. /// <summary>
  571. /// 失物招领
  572. /// </summary>
  573. /// <returns></returns>
  574. [HttpGet]
  575. public ActionResult ListContentIndexLost()
  576. {
  577. return View();
  578. }
  579. #endregion
  580. #region 开发向导
  581. /// <summary>
  582. /// pc端开发向导
  583. /// </summary>
  584. /// <returns></returns>
  585. [HttpGet]
  586. [HandlerLogin(FilterMode.Enforce)]
  587. public ActionResult PCDevGuideIndex()
  588. {
  589. return View();
  590. }
  591. /// <summary>
  592. /// 移动端开发向导
  593. /// </summary>
  594. /// <returns></returns>
  595. [HttpGet]
  596. [HandlerLogin(FilterMode.Enforce)]
  597. public ActionResult AppDevGuideIndex()
  598. {
  599. return View();
  600. }
  601. #endregion
  602. }
  603. }