You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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