/// <summary> /// 认证用户信息 /// </summary> /// <param></param> /// <returns></returns> private bool Auth(TextMessage tm, ref string response) { SessionItem sessionItem = null; if (string.Equals(tm.Content, "Auth", StringComparison.OrdinalIgnoreCase)) { //检查是否已经认证,业务组件验证 if (UserManager.IsAuth(tm.FromUserName)) { //如果已经认证,提示 tm.Content = "您已经认证过了,无需再次认证!"; } else { AuthSessionItem authSessionItem = new AuthSessionItem(); authSessionItem.FromUserName = tm.FromUserName; sessionItem.Oper = Operation.Auth; sessionItem.Stage = OperationStage.First; sessionItem.Data = authSessionItem; Session.Set(tm.FromUserName, sessionItem); //输入账号,并将数据和步骤,写入缓存 tm.Content = "请输入您的个人账号"; } response = ResponseText(tm); return false; } //从Session获取用户信息 sessionItem = Session.Get(tm.FromUserName) as SessionItem; //如果会话存在,且当前操作为用户认证 if (sessionItem != null && sessionItem.Oper == Operation.Auth) { if (sessionItem.Stage == OperationStage.First) { tm.Content = tm.Content.Trim(); if (string.IsNullOrEmpty(tm.Content) || tm.Content.Length > 20) { tm.Content = "输入的个人账号不合法,请重新输入。"; response = ResponseText(tm); return false; } AuthSessionItem authSessionItem = sessionItem.Data as AuthSessionItem; if (authSessionItem != null) { authSessionItem.Code = tm.Content; } //更新缓存 sessionItem.Stage = OperationStage.Second; Session.Set(tm.FromUserName, sessionItem); tm.Content = "请输入您的员工卡号!\n退出认证请输入Exit。"; response = ResponseText(tm); } else if (sessionItem.Stage == OperationStage.Second) { string cardNum = null; if (!Common.TryConvertToCardNum(tm.Content, out cardNum)) { tm.Content = "员工卡号不合法,请重新输入。\n退出认证请输入Exit。"; response = ResponseText(tm); return false; } AuthSessionItem authSessionItem = sessionItem.Data as AuthSessionItem; if (authSessionItem != null) { authSessionItem.ID = cardNum; } //认证 string message; if (UserManager.Authenticate(authSessionItem, out message)) { tm.Content = "祝贺您,已经认证成功,可以使用通讯录的查询功能呢。"; //清理缓存 Session.Remove(tm.FromUserName); response = ResponseText(tm); return true; } else if (!string.IsNullOrEmpty(message)) { tm.Content = message; } else { tm.Content = "您输入的信息有误。\n重新认证请输入:Auth!"; } //过程结束:清理Session Session.Remove(tm.FromUserName); response = ResponseText(tm); return false; } } return false; }
3、退出会话,清理Session
在认证过程中,用户可以通过命令,强制退出当前操作,在退出当前操作时,需要清理会话信息。
/// <summary> /// 退出,并清理Session /// </summary> /// <param></param> /// <param></param> /// <returns></returns> private bool Exit(TextMessage tm, ref string response) { //退出 if (string.Equals(tm.Content, "Exit", StringComparison.OrdinalIgnoreCase)) { //清除Session Session.Remove(tm.FromUserName); tm.Content = "您已退出当前操作,请执行其他操作。"; response = ResponseText(tm); return true; } return false; }
三、用户认证通过,绑定微信账户
用户认证通过,并绑定微信OpenId,通过OpenId即可查询通讯录、查询个人积分以及消费记录等操作了。用户认证是一个身份认证过程,也是一个用户绑定过程。用户身份认证通过,即可通过微信账号查询具体信息了。这时候业务层可以根据微信分配的OpenId直接查询用户相关信息。
四、后记
通过这种方法,公众账号,可以通过小小的文本输入框,实现更多、更复杂的业务应用。当然,还是通过提供网页来进行信息录入,更直观便捷。