예제 #1
0
        /// <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();
        }
예제 #2
0
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <returns></returns>
        public ConfigInfo GetConfigInfo()
        {
            ConfigInfo info = new ConfigInfo();

            info.HistoryCookies = new List <CookieInfo>();
            info.IsTestMode     = false;
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(this.fileName);
                XmlNode root = doc.DocumentElement;

                //历史Cookies
                XmlNodeList historyCookieNodes = root.SelectNodes("ItemGroup/Cookie");
                foreach (XmlNode cookieNode in historyCookieNodes)
                {
                    var cookieInfo = new CookieInfo();
                    cookieInfo.Cookie = cookieNode.InnerText.Trim();
                    cookieInfo.Name   = GetNodeAttribute(cookieNode, "name", "");
                    var strId = GetNodeAttribute(cookieNode, "id", null);
                    var id    = 0l;
                    if (long.TryParse(strId, out id))
                    {
                        cookieInfo.Id = id;
                    }
                    cookieInfo.CertData = GetNodeAttribute(cookieNode, "certData", null);

                    info.HistoryCookies.Add(cookieInfo);
                }

                //是否为测试模式
                XmlNode isTestModeNode = root.SelectSingleNode("PropertyGroup/IsTestMode");
                bool    isTestMode;
                if (bool.TryParse(isTestModeNode.InnerText, out isTestMode))
                {
                    info.IsTestMode = isTestMode;
                }

                // 随机步数
                XmlNode randomStepNode = root.SelectSingleNode("PropertyGroup/RandomStep");
                info.RandomMinStep =
                    Convert.ToInt32(GetNodeAttribute(randomStepNode, "min",
                                                     ConfigInfo.Default_Random_Min_Step.ToString()));
                info.RandomMaxStep =
                    Convert.ToInt32(GetNodeAttribute(randomStepNode, "max",
                                                     ConfigInfo.Default_Random_Max_Step.ToString()));

                // 请求设置
                XmlNode reqNode = root.SelectSingleNode("PropertyGroup/Request");
                info.RequestTimeoutInSeconds =
                    Convert.ToInt32(GetNodeAttribute(reqNode, "timeout",
                                                     ConfigInfo.Default_Request_Timeout.ToString()));

                info.IsAutoFetchYesterdayBonus =
                    Convert.ToBoolean(GetNodeValue(
                                          root.SelectSingleNode("PropertyGroup/IsAutoFetchYesterdayBonus"), "true"));

                info.IsAutoGrabGold =
                    Convert.ToBoolean(GetNodeValue(
                                          root.SelectSingleNode("PropertyGroup/IsAutoGrabGold"), "true"));

                info.IsAutoGrabTreasure =
                    Convert.ToBoolean(GetNodeValue(
                                          root.SelectSingleNode("PropertyGroup/IsAutoGrabTreasure"), "false"));

                info.IsAutoUploadWalkData =
                    Convert.ToBoolean(GetNodeValue(
                                          root.SelectSingleNode("PropertyGroup/IsAutoUploadWalkData"), "false"));

                info.CreateOrderTimespan =
                    Convert.ToInt32(GetNodeValue(root.SelectSingleNode("PropertyGroup/CreateOrderTimespan"), "false"));
            }
            catch (Exception ex)
            {
                // 修复配置文件不存在时,导致读取的RequestTimeoutInSeconds为0,从而查询接口时超时的bug
                info.RandomMinStep             = 30000;
                info.RandomMaxStep             = 40000;
                info.IsTestMode                = false;
                info.IsAutoFetchYesterdayBonus = true;
                info.IsAutoGrabGold            = true;
                info.IsAutoGrabTreasure        = true;
                info.IsAutoUploadWalkData      = true;
                info.RequestTimeoutInSeconds   = 15000;
                info.CreateOrderTimespan       = 14;
            }

            return(info);
        }
예제 #3
0
 /// <summary>
 /// 重新读取配置文件
 /// </summary>
 public static void RereadConfig()
 {
     CurrentConfigInfo = ConfigStorage.GetInstance().GetConfigInfo();
     HistoryCookies    = CurrentConfigInfo.HistoryCookies;
     IsTestMode        = CurrentConfigInfo.IsTestMode;
 }