private void cmboCookieType_SelectedIndexChanged(object sender, EventArgs e) { switch (this.cmboCookieType.SelectedIndex) { case 0: this.mCurrentCookie = null; break; case 1: this.mCurrentCookie = GlobalContext.CurrentCookieString; break; case 2: var frm = new FrmInputText(); var point = this.cmboCookieType.Parent.PointToScreen(this.cmboCookieType.Location); point.Y += this.cmboCookieType.Height + 1; frm.Location = point; frm.ShowDialog(); if (frm.DialogResult == DialogResult.OK) { this.mCurrentCookie = frm.InputText; } break; } }
/// <summary> /// 确定按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonOk_Click(object sender, EventArgs e) { string cookie = base.GetControlText(this.textBoxCookie).Trim(); if (cookie.Length < 1) { MessageBox.Show("请输入Cookie!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //登录 this.CookieString = cookie; var config = new ConfigStorage(GlobalContext.ConfigFileName); Thread thread = new Thread(() => { try { ChangeControlState(false); this.MyUserInfo = GlobalContext.PH.GetUserInfo(cookie); if (CertPath == null) { CookieInfo foundCookieInfo; if ((foundCookieInfo = config.GetCookieInfoByUserId(this.MyUserInfo.id)) != null) { CertPath = foundCookieInfo.CertPath; } } // 更新配置文件中的账户信息 config.UpdateCookieInfo(new CookieInfo() { Id = this.MyUserInfo.id, Name = this.MyUserInfo.nick, Cookie = cookie, CertPath = CertPath }); DialogResult = DialogResult.OK; } catch (Exception ex) { GlobalContext.MainForm.Output("登录失败:" + ex.Message); this.CookieString = null; this.CertPath = null; if (ex.Message.Contains("已过期")) { GlobalContext.Output("Cookie已过期,正在更新Cookie..."); string tk = CookieUtil.GetCookieValue(cookie, "_tk"); var cookieInfo = config.GetCookieInfoByCookieUserToken(tk); string id = null; if (cookieInfo == null || cookieInfo.Id == null) { var frm = new FrmInputText(); frm.Text = "请输入健康号"; frm.FormBorderStyle = FormBorderStyle.Sizable; frm.StartPosition = FormStartPosition.CenterScreen; if (frm.ShowDialog() != DialogResult.OK) { ChangeControlState(true); return; } id = frm.InputText.Trim(); } else { id = cookieInfo.Id.ToString(); } string certPath = null; if (cookieInfo == null || !File.Exists(cookieInfo.CertPath)) { var ofd = new OpenFileDialog(); ofd.Title = "请选择证书"; ofd.Filter = "证书文件(*.pfx)|*.pfx"; ofd.FilterIndex = 1; ofd.Multiselect = false; if (ofd.ShowDialog() != DialogResult.OK) { ChangeControlState(true); return; } certPath = ofd.FileName; } else { certPath = cookieInfo.CertPath; } try { var result = GlobalContext.PH.RenewUserTokenAndWebToken(id, cookie, certPath); if (result.Stat.Code == 0) { string cookieNew = string.Empty; cookieNew += "_tk=" + System.Web.HttpUtility.UrlEncode(result.Content[0].Token) + ";"; // 必须进行urlencode cookieNew += "_wtk=" + result.Content[0].WebToken + ";"; SetControlText(this.textBoxCookie, cookieNew); this.CertPath = certPath; // 重新登录 buttonOk_Click(null, null); //StringBuilder builder = new StringBuilder(); //builder.AppendLine("Uid:" + result.Content[0].Uid); //builder.AppendLine("Tk:" + result.Content[0].Token); //builder.AppendLine("Wtk:" + result.Content[0].WebToken); //builder.AppendLine("Expire:" + result.Content[0].GetExpireTime()); //builder.AppendLine("LockTime:" + result.Content[0].LockTime); //builder.AppendLine("请重新点击登录!"); //MsgBox.ShowInfo(builder.ToString(), "更新成功"); } } catch (Exception ex2) { MsgBox.ShowInfo("更新失败," + ex2.Message); } ChangeControlState(true); return; } else if (ex.Message.Contains("设备上退出")) { config.RemoveCookieInfo(new CookieInfo { Cookie = this.CookieString }); } MessageBox.Show("登录失败," + ex.Message); ChangeControlState(true); } finally { //重新读取历史Cookie信息 ConfigInfo configInfo = config.GetConfigInfo(); GlobalContext.HistoryCookies = configInfo.HistoryCookies; } }); thread.IsBackground = true; thread.SetApartmentState(ApartmentState.STA); thread.Start(); }