Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

248 wiersze
8.2 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.WorkFlowServer
  9. {
  10. /// <summary>
  11. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  12. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  13. /// 创建人:陈彬彬
  14. /// 日 期:2017.05.12
  15. /// 描 述:Nancy-Api基础模块
  16. /// </summary>
  17. public class BaseApi : NancyModule
  18. {
  19. #region 构造函数
  20. public BaseApi()
  21. : base()
  22. {
  23. Before += BeforeRequest;
  24. OnError += OnErroe;
  25. }
  26. public BaseApi(string baseUrl)
  27. : base(baseUrl)
  28. {
  29. Before += BeforeRequest;
  30. OnError += OnErroe;
  31. }
  32. #endregion
  33. #region 获取请求数据
  34. /// <summary>
  35. /// 获取请求数据
  36. /// </summary>
  37. /// <typeparam name="T"></typeparam>
  38. /// <returns></returns>
  39. public T GetReqData<T>() where T : class
  40. {
  41. try
  42. {
  43. ReqParameter<string> req = this.Bind<ReqParameter<string>>();
  44. return req.data.ToObject<T>();
  45. }
  46. catch (Exception)
  47. {
  48. throw;
  49. }
  50. }
  51. /// <summary>
  52. /// 获取请求数据
  53. /// </summary>
  54. /// <typeparam name="T"></typeparam>
  55. /// <returns></returns>
  56. public T GetReq<T>() where T : class
  57. {
  58. try
  59. {
  60. T req = this.Bind<T>();
  61. return req;
  62. }
  63. catch (Exception)
  64. {
  65. throw;
  66. }
  67. }
  68. #endregion
  69. #region 响应接口
  70. /// <summary>
  71. /// 成功响应数据
  72. /// </summary>
  73. /// <param name="msg"></param>
  74. /// <returns></returns>
  75. public Response Success(string info)
  76. {
  77. ResParameter res = new ResParameter { code = ResponseCode.success, info = info, data = new object { } };
  78. return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK);
  79. }
  80. /// <summary>
  81. /// 成功响应数据
  82. /// </summary>
  83. /// <param name="res"></param>
  84. /// <returns></returns>
  85. public Response Success(object data)
  86. {
  87. ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data };
  88. return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK);
  89. }
  90. /// <summary>
  91. /// 成功响应数据
  92. /// </summary>
  93. /// <typeparam name="T"></typeparam>
  94. /// <param name="res"></param>
  95. /// <returns></returns>
  96. public Response Success<T>(T data) where T : class
  97. {
  98. ResParameter res = new ResParameter { code = ResponseCode.success, info = "响应成功", data = data };
  99. return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK);
  100. }
  101. /// <summary>
  102. /// 接口响应失败
  103. /// </summary>
  104. /// <param name="msg"></param>
  105. /// <returns></returns>
  106. public Response Fail(string info)
  107. {
  108. ResParameter res = new ResParameter { code = ResponseCode.fail, info = info, data = new object { } };
  109. return Response.AsText(res.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK);
  110. }
  111. #endregion
  112. #region 权限验证
  113. public UserInfo userInfo;
  114. /// <summary>
  115. /// 验证登录票据
  116. /// </summary>
  117. /// <param name="ctx">上下文数据</param>
  118. /// <returns></returns>
  119. private Response BeforeRequest(NancyContext ctx)
  120. {
  121. //验证登录状态
  122. ReqParameter req = this.Bind<ReqParameter>();
  123. OperatorResult res = OperatorHelper.Instance.IsOnLine(req.token, req.loginMark);
  124. if (res.stateCode == -1)
  125. {
  126. return this.Fail("未找到登录信息");
  127. }
  128. if (res.stateCode == 0)
  129. {
  130. return this.Fail("登录信息已过期");
  131. }
  132. else
  133. {
  134. // 获取登录者信息
  135. userInfo = res.userInfo;
  136. }
  137. return null;
  138. }
  139. #endregion
  140. #region 异常抓取
  141. /// <summary>
  142. /// 日志对象实体
  143. /// </summary>
  144. private Log _logger;
  145. /// <summary>
  146. /// 日志操作
  147. /// </summary>
  148. public Log Logger
  149. {
  150. get { return _logger ?? (_logger = LogFactory.GetLogger(this.GetType().ToString())); }
  151. }
  152. /// <summary>
  153. /// 监听接口异常
  154. /// </summary>
  155. /// <param name="ctx">连接上下信息</param>
  156. /// <param name="ex">异常信息</param>
  157. /// <returns></returns>
  158. private Response OnErroe(NancyContext ctx, Exception ex)
  159. {
  160. try
  161. {
  162. this.WriteLog(ctx, ex);
  163. }
  164. catch (Exception)
  165. {
  166. }
  167. string msg = "智慧校园提醒您:" + ex.Message;
  168. return Response.AsText(new ResParameter { code = ResponseCode.exception, info = msg }.ToJson()).WithContentType("application/json").WithStatusCode(HttpStatusCode.OK);
  169. }
  170. /// <summary>
  171. /// 写入日志(log4net)
  172. /// </summary>
  173. /// <param name="context">提供使用</param>
  174. private void WriteLog(NancyContext context, Exception ex)
  175. {
  176. if (context == null)
  177. return;
  178. string path = context.ResolvedRoute.Description.Path;
  179. var userInfo = LoginUserInfo.Get();
  180. var log = LogFactory.GetLogger("workflowapi");
  181. Exception Error = ex;
  182. LogMessage logMessage = new LogMessage();
  183. logMessage.OperationTime = DateTime.Now;
  184. logMessage.Url = path;
  185. logMessage.Class = "workflowapi";
  186. logMessage.Ip = Net.Ip;
  187. logMessage.Host = Net.Host;
  188. logMessage.Browser = Net.Browser;
  189. if (userInfo != null)
  190. {
  191. logMessage.UserName = userInfo.account + "(" + userInfo.realName + ")";
  192. }
  193. if (Error.InnerException == null)
  194. {
  195. logMessage.ExceptionInfo = Error.Message;
  196. }
  197. else
  198. {
  199. logMessage.ExceptionInfo = Error.InnerException.Message;
  200. }
  201. logMessage.ExceptionSource = Error.Source;
  202. logMessage.ExceptionRemark = Error.StackTrace;
  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. if (userInfo != null)
  211. {
  212. logEntity.F_OperateUserId = userInfo.userId;
  213. }
  214. logEntity.F_ExecuteResult = -1;
  215. logEntity.F_ExecuteResultJson = strMessage;
  216. logEntity.F_Description = "PC端";
  217. logEntity.WriteLog();
  218. SendMail(strMessage);
  219. }
  220. /// <summary>
  221. /// 发送邮件
  222. /// </summary>
  223. /// <param name="body">消息</param>
  224. private void SendMail(string body)
  225. {
  226. bool ErrorToMail = Config.GetValue("ErrorToMail").ToBool();
  227. if (ErrorToMail == true)
  228. {
  229. string SystemName = Config.GetValue("SystemName");//系统名称
  230. string recMail = Config.GetValue("RereceiveErrorMail");//接收错误信息邮箱
  231. MailHelper.Send("receivebug@learun.cn", SystemName + " - 发生异常", body.Replace("-", ""));
  232. }
  233. }
  234. #endregion
  235. }
  236. }