You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

383 lines
14 KiB

  1. using Nancy;
  2. using Learun.Util;
  3. using System.Collections.Generic;
  4. using Learun.Application.TwoDevelopment.EducationalAdministration;
  5. using System;
  6. using Learun.Application.Base.SystemModule;
  7. using System.IO;
  8. using System.Web.Mvc;
  9. using Learun.Application.WebApi.Modules;
  10. using System.Linq;
  11. using System.Net;
  12. using Learun.Cache.Base;
  13. using Learun.Cache.Factory;
  14. using Nancy.Responses;
  15. using Quanjiang.DigitalScholl.SendSms;
  16. namespace Learun.Application.WebApi
  17. {
  18. /// <summary>
  19. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  20. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  21. /// 创 建:超级管理员
  22. /// 日 期:2020-06-03 14:29
  23. /// 描 述:听课记录
  24. /// </summary>
  25. public class SunshineEducationApi : BaseNoLoginApi
  26. {
  27. private SunshineEducationIBLL sunshineEducationIBLL = new SunshineEducationBLL();
  28. private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
  29. private ICache redisCache = CacheFactory.CaChe();
  30. private readonly ISms yixintongSms = new YixintongSms();
  31. /// <summary>
  32. /// 阳光教育
  33. /// <summary>
  34. public SunshineEducationApi()
  35. : base("/Learun/adms/SunshineEducation")
  36. {
  37. Get["/pagelist"] = GetPageList;
  38. Get["/list"] = GetList;
  39. Get["/form"] = GetForm;
  40. Get["/statistics"] = StatisticsForm;
  41. //Post["/delete"] = DeleteForm;
  42. Get["/down"] = DownAnnexesFile;
  43. Post["/upload"] = Upload;
  44. Post["/save"] = SaveForm;//提交,回复,评论
  45. Post["/clicks"] = ClicksForm;
  46. Get["/getverifycode"] = GetVerifyCode;
  47. Post["/checkverifycode"] = CheckVerifyCode;
  48. }
  49. #region 获取数据
  50. /// <summary>
  51. /// 检查图形验证码并发送短信验证码
  52. /// </summary>
  53. /// <param name="_"></param>
  54. /// <returns></returns>
  55. public Response CheckVerifyCode(dynamic _)
  56. {
  57. MobileVerify mobileVerify = this.GetReqData<MobileVerify>();
  58. var code = redisCache.Read<string>("sunshineimgvcode_" + mobileVerify.pagecode);
  59. LogEntity logEntity = null;
  60. if (!string.IsNullOrEmpty(code) && code.ToLower() == mobileVerify.verifycode.ToLower())
  61. {
  62. //发送短信
  63. try
  64. {
  65. //学生增加短信发送验证
  66. string raRndNum = Learun.Util.CommonHelper.RndNum(6);
  67. var listStr = new List<string>();
  68. var str1 = $"您本次提交的验证码是" + raRndNum + "。";
  69. listStr.Add(str1);
  70. var result = yixintongSms.SendSmsToSingle(mobileVerify.mobile, SmsType.LoginBind, listStr);
  71. if (result.Result.code == "0")
  72. {
  73. redisCache.Write<string>("sunshinesmscode_"+mobileVerify.pagecode +"_"+ mobileVerify.mobile, raRndNum, new TimeSpan(0, 5, 0));
  74. redisCache.Remove("sunshineimgvcode_" + mobileVerify.pagecode);
  75. //日志
  76. logEntity = new LogEntity();
  77. logEntity.F_CategoryId = 3;
  78. logEntity.F_OperateTypeId = "sms";
  79. logEntity.F_OperateType = "sms";
  80. logEntity.F_OperateAccount = "system";
  81. logEntity.F_ExecuteResult = 200;
  82. logEntity.F_ExecuteResultJson = "短信发送成功:" + result.Result.message;
  83. logEntity.F_Description = "短信发送:" + mobileVerify.mobile;
  84. logEntity.WriteLog();
  85. }
  86. else
  87. {
  88. logEntity = new LogEntity();
  89. logEntity.F_CategoryId = 4;
  90. logEntity.F_OperateTypeId = "sms";
  91. logEntity.F_OperateType = "sms";
  92. logEntity.F_OperateAccount = "system";
  93. logEntity.F_ExecuteResult = 400;
  94. logEntity.F_ExecuteResultJson = "短信发送失败:" + result.Result.message + result.Result.errorType;
  95. logEntity.F_Description = "短信发送:" + mobileVerify.mobile;
  96. logEntity.WriteLog();
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. logEntity = new LogEntity();
  102. logEntity.F_CategoryId = 4;
  103. logEntity.F_OperateTypeId = "sms";
  104. logEntity.F_OperateType = "sms";
  105. logEntity.F_OperateAccount = "system";
  106. logEntity.F_ExecuteResult = 400;
  107. logEntity.F_ExecuteResultJson = "短信发送失败:" + e.Message;
  108. logEntity.F_Description = "短信发送:" + mobileVerify.mobile;
  109. logEntity.WriteLog();
  110. }
  111. return Success("短信验证码已成功发送。");
  112. }
  113. else
  114. {
  115. return Fail("验证失败,验证码错误或已失效。");
  116. }
  117. }
  118. /// <summary>
  119. /// 获取图形验证码
  120. /// </summary>
  121. /// <param name="_">页面编号</param>
  122. /// <returns></returns>
  123. public Response GetVerifyCode(dynamic _)
  124. {
  125. string pagecode = this.GetReqData();
  126. return new StreamResponse(() => GetImgVerifyCode(pagecode), "image/Gif");
  127. }
  128. public Stream GetImgVerifyCode(string pagecode)
  129. {
  130. MemoryStream ms = null;
  131. try
  132. {
  133. (byte[] buffer,string code) = new VerifyCode().GetVerifyCodeForMobileApi();
  134. redisCache.Write<string>("sunshineimgvcode_" + pagecode, code, new TimeSpan(0, 30, 0));
  135. ms = new MemoryStream(buffer);
  136. return ms;
  137. }
  138. catch (Exception e)
  139. {
  140. return null;
  141. }
  142. //finally
  143. //{
  144. // if (ms != null)
  145. // {
  146. // ms.Close();
  147. // ms.Dispose();
  148. // }
  149. //}
  150. }
  151. /// <summary>
  152. /// 获取页面显示列表分页数据
  153. /// <summary>
  154. /// <param name="_"></param>
  155. /// <returns></returns>
  156. public Response GetPageList(dynamic _)
  157. {
  158. ReqPageParam parameter = this.GetReqData<ReqPageParam>();
  159. var data = sunshineEducationIBLL.GetPageList(parameter.pagination, parameter.queryJson);
  160. var jsonData = new
  161. {
  162. rows = data,
  163. total = parameter.pagination.total,
  164. page = parameter.pagination.page,
  165. records = parameter.pagination.records
  166. };
  167. return Success(jsonData);
  168. }
  169. /// <summary>
  170. /// 获取页面显示列表数据
  171. /// <summary>
  172. /// <param name="_"></param>
  173. /// <returns></returns>
  174. public Response GetList(dynamic _)
  175. {
  176. string queryJson = this.GetReqData();
  177. var data = sunshineEducationIBLL.GetList(queryJson);
  178. return Success(data);
  179. }
  180. /// <summary>
  181. /// 获取表单数据
  182. /// <summary>
  183. /// <param name="_"></param>
  184. /// <returns></returns>
  185. public Response GetForm(dynamic _)
  186. {
  187. string keyValue = this.GetReqData();
  188. var sunshineEducationData = sunshineEducationIBLL.GetEntity(keyValue);
  189. sunshineEducationData.Contents = WebHelper.HtmlDecode(sunshineEducationData.Contents);
  190. var jsonData = new
  191. {
  192. sunshineEducation = sunshineEducationData,
  193. };
  194. return Success(jsonData);
  195. }
  196. public Response StatisticsForm(dynamic _)
  197. {
  198. var data = sunshineEducationIBLL.GetAllList();
  199. var zsNum = data.Count();
  200. var yclNum = data.Count(x => x.Status == 2);
  201. var slzNum = data.Count(x => x.Status == 1);
  202. var wclNum = data.Count(x => x.Status == 0);
  203. return Success(new { zsNum, yclNum, wclNum,slzNum });
  204. }
  205. #endregion
  206. #region 提交数据
  207. ///// <summary>
  208. ///// 删除实体数据
  209. ///// <param name="_"></param>
  210. ///// <summary>
  211. ///// <returns></returns>
  212. //public Response DeleteForm(dynamic _)
  213. //{
  214. // string keyValue = this.GetReqData();
  215. // sunshineEducationIBLL.DeleteEntity(keyValue);
  216. // return Success("删除成功!");
  217. //}
  218. /// <summary>
  219. /// 保存实体数据(新增、修改)
  220. /// <param name="_"></param>
  221. /// <summary>
  222. /// <returns></returns>
  223. public Response SaveForm(dynamic _)
  224. {
  225. ReqFormEntity parameter = this.GetReqData<ReqFormEntity>();
  226. SunshineEducationEntity entity = parameter.strEntity.ToObject<SunshineEducationEntity>();
  227. if (!string.IsNullOrEmpty(parameter.keyValue))
  228. {
  229. var data = sunshineEducationIBLL.GetEntity(parameter.keyValue);
  230. if (data.AcceptanceCode != entity.AcceptanceCode)
  231. {
  232. return Fail("评论失败,受理单号不正确");
  233. }
  234. entity.Contents = data.Contents;
  235. sunshineEducationIBLL.SaveEntity(parameter.keyValue, entity);
  236. return Success("提交成功!");
  237. }
  238. else
  239. {
  240. entity.Contents = WebHelper.HtmlEncode(entity.Contents);
  241. var code = redisCache.Read<string>("sunshinesmscode_" +entity.pagecode+"_" + entity.mobile);
  242. if (entity.smscode== code)
  243. {
  244. sunshineEducationIBLL.SaveEntity(parameter.keyValue, entity);
  245. return Success("提交成功!");
  246. }
  247. else
  248. {
  249. return Fail("验证失败,验证码错误或已失效。");
  250. }
  251. }
  252. }
  253. /// <summary>
  254. /// 点击累计
  255. /// <param name="_"></param>
  256. /// <summary>
  257. /// <returns></returns>
  258. public Response ClicksForm(dynamic _)
  259. {
  260. string keyValue = this.GetReqData();
  261. sunshineEducationIBLL.ClicksEntity(keyValue);
  262. return Success("点击数据!");
  263. }
  264. #endregion
  265. #region 上传附件图片文件
  266. /// <summary>
  267. /// 上传附件图片文件
  268. /// <summary>
  269. /// <returns></returns>
  270. public Response Upload(dynamic _)
  271. {
  272. var files = (List<HttpFile>)this.Context.Request.Files;
  273. string folderId = Guid.NewGuid().ToString();
  274. string filePath = Config.GetValue("AnnexesFile");
  275. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  276. string fileEextension = Path.GetExtension(files[0].Name);
  277. string fileType = fileEextension.Replace(".", "");
  278. string fileGuid = Guid.NewGuid().ToString();
  279. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, "system", uploadDate, fileGuid, fileEextension);
  280. //创建文件夹
  281. string path = Path.GetDirectoryName(virtualPath);
  282. Directory.CreateDirectory(path);
  283. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  284. if (!System.IO.File.Exists(virtualPath))
  285. {
  286. byte[] bytes = new byte[files[0].Value.Length];
  287. files[0].Value.Read(bytes, 0, bytes.Length);
  288. FileInfo file = new FileInfo(virtualPath);
  289. FileStream fs = file.Create();
  290. fs.Write(bytes, 0, bytes.Length);
  291. fs.Close();
  292. //文件信息写入数据库
  293. fileAnnexesEntity.F_Id = fileGuid;
  294. fileAnnexesEntity.F_FileName = files[0].Name;
  295. fileAnnexesEntity.F_FilePath = virtualPath;
  296. fileAnnexesEntity.F_FileSize = files[0].Value.Length.ToString();
  297. fileAnnexesEntity.F_FileExtensions = fileEextension;
  298. fileAnnexesEntity.F_FileType = fileType;
  299. fileAnnexesEntity.F_CreateUserId = "system";
  300. fileAnnexesEntity.F_CreateUserName = "system";
  301. annexesFileIBLL.SaveEntity(folderId, fileAnnexesEntity);
  302. }
  303. return SuccessString(folderId);
  304. }
  305. /// <summary>
  306. /// 删除文件
  307. /// </summary>
  308. /// <param name="_"></param>
  309. /// <returns></returns>
  310. public Response DeleteFile(dynamic _)
  311. {
  312. var fileId = this.GetReqData();
  313. AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId);
  314. annexesFileIBLL.DeleteEntity(fileId);
  315. //删除文件
  316. if (System.IO.File.Exists(fileInfoEntity.F_FilePath))
  317. {
  318. System.IO.File.Delete(fileInfoEntity.F_FilePath);
  319. }
  320. return Success("删除成功");
  321. }
  322. /// <summary>
  323. /// 下载文件
  324. /// </summary>
  325. /// <param name="_"></param>
  326. /// <returns></returns>
  327. public Response DownAnnexesFile(dynamic _)
  328. {
  329. string name = this.GetReqData();
  330. string fileId = name.Split('.')[0];
  331. var data = annexesFileIBLL.GetEntity(fileId);
  332. string filepath = data.F_FilePath;
  333. if (FileDownHelper.FileExists(filepath))
  334. {
  335. FileDownHelper.DownLoadnew(filepath);
  336. }
  337. return Success("");
  338. }
  339. #endregion
  340. #region 私有类
  341. /// <summary>
  342. /// 表单实体类
  343. /// <summary>
  344. private class ReqFormEntity
  345. {
  346. public string keyValue { get; set; }
  347. public string strEntity { get; set; }
  348. }
  349. public class MobileVerify
  350. {
  351. public string mobile { get; set; }
  352. public string verifycode { get; set; }
  353. public string pagecode { get;set; }
  354. }
  355. #endregion
  356. }
  357. }