AccountController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Ant.Service.Common;
  7. using System.Collections;
  8. using System.Xml;
  9. using MES.Production.Service;
  10. using System.Net;
  11. using Ant.Service.Utility;
  12. using MES.Production.Entity;
  13. using Ant.ORM;
  14. using Ant.Data;
  15. using System.Runtime.InteropServices;
  16. using System.Management;
  17. using System.Text;
  18. using JsonHelper = Ant.Service.Common.JsonHelper;
  19. namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
  20. {
  21. /// <summary>
  22. /// 登录控制器
  23. /// add 作者: 季健国 QQ:181589805 by 2016-05-22
  24. /// </summary>
  25. public class AccountController : Controller
  26. {
  27. [DllImport("Iphlpapi.dll")]
  28. private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
  29. [DllImport("Ws2_32.dll")]
  30. private static extern Int32 inet_addr(string ip);
  31. #region 声明容器
  32. MES.Production.Service.IService.IUserManage UserManage { get; set; }
  33. MES.Production.Service.IService.ISystemManage SystemManage { get; set; }
  34. string backurl = string.Empty;
  35. log4net.Ext.IExtLog log = log4net.Ext.ExtLogManager.GetLogger("dblog");
  36. #endregion
  37. /// <summary>
  38. /// 初始视图
  39. /// add 作者: 季健国 QQ:181589805 by 2016-05-10
  40. /// </summary>
  41. public ActionResult Index()
  42. {
  43. //移除Session
  44. SessionHelper.Remove("CurrentUser");
  45. CookieHelper.ClearCookie("cookie_rememberme");
  46. //try
  47. //{
  48. // IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[1];//获得当前IP地址
  49. // string ip = ipAddr.ToString();
  50. // string userip = Request.UserHostAddress;
  51. // string strClientIP = Request.UserHostAddress.ToString().Trim();
  52. // Int32 ldest = inet_addr(ip); //目的地的ip
  53. // Int64 macinfo = new Int64();
  54. // Int32 len = 6;
  55. // int res = SendARP(ldest, 0, ref macinfo, ref len);
  56. // string mac_src = macinfo.ToString("X");
  57. // if (mac_src == "0")
  58. // {
  59. // if (userip == "127.0.0.1")
  60. // {
  61. // Response.Write("正在访问Localhost!");
  62. // }
  63. // else
  64. // {
  65. // // Response.Write("欢迎来自IP为" + userip + "的朋友!" + "<br>");
  66. // }
  67. // }
  68. // while (mac_src.Length < 12)
  69. // {
  70. // mac_src = mac_src.Insert(0, "0");
  71. // }
  72. // string mac_dest = "";
  73. // for (int i = 0; i < 11; i++)
  74. // {
  75. // if (0 == (i % 2))
  76. // {
  77. // if (i == 10)
  78. // {
  79. // mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
  80. // }
  81. // else
  82. // {
  83. // mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
  84. // }
  85. // }
  86. // }
  87. // Response.Write("欢迎来自IP为" + userip + "<br>" + ",MAC地址为" + mac_dest + "的朋友!" + "<br>");
  88. //}
  89. //catch (Exception err)
  90. //{
  91. // Response.Write(err.Message);
  92. //}
  93. return View();
  94. }
  95. ///<summary>
  96. /// SendArp获取MAC地址
  97. ///</summary>
  98. ///<param name="RemoteIP">目标机器的IP地址如(192.168.1.1)</param>
  99. ///<returns>目标机器的mac 地址</returns>
  100. public static string GetMacAddress(string RemoteIP)
  101. {
  102. StringBuilder macAddress = new StringBuilder();
  103. try
  104. {
  105. Int32 remote = inet_addr(RemoteIP);
  106. Int64 macInfo = new Int64();
  107. Int32 length = 6;
  108. SendARP(remote, 0, ref macInfo, ref length);
  109. string temp = Convert.ToString(macInfo, 16).PadLeft(12, '0').ToUpper();
  110. int x = 12;
  111. for (int i = 0; i < 6; i++)
  112. {
  113. if (i == 5)
  114. {
  115. macAddress.Append(temp.Substring(x - 2, 2));
  116. }
  117. else
  118. {
  119. macAddress.Append(temp.Substring(x - 2, 2) + "-");
  120. }
  121. x -= 2;
  122. }
  123. return macAddress.ToString();
  124. }
  125. catch
  126. {
  127. return macAddress.ToString();
  128. }
  129. }
  130. /// <summary>
  131. /// 获取网卡地址信息
  132. /// </summary>
  133. /// <returns></returns>
  134. string GetMacAddress()
  135. {
  136. try
  137. {
  138. string mac = "";
  139. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  140. ManagementObjectCollection moc = mc.GetInstances();
  141. foreach (ManagementObject mo in moc)
  142. {
  143. if ((bool)mo["IPEnabled"] == true)
  144. {
  145. mac = mo["MacAddress"].ToString();
  146. break;
  147. }
  148. }
  149. moc = null;
  150. mc = null;
  151. return mac;
  152. }
  153. catch
  154. {
  155. return "unknow";
  156. }
  157. }
  158. /// <summary>
  159. ///
  160. /// </summary>
  161. /// <returns></returns>
  162. [HttpGet]
  163. public JsonResult getVehicleTrajectory()
  164. {
  165. string strjson = "";
  166. string start = Request.QueryString["start"];
  167. string end = Request.QueryString["end"];
  168. string carid = Request.QueryString["carid"];
  169. ResponseTrajectory mod = new ResponseTrajectory();
  170. mod.result = new List<Lnglat>();
  171. using (AntORM orm = new AntORM())
  172. {
  173. orm.db = DataAccessFactory.CreateDataConnection("CyclingItemLog");
  174. RequestModel res = new RequestModel
  175. {
  176. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取车辆信息" }
  177. };
  178. var q = orm.Queryable<Ent_YW_DrivingTrack>();
  179. if (!carid.IsEmpty())
  180. {
  181. q = q.Where(p => p.TripID == carid);
  182. }
  183. if (!start.IsEmpty() && !end.IsEmpty())
  184. {
  185. DateTime startDate = start.ToDateTime();
  186. DateTime endDate = end.ToDateTime();
  187. q = q.Where(p => p.Add_Time >= startDate && p.Add_Time <= endDate);
  188. }
  189. var count = q.Count();
  190. if (count > 1000)
  191. {
  192. q = q.Top(500);
  193. }
  194. var reslut = q.OrderBy(p => p.Add_Time).ToList(res);
  195. if (reslut.IsSuccess)
  196. {
  197. foreach (Ent_YW_DrivingTrack beatenty in reslut.ResultModel)
  198. {
  199. mod.imei = beatenty.IMEI;
  200. var GPS = Ant.SuperSocket.Common.ToolGPSamap.GPSToGDGPS(beatenty.Lng.ToDou(), beatenty.Lat.ToDou());
  201. Lnglat enty = new Lnglat();
  202. enty.lng = GPS.Split(',')[0];
  203. enty.lat = GPS.Split(',')[1];
  204. enty.taskTime = beatenty.Add_Time.ToDateTime().ToString("yyyy-MM-dd HH:mm:ss");
  205. mod.result.Add(enty);
  206. }
  207. mod.status = (mod.result.Count > 0) ? "1" : "0";
  208. }
  209. }
  210. return Json(mod, JsonRequestBehavior.AllowGet);
  211. }
  212. /// <summary>
  213. /// 登录验证
  214. /// add 作者: 季健国 QQ:181589805 by 2016-05-10
  215. /// </summary>
  216. public ActionResult login(Account item)
  217. {
  218. var json = new JsonHelper() { Msg = "登录成功", Status = "n", ReUrl = "/Sys/Home/Index" };
  219. try
  220. {
  221. var code = Request.Form["code"];
  222. var rememberme = Request.Form["remember"];
  223. if (Session["gif"] != null)
  224. {
  225. //if (!string.IsNullOrEmpty(code) && code.ToLower() == Session["gif"].ToString().ToLower())
  226. if (!string.IsNullOrEmpty(code))
  227. {
  228. var pwd = PasswordUtils.MD5_Encrypt(item.PassWord.Trim(), 32);// Ant.Service.Common.CryptHelper.DESCrypt.Encrypt(item.PassWord.Trim())
  229. string userip = Request.UserHostAddress;
  230. var users = UserManage.UserLogin(item.LogName.Trim(), pwd);
  231. if (users != null)
  232. {
  233. //if (item.LogName == "admin" && Utils.GetIP() != "218.93.67.45")
  234. //{
  235. // json.Msg = "用户已锁定,禁止登录,需要进行解锁才能登录";
  236. // log.Warn(Utils.GetIP(), users.NAME, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  237. // return Json(json);
  238. //}
  239. if (users.USERTYPE == 0)
  240. {
  241. if (users.DPTID != null)
  242. {
  243. using (AntORM orm = new AntORM())
  244. {
  245. try
  246. {
  247. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  248. RequestModel request = new RequestModel
  249. {
  250. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询教练的单个实体方法" },
  251. Oid = users.DPTID.ToString()
  252. };
  253. var reslut = orm.GetEntity<YM_School>(request);
  254. if (reslut.IsSuccess)
  255. {
  256. var mod = (YM_School)reslut.ResultModel;
  257. if (mod.Status == 2 || mod.Status == 3)
  258. {
  259. json.Msg = "帐号已锁定,禁止登录,请联系管理员进行解锁";
  260. log.Warn(Utils.GetIP(), users.NAME, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  261. return Json(json);
  262. }
  263. }
  264. }
  265. catch (Exception e)
  266. {
  267. }
  268. }
  269. }
  270. }
  271. if (users.ISCANLOGIN == true)
  272. {
  273. json.Msg = "用户已锁定,禁止登录,请联系管理员进行解锁";
  274. log.Warn(Utils.GetIP(), users.NAME, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  275. return Json(json);
  276. }
  277. var acconut = this.UserManage.GetAccountByUser(users);
  278. //写入Session 当前登录用户
  279. SessionHelper.SetSession("CurrentUser", acconut);
  280. //默认记住我
  281. string cookievalue = "{\"id\":\"" + acconut.Id + "\",\"username\":\"" + acconut.LogName +
  282. "\",\"password\":\"" + acconut.PassWord + "\",\"ToKen\":\"" +
  283. Session.SessionID + "\"}";
  284. CookieHelper.SetCookie("cookie_rememberme", Ant.Service.Common.CryptHelper.DESCrypt.Encrypt(cookievalue),
  285. null);
  286. json.Status = "y";
  287. log.Info(Utils.GetIP(), users.NAME, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  288. }
  289. else
  290. {
  291. json.Msg = "用户名或密码不正确";
  292. log.Error(Utils.GetIP(), item.LogName, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  293. }
  294. }
  295. else
  296. {
  297. json.Msg = "验证码不正确";
  298. log.Error(Utils.GetIP(), item.LogName, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  299. }
  300. }
  301. else
  302. {
  303. json.Msg = "验证码已过期,请刷新验证码";
  304. log.Error(Utils.GetIP(), item.LogName, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  305. }
  306. }
  307. catch (Exception e)
  308. {
  309. json.Msg = e.Message;
  310. log.Fatal(Utils.GetIP(), item.LogName, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
  311. }
  312. return Json(json, JsonRequestBehavior.AllowGet);
  313. }
  314. /// <summary>
  315. /// 验证是否有登录权限
  316. /// </summary>
  317. public bool ValidateLogin(HttpRequestBase Request, out string backurl)
  318. {
  319. //获取返回页面
  320. backurl = Request["backurl"];
  321. string param = Request["para"];
  322. try
  323. {
  324. if (!string.IsNullOrEmpty(backurl))
  325. {
  326. if (!string.IsNullOrEmpty(param))
  327. {
  328. //解密
  329. param = Ant.Service.Common.CryptHelper.DESCrypt.Decrypt(param);
  330. if (!string.IsNullOrEmpty(param) && param.IndexOf('=') > 0 && param.ToLower().Contains("siteid"))
  331. {
  332. var siteId = param.Split('=')[1];
  333. //判断来源站点ID
  334. if (this.SystemManage.IsExist(p => p.ID == siteId))
  335. {
  336. return true;
  337. }
  338. else
  339. {
  340. log.Warn(Utils.GetIP(), "其他系统访问者", backurl, "Login", "其他系统登录失败,原因:来源站点不可信,系统拒绝登录");
  341. }
  342. }
  343. else
  344. {
  345. log.Warn(Utils.GetIP(), "其他系统访问者", backurl, "Login", "其他系统登录失败,原因:系统验证参数错误,系统拒绝登录");
  346. }
  347. }
  348. else
  349. {
  350. log.Warn(Utils.GetIP(), "其他系统访问者", backurl, "Login", "其他系统登录失败,原因:未传递系统验证参数,系统拒绝登录");
  351. }
  352. }
  353. }
  354. catch (Exception e)
  355. {
  356. log.Warn(Utils.GetIP(), "其他系统访问者", "", "Login", "其他系统登录失败,系统错误", e);
  357. }
  358. return false;
  359. }
  360. /// <summary>
  361. /// 验证码
  362. /// </summary>
  363. public FileContentResult ValidateCode()
  364. {
  365. string code = "";
  366. System.IO.MemoryStream ms = new ChangFaModels.verify_code().Create(out code);
  367. Session["gif"] = code;//验证码存储在Session中,供验证。
  368. Response.ClearContent();//清空输出流
  369. return File(ms.ToArray(), @"image/png");
  370. }
  371. }
  372. }