Login.cs 15 KB

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