Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

231 righe
7.6 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Loger;
  3. using Learun.Util;
  4. using Learun.Util.Operat;
  5. using Nancy;
  6. using Nancy.ModelBinding;
  7. using System;
  8. namespace Learun.Application.WebApi
  9. {
  10. /// <summary>
  11. /// 版 本 Learun-ADMS V7.0.0 数字化智慧校园
  12. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  13. /// 创建人:数字化智慧校园-框架开发组
  14. /// 日 期:2017.05.12
  15. /// 描 述:Nancy-Api基础模块
  16. /// </summary>
  17. public class FeixingNoAuthentication : NancyModule
  18. {
  19. #region 构造函数
  20. public FeixingNoAuthentication()
  21. : base()
  22. {
  23. OnError += OnErroe;
  24. }
  25. public FeixingNoAuthentication(string baseUrl)
  26. : base(baseUrl)
  27. {
  28. OnError += OnErroe;
  29. }
  30. #endregion
  31. #region 获取请求数据
  32. /// <summary>
  33. /// 获取请求数据
  34. /// </summary>
  35. /// <typeparam name="T"></typeparam>
  36. /// <returns></returns>
  37. public T GetReqData<T>() where T : class
  38. {
  39. try
  40. {
  41. ReqParameter<string> req = this.Bind<ReqParameter<string>>();
  42. return req.data.ToObject<T>();
  43. }
  44. catch (Exception)
  45. {
  46. throw;
  47. }
  48. }
  49. /// <summary>
  50. /// 获取请求数据
  51. /// </summary>
  52. /// <returns></returns>
  53. public string GetReqData()
  54. {
  55. try
  56. {
  57. ReqParameter<string> req = this.Bind<ReqParameter<string>>();
  58. return req.data;
  59. }
  60. catch (Exception)
  61. {
  62. throw;
  63. }
  64. }
  65. /// <summary>
  66. /// 获取请求数据
  67. /// </summary>
  68. /// <typeparam name="T"></typeparam>
  69. /// <returns></returns>
  70. public T GetReq<T>() where T : class
  71. {
  72. try
  73. {
  74. T req = this.Bind<T>();
  75. return req;
  76. }
  77. catch (Exception)
  78. {
  79. throw;
  80. }
  81. }
  82. #endregion
  83. #region 响应接口
  84. /// <summary>
  85. /// 成功响应数据
  86. /// </summary>
  87. /// <param name="msg"></param>
  88. /// <returns></returns>
  89. public Response Success(string info)
  90. {
  91. var res = new { type = 1, message = info, resultdata = new object { } };
  92. return Response.AsText(res.ToJson()).WithContentType("application/json");
  93. }
  94. /// <summary>
  95. /// 成功响应数据
  96. /// </summary>
  97. /// <typeparam name="T"></typeparam>
  98. /// <param name="res"></param>
  99. /// <returns></returns>
  100. public Response Success(string message,object data)
  101. {
  102. var res = new { type =1, message, resultdata = data };
  103. return Response.AsText(res.ToJson()).WithContentType("application/json");
  104. }
  105. /// <summary>
  106. /// 成功响应数据
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. /// <param name="res"></param>
  110. /// <returns></returns>
  111. public Response Success<T>(T data) where T : class
  112. {
  113. var res = new { type = 1, message = "响应成功", resultdata = data };
  114. return Response.AsText(res.ToJson()).WithContentType("application/json");
  115. }
  116. public Response Fail<T>(T data) where T : class
  117. {
  118. var res = new { type = 2, message = "响应失败", resultdata = data };
  119. return Response.AsText(res.ToJson()).WithContentType("application/json");
  120. }
  121. /// <summary>
  122. /// 成功响应数据
  123. /// </summary>
  124. /// <typeparam name="T"></typeparam>
  125. /// <param name="res"></param>
  126. /// <returns></returns>
  127. public Response SuccessString(string data)
  128. {
  129. ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data };
  130. return Response.AsText(res.ToJson()).WithContentType("application/json");
  131. }
  132. /// <summary>
  133. /// 接口响应失败
  134. /// </summary>
  135. /// <param name="msg"></param>
  136. /// <returns></returns>
  137. public Response Fail(string info)
  138. {
  139. var res = new { type =2, message = info, resultdata = new object { } };
  140. return Response.AsText(res.ToJson()).WithContentType("application/json");
  141. }
  142. public Response FailNoLogin(string info)
  143. {
  144. ResParameter res = new ResParameter { code = ResponseCode.nologin, info = info, data = new object { } };
  145. return Response.AsText(res.ToJson()).WithContentType("application/json");
  146. }
  147. #endregion
  148. #region 异常抓取
  149. /// <summary>
  150. /// 日志对象实体
  151. /// </summary>
  152. private Log _logger;
  153. /// <summary>
  154. /// 日志操作
  155. /// </summary>
  156. public Log Logger
  157. {
  158. get { return _logger ?? (_logger = LogFactory.GetLogger(this.GetType().ToString())); }
  159. }
  160. /// <summary>
  161. /// 监听接口异常
  162. /// </summary>
  163. /// <param name="ctx">连接上下信息</param>
  164. /// <param name="ex">异常信息</param>
  165. /// <returns></returns>
  166. private Response OnErroe(NancyContext ctx, Exception ex)
  167. {
  168. try
  169. {
  170. this.WriteLog(ctx, ex);
  171. }
  172. catch (Exception)
  173. {
  174. }
  175. string msg = "提示:" + ex.Message;
  176. return Response.AsText(new ResParameter { code = ResponseCode.exception, info = msg }.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK);
  177. }
  178. /// <summary>
  179. /// 写入日志(log4net)
  180. /// </summary>
  181. /// <param name="context">提供使用</param>
  182. private void WriteLog(NancyContext context, Exception ex)
  183. {
  184. if (context == null)
  185. return;
  186. string path = context.ResolvedRoute.Description.Path;
  187. var log = LogFactory.GetLogger("workflowapi");
  188. Exception Error = ex;
  189. LogMessage logMessage = new LogMessage();
  190. logMessage.OperationTime = DateTime.Now;
  191. logMessage.Url = path;
  192. logMessage.Class = "learunwebapi";
  193. logMessage.Ip = Net.Ip;
  194. logMessage.Host = Net.Host;
  195. logMessage.Browser = Net.Browser;
  196. if (Error.InnerException == null)
  197. {
  198. logMessage.ExceptionInfo = Error.Message;
  199. logMessage.ExceptionSource = Error.Source;
  200. logMessage.ExceptionRemark = Error.StackTrace;
  201. }
  202. else
  203. {
  204. logMessage.ExceptionInfo = Error.InnerException.Message;
  205. logMessage.ExceptionSource = Error.InnerException.Source;
  206. logMessage.ExceptionRemark = Error.InnerException.StackTrace;
  207. }
  208. string strMessage = new LogFormat().ExceptionFormat(logMessage);
  209. log.Error(strMessage);
  210. LogEntity logEntity = new LogEntity();
  211. logEntity.F_CategoryId = 4;
  212. logEntity.F_OperateTypeId = ((int)OperationType.Exception).ToString();
  213. logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Exception);
  214. logEntity.F_OperateAccount = logMessage.UserName;
  215. logEntity.F_ExecuteResult = -1;
  216. logEntity.F_ExecuteResultJson = strMessage;
  217. logEntity.F_Description = "移动端";
  218. logEntity.WriteLog();
  219. }
  220. #endregion
  221. }
  222. }