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.WebApi
{
///
/// 版 本 Learun-ADMS V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:数字化智慧校园-框架开发组
/// 日 期:2017.05.12
/// 描 述:Nancy-Api基础模块
///
public class FeixingNoAuthentication : NancyModule
{
#region 构造函数
public FeixingNoAuthentication()
: base()
{
OnError += OnErroe;
}
public FeixingNoAuthentication(string baseUrl)
: base(baseUrl)
{
OnError += OnErroe;
}
#endregion
#region 获取请求数据
///
/// 获取请求数据
///
///
///
public T GetReqData() where T : class
{
try
{
ReqParameter req = this.Bind>();
return req.data.ToObject();
}
catch (Exception)
{
throw;
}
}
///
/// 获取请求数据
///
///
public string GetReqData()
{
try
{
ReqParameter req = this.Bind>();
return req.data;
}
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)
{
var res = new { type = 1, message = info, resultdata = new object { } };
return Response.AsText(res.ToJson()).WithContentType("application/json");
}
///
/// 成功响应数据
///
///
///
///
public Response Success(string message,object data)
{
var res = new { type =1, message, resultdata = data };
return Response.AsText(res.ToJson()).WithContentType("application/json");
}
///
/// 成功响应数据
///
///
///
///
public Response Success(T data) where T : class
{
var res = new { type = 1, message = "响应成功", resultdata = data };
return Response.AsText(res.ToJson()).WithContentType("application/json");
}
public Response Fail(T data) where T : class
{
var res = new { type = 2, message = "响应失败", resultdata = data };
return Response.AsText(res.ToJson()).WithContentType("application/json");
}
///
/// 成功响应数据
///
///
///
///
public Response SuccessString(string data)
{
ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data };
return Response.AsText(res.ToJson()).WithContentType("application/json");
}
///
/// 接口响应失败
///
///
///
public Response Fail(string info)
{
var res = new { type =2, message = info, resultdata = new object { } };
return Response.AsText(res.ToJson()).WithContentType("application/json");
}
public Response FailNoLogin(string info)
{
ResParameter res = new ResParameter { code = ResponseCode.nologin, info = info, data = new object { } };
return Response.AsText(res.ToJson()).WithContentType("application/json");
}
#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 log = LogFactory.GetLogger("workflowapi");
Exception Error = ex;
LogMessage logMessage = new LogMessage();
logMessage.OperationTime = DateTime.Now;
logMessage.Url = path;
logMessage.Class = "learunwebapi";
logMessage.Ip = Net.Ip;
logMessage.Host = Net.Host;
logMessage.Browser = Net.Browser;
if (Error.InnerException == null)
{
logMessage.ExceptionInfo = Error.Message;
logMessage.ExceptionSource = Error.Source;
logMessage.ExceptionRemark = Error.StackTrace;
}
else
{
logMessage.ExceptionInfo = Error.InnerException.Message;
logMessage.ExceptionSource = Error.InnerException.Source;
logMessage.ExceptionRemark = Error.InnerException.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;
logEntity.F_ExecuteResult = -1;
logEntity.F_ExecuteResultJson = strMessage;
logEntity.F_Description = "移动端";
logEntity.WriteLog();
}
#endregion
}
}