Login.cs 14 KB

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