Пример #1
0
        public async Task <bool> LoginAsync(string username, string password, int questionId, string answer)
        {
            // 先清除 cookie
            _httpClient.ClearCookies();

            var postData = new List <KeyValuePair <string, object> >();

            postData.Add(new KeyValuePair <string, object>("username", username));
            postData.Add(new KeyValuePair <string, object>("password", password));
            postData.Add(new KeyValuePair <string, object>("questionid", questionId));
            postData.Add(new KeyValuePair <string, object>("answer", answer));

            var    cts           = new CancellationTokenSource();
            string resultContent = await _httpClient.PostAsync("http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1", postData, cts);

            // 实例化 HtmlAgilityPack.HtmlDocument 对象
            HtmlDocument doc = new HtmlDocument();

            // 载入HTML
            doc.LoadHtml(resultContent);

            var    root = doc.DocumentNode;
            string loginResultMessage = root.InnerText.Replace("<![CDATA[", string.Empty).Replace("]]>", string.Empty);

            if (loginResultMessage.Contains("欢迎") && !loginResultMessage.Contains("错误") && !loginResultMessage.Contains("失败") && !loginResultMessage.Contains("非激活"))
            {
                // 登录成功就获取一次 formhash/uid/hash,用于发布文本信息和上载图片
                await LoadHashAndUserIdAsync();

                Username = username;

                // 先清除之前账号的首选状态
                ClearDefaultStatus();

                var    acc     = new AccountItemModel(UserId, username, password, questionId, answer, true);
                string jsonStr = JsonConvert.SerializeObject(acc);
                if (_container.Values.ContainsKey(UserId.ToString()))
                {
                    _container.Values[UserId.ToString()] = jsonStr;
                }
                else
                {
                    _container.Values.Add(UserId.ToString(), jsonStr);
                }

                return(true);
            }
            else
            {
                await new MessageDialog(loginResultMessage, "登录失败").ShowAsync();
                return(false);
            }
        }
        /// <summary>
        /// 为科目设置上下级关系
        /// </summary>
        /// <param name="accountItem"></param>
        /// <param name="allItems"></param>
        public void ProcessAccountItemParent(AccountItemModel accountItem, List <AccountItemModel> allItems)
        {
            var children = allItems.Where(x => x.AccountLevel == accountItem.AccountLevel + 1 && x.AccountCode.Contains(accountItem.AccountCode)).ToList();

            if (children.Count > 0)
            {
                foreach (var item in children)
                {
                    item.ParentCode = accountItem.AccountCode;
                    ProcessAccountItemParent(item, allItems);
                }
            }
            else
            {
                accountItem.IsLeaf = 1;
            }
            accountItem.AccountTypeId    = Convert.ToInt32(accountItem.AccountCode.Substring(0, 4)) < 6401 ? 1 : 2;
            accountItem.AccountSumTypeId = accountItem.AccountCode == "6813" ? 2 : 1;
        }