|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- 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
- {
- /// <summary>
- /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
- /// Copyright (c) 2013-2018 北京泉江科技有限公司
- /// 创 建:超级管理员
- /// 日 期:2020-06-03 14:29
- /// 描 述:听课记录
- /// </summary>
- 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();
-
- /// <summary>
- /// 阳光教育
- /// <summary>
- 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 获取数据
-
- /// <summary>
- /// 检查图形验证码并发送短信验证码
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- public Response CheckVerifyCode(dynamic _)
- {
- MobileVerify mobileVerify = this.GetReqData<MobileVerify>();
- var code = redisCache.Read<string>("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<string>();
- var str1 = $"您本次提交的验证码是" + raRndNum + "。";
- listStr.Add(str1);
- var result = yixintongSms.SendSmsToSingle(mobileVerify.mobile, SmsType.LoginBind, listStr);
- if (result.Result.code == "0")
- {
- redisCache.Write<string>("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("验证失败,验证码错误或已失效。");
- }
- }
-
- /// <summary>
- /// 获取图形验证码
- /// </summary>
- /// <param name="_">页面编号</param>
- /// <returns></returns>
- 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<string>("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();
- // }
- //}
- }
-
- /// <summary>
- /// 获取页面显示列表分页数据
- /// <summary>
- /// <param name="_"></param>
- /// <returns></returns>
- public Response GetPageList(dynamic _)
- {
- ReqPageParam parameter = this.GetReqData<ReqPageParam>();
- 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);
- }
- /// <summary>
- /// 获取页面显示列表数据
- /// <summary>
- /// <param name="_"></param>
- /// <returns></returns>
- public Response GetList(dynamic _)
- {
- string queryJson = this.GetReqData();
- var data = sunshineEducationIBLL.GetList(queryJson);
- return Success(data);
- }
- /// <summary>
- /// 获取表单数据
- /// <summary>
- /// <param name="_"></param>
- /// <returns></returns>
- 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 提交数据
-
- ///// <summary>
- ///// 删除实体数据
- ///// <param name="_"></param>
- ///// <summary>
- ///// <returns></returns>
- //public Response DeleteForm(dynamic _)
- //{
- // string keyValue = this.GetReqData();
- // sunshineEducationIBLL.DeleteEntity(keyValue);
- // return Success("删除成功!");
- //}
-
- /// <summary>
- /// 保存实体数据(新增、修改)
- /// <param name="_"></param>
- /// <summary>
- /// <returns></returns>
- public Response SaveForm(dynamic _)
- {
- ReqFormEntity parameter = this.GetReqData<ReqFormEntity>();
- SunshineEducationEntity entity = parameter.strEntity.ToObject<SunshineEducationEntity>();
- 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<string>("sunshinesmscode_" +entity.pagecode+"_" + entity.mobile);
- if (entity.smscode== code)
- {
- sunshineEducationIBLL.SaveEntity(parameter.keyValue, entity);
- return Success("提交成功!");
- }
- else
- {
- return Fail("验证失败,验证码错误或已失效。");
- }
- }
- }
-
- /// <summary>
- /// 点击累计
- /// <param name="_"></param>
- /// <summary>
- /// <returns></returns>
- public Response ClicksForm(dynamic _)
- {
- string keyValue = this.GetReqData();
- sunshineEducationIBLL.ClicksEntity(keyValue);
- return Success("点击数据!");
- }
- #endregion
-
- #region 上传附件图片文件
-
- /// <summary>
- /// 上传附件图片文件
- /// <summary>
- /// <returns></returns>
- public Response Upload(dynamic _)
- {
- var files = (List<HttpFile>)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);
- }
-
- /// <summary>
- /// 删除文件
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- 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("删除成功");
- }
-
- /// <summary>
- /// 下载文件
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- 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 私有类
-
- /// <summary>
- /// 表单实体类
- /// <summary>
- 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
-
- }
- }
|