Нет описания

Global.asax.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using CallCenter.Utility;
  2. using CallCenterApi.Common;
  3. using CallCenterApi.Interface.Models.Common;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Runtime.CompilerServices;
  12. using System.Web;
  13. using System.Web.Mvc;
  14. using System.Web.Routing;
  15. using System.Web.Script.Serialization;
  16. using System.Web.Security;
  17. namespace CallCenterApi.Interface
  18. {
  19. public class MvcApplication : System.Web.HttpApplication
  20. {
  21. System.Threading.Timer timer;
  22. private static int count = 1;
  23. protected void Application_Start()
  24. {
  25. AreaRegistration.RegisterAllAreas();
  26. RouteConfig.RegisterRoutes(RouteTable.Routes);
  27. //clq 增加异常日志记录 自定义 HandleErrorAttribute
  28. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  29. timer = new System.Threading.Timer(SetCensusURL, null, 0, 1000 * 60);
  30. }
  31. public void SetCensusURL(object obj)
  32. {
  33. var modellist = new BLL.T_Bus_WorkOrder().GetModelList("F_IsDelete=0 and F_State in(1,3,5)");
  34. if (modellist!=null )
  35. {
  36. foreach (var it in modellist)
  37. {
  38. var modellast = new BLL.T_Wo_WorkOrderItem_New().GetModelList("F_WoID = " + it.F_Id + "and F_IsUsed =0 order by F_ID desc").FirstOrDefault ();
  39. System.TimeSpan id = DateTime.Now - DateTime.Parse(modellast.F_CreateTime.ToString () );
  40. if (id.TotalMinutes <120)
  41. {
  42. continue;
  43. }
  44. else
  45. {
  46. try
  47. {
  48. int a = (int)modellast.F_IsSMS ;
  49. if (id.TotalMinutes >= 120 + a*10 && id.TotalMinutes < 130 +a * 10)
  50. {
  51. bool n = SendSMS(modellast);
  52. if (n)
  53. AddLog(modellast, a + 1);
  54. }
  55. else
  56. continue;
  57. }
  58. catch
  59. {
  60. bool n = SendSMS(modellast);
  61. if (n)
  62. AddLog(modellast, 1);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. private bool SendSMS(Model .T_Wo_WorkOrderItem_New modellast)
  69. {
  70. if (!string.IsNullOrEmpty(modellast.F_NextUser))
  71. {
  72. var usercount = new BLL.T_Sys_UserAccount().GetModel(modellast.F_NextUser);
  73. if (usercount != null)
  74. {
  75. if (!string.IsNullOrEmpty(usercount.F_Mobile))
  76. {
  77. string msg = "你有新的工单,请及时处理";
  78. Dictionary<string, string> paras = new Dictionary<string, string>();
  79. paras.Add("@JSHM", usercount.F_Mobile);
  80. paras.Add("@DXNR", msg);
  81. paras.Add("@Return", "0");
  82. var par = RunProcedure("sp_SmsSend", paras, "sp_SmsSend");
  83. if (par != null)
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. public static string connectionString = ConfigurationManager.ConnectionStrings["connectionSMS"].ConnectionString;
  91. /// <summary>
  92. /// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
  93. /// </summary>
  94. /// <param name="storedProcName">存储过程名</param>
  95. /// <param name="parameters">存储过程参数</param>
  96. /// <returns>SqlDataReader</returns>
  97. public static DataSet RunProcedure(string storedProcName, Dictionary<String, String> paras, string tableName)
  98. {
  99. List<SqlParameter> ps = new List<SqlParameter>();
  100. foreach (KeyValuePair<string, string> kvp in paras)
  101. {
  102. ps.Add(new SqlParameter(kvp.Key, kvp.Value));
  103. }
  104. try
  105. {
  106. using (SqlConnection connection = new SqlConnection(connectionString))
  107. {
  108. DataSet dataSet = new DataSet();
  109. connection.Open();
  110. SqlDataAdapter sqlDA = new SqlDataAdapter();
  111. sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, ps.ToArray());
  112. sqlDA.Fill(dataSet, tableName);
  113. connection.Close();
  114. return dataSet;
  115. }
  116. }
  117. catch (Exception e)
  118. {
  119. return null ;
  120. }
  121. }
  122. private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
  123. {
  124. SqlCommand command = new SqlCommand(storedProcName, connection);
  125. command.CommandType = CommandType.StoredProcedure;
  126. foreach (SqlParameter parameter in parameters)
  127. {
  128. if (parameter != null)
  129. {
  130. // 检查未分配值的输出参数,将其分配以DBNull.Value.
  131. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  132. (parameter.Value == null))
  133. {
  134. parameter.Value = DBNull.Value;
  135. }
  136. command.Parameters.Add(parameter);
  137. }
  138. }
  139. return command;
  140. }
  141. BLL.T_Wo_WorkOrderItem_New itembll = new BLL.T_Wo_WorkOrderItem_New();
  142. public bool AddLog(Model.T_Wo_WorkOrderItem_New itemlist, int F_IsSMS )
  143. {
  144. itemlist.F_IsSMS = F_IsSMS;
  145. var res = itembll.Update (itemlist);
  146. return res;
  147. }
  148. protected void Application_AuthenticateRequest(object sender, EventArgs e)
  149. {
  150. HttpApplication app = (HttpApplication)sender;
  151. var context = app.Context;
  152. if (context == null) throw new ArgumentNullException("context");
  153. var token = context.Request["token"];
  154. if (string.IsNullOrWhiteSpace(token)) return;
  155. try
  156. {
  157. ////获取缓存
  158. //var dict = CacheHelper.Get(token);
  159. //获取redis缓存
  160. var dict = RedisHelper.StringGet(token);
  161. if (dict == null) return;
  162. Cache.Models.CurrentUserInfo userData = null;
  163. //获取FormsAuthenticationTicket对象
  164. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
  165. if (ticket != null && string.IsNullOrEmpty(ticket.UserData) == false)
  166. userData = (new JavaScriptSerializer()).Deserialize<Cache.Models.CurrentUserInfo>(ticket.UserData); //还原用户数据
  167. if (ticket != null && userData != null)
  168. context.User = new FormsPrincipal<Cache.Models.CurrentUserInfo> (ticket, userData);//重新给context.User赋值。
  169. }
  170. catch { /* 有异常也不要抛出,防止攻击者试探。 */ }
  171. }
  172. }
  173. }