概述
最近在整一个微信扫码用户帐号绑定功能。为了满足用户帐号绑定场景的需要,通过生成用户自己的二维码,用户扫描后,公众号可以接收到事件推送。
如下
1、用户登录扫码
2、绑定成功
实现思路
扫码绑定账户,其实就是扫描带有用户信息的二维码,微信端接收到消息,会向服务器推送一个扫描事件,服务端接收到信息,进行处理,进而在界面进行信息反馈。
用户登录系统=》生成带有用户id的二维码=》用户打开微信扫码=》
微信服务器向我们的系统回调扫描事件=》得到扫描的用户id+openid=>
进行绑定操作=》进而把结果反映到界面
主要代码实现
1、获取二维码
string domain = BqoolWebSite.WebSiteMapping[BqoolWebSiteType.BigCRMWebService][CommonSetting.GetEnvironmentMode()];string url = domain + "/api/WeChat/GetQrCode";var data = new GetQrCodeParam(){Account = "xxxxx", //AccountUser_ID = "xxxxx",//User_IDDate = DateTime.UtcNow.ToUnixTimestamp(),Action = BaseRequestParam.ActionType.Get,IsRefresh = false //是否强制刷新 默认false};_nLogger.Info($"WeChat GetQrCode Call BigCRM Web - Url : {url}, data : {data.ToJsonString()}");var result = HttpTools.EncryptPost<GetQrCodeParam, ApiResult>(url, data);_nLogger.Info($"WeChat GetQrCode Call BigCRM Web Result - Url : {url}, result : {result.ToJsonString()}");
if (result.Success && result.Data != null){// result.Data GetQrCodeResult}
2、获取绑定信息
string domain = BqoolWebSite.WebSiteMapping[BqoolWebSiteType.BigCRMWebService][CommonSetting.GetEnvironmentMode()];string url = domain + "/api/WeChat/GetBindInfo";var data = new GetBindInfoParam(){Account = "xxxxx", //AccountUser_ID = "xxxxxx",//User_IDDate = DateTime.UtcNow.ToUnixTimestamp(),Action = BaseRequestParam.ActionType.Get};_nLogger.Info($"WeChat GetBindInfo Call BigCRM Web - Url : {url}, data : {data.ToJsonString()}");var result = HttpTools.EncryptPost<GetBindInfoParam, ApiResult>(url, data);_nLogger.Info($"WeChat GetBindInfo Call BigCRM Web Result - Url : {url}, result : {result.ToJsonString()}");if (result.Success && result.Data != null){// result.Data GetBindInfoResult}
3、取消订阅
string domain = BqoolWebSite.WebSiteMapping[BqoolWebSiteType.BigCRMWebService][CommonSetting.GetEnvironmentMode()];string url = domain + "/api/WeChat/Disconnect";var data = new GetBindInfoParam (){Account = "xxxx", //AccountUser_ID = "xxxxx",//User_IDDate = DateTime.UtcNow.ToUnixTimestamp(),Action = BaseRequestParam.ActionType.Update};_nLogger.Info($"WeChat Disconnect Call BigCRM Web - Url : {url}, data : {data.ToJsonString()}");var result = HttpTools.EncryptPost< GetBindInfoParam, ApiResult>(url, data);_nLogger.Info($"WeChat Disconnect Call BigCRM Web Result - Url : {url}, result : {result.ToJsonString()}");if (result.Success ){//ApiResult}