Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

226 linhas
7.5 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 BaseNoAuthentication : NancyModule
  18. {
  19. #region 构造函数
  20. public BaseNoAuthentication()
  21. : base()
  22. {
  23. OnError += OnErroe;
  24. }
  25. public BaseNoAuthentication(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. ResParameter res = new ResParameter { code = ResponseCode.success, info = info, data = 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(object data)
  101. {
  102. ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = 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. ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data };
  114. return Response.AsText(res.ToJson()).WithContentType("application/json");
  115. }
  116. /// <summary>
  117. /// 成功响应数据
  118. /// </summary>
  119. /// <typeparam name="T"></typeparam>
  120. /// <param name="res"></param>
  121. /// <returns></returns>
  122. public Response SuccessString(string data)
  123. {
  124. ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data };
  125. return Response.AsText(res.ToJson()).WithContentType("application/json");
  126. }
  127. /// <summary>
  128. /// 接口响应失败
  129. /// </summary>
  130. /// <param name="msg"></param>
  131. /// <returns></returns>
  132. public Response Fail(string info)
  133. {
  134. ResParameter res = new ResParameter { code = ResponseCode.fail, info = info, data = new object { } };
  135. return Response.AsText(res.ToJson()).WithContentType("application/json");
  136. }
  137. public Response FailNoLogin(string info)
  138. {
  139. ResParameter res = new ResParameter { code = ResponseCode.nologin, info = info, data = new object { } };
  140. return Response.AsText(res.ToJson()).WithContentType("application/json");
  141. }
  142. #endregion
  143. #region 异常抓取
  144. /// <summary>
  145. /// 日志对象实体
  146. /// </summary>
  147. private Log _logger;
  148. /// <summary>
  149. /// 日志操作
  150. /// </summary>
  151. public Log Logger
  152. {
  153. get { return _logger ?? (_logger = LogFactory.GetLogger(this.GetType().ToString())); }
  154. }
  155. /// <summary>
  156. /// 监听接口异常
  157. /// </summary>
  158. /// <param name="ctx">连接上下信息</param>
  159. /// <param name="ex">异常信息</param>
  160. /// <returns></returns>
  161. private Response OnErroe(NancyContext ctx, Exception ex)
  162. {
  163. try
  164. {
  165. this.WriteLog(ctx, ex);
  166. }
  167. catch (Exception)
  168. {
  169. }
  170. string msg = "提示:" + ex.Message;
  171. return Response.AsText(new ResParameter { code = ResponseCode.exception, info = msg }.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK);
  172. }
  173. /// <summary>
  174. /// 写入日志(log4net)
  175. /// </summary>
  176. /// <param name="context">提供使用</param>
  177. private void WriteLog(NancyContext context, Exception ex)
  178. {
  179. if (context == null)
  180. return;
  181. string path = context.ResolvedRoute.Description.Path;
  182. var log = LogFactory.GetLogger("workflowapi");
  183. Exception Error = ex;
  184. LogMessage logMessage = new LogMessage();
  185. logMessage.OperationTime = DateTime.Now;
  186. logMessage.Url = path;
  187. logMessage.Class = "learunwebapi";
  188. logMessage.Ip = Net.Ip;
  189. logMessage.Host = Net.Host;
  190. logMessage.Browser = Net.Browser;
  191. if (Error.InnerException == null)
  192. {
  193. logMessage.ExceptionInfo = Error.Message;
  194. logMessage.ExceptionSource = Error.Source;
  195. logMessage.ExceptionRemark = Error.StackTrace;
  196. }
  197. else
  198. {
  199. logMessage.ExceptionInfo = Error.InnerException.Message;
  200. logMessage.ExceptionSource = Error.InnerException.Source;
  201. logMessage.ExceptionRemark = Error.InnerException.StackTrace;
  202. }
  203. string strMessage = new LogFormat().ExceptionFormat(logMessage);
  204. log.Error(strMessage);
  205. LogEntity logEntity = new LogEntity();
  206. logEntity.F_CategoryId = 4;
  207. logEntity.F_OperateTypeId = ((int)OperationType.Exception).ToString();
  208. logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Exception);
  209. logEntity.F_OperateAccount = logMessage.UserName;
  210. logEntity.F_ExecuteResult = -1;
  211. logEntity.F_ExecuteResultJson = strMessage;
  212. logEntity.F_Description = "移动端";
  213. logEntity.WriteLog();
  214. }
  215. #endregion
  216. }
  217. }