当前位置:网站首页>. Net core - a queuing system for wechat official account
. Net core - a queuing system for wechat official account
2022-07-03 10:52:00 【Dotnet cross platform】
Part1 Preface
Wechat code scanning login , Wechat scanning code to obtain queuing information is very common , But you know how the principle is realized ? Today I'm going to solve your doubts !
Part2 Overall process
1 The browser requests the server to get the QR code image
The browser generates a unique value uuid
adopt uuid Get the QR code with parameters , take uuid As key Put in redis Cache server
Server generation uuid Back to the browser
adopt uuid Get the QR code with parameters , take uuid As key Put in redis Cache server
2 User scan code
Scan QR code by mobile phone , Then carry it with your mobile phone uuid And user information to send a request to the server of the mobile phone , The mobile server will carry it after receiving it uuid Go to redis The server queries the user .
Sweep the login code :
After querying the user successfully, a Token To web server , By parsing this Token You can take out the user's information , Then the browser successfully logged into wechat .
Push queue information Call the template message after querying the user successfully , Then the queue information push succeeds .
Part3 Development
Take wechat scanning code to obtain queuing information as an example
First step , obtain uuid
To simplify the process , And generated by the front end uuid.
The second step , obtain Access token
public async Task<string> GetAccessToken()
{
var accesstoken = await "https://api.weixin.qq.com/cgi-bin/token"
.SetQueryParams(new
{
grant_type = "client_credential",
appid = WeixinSetting.Appid,
secret = WeixinSetting.Appsecret
})
.GetJsonAsync<AccessToken>();
return accesstoken.Access_Token;
}The third step , Use uuid Create QR code ticket
public async Task<string> GetTicket(string uuid, string token)
{
var accesstoken = await "https://api.weixin.qq.com/cgi-bin/qrcode/create"
.SetQueryParams(new
{
access_token = token
})
.PostJsonAsync(new
{
expire_seconds = 604800,
action_name = "QR_STR_SCENE",
action_info = new
{
scene = new
{
scene_str = uuid
}
}
}).ReceiveJson<TicketUrl>();
return accesstoken.Ticket;
}Step four , adopt ticket In exchange for QR code
If you have passed ticket Requirements for generating QR code , It can be handled by itself , If there is no interface that can directly access wechat official account
public async Task<byte[]> GetQrCode(string ticket)
{
return await "https://mp.weixin.qq.com/cgi-bin/showqrcode"
.SetQueryParams(new
{
ticket = ticket
})
.GetBytesAsync();
}The complete code is as follows
[HttpGet("GetQrCode")]
public async Task<IActionResult> GetQrCode(string uuid)
{
return new FileContentResult( await _qrCodeServices.ShowQrCode(uuid), "image/jpeg");
}public async Task<byte[]> ShowQrCode(string uuid)
{
string token = await GetAccessToken();
string ticket = await GetTicket(uuid, token);
return await GetQrCode(ticket);
}In this way, we will generate a QR code image with parameters

Step six , Wechat scanning code for callback
Handle the message template push logic in the callback method
[HttpPost]
public async Task<string> post()
{
IHttpBodyControlFeature httpBodyControlFeature = Request.HttpContext.Features.Get<IHttpBodyControlFeature>();
if (httpBodyControlFeature != null )
{
httpBodyControlFeature.AllowSynchronousIO = true;
}
string content = new StreamReader(Request.Body).ReadToEnd();
await qrCodeServices.PushTemplateMessage(content);
return "success";
}public async Task PushTemplateMessage(string content)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(content);
if (doc.DocumentElement["MsgType"].InnerText != "Event") return;
if (doc.DocumentElement["Event"].InnerText != "SCAN") return;
string value = doc.DocumentElement["FromUserName"].InnerText;
string token = await GetAccessToken();
var accesstoken = await "https://api.weixin.qq.com/cgi-bin/message/template/send"
.SetQueryParams(new
{
access_token = token
})
.PostJsonAsync(new
{
touser = value,
template_id = "i4h4yHvgMgIoJ8-Mr49XcpbhMxmRXFJ5EF5DoOTGOMc",
data = new
{
first = new
{
value = " Current queue location ",
color = "#173177"
},
keyword1 = new
{
value = "70",
color = "#173177"
},
keyword2 = new
{
value = "40 minute ",
color = "#173177"
}
}
});
}In this way, it is successful to scan wechat code to obtain queuing information

Part4 summary
The article comes from every bit of life , This is also my improvisation , If you have better skills , Welcome to exchange , It is much joyful to share the joy than enjoy alone. , That's all for this article , I hope it will help you .
边栏推荐
- [roast & brain hole] Some Thoughts on the bullet screen interactive game of Wei Shu Wu Three Kingdoms when visiting station B
- 独家分析 | 关于简历和面试的真 相
- Bid -- service commitment -- self summary
- 分组函数之rollup、cube函数、grouping sets函数
- 现在零基础转行软件测试还OK吗?
- Leetcode skimming ---977
- 测试理论概述
- Classification (data consolidation and grouping aggregation)
- User recommendation preference model based on attention enhanced knowledge perception
- Flink -- 内置函数(ALL)
猜你喜欢

缓存路由组件
![[untitled]](/img/41/adf5638e4a36417ce8dba3f2c4d9ed.jpg)
[untitled]

Entropy method to calculate weight

Set ArrayList nested map set loop traversal

使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》

C project - dormitory management system (1)

Interviewer: what is the internal implementation of the list in redis?

Drop out (pytoch)

Unity学习笔记:联网游戏Pixel Adventure 1学习过程&纠错心得

.Net Core-做一个微信公众号的排队系统
随机推荐
Flink <-->Redis的使用介绍+with参数
QT:QSS自定义 QSpinBox实例
ThreadLocal principle and usage scenario
QT:QSS自定义 QTabWidget 和 QTabBar实例
[combinatorial mathematics] pigeon's nest principle (simple form of pigeon's nest principle | simple form examples of pigeon's nest principle 1, 2, 3)
我,大厂测试员,降薪50%去国企,后悔了...
有些能力,是工作中学不来的,看看这篇超过90%同行
现在零基础转行软件测试还OK吗?
【蓝桥杯选拔赛真题44】Scratch消灭骷髅军团 少儿编程scratch蓝桥杯选拔赛真题讲解
Take you into the cloud native database industry, Amazon Aurora
《通信软件开发与应用》
Mysql5.7 installation and configuration tutorial (Graphic ultra detailed version)
Model selection for neural network introduction (pytorch)
独家分析 | 关于简历和面试的真 相
Wechat applet training notes 1
那些一门心思研究自动化测试的人,后来怎样了?
The story of a 30-year-old tester struggling, even lying flat is extravagant
Buy health products for parents
Extern keyword
6、 Data definition language of MySQL (1)