WeChat public platform development Session processing
Mar 06, 2017 am 09:12 AM
In the WeChat window, the input information is limited, and we need to request some information multiple times.
For example: when binding users, we need to enter relevant information of the user, such as: user name, password, or name, phone number, and if the server side is verified, the System users are bound to WeChat users.
Then, this WeChat account will have certain functional permissions, and you can check points, consumption records, etc. Service account: China Merchants Bank credit card has many functions.
The WeChat client cannot cache information, and the input information is limited. It needs to make multiple requests and save the current session status on the server. This requires Session.
This article takes user authentication and account binding as an example to illustrate the specific processing.
1. Create a general Session processing mechanism.
In order to better explain the principle and facilitate expansion, we will design the Session ourselves. Of course, System.Web.SessionState.HttpSessionState can also be used here, which is a commonly used Session mechanism on the Web.
1. Custom Session
is used to store session fragments and related data.
class Session { /// <summary> /// 緩存hashtable /// </summary> private static Hashtable mDic = new Hashtable(); /// <summary> /// 添加 /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> public static void Add(string key, object value) { mDic[key] = value; } /// <summary> /// 移除 /// </summary> /// <param name="key">key</param> public static void Remove(string key) { if (Contains(key)) { mDic.Remove(key); } } /// <summary> /// 設置值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void Set(string key, object value) { mDic[key] = value; } /// <summary> /// 獲取值 /// </summary> /// <param name="key"></param> /// <returns></returns> public static object Get(string key) { return mDic[key]; } /// <summary> /// 是否含有 /// </summary> /// <param name="key">key</param> /// <returns>bool</returns> public static bool Contains(string key) { return mDic.ContainsKey(key); } /// <summary> /// 清空所有項 /// </summary> public static void Clear() { mDic.Clear(); } }
2. Operation type
Record the specific operation type and identify the specific operation of the current session
/// <summary> /// 操作類型 /// </summary> enum Operation { /// <summary> /// 認證 /// </summary> Auth, /// <summary> /// 添加用戶 /// </summary> CreateUser }
3. Operation process enumeration
is used to identify the current operation and which stage it is in. Different processes are performed at different stages.
/// <summary> /// 操作過程 /// </summary> enum OperationStage { /// <summary> /// 默認 /// </summary> Default, /// <summary> /// 第一步 /// </summary> First, /// <summary> /// 第二步 /// </summary> Second, /// <summary> /// 第三步 /// </summary> Third }
4. Session cache item
Cache record item, which records the operation type, operation steps and session object. In order to facilitate session management, the last access time and whether to automatically clear the logo are also added.
class SessionItem { /// <summary> /// 操作類型 /// </summary> public Operation Oper { get; set; } /// <summary> /// 當前步驟 /// </summary> public OperationStage Stage { get; set; } /// <summary> /// 數(shù)據(jù)對象 /// </summary> public object Data { get; set; } /// <summary> /// 是否自動刪除 /// </summary> public bool AutoRemove { get; set; } /// <summary> /// 最后更新時間 /// </summary> public DateTime UpdateTime { get; set; } }
Second, we need to add Session processing to the message processing.
1. Add cache item data object
This object records the relevant information entered by the user during the session. It is also used as an object for business processing data.
class AuthSessionItem { /// <summary> /// 用戶名 /// </summary> public string FromUserName { get; set; } /// <summary> /// 賬號 /// </summary> public string Code { get; set; } /// <summary> /// 唯一標識 /// </summary> public string ID { get; set; } }
2. Authentication processing process
1) Start entering the authentication, identify according to the authentication keyword, and start session, and cache relevant data
2) Prompt to enter personal account information
3) WeChat users enter personal account, the server records the account information, and prompts to enter employee card number
4) The WeChat user enters the card number information, the server records the card number information, and calls the specific authentication logic
5) The user passes the authentication, binds the WeChat OpenId, prompts for successful binding information, and clears the session.
During the authentication process, the legality of user input information needs to be verified, and during the session, the user is supported to exit the current operation.
/// <summary> /// 認證用戶信息 /// </summary> /// <param name="tm"></param> /// <returns></returns> private bool Auth(TextMessage tm, ref string response) { SessionItem sessionItem = null; if (string.Equals(tm.Content, "Auth", StringComparison.OrdinalIgnoreCase)) { //檢查是否已經認證,業(yè)務組件驗證 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); //輸入賬號,并將數(shù)據(jù)和步驟,寫入緩存 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. Exit the session and clean up the Session
During the authentication process, the user can forcefully exit the current operation through commands. After exiting the current During operation, session information needs to be cleared.
/// <summary> /// 退出,并清理Session /// </summary> /// <param name="tm"></param> /// <param name="response"></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 = "您已退出當前操作,請執(zhí)行其他操作。"; response = ResponseText(tm); return true; } return false; }
3. User authentication passed, binding WeChat account
User authentication passed, and binding WeChat OpenId, you can query through OpenId Address book, query personal points and consumption records and other operations. User authentication is an identity authentication process and a user binding process. If the user identity is authenticated, you can query specific information through your WeChat account. At this time, the business layer can directly query user-related information based on the OpenId assigned by WeChat.
4. Postscript
Through this method, public accounts can realize more and more complex business applications through small text input boxes. Of course, it is more intuitive and convenient to enter information by providing a web page.
For more articles related to WeChat public platform development and Session processing, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)