当前位置:网站首页>.Net Core-做一个微信公众号的排队系统
.Net Core-做一个微信公众号的排队系统
2022-07-03 09:38:00 【dotNET跨平台】
Part1前言
微信扫码登录,微信扫码获取排队信息这些场景很常见,但是大家知道里面的原理是怎么实现的呢?今天我就来给大家解惑!
Part2总体流程
1浏览器请求服务器获取到二维码图片
浏览器生成唯一值uuid
通过uuid获取到带参数的二维码,将uuid作为key存进redis缓存服务器里面
服务端生成uuid返回给浏览器
通过uuid获取到带参数的二维码,将uuid作为key存进redis缓存服务器里面
2用户扫码
手机进行扫描二维码,然后手机携带uuid和用户信息向手机的服务器发送请求,手机服务器收到之后携带uuid去redis服务器查询用户。
扫码登录:
查询用户成功之后返回一个Token给网页服务器,通过解析这个Token可以拿出用户的信息,然后浏览器就成功登录微信了。
推送排队信息 查询用户成功之后调用模板消息,然后排队信息推送就成功了。
Part3开发
以微信扫码获取排队信息为例
第一步,获取uuid
为了简化流程,且由前端生成uuid。
第二步,获取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;
}
第三步,使用uuid创建二维码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;
}
第四步,通过 ticket 换取二维码
如果自己有通过ticket生成二维码的需求,可自行处理, 如果没有可以直接调取微信公众号接口
public async Task<byte[]> GetQrCode(string ticket)
{
return await "https://mp.weixin.qq.com/cgi-bin/showqrcode"
.SetQueryParams(new
{
ticket = ticket
})
.GetBytesAsync();
}
完整代码如下
[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);
}
这样我们就生成了带参数的二维码图片了
第六步,微信扫码进行回调
在回调方法处理消息模板推送逻辑
[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 = "当前排队位置",
color = "#173177"
},
keyword1 = new
{
value = "70",
color = "#173177"
},
keyword2 = new
{
value = "40分钟",
color = "#173177"
}
}
});
}
这样以微信扫码获取排队信息就成功了
Part4总结
文章来源于生活中的点点滴滴,这也是我的即兴使用,大家要是有更好的使用技巧,欢迎交流,独乐乐不如众乐乐,本篇就说到这里啦,希望对您有帮助。
边栏推荐
- Take you into the cloud native database industry, Amazon Aurora
- How to monitor the incoming and outgoing traffic of the server host?
- Leetcode skimming ---367
- Common scenarios in which Seata distributed transactions fail and do not take effect (transactions do not rollback)
- 权重衰退(PyTorch)
- Leetcode skimming ---75
- Ind yff first week
- Buy health products for parents
- Unity学习笔记:联网游戏Pixel Adventure 1学习过程&纠错心得
- Leetcode刷题---977
猜你喜欢
Drop out (pytoch)
DAY 7 小练习
神经网络入门之预备知识(PyTorch)
MySql 怎么查出符合条件的最新的数据行?
帶你走進雲原生數據庫界扛把子Amazon Aurora
Mysql5.7 installation and configuration tutorial (Graphic ultra detailed version)
The story of a 30-year-old tester struggling, even lying flat is extravagant
Weight decay (pytorch)
[SQL] an article takes you to master the operations related to query and modification of SQL database
Classification (data consolidation and grouping aggregation)
随机推荐
Set ArrayList nested map set loop traversal
Leetcode skimming ---189
Promoted, colleagues become subordinates and don't cooperate with work
缓存路由组件
[untitled]
If you always feel that you need to persist in learning English
如何在游戏中制作一个血条
Wechat applet training 2
The story of a 30-year-old tester struggling, even lying flat is extravagant
Buy health products for parents
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
QT:QSS自定义QToolButton实例
Ut2016 learning notes
深度学习入门之线性回归(PyTorch)
Bidding website architecture project progress -- Network Security
Ut2014 learning notes
Automatic derivation of introduction to deep learning (pytoch)
6、 Data definition language of MySQL (1)
Leetcode刷题---704
Flink <-->JDBC的使用介绍+with参数