sync.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using BizCom;
  2. using SiteCore.Redis;
  3. using SQLData;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Reflection;
  8. using System.Text.RegularExpressions;
  9. using System.Web;
  10. using Utils;
  11. namespace SiteCore.Handler
  12. {
  13. public partial class sync : BaseHandler, IHttpHandler
  14. {
  15. //private static log4net.ILog logger = log4net.LogManager.GetLogger("Logger");
  16. //private static readonly log4net.ILog applicationInfoLog = log4net.LogManager.GetLogger("ApplicationInfoLog");
  17. public void ProcessRequest(HttpContext context)
  18. {
  19. //if (CurrentUser == null)
  20. //{
  21. // ReturnLoginMsg("操作己过期,请重新登录!");
  22. // return;
  23. //}
  24. con = context;
  25. string methodName = GetString("t");
  26. Type type = this.GetType();
  27. MethodInfo method = type.GetMethod(methodName);
  28. if (method == null)
  29. throw new Exception("method is null");
  30. if (isLoginOut())
  31. {
  32. if (methodName != "erp_user_login" && methodName != "loginout" && methodName != "file_client_down_url")
  33. {
  34. returnErrorMsg("操作己过期,请重新登录!");
  35. return;
  36. }
  37. }
  38. try
  39. {
  40. method.Invoke(this, null);
  41. if (CurrentUser != null)
  42. {
  43. LogOperate.Save(CurrentUser.UserID, CurrentUser.UserName, CurrentUser.User.LoginIP, methodName);
  44. }
  45. /*if (operate_Tag != "") WebLog.SysLog(CurrentUser.UserID,operate_Tag);
  46. else WebLog.SysLog(CurrentUser.UserID, methodName);*/
  47. }
  48. catch (Exception ex)
  49. {
  50. //WebLog.SysLog(CurrentUser.UserID, ex);
  51. returnErrorMsg("操作失败!" + ex.Message);
  52. }
  53. //if (methodName.IndexOf("get_") == -1)
  54. //{
  55. // using (new SessionScope())
  56. // {
  57. // }
  58. //}
  59. //else
  60. //{
  61. // try
  62. // {
  63. // method.Invoke(this, null);
  64. // }
  65. // catch (Exception ex)
  66. // {
  67. // XLog.SaveLog(CurrentUser.UserID, ex.Message);
  68. // returnErrorMsg("操作失败!" + ex.Message);
  69. // }
  70. //}
  71. }
  72. public void isAccLogin()
  73. {
  74. HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  75. if (lcCookie != null)
  76. {
  77. if (CurrentUser != null && CurrentUser.User.IsMultiLogin == 1)
  78. {
  79. returnSuccessMsg("1");
  80. return;
  81. }
  82. string name = HttpUtility.UrlDecode(lcCookie.Values["user"]);
  83. string ticket = lcCookie.Values["ticket"];
  84. string dl_ticket = erpRedis.RedisHelper.StringGet("dl_" + name);
  85. if (ticket != dl_ticket)
  86. returnSuccessMsg("0");
  87. else
  88. returnSuccessMsg("1");
  89. //if (String.IsNullOrEmpty(dl_ticket))
  90. //{
  91. // erpRedis.RedisHelper.StringSet("dl_" + name, ticket);
  92. // returnSuccessMsg("1");
  93. //}
  94. //else
  95. //{
  96. //}
  97. return;
  98. }
  99. returnSuccessMsg("0");
  100. }
  101. public void trans_user_role()
  102. {
  103. if (UrlPostParmsCheck("r"))
  104. {
  105. string user = CurrentUser.User.Account;
  106. string pwd = CurrentUser.User.PassWord;
  107. user = user.Split('_')[0];
  108. int r = GetPostInt("r");
  109. switch (r)
  110. {
  111. case 1: user = user + "_kf"; break;
  112. case 2: user = user + "_sj"; break;
  113. case 3: user = user + "_sh"; break;
  114. case 4: user = user + "_cw"; break;
  115. default: break;
  116. }
  117. CeErpUser entity = CeErpUser.GetByLogin(user, pwd);
  118. if (entity != null)
  119. {
  120. entity.LoginIP = CommonHelper.ClientIP;
  121. entity.Ticket = WebHelper.getLoginTicket(entity.Account, entity.LoginIP);
  122. if (entity.CurLoginTime.ToString() != "")
  123. entity.PreLoginTime = Convert.ToDateTime(entity.CurLoginTime);
  124. entity.CurLoginTime = DateTime.Now;
  125. entity.Update();
  126. HttpCookie cookie = new HttpCookie(webConfig.CookieName);
  127. cookie.Values.Clear();
  128. cookie.Values.Add("user", user);
  129. cookie.Values.Add("ticket", entity.Ticket);
  130. cookie.Path = "/";
  131. //cookie.Domain = SiteDomain;
  132. //if (chkWeek.Checked) cookie.Expires = DateTime.Now.AddDays(14);
  133. cookie.Expires = DateTime.Now.AddDays(2);
  134. HttpContext.Current.Response.Cookies.Add(cookie);
  135. returnSuccessMsg(HttpUtility.UrlEncode(SecurityHelper.EncryptSymmetric(user + "|" + DateTime.Now.ToString("yyyy-MM-dd"))));
  136. return;
  137. }
  138. }
  139. returnErrorMsg("无法切换,可能没有该角色!");
  140. }
  141. public static FrequencyControler LoginFrequency = new FrequencyControler("erplogin", 10, 4);
  142. public void erp_user_login()
  143. {
  144. if (LoginFrequency.IsTooFrequently(true))
  145. {
  146. returnErrorMsg("访问太频繁");
  147. return;
  148. }
  149. //XLog.SaveLog(0, "123");
  150. if (!UrlPostParmsCheck("user,pwd"))
  151. {
  152. returnErrorMsg("参数有误!");
  153. return;
  154. }
  155. string account = GetPostString("user");
  156. string pwd = GetPostString("pwd");
  157. if (account == "" || pwd == "")
  158. {
  159. returnErrorMsg("请输入用户名或密码!");
  160. return;
  161. }
  162. string code = GetPostString("code");
  163. if (con.Session["vCode"] != null)
  164. {
  165. if (code == "" || code.ToLower() != con.Session["vCode"].ToString().ToLower())
  166. {
  167. returnErrorMsg("验证码不正确!");
  168. return;
  169. }
  170. }
  171. CeErpUser entity = CeErpUser.GetByLogin(account, SecurityHelper.EncryptSymmetric(pwd));
  172. string ip = WebHelper.GetIP();
  173. if (SiteInfo.isPassIp(ip) == false)
  174. {
  175. if (!(entity != null && entity.State == 5))
  176. {
  177. returnErrorMsg("未授权的IP:" + ip);
  178. return;
  179. }
  180. }
  181. if (!string.IsNullOrEmpty(erpRedis.RedisHelper.StringGet("lock_" + account)))
  182. {
  183. returnErrorMsg("账户于" + erpRedis.RedisHelper.StringGet("lock_" + account) + "锁定!");
  184. return;
  185. }
  186. //XLog.SaveLog(0, "456");
  187. if (entity == null)
  188. {
  189. if (con.Session["lerr" + account] != null)
  190. con.Session["lerr" + account] = Convert.ToInt32(con.Session["lerr" + account]) + 1;
  191. else
  192. con.Session["lerr" + account] = 1;
  193. con.Session["lerr"] = con.Session["lerr" + account];
  194. string errMsg = "";
  195. if (Convert.ToInt32(con.Session["lerr"]) > 5)
  196. {
  197. erpRedis.RedisHelper.StringSet("lock_" + account, DateTime.Now.ToLongTimeString(), new TimeSpan(0, 10, 0));
  198. errMsg = con.Session["lerr"] + "|账号或密码不正确!账户锁定10分钟。";
  199. }
  200. else
  201. {
  202. errMsg = con.Session["lerr"] + "|账号或密码不正确!";
  203. }
  204. Log_Login.Save(account, ip, false, errMsg);
  205. returnErrorMsg(errMsg);
  206. return;
  207. }
  208. if (entity.State == 1)
  209. {
  210. returnErrorMsg("账户已被冻结!");
  211. Log_Login.Save(account, ip, false, "账户已被冻结!");
  212. return;
  213. }
  214. entity.LoginIP = ip;
  215. entity.Ticket = WebHelper.getLoginTicket(entity.Account + DateTime.Now.ToString("yyyyMMddHHmmssffff"));
  216. if (entity.CurLoginTime.ToString() != "")
  217. {
  218. entity.PreLoginTime = Convert.ToDateTime(entity.CurLoginTime);
  219. }
  220. entity.CurLoginTime = DateTime.Now;
  221. //查询上次登录时间是否为昨天,一天第一次登录清空设计师派单数据
  222. //DateTime ispre = Convert.ToDateTime(entity.CurLoginTime).AddDays(-1);
  223. //if(ispre.Day == Convert.ToDateTime(entity.PreLoginTime).Day)
  224. //{
  225. // CeErpUserInfo userInfo = CeErpUserInfo.Get(entity.ID);
  226. // userInfo.DayOrderPer = 0;
  227. // userInfo.DayOrderReceive = 0;
  228. // userInfo.OnDuty = 0;
  229. // userInfo.Update();
  230. //}
  231. entity.Update();
  232. Log_Login.Save(account, ip, false, "登录成功!");
  233. string isFromClient = GetPostString("isFromClient");//C端登录
  234. if ("1" == isFromClient)
  235. {
  236. returnSuccessMsg(entity.ID.ToString());
  237. return;
  238. }
  239. HttpCookie cookie = new HttpCookie(webConfig.CookieName);
  240. cookie.Values.Clear();
  241. cookie.Values.Add("user", HttpUtility.UrlEncode(account));
  242. cookie.Values.Add("ticket", entity.Ticket);
  243. //cookie.Values.Add("mullogin", entity.IsMultiLogin.ToString());
  244. cookie.Path = "/";
  245. //cookie.Domain = SiteDomain;
  246. //if (chkWeek.Checked) cookie.Expires = DateTime.Now.AddDays(14);
  247. cookie.Expires = DateTime.Now.AddHours(18);
  248. HttpContext.Current.Response.Cookies.Add(cookie);
  249. erpRedis.RedisHelper.StringSet("dl_" + account, entity.Ticket, new TimeSpan(18, 0, 0));
  250. //erpRedis.RedisHelper.StringSet(user, con.Session.SessionID);
  251. //erpRedis.RedisHelper.StringSet("PUB_" + user, con.Session.SessionID);
  252. con.Session["vCode"] = null;
  253. con.Session["lerr"] = null;
  254. con.Session["userId"] = entity.ID;
  255. //WebUser.SetUser(account, entity.Ticket);
  256. con.Session["_t"] = DateTime.Now.ToString("yyyyMMddHHmmssffff");
  257. if (isSimplePwd(pwd))
  258. con.Session["isSimplePwd"] = 1;
  259. else
  260. con.Session["isSimplePwd"] = null;
  261. returnSuccessMsg("登录成功!");
  262. return;
  263. }
  264. private bool isSimplePwd(string pwd)
  265. {
  266. Match result = Regex.Match(pwd, "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,20}");
  267. if (result.Length > 0)
  268. {
  269. return false;
  270. }
  271. return true;
  272. }
  273. private bool isLoginOut()
  274. {
  275. ////人员是否已登录
  276. //if(con.Session["userId"] == null || con.Session["userId"].ToString() == "")
  277. // return true;
  278. //传过来的Ticket是否有效
  279. HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  280. if (lcCookie == null)
  281. {
  282. return true;
  283. }
  284. string name = lcCookie.Values["user"];
  285. string ticket = lcCookie.Values["ticket"];
  286. if (name == null || ticket == null || name == "" || ticket == "")
  287. {
  288. return true;
  289. }
  290. name = HttpUtility.UrlDecode(name);
  291. string dl_ticket = erpRedis.RedisHelper.StringGet("dl_" + name);
  292. if (ticket != dl_ticket)
  293. {
  294. return true;
  295. }
  296. return false;
  297. }
  298. public void loginout()
  299. {
  300. HttpCookie cookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  301. string name = cookie.Values["user"];
  302. if (name != null && name != null)
  303. {
  304. name = HttpUtility.UrlDecode(name);
  305. bool b = erpRedis.RedisHelper.KeyDelete("dl_" + name);
  306. WebUser.RemoveUserCache(name);
  307. WebUser.RemovePermissionCache(name);
  308. }
  309. cookie.Expires = DateTime.Now.AddDays(-1d);
  310. HttpContext.Current.Response.Cookies.Add(cookie);
  311. cookie.Values.Clear();
  312. HttpContext.Current.Session["WEBUSER"] = null;
  313. con.Session["userId"] = null;
  314. con.Session["_t"] = null;
  315. returnSuccessMsg("退出成功!");
  316. }
  317. public static FrequencyControler DoFrequency = new FrequencyControler("xinyue", 10, 3);
  318. //定义访问控制器允许10秒内3次请求
  319. public void get_syslog()
  320. {
  321. DataStruct dStruct = GetPostStruct();
  322. dStruct.Order = "id desc";
  323. DataTable dt = WebCache.GetData("x_log", dStruct);
  324. string data = Utils.Serialization.JsonString.DataTable2MiniAjaxJson(dt);
  325. data = data.Replace("\\", "\\\\");
  326. writeGridJson(dStruct.TotalCount, data);
  327. }
  328. public void clear_syslog()
  329. {
  330. string sql = "truncate table x_log";
  331. DbHelper.DbConn.ExecuteNonQuery(sql);
  332. returnSuccessMsg("清空完成");
  333. }
  334. public void get_erp_ipWhitelist()
  335. {
  336. DataStruct dStruct = GetPostStruct();
  337. dStruct.Order = "id desc";
  338. List<string> lw = new List<string>();
  339. string ip = GetPostString("ip");
  340. if (ip.Length > 0) lw.Add(string.Format("ip_white_list like '%{0}%'", ip));
  341. string remark = GetPostString("remark");
  342. if (remark.Length > 0) lw.Add(string.Format("remark like '%{0}%'", remark));
  343. dStruct.MainWhere = string.Join(" and ", lw.ToArray());
  344. DataTable dt = WebCache.GetData("SiteInfo", dStruct);
  345. writeGridDataTableJson(dStruct.TotalCount, dt);
  346. }
  347. public void get_login_list()
  348. {
  349. DataStruct dStruct = GetPostStruct();
  350. dStruct.Order = "login_time desc";
  351. List<string> lw = new List<string>();
  352. lw.Add(string.Format("user_name != '{0}'", "xfd666"));
  353. string ip = GetPostString("ip");
  354. if (ip.Length > 0) lw.Add(string.Format("login_ip like '%{0}%'", ip));
  355. string name = GetPostString("uName");
  356. if (name.Length > 0) lw.Add(string.Format("user_name like '%{0}%'", name));
  357. string sTime = GetPostString("sTime");
  358. string eTime = GetPostString("eTime");
  359. if (sTime.Length > 0)
  360. {
  361. string dw = GetDateMinuteWhere("login_time", sTime, eTime);
  362. if (dw.Length > 0) lw.Add(dw);
  363. }
  364. dStruct.MainWhere = string.Join(" and ", lw.ToArray());
  365. DataTable dt = WebCache.GetData("Log_Login", dStruct);
  366. writeGridDataTableJson(dStruct.TotalCount, dt);
  367. }
  368. public void get_supplierlog()
  369. {
  370. DataStruct dStruct = GetPostStruct();
  371. dStruct.Order = "id desc";
  372. DataTable dt = WebCache.GetData("SupplierLog", dStruct);
  373. string data = Utils.Serialization.JsonString.DataTable2MiniAjaxJson(dt);
  374. data = data.Replace("\\", "\\\\");
  375. writeGridJson(dStruct.TotalCount, data);
  376. }
  377. }
  378. }