/// <summary> /// 登录 /// </summary> public void LogOn() { if (CurrentState != State.LoggedOff) return; CurrentState = State.LoggingOn; var username = Settings.User.Username ?? string.Empty; var password = Settings.User.Password ?? string.Empty; ThreadPool.QueueUserWorkItem(state => { LogOnResult result = null; List<string> errorMessages = new List<string>(); if (Regex.Match(username, @"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$", RegexOptions.IgnoreCase).Success) { var resultEmail = LogOnWithEmail(username, password); if (resultEmail != null) { if (!resultEmail.r) { result = resultEmail; } else { errorMessages.Add(resultEmail.err); } } } if (result == null) { var resultUsername = LogOnWithUsername(username, password); if (resultUsername != null) { if (!resultUsername.r) { result = resultUsername; } else { errorMessages.Add(resultUsername.err); } } } if (result == null) { result = new LogOnResult {r = true}; var invalidUsername = new[] {"invalid_user_name", "invalidate_email", "wrong_email"}; errorMessages = (from message in errorMessages where !string.IsNullOrEmpty(message) select invalidUsername.Contains(message) ? "invalid_username" : message).Distinct().ToList(); if (errorMessages.Count == 0) { result.err = "unknown_error"; } else { result.err = errorMessages.FirstOrDefault(message => message != "invalid_username") ?? errorMessages[0]; } } UpdateWhenLogOn(result); }); }
///// <summary> ///// 验证码ID ///// </summary> //private string captchaId; #endregion #region 成员方法 ///// <summary> ///// 更新验证码 ///// </summary> //public void UpdateCaptcha() //{ // HasCaptcha = true; // captchaId = null; // CaptchaUrl = null; // ThreadPool.QueueUserWorkItem(new WaitCallback((state) => // { // captchaId = new ConnectionBase().Get("http://douban.fm/j/new_captcha"); // Dispatcher.BeginInvoke(new Action(() => // { // if (string.IsNullOrEmpty(captchaId)) // { // HasCaptcha = false; // captchaId = null; // CaptchaUrl = null; // } // else // { // captchaId = captchaId.Trim('\"'); // HasCaptcha = true; // CaptchaUrl = @"http://douban.fm/misc/captcha?size=m&id=" + captchaId; // } // })); // })); //} ///// <summary> ///// 根据douban.fm的主页的HTML代码更新登录状态 ///// </summary> ///// <param name="html">HTML文件</param> //internal void Update(string html) //{ // if (!string.IsNullOrEmpty(html)) // { // //获取昵称和播放记录 // Match match2 = Regex.Match(html, @"""user_name""[^<>]*?>(?!{{)([^<>]*?)\s*<", RegexOptions.Singleline); // string nickname = match2.Groups[1].Value; // Match match3 = Regex.Match(html, @"累积收听.*?(\d+).*?首"); // int played = 0; // int.TryParse(match3.Groups[1].Value, out played); // Match match4 = Regex.Match(html, @"加红心.*?(\d+).*?首"); // int liked = 0; // int.TryParse(match4.Groups[1].Value, out liked); // Match match5 = Regex.Match(html, @"(\d+).*?首不再播放"); // int banned = 0; // int.TryParse(match5.Groups[1].Value, out banned); // //更改属性 // Dispatcher.Invoke(new Action(() => // { // /*System.Diagnostics.Debug.WriteLine("**********************************************************************"); // System.Diagnostics.Debug.WriteLine(DateTime.Now + " 以下是本次“登录/注销”返回的结果页面"); // System.Diagnostics.Debug.Indent(); // System.Diagnostics.Debug.WriteLine(html); // System.Diagnostics.Debug.Unindent(); // System.Diagnostics.Debug.WriteLine("**********************************************************************"); // */ // if (!string.IsNullOrEmpty(nickname)) // { // Settings.User.Nickname = nickname; // Settings.User.Played = played; // Settings.User.Liked = liked; // Settings.User.Banned = banned; // System.Diagnostics.Debug.WriteLine("已登录"); // CurrentState = State.LoggedOn; // } // else // { // Settings.User.Nickname = string.Empty; // System.Diagnostics.Debug.WriteLine("已注销"); // CurrentState = State.LoggedOff; // } // })); // } // else // Dispatcher.Invoke(new Action(() => // { // if (CurrentState == State.LoggingOn) CurrentState = State.LoggedOff; // else if (CurrentState == State.LoggingOff) CurrentState = State.LoggedOn; // else if (CurrentState == State.Unknown) CurrentState = State.LoggedOff; // else CurrentState = State.LoggedOff; // })); //} /// <summary> /// 根据服务器返回的登录结果更新登录状态 /// </summary> private void UpdateWhenLogOn(LogOnResult result) { Debug.Assert(result != null, "result != null"); string errorMessage = null; if (!result.r) { Settings.User.UserID = result.user_id; Settings.User.Token = result.token; Settings.User.Expire = result.expire; Settings.User.Nickname = result.user_name; Settings.User.Email = result.email; UpdateUserInfo(); } else { Debug.WriteLine(result.err); switch (result.err) { case "invalid_username": errorMessage = Resources.Resources.InvalidUsername; break; case "wrong_password": errorMessage = Resources.Resources.WrongPassword; break; case null: case "": case "unknown_error": errorMessage = Resources.Resources.UnknownError; break; default: errorMessage = result.err; break; } } Dispatcher.Invoke(new Action(() => { LogOnFailedMessage = errorMessage; if (!string.IsNullOrEmpty(Settings.User.Token)) { CurrentState = State.LoggedOn; } else { ResetUser(); } })); }