Login.cs 15 KB

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