using Learun.Application.Base.SystemModule; using Learun.Loger; using Learun.Util; using Learun.Util.Operat; using Nancy; using Nancy.ModelBinding; using System; namespace Learun.Application.WorkFlowServer { /// /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创建人:陈彬彬 /// 日 期:2017.05.12 /// 描 述:Nancy-Api基础模块 /// public class BaseApi : NancyModule { #region 构造函数 public BaseApi() : base() { Before += BeforeRequest; OnError += OnErroe; } public BaseApi(string baseUrl) : base(baseUrl) { Before += BeforeRequest; OnError += OnErroe; } #endregion #region 获取请求数据 /// /// 获取请求数据 /// /// /// public T GetReqData() where T : class { try { ReqParameter req = this.Bind>(); return req.data.ToObject(); } catch (Exception) { throw; } } /// /// 获取请求数据 /// /// /// public T GetReq() where T : class { try { T req = this.Bind(); return req; } catch (Exception) { throw; } } #endregion #region 响应接口 /// /// 成功响应数据 /// /// /// public Response Success(string info) { ResParameter res = new ResParameter { code = ResponseCode.success, info = info, data = new object { } }; return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK); } /// /// 成功响应数据 /// /// /// public Response Success(object data) { ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data }; return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK); } /// /// 成功响应数据 /// /// /// /// public Response Success(T data) where T : class { ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data }; return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK); } /// /// 接口响应失败 /// /// /// public Response Fail(string info) { ResParameter res = new ResParameter { code = ResponseCode.fail, info = info, data = new object { } }; return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK); } #endregion #region 权限验证 public UserInfo userInfo; /// /// 验证登录票据 /// /// 上下文数据 /// private Response BeforeRequest(NancyContext ctx) { //验证登录状态 ReqParameter req = this.Bind(); OperatorResult res = OperatorHelper.Instance.IsOnLine(req.token, req.loginMark); if (res.stateCode == -1) { return this.Fail("未找到登录信息"); } if (res.stateCode == 0) { return this.Fail("登录信息已过期"); } else { // 获取登录者信息 userInfo = res.userInfo; } return null; } #endregion #region 异常抓取 /// /// 日志对象实体 /// private Log _logger; /// /// 日志操作 /// public Log Logger { get { return _logger ?? (_logger = LogFactory.GetLogger(this.GetType().ToString())); } } /// /// 监听接口异常 /// /// 连接上下信息 /// 异常信息 /// private Response OnErroe(NancyContext ctx, Exception ex) { try { this.WriteLog(ctx, ex); } catch (Exception) { } string msg = "智慧校园提醒您:" + ex.Message; return Response.AsText(new ResParameter { code = ResponseCode.exception, info = msg }.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK); } /// /// 写入日志(log4net) /// /// 提供使用 private void WriteLog(NancyContext context, Exception ex) { if (context == null) return; string path = context.ResolvedRoute.Description.Path; var userInfo = LoginUserInfo.Get(); var log = LogFactory.GetLogger("workflowapi"); Exception Error = ex; LogMessage logMessage = new LogMessage(); logMessage.OperationTime = DateTime.Now; logMessage.Url = path; logMessage.Class = "workflowapi"; logMessage.Ip = Net.Ip; logMessage.Host = Net.Host; logMessage.Browser = Net.Browser; if (userInfo != null) { logMessage.UserName = userInfo.account + "(" + userInfo.realName + ")"; } if (Error.InnerException == null) { logMessage.ExceptionInfo = Error.Message; } else { logMessage.ExceptionInfo = Error.InnerException.Message; } logMessage.ExceptionSource = Error.Source; logMessage.ExceptionRemark = Error.StackTrace; string strMessage = new LogFormat().ExceptionFormat(logMessage); log.Error(strMessage); LogEntity logEntity = new LogEntity(); logEntity.F_CategoryId = 4; logEntity.F_OperateTypeId = ((int)OperationType.Exception).ToString(); logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Exception); logEntity.F_OperateAccount = logMessage.UserName; if (userInfo != null) { logEntity.F_OperateUserId = userInfo.userId; } logEntity.F_ExecuteResult = -1; logEntity.F_ExecuteResultJson = strMessage; logEntity.F_Description = "PC端"; logEntity.WriteLog(); SendMail(strMessage); } /// /// 发送邮件 /// /// 消息 private void SendMail(string body) { bool ErrorToMail = Config.GetValue("ErrorToMail").ToBool(); if (ErrorToMail == true) { string SystemName = Config.GetValue("SystemName");//系统名称 string recMail = Config.GetValue("RereceiveErrorMail");//接收错误信息邮箱 MailHelper.Send("receivebug@learun.cn", SystemName + " - 发生异常", body.Replace("-", "")); } } #endregion } }