public async override Task UpdateCoreAsync() { await base.UpdateCoreAsync(); Site.GetService <AccountManager>().EnsureLoggedIn(); var doc = await XjtuUtility.OpenHtmlPageAsync(CardTransferPageUrl, Client); // 如果刚刚登录,则会跳转到校园卡管理主页面。 if (XjtuUtility.GetTitle(doc).Contains("校园卡电子服务平台")) { doc = await XjtuUtility.OpenHtmlPageAsync(CardTransferPageUrl, Client); if (XjtuUtility.GetTitle(doc).Contains("校园卡电子服务平台")) { throw new UnexpectedHtmlException(CardTransferPageUrl); } } var str = doc.DocumentNode.Descendants("p") .Select(e => e.InnerText) .FirstOrDefault(t => t?.Contains("校园卡余额") ?? false); var match = decimalMatcher.Match(str); if (match.Success) { _Balance = Convert.ToDecimal(match.Value); } else { _Balance = null; } }
public override async Task UpdateCoreAsync() { await base.UpdateCoreAsync(); var doc = await XjtuUtility.OpenHtmlPageAsync(PortalPageUrl, Client); var loginForm = doc.GetElementById("fm1"); if (loginForm == null) { //已经登录。 // my.xjtu.edu.cn _IsLoggedIn = true; casAuthenticationDictBuffer = null; var node = doc.GetElementById("portalWelcome"); _UserName = HtmlEntity.DeEntitize(node.Element("span").InnerText)?.Trim(); } else { _IsLoggedIn = false; casAuthenticationUrl = "https://cas.xjtu.edu.cn" + loginForm.GetAttributeValue("action", ""); // 修正 HAP 识别 loginForm 的时候出现的某些问题。 //while (loginForm != null && loginForm.ChildNodes.All(n => n.NodeType != HtmlNodeType.Element)) //{ // loginForm = loginForm.NextSibling; //} //if (loginForm == null) throw new UnexpectedHtmlException(PortalPageUrl); loginForm = loginForm.ParentNode; if (casAuthenticationDictBuffer == null) { casAuthenticationDictBuffer = new Dictionary <string, string>(); } else { casAuthenticationDictBuffer.Clear(); } foreach (var e in loginForm.Descendants("input")) { var name = e.GetAttributeValue("name", ""); if (!string.IsNullOrEmpty(name)) { casAuthenticationDictBuffer.Add(name, e.GetAttributeValue("value", "")); } } /* * TODO: 检查验证码。 * <div id="codeDiv" style="float:left;margin:5px 0;display:none;"> * <label for="password" style="float:left; line-height:22px;"> * 验证码 * </label> * <input style="float:left;width:60px; border:1px solid #8e8e8e;" type="text" id="code" name="code" maxlength="4"> <img style="float:left;" alt="" id="ImageCodeServlet" src="/ImageCodeServlet"><a class="code-text" href="#" onclick="javascript:reloadCode();" title="点击更新验证码">看不清?</a> * </div> */ } }
/// <summary> /// 向校园卡转账。 /// </summary> public async Task <bool> Transfer(decimal amount) { if (amount <= 0) { throw new ArgumentOutOfRangeException(nameof(amount)); } var vcr = Site.RequireService <IVerificationCodeRecognizer>(); var pp = Site.RequireService <IXjtuCardPasswordProvider>(); var doc = await XjtuUtility.OpenHtmlPageAsync(CardTransferPageUrl, Client); var captchaSrc = doc.GetElementById("img_transCheckCode")?.GetAttributeValue("src", ""); if (string.IsNullOrEmpty(captchaSrc)) { throw new UnexpectedHtmlException(CardTransferPageUrl); } captchaSrc = SiteRootUrl + captchaSrc; /* * password=xxxx&checkCode=1222&amt=10.00&fcard=bcard&tocard=card&bankno=&bankpwd= */ var dict = new Dictionary <string, string>() { { "password", "xxxx" }, { "checkCode", "xxxx" }, { "amt", amount.ToString() }, { "fcard", "bcard" }, { "tocard", "card" }, { "bankno", "" }, { "bankpwd", "" } }; using (var kps = await Client.DownloadStreamAsync(CardTransferCaptchaKeypadUrl)) { var result = await pp.GetPasswordAsync(kps, mapAreas, Site); if (result == null) { return(false); } dict["password"] = result; } using (var cs = await Client.DownloadStreamAsync(captchaSrc)) { var result = await vcr.RecognizeAsync(cs, Site); if (result == null) { return(false); } dict["checkCode"] = result; } using (var resp = await Client.UploadAsync(CardTransferPostUrl, dict)) { var result = await resp.Content.ReadAsStringAsync(); var jres = JObject.Parse(result); //{"ret":false,"msg":"验证码不正确"} if ((bool?)jres["ret"] != true) { throw new OperationFailedException((string)jres["msg"]); } } return(true); }