一入微信,满地是坑,这个坑,你得一个个填过去。
产品要求用户通过其他用户的推荐二维码进入小程序,并且绑定二维码所带的参数
微信小程序的二维码生成分为A接口和B接口:
这是微信的官方文档介绍(文档链接如下https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html)
A接口上限是10W个,B接口没有上限
对于这个应用场景,我选择了B接口生成小程序码
首先我们要获取到申请接口所有用的assess_token(附上官方文档https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html)
通过GET请求到access_token,因为我们前端、API、后台都需要用到access_token,所以是将其产生时间和token保存在数据库中
public static AccessToken GetACCESS_TOKEN(AccessToken model)
{
try
{
//token 使用时效为2小时, 5分钟的过期时间是前后两个都能用
if (model.access_token_time.AddSeconds(model.expires_in - 5) > DateTime.Now)
{
return model;
}
#region 调用 ACCESS_TOKEN 接口
//拼接URL
string url = "https://api.weixin.qq.com/cgi-bin/token";
url += "?grant_type=client_credential";
url += "&appid=" + WxConfig.APPID;//小程序APPID
url += "&secret=" + WxConfig.APPSECRET;//小程序APPSECRET
Uri address = new Uri(url);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "GET";
request.Content