Login.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using CCWin;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using SufeiUtil;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data.SQLite;
  8. using System.IO;
  9. using System.Net;
  10. using System.Net.Security;
  11. using System.Security.Cryptography.X509Certificates;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. namespace MOKA_Factory_Tools
  15. {
  16. public partial class login : Skin_Color
  17. {
  18. /// <summary>
  19. /// G客户NTF功能是否开启;
  20. /// </summary>
  21. public bool IsNTF { get; set; } = false;
  22. public int enable;
  23. string path = AppDomain.CurrentDomain.BaseDirectory;
  24. LoginOutput loginOutput1 = new LoginOutput()
  25. {
  26. factoryname = "",
  27. factorynum = ""
  28. };
  29. string ErrorDBPath = AppDomain.CurrentDomain.BaseDirectory + "\\Error.db";
  30. SQLiteConnection ErrorDB = null;
  31. public login()
  32. {
  33. InitializeComponent();
  34. AccountText.Focus();
  35. CommonMethod.CreateDirectory(path + "\\download");
  36. CommonMethod.CreateDirectory(path + "\\Keys");
  37. CommonMethod.CreateDirectory(path + "\\Keys\\Demo");
  38. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\CIKEY");
  39. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\DeviceID");
  40. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\ESN");
  41. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\HDCPKEY");
  42. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\HDCPKEY22");
  43. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\MAC");
  44. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\Widevine");
  45. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\WiDi");
  46. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\Attestation");
  47. CommonMethod.CreateDirectory(path + "\\Keys\\Demo\\MGK");
  48. if (!File.Exists(ErrorDBPath))
  49. {
  50. if (SQLiteHelper.NewDbFile(ErrorDBPath))
  51. {
  52. SQLiteHelper.NewTable(ErrorDBPath, "ErrorReport", "(ID INTEGER PRIMARY KEY AUTOINCREMENT,url varchar,content varvhar,gener_date DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')),report_date DATETIME)");
  53. SQLiteHelper.NewTable(ErrorDBPath, "CopyDelayReport", "(ID INTEGER PRIMARY KEY AUTOINCREMENT,bid varchar,url varchar,content varvhar,copy_date DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')),report_date DATETIME)");
  54. SQLiteHelper.NewTable(ErrorDBPath, "ProductionCount", "(bid varchar,count varchar)");
  55. }
  56. }
  57. if (ErrorDB == null)
  58. {
  59. ErrorDB = new SQLiteConnection("data source=" + ErrorDBPath);
  60. ErrorDB.Open();
  61. }
  62. getTestModeInfo();
  63. }
  64. private void getTestModeInfo()
  65. {
  66. // 读取win.ini或注册表;
  67. StringBuilder MyString = new StringBuilder(256);
  68. CommonMethod.GetProfileString("MOKAFactoryTools", "TestMode", "0", MyString, 256);
  69. if (MyString.ToString().Equals("1"))
  70. {
  71. TestMode.testMode = true;
  72. TestMode.SetTestMode(true);
  73. V2Method.SetTestUrl();
  74. // 开启了测试模式;
  75. CommonMethod.GetProfileString("MOKAFactoryTools", "VirMAC", "B0-00-00-00-00-0B", MyString, 256);
  76. if (MyString.ToString().Equals("B0-00-00-00-00-0B"))
  77. {// 惠州工厂;
  78. AccountText.Text = "Huizhou";
  79. PwdText.Text = "admin";
  80. }
  81. else if (MyString.ToString().Equals("A0-00-00-00-00-0A"))
  82. {// 三华工厂;
  83. AccountText.Text = "SanHua";
  84. PwdText.Text = "admin";
  85. }
  86. else if (MyString.ToString().Equals("C0-00-00-00-00-0C"))
  87. {// 泰霖工厂;
  88. AccountText.Text = "Talent";
  89. PwdText.Text = "admin";
  90. }
  91. TestMode.MAC = MyString.ToString();
  92. JObject jObject;
  93. using (StreamReader file = File.OpenText(LocalPath.localpath + "\\Config.json"))
  94. {
  95. using (JsonTextReader reader = new JsonTextReader(file))
  96. {
  97. jObject = (JObject)JToken.ReadFrom(reader);
  98. reader.Close();
  99. jObject["FactoryName"] = AccountText.Text;
  100. }
  101. }
  102. using (StreamWriter writer = new StreamWriter(LocalPath.localpath + "\\Config.json"))
  103. {
  104. writer.Write(jObject.ToString());
  105. }
  106. }
  107. else
  108. {
  109. TestMode.testMode = false;
  110. TestMode.SetTestMode();
  111. #if DEBUG
  112. TestMode.MAC = "C4-00-AD-17-61-3D&C4-00-AD-17-61-3C&";
  113. #else
  114. TestMode.MAC = CommonMethod.GetMacAddress();
  115. #endif
  116. }
  117. }
  118. private void OnBtnLogin(object sender, EventArgs e)
  119. {
  120. if (PwdText.Text == "zhangyi" && AccountText.Text == "zhangyi")
  121. enable = 1;
  122. else if (PwdText.Text == "offline" && AccountText.Text == "offline")
  123. enable = 3;
  124. else if (PwdText.Text.Trim().Length > 0 && AccountText.Text.Trim().Length > 0)
  125. {
  126. #region G客户NTF功能开启;
  127. if ( IsNTF )
  128. {
  129. if ( LineText.Text.Length == 0 )
  130. {
  131. MessageBox.Show("Line should not be empty!");
  132. return;
  133. }
  134. }
  135. #endregion
  136. if (CommonMethod.UserLogin(TestMode.loginUrl, AccountText.Text, PwdText.Text, TestMode.MAC, ErrorDB,out loginOutput1))
  137. {
  138. enable = 0;
  139. JObject jObject;
  140. using (StreamReader file = File.OpenText(LocalPath.localpath + "\\Config.json"))
  141. {
  142. using (JsonTextReader reader = new JsonTextReader(file))
  143. {
  144. jObject = (JObject)JToken.ReadFrom(reader);
  145. reader.Close();
  146. jObject["FactoryName"] = loginOutput1.factoryname;
  147. jObject["FactoryNum"] = loginOutput1.factorynum;
  148. jObject["Line"] = LineText.Text;
  149. }
  150. }
  151. using (StreamWriter writer = new StreamWriter(LocalPath.localpath + "\\Config.json"))
  152. {
  153. writer.Write(jObject.ToString());
  154. }
  155. }
  156. else
  157. {
  158. MessageBox.Show(LResource.LoginFail + "\r\n" + loginOutput1.msg);
  159. return;
  160. }
  161. }
  162. else
  163. {
  164. enable = 2;
  165. }
  166. // 保存登录信息;
  167. CommonMethod.SaveLoginInfo(AccountText.Text, PwdText.Text);
  168. #region 三华MES登录数据
  169. if (CommonMethod.GetLoginAccout().Equals("Sanhua", StringComparison.OrdinalIgnoreCase))
  170. {
  171. SanhuaMES sanhuaMES = new SanhuaMES();
  172. if (sanhuaMES.ShowDialog() != DialogResult.OK)
  173. {
  174. return;
  175. }
  176. }
  177. #endregion
  178. CommonMethod.FactoryName = loginOutput1.factoryname;
  179. UpgradeMsgList upgradeMsgList = new UpgradeMsgList();
  180. var FactoryNum = loginOutput1.factorynum;
  181. if(FactoryNum.Length>0)
  182. {
  183. try
  184. {
  185. if (CommonMethod.UpgradeCheck(TestMode.upgradeUrl, FactoryNum, ErrorDB, out upgradeMsgList))
  186. {
  187. if (CommonMethod.CompareVersion(upgradeMsgList.upgrade.version, System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()))
  188. {
  189. Upgrade upgrade = new Upgrade(ErrorDB, upgradeMsgList.upgrade.fileurl, loginOutput1.factorynum);
  190. upgrade.Show();
  191. }
  192. else
  193. {
  194. ErrorDB.Close();
  195. ErrorDB.Dispose();
  196. Main form1 = new Main(enable);
  197. form1.Show();
  198. this.Hide();
  199. }
  200. }
  201. else
  202. {
  203. MessageBox.Show(LResource.UpgradeCheckFail);
  204. ErrorDB.Close();
  205. ErrorDB.Dispose();
  206. Main form1 = new Main(enable);
  207. form1.Show();
  208. this.Hide();
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. MessageBox.Show(ServicePointManager.SecurityProtocol.ToString());
  214. MessageBox.Show(ex.Message);
  215. MessageBox.Show(LResource.UpgradeCheckFail);
  216. ErrorDB.Close();
  217. ErrorDB.Dispose();
  218. Main form1 = new Main(enable);
  219. form1.Show();
  220. this.Hide();
  221. }
  222. }
  223. else
  224. {
  225. ErrorDB.Close();
  226. ErrorDB.Dispose();
  227. MessageBox.Show(LResource.PleaseLogin);
  228. Main form1 = new Main(enable);
  229. form1.Show();
  230. this.Hide();
  231. }
  232. }
  233. public string format(uint data,uint length)
  234. {
  235. return String.Format("{0:D" + length.ToString() + "}", data);
  236. }
  237. private void login_Load(object sender, EventArgs e)
  238. {
  239. using (StreamReader file = File.OpenText(LocalPath.localpath + "\\Config.json"))
  240. {
  241. using (JsonTextReader reader = new JsonTextReader(file))
  242. {
  243. JObject jObject = (JObject)JToken.ReadFrom(reader);
  244. reader.Close();
  245. AccountText.Text = jObject["FactoryName"] == null ? (!CommonMethod.AddJsonConfig("FactoryName", null) ? "" : "") : jObject["FactoryName"].Value<string>();
  246. loginOutput1.factoryname = jObject["FactoryName"] == null ? "" : jObject["FactoryName"].Value<string>();
  247. loginOutput1.factorynum=jObject["FactoryNum"] == null ? "" : jObject["FactoryNum"].Value<string>();
  248. HttpHelper.Tracert = jObject["Tracert"] == null ? false : jObject["Tracert"].Value<bool>();
  249. #region G客户NTF功能开启;
  250. if (jObject["NTF"] != null)
  251. {
  252. IsNTF = jObject["NTF"].Value<bool>();
  253. if (IsNTF)
  254. {
  255. skinLabel3.Visible = true;
  256. LineText.Visible = true;
  257. BtnLogin.Location = new System.Drawing.Point(70, 152);
  258. BtnLogin.Size = new System.Drawing.Size(90, 33);
  259. this.Size = new System.Drawing.Size(230, 200);
  260. }
  261. if (jObject["Line"] != null)
  262. {
  263. string Line = jObject["Line"].Value<string>();
  264. LineText.Text = Line;
  265. }
  266. }
  267. #endregion
  268. #region 加载三华登录数据库;
  269. if (jObject["SanhuaLoginInfo"] != null)
  270. {
  271. SanhuaMethod.sanhuaLoginInfo.url = jObject["SanhuaLoginInfo"]["url"].Value<string>();
  272. SanhuaMethod.sanhuaLoginInfo.staffCode = jObject["SanhuaLoginInfo"]["staffCode"].Value<string>();
  273. SanhuaMethod.sanhuaLoginInfo.password = jObject["SanhuaLoginInfo"]["password"].Value<string>();
  274. SanhuaMethod.sanhuaLoginInfo.resourceCode = jObject["SanhuaLoginInfo"]["resourceCode"].Value<string>();
  275. SanhuaMethod.sanhuaLoginInfo.jigEdtion = jObject["SanhuaLoginInfo"]["jigEdtion"].Value<string>();
  276. }
  277. #endregion
  278. }
  279. }
  280. System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("SCBC Factory Tools");//获取指定的进程名
  281. if (myProcesses.Length > 1)
  282. {
  283. MessageBox.Show(LResource.AlreadyRun);
  284. Application.Exit();
  285. }
  286. System.Diagnostics.Process[] myProcesses2 = System.Diagnostics.Process.GetProcessesByName("MOKA Factory Tools");//获取指定的进程名
  287. if (myProcesses2.Length > 1)
  288. {
  289. MessageBox.Show(LResource.AlreadyRun);
  290. Application.Exit();
  291. }
  292. }
  293. private void login_KeyDown(object sender, KeyEventArgs e)
  294. {
  295. // 判断是否回车登录;
  296. if (e.KeyCode == Keys.Enter)
  297. {
  298. OnBtnLogin(sender, e);
  299. }
  300. }
  301. private void PwdText_KeyDown(object sender, KeyEventArgs e)
  302. {
  303. // 判断是否回车登录;
  304. if (e.KeyCode == Keys.Enter)
  305. {
  306. OnBtnLogin(sender, e);
  307. }
  308. }
  309. private void LineText_KeyDown(object sender, KeyEventArgs e)
  310. {
  311. // 判断是否回车登录;
  312. if (e.KeyCode == Keys.Enter)
  313. {
  314. OnBtnLogin(sender, e);
  315. }
  316. }
  317. }
  318. }