/// <summary> /// 操作步聚:使用账号密码验证码登录系统 /// </summary> /// <param name="model"></param> /// <param name="captchaValue"></param> private void StepByLogin(QuestSourceModel model, string captchaValue) { ShowStatusText(string.Format("{0}[登录],正在登录系统", model.Name)); bool isSuccess = false; if (!VerifyHelper.IsEmpty(captchaValue)) { var result = AppHelper.GetLoginResult(model.Account, model.Password, captchaValue, model.Cookies, (res, loginModel) => { SetSourceLoginStatus(model, res, loginModel); }, (res, loginModel) => { model.Message = "登录失败"; model.IsLogin = false; }); } ShowStatusText(string.Format("{0}[登录], {1}", model.Name, isSuccess ? "成功" : "失败")); //{ // "success" : true, // "msg" : "登录成功", // "obj" : { // "returnUrl" : "?UUID=94b3bf6d002f4fe586e122ce93c0299d&name=%E5%B0%B9%E4%BC%8A%E5%A8%9C&mail=1161536884%40qq.com", // "akashaUrl" : "http://order.etest.net.cn/order/mySignUp", // "formData" : null, // "userName" : "尹伊娜" // }, // "token" : "" // } }
/// <summary> /// 操作步聚:获取验证码内容(从聚合API接口) /// </summary> /// <param name="base64"></param> /// <returns></returns> private string StepByGetCaptchaCodeByJUHE(QuestSourceModel model, string base64) { ShowStatusText(string.Format("{0}[登录],自动识别验证码", model.Name)); string captchaCodeValue = string.Empty; if (!VerifyHelper.IsEmpty(base64)) { var options = new HttpClientOptions(); var client = new HttpWebClientUtility(); options.URL = AppHelper.UrlCaptchaValJuHe; options.Method = "POST"; options.ContentType = "application/x-www-form-urlencoded"; options.PostData = string.Format("key={0}&codeType={1}&base64Str={2}", AppHelper.ValueJuHeKey1, AppHelper.ValueJuHeKey2, CodingHelper.UrlEncoding(base64)); var result = client.Request(options); if (!VerifyHelper.IsEmpty(result.Content)) { var obj = JsonHelper.Deserialize <JObject>(result.Content); if (obj != null && StringHelper.Get(obj["error_code"]) == "0") { captchaCodeValue = StringHelper.Get(obj["result"]); } } } ShowStatusText(string.Format("{0}[登录],验证码识别 {1}", model.Name, !VerifyHelper.IsEmpty(captchaCodeValue) ? "成功" : "失败")); return(captchaCodeValue); }
/// <summary> /// 申请执行 /// </summary> /// <param name="model"></param> private void WorkApply(QuestSourceModel model) { if (model == null) { return; } if (model.IsWork) { return; } //登录处理 if (!model.IsLogin) { WorkLogin(model); } //登陆失败 if (!model.IsLogin) { return; } //申请处理 StepByApply(model); }
/// <summary> /// 将Excel 数据源 Row 转为 QuestSourceModel /// </summary> private QuestSourceModel ExcelRowToSource(IRow head, IRow row) { QuestSourceModel model = null; if (row != null) { model = new QuestSourceModel(); model.Id = Guid.NewGuid(); model.Name = StringHelper.Get(row.Cells[1]); model.Account = StringHelper.Get(row.Cells[2]); model.Password = StringHelper.Get(row.Cells[3]); model.Cookies = new CookieCollection(); model.Message = ""; model.Examination = ""; model.UserId = ""; model.Examination = ""; model.RelationshipValue = ""; model.AreaValue = ""; model.SubjectValue = ""; model.ExamValue = ""; model.PackageValue = ""; model.ExamText = StringHelper.Get(row.Cells[6]); model.AreaText = StringHelper.Get(row.Cells[7]); model.OrgText = StringHelper.Get(row.Cells[8]); model.CategoryText = StringHelper.Get(row.Cells[9]); model.RetryTotal = 0; model.IsLogin = false; model.IsWork = false; } return(model); }
/// <summary> /// 操作步聚:访问站点,获取Cookies及验证码Base64 /// </summary> /// <param name="model"></param> /// <returns></returns> private string StepByOpenAndGetCaptchaBase64(QuestSourceModel model) { ShowStatusText(string.Format("{0}[登录],正在读取验证码", model.Name)); string codeBase64 = string.Empty; var result = AppHelper.GetCaptchResult(); if (!VerifyHelper.IsEmpty(result.Content)) { model.Cookies = result.CookieCollection; codeBase64 = result.Content; } ShowStatusText(string.Format("{0}[登录],验证码读取 {1}", model.Name, !VerifyHelper.IsEmpty(codeBase64) ? "成功" : "失败")); return(codeBase64); }
/// <summary> /// 设置登录成功后的登录状态 /// </summary> public void SetSourceLoginStatus(QuestSourceModel source, HttpClientResult result, LoginResultModel loginModel) { if (!VerifyHelper.IsEmpty(loginModel) && !VerifyHelper.IsEmpty(loginModel.obj) && loginModel.success) { var resultObj = loginModel.obj as JObject; var paramList = StringHelper.GetToListKeyValue(resultObj.GetString("returnUrl").Replace("?", ""), listSplit: '&'); if (!VerifyHelper.IsEmpty(paramList) && paramList.Where(x => x.Key.ToLower() == "uuid").Count() > 0) { source.Message = ""; source.RetryTotal = 0; source.Cookies = result.CookieCollection; source.IsLogin = true; source.UserId = paramList.Where(x => x.Key.ToLower() == "uuid").FirstOrDefault().Value; } } }
/// <summary> /// 自动登录 /// </summary> /// <param name="model"></param> private void WorkLogin(QuestSourceModel model) { if (model == null) { return; } if (model.IsLogin) { return; } string captchaBase64 = string.Empty; string captchaCode = string.Empty; //(0)获取验证码,及第一次请求时Cookies captchaBase64 = StepByOpenAndGetCaptchaBase64(model); //(1)验证码识别 if (VerifyHelper.IsEmpty(captchaBase64)) { model.Message = "获取验证码失败"; } else { captchaCode = StepByGetCaptchaCodeByJUHE(model, captchaBase64); } //(2)登录操作 if (VerifyHelper.IsEmpty(captchaCode)) { model.Message = "验证码识别失败"; } else { StepByLogin(model, captchaCode); } ShowStatusText(string.Format("{0}[登录]({1})", model.Name, model.IsLogin ? "成功" : "失败"), loggerLev: model.IsLogin?EnumLoggerLev.Info: EnumLoggerLev.Error); }
/// <summary> /// 操作步聚:报名申请 /// </summary> /// <param name="model"></param> private void StepByApply(QuestSourceModel model) { ShowStatusText(string.Format("{0}[申请],正在申请", model.Name)); var options = new HttpClientOptions(); var client = new HttpWebClientUtility(); options.URL = AppHelper.UrlApply; options.Method = "POST"; options.ContentType = "application/x-www-form-urlencoded"; options.CookieCollection = model.Cookies; List <KeyValueModel> datas = new List <KeyValueModel>(); var examValueArr = model.ExamValue.Split('|'); var examNameArr = model.ExamNameValue.Split('|'); var areaValueArr = model.AreaValue.Split('|'); var relationshipValueArr = model.RelationshipValue.Split('|'); var categoryValueArr = model.CategoryValue.Split('|'); var subjectValueArr = model.SubjectValue.Split('|'); var packageValueArr = model.PackageValue.Split('|'); datas.Add(new KeyValueModel("scoreId", "")); datas.Add(new KeyValueModel("uId", model.UserId)); datas.Add(new KeyValueModel("proId", AppHelper.ValueProId)); datas.Add(new KeyValueModel("nodeCode", AppHelper.ValueNodeCode)); datas.Add(new KeyValueModel("studentType", AppHelper.ValueStudentType)); datas.Add(new KeyValueModel("paymentType", AppHelper.ValuePaymentType)); datas.Add(new KeyValueModel("examId", examValueArr[0])); datas.Add(new KeyValueModel("orgGeo", areaValueArr[0])); datas.Add(new KeyValueModel("relationshipId", relationshipValueArr[0])); datas.Add(new KeyValueModel("packageId", string.Format("{0}#", categoryValueArr[0]))); for (var i = 0; i < model.PackageIdValue.Split('|').Length; i++) { datas.Add(new KeyValueModel("regList[0].examId", examValueArr[i])); datas.Add(new KeyValueModel("regList[0].examDate", examNameArr[i])); datas.Add(new KeyValueModel("regList[0].orgGeo", areaValueArr[i])); datas.Add(new KeyValueModel("regList[0].relationshipId", relationshipValueArr[i])); datas.Add(new KeyValueModel("regList[0].categoryId", categoryValueArr[i])); datas.Add(new KeyValueModel("regList[0].subjectId", subjectValueArr[i])); datas.Add(new KeyValueModel("regList[0].packageId", packageValueArr[i])); } options.PostData = StringHelper.GetFormStringByKeyValues(datas); var result = client.Request(options); if (!VerifyHelper.IsEmpty(result.Content)) { var resModel = JsonHelper.Deserialize <ApplyResultModel>(result.Content); if (resModel != null && resModel.success) { model.Message = "申请成功"; model.RetryTotal = 0; model.IsWork = true; } else { model.Message = resModel.msg; } } ShowStatusText(string.Format("{0}[申请]({1})", model.Name, model.IsWork ? "成功" : "失败"), loggerLev: model.IsWork?EnumLoggerLev.Info: EnumLoggerLev.Error); }