using Nancy; using Learun.Util; using System.Collections.Generic; using Learun.Application.TwoDevelopment.EducationalAdministration; using System; using Learun.Application.Base.SystemModule; using System.IO; using System.Web.Mvc; using Learun.Application.WebApi.Modules; using System.Linq; using System.Net; using Learun.Cache.Base; using Learun.Cache.Factory; using Nancy.Responses; using Quanjiang.DigitalScholl.SendSms; namespace Learun.Application.WebApi { /// /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创 建:超级管理员 /// 日 期:2020-06-03 14:29 /// 描 述:听课记录 /// public class SunshineEducationApi : BaseNoLoginApi { private SunshineEducationIBLL sunshineEducationIBLL = new SunshineEducationBLL(); private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL(); private ICache redisCache = CacheFactory.CaChe(); private readonly ISms yixintongSms = new YixintongSms(); /// /// 阳光教育 /// public SunshineEducationApi() : base("/Learun/adms/SunshineEducation") { Get["/pagelist"] = GetPageList; Get["/list"] = GetList; Get["/form"] = GetForm; Get["/statistics"] = StatisticsForm; //Post["/delete"] = DeleteForm; Get["/down"] = DownAnnexesFile; Post["/upload"] = Upload; Post["/save"] = SaveForm;//提交,回复,评论 Post["/clicks"] = ClicksForm; Get["/getverifycode"] = GetVerifyCode; Post["/checkverifycode"] = CheckVerifyCode; } #region 获取数据 /// /// 检查图形验证码并发送短信验证码 /// /// /// public Response CheckVerifyCode(dynamic _) { MobileVerify mobileVerify = this.GetReqData(); var code = redisCache.Read("sunshineimgvcode_" + mobileVerify.pagecode); LogEntity logEntity = null; if (!string.IsNullOrEmpty(code) && code.ToLower() == mobileVerify.verifycode.ToLower()) { //发送短信 try { //学生增加短信发送验证 string raRndNum = Learun.Util.CommonHelper.RndNum(6); var listStr = new List(); var str1 = $"您本次提交的验证码是" + raRndNum + "。"; listStr.Add(str1); var result = yixintongSms.SendSmsToSingle(mobileVerify.mobile, SmsType.LoginBind, listStr); if (result.Result.code == "0") { redisCache.Write("sunshinesmscode_"+mobileVerify.pagecode +"_"+ mobileVerify.mobile, raRndNum, new TimeSpan(0, 5, 0)); //日志 logEntity = new LogEntity(); logEntity.F_CategoryId = 3; logEntity.F_OperateTypeId = "sms"; logEntity.F_OperateType = "sms"; logEntity.F_OperateAccount = "system"; logEntity.F_ExecuteResult = 200; logEntity.F_ExecuteResultJson = "短信发送成功:" + result.Result.message; logEntity.F_Description = "短信发送:" + mobileVerify.mobile; logEntity.WriteLog(); } else { logEntity = new LogEntity(); logEntity.F_CategoryId = 4; logEntity.F_OperateTypeId = "sms"; logEntity.F_OperateType = "sms"; logEntity.F_OperateAccount = "system"; logEntity.F_ExecuteResult = 400; logEntity.F_ExecuteResultJson = "短信发送失败:" + result.Result.message + result.Result.errorType; logEntity.F_Description = "短信发送:" + mobileVerify.mobile; logEntity.WriteLog(); } } catch (Exception e) { logEntity = new LogEntity(); logEntity.F_CategoryId = 4; logEntity.F_OperateTypeId = "sms"; logEntity.F_OperateType = "sms"; logEntity.F_OperateAccount = "system"; logEntity.F_ExecuteResult = 400; logEntity.F_ExecuteResultJson = "短信发送失败:" + e.Message; logEntity.F_Description = "短信发送:" + mobileVerify.mobile; logEntity.WriteLog(); } return Success("短信验证码已成功发送。"); } else { return Fail("验证失败,验证码错误或已失效。"); } } /// /// 获取图形验证码 /// /// 页面编号 /// public Response GetVerifyCode(dynamic _) { string pagecode = this.GetReqData(); return new StreamResponse(() => GetImgVerifyCode(pagecode), "image/Gif"); } public Stream GetImgVerifyCode(string pagecode) { MemoryStream ms = null; try { (byte[] buffer,string code) = new VerifyCode().GetVerifyCodeForMobileApi(); redisCache.Write("sunshineimgvcode_" + pagecode, code, new TimeSpan(0, 30, 0)); ms = new MemoryStream(buffer); return ms; } catch (Exception e) { return null; } //finally //{ // if (ms != null) // { // ms.Close(); // ms.Dispose(); // } //} } /// /// 获取页面显示列表分页数据 /// /// /// public Response GetPageList(dynamic _) { ReqPageParam parameter = this.GetReqData(); var data = sunshineEducationIBLL.GetPageList(parameter.pagination, parameter.queryJson); var jsonData = new { rows = data, total = parameter.pagination.total, page = parameter.pagination.page, records = parameter.pagination.records }; return Success(jsonData); } /// /// 获取页面显示列表数据 /// /// /// public Response GetList(dynamic _) { string queryJson = this.GetReqData(); var data = sunshineEducationIBLL.GetList(queryJson); return Success(data); } /// /// 获取表单数据 /// /// /// public Response GetForm(dynamic _) { string keyValue = this.GetReqData(); var sunshineEducationData = sunshineEducationIBLL.GetEntity(keyValue); sunshineEducationData.Contents = WebHelper.HtmlDecode(sunshineEducationData.Contents); var jsonData = new { sunshineEducation = sunshineEducationData, }; return Success(jsonData); } public Response StatisticsForm(dynamic _) { var data = sunshineEducationIBLL.GetAllList(); var zsNum = data.Count(); var yclNum = data.Count(x => x.Status == 2); var slzNum = data.Count(x => x.Status == 1); var wclNum = data.Count(x => x.Status == 0); return Success(new { zsNum, yclNum, wclNum,slzNum }); } #endregion #region 提交数据 ///// ///// 删除实体数据 ///// ///// ///// //public Response DeleteForm(dynamic _) //{ // string keyValue = this.GetReqData(); // sunshineEducationIBLL.DeleteEntity(keyValue); // return Success("删除成功!"); //} /// /// 保存实体数据(新增、修改) /// /// /// public Response SaveForm(dynamic _) { ReqFormEntity parameter = this.GetReqData(); SunshineEducationEntity entity = parameter.strEntity.ToObject(); if (!string.IsNullOrEmpty(parameter.keyValue)) { var data = sunshineEducationIBLL.GetEntity(parameter.keyValue); if (data.AcceptanceCode != entity.AcceptanceCode) { return Fail("评论失败,受理单号不正确"); } entity.Contents = data.Contents; sunshineEducationIBLL.SaveEntity(parameter.keyValue, entity); return Success("提交成功!"); } else { entity.Contents = WebHelper.HtmlEncode(entity.Contents); var code = redisCache.Read("sunshinesmscode_" +entity.pagecode+"_" + entity.mobile); if (entity.smscode== code) { sunshineEducationIBLL.SaveEntity(parameter.keyValue, entity); return Success("提交成功!"); } else { return Fail("验证失败,验证码错误或已失效。"); } } } /// /// 点击累计 /// /// /// public Response ClicksForm(dynamic _) { string keyValue = this.GetReqData(); sunshineEducationIBLL.ClicksEntity(keyValue); return Success("点击数据!"); } #endregion #region 上传附件图片文件 /// /// 上传附件图片文件 /// /// public Response Upload(dynamic _) { var files = (List)this.Context.Request.Files; string folderId = Guid.NewGuid().ToString(); string filePath = Config.GetValue("AnnexesFile"); string uploadDate = DateTime.Now.ToString("yyyyMMdd"); string fileEextension = Path.GetExtension(files[0].Name); string fileType = fileEextension.Replace(".", ""); string fileGuid = Guid.NewGuid().ToString(); string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, "system", uploadDate, fileGuid, fileEextension); //创建文件夹 string path = Path.GetDirectoryName(virtualPath); Directory.CreateDirectory(path); AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity(); if (!System.IO.File.Exists(virtualPath)) { byte[] bytes = new byte[files[0].Value.Length]; files[0].Value.Read(bytes, 0, bytes.Length); FileInfo file = new FileInfo(virtualPath); FileStream fs = file.Create(); fs.Write(bytes, 0, bytes.Length); fs.Close(); //文件信息写入数据库 fileAnnexesEntity.F_Id = fileGuid; fileAnnexesEntity.F_FileName = files[0].Name; fileAnnexesEntity.F_FilePath = virtualPath; fileAnnexesEntity.F_FileSize = files[0].Value.Length.ToString(); fileAnnexesEntity.F_FileExtensions = fileEextension; fileAnnexesEntity.F_FileType = fileType; fileAnnexesEntity.F_CreateUserId = "system"; fileAnnexesEntity.F_CreateUserName = "system"; annexesFileIBLL.SaveEntity(folderId, fileAnnexesEntity); } return SuccessString(folderId); } /// /// 删除文件 /// /// /// public Response DeleteFile(dynamic _) { var fileId = this.GetReqData(); AnnexesFileEntity fileInfoEntity = annexesFileIBLL.GetEntity(fileId); annexesFileIBLL.DeleteEntity(fileId); //删除文件 if (System.IO.File.Exists(fileInfoEntity.F_FilePath)) { System.IO.File.Delete(fileInfoEntity.F_FilePath); } return Success("删除成功"); } /// /// 下载文件 /// /// /// public Response DownAnnexesFile(dynamic _) { string name = this.GetReqData(); string fileId = name.Split('.')[0]; var data = annexesFileIBLL.GetEntity(fileId); string filepath = data.F_FilePath; if (FileDownHelper.FileExists(filepath)) { FileDownHelper.DownLoadnew(filepath); } return Success(""); } #endregion #region 私有类 /// /// 表单实体类 /// private class ReqFormEntity { public string keyValue { get; set; } public string strEntity { get; set; } } public class MobileVerify { public string mobile { get; set; } public string verifycode { get; set; } public string pagecode { get;set; } } #endregion } }