标签:
1、生成场景号
根据业务需求生产场景号。
用户扫描带场景值二维码时,可能推送以下两种事件:
2、创建二维码ticket
每次创建二维码ticket需要提供一个开发者自行设定的参数(scene_id),分别介绍临时二维码和永久二维码的创建二维码ticket过程
/// <summary>
/// 创建临时二维码
/// </summary>
/// <param name="sceneId">二维码场景</param>
/// <param name="expireSeconds">二维码的有效时长 单位:秒(范围:1 - 604800)</param>
/// <exception cref="System.ArgumentNullException">当SceneId为空时</exception>
/// <returns></returns>
public virtual QRCodeInfoResult CreateQRCode(int sceneId, int expireSeconds)
{
QRCodeInfo code = new QRCodeInfo()
{
IsForever = false,
SceneId = sceneId
};
code.ExpireSeconds = expireSeconds;
return this.CreateQRCode(code);
}
/// <summary>
/// 创建永久二维码
/// </summary>
/// <param name="sceneId">二维码场景Id(范围:1 - 100000)</param>
/// <exception cref="System.ArgumentNullException">当SceneId为空时</exception>
/// <exception cref="System.ArgumentNullException">当二维码时场景号不在1 - 100000范围内时</exception>
/// <returns></returns>
public virtual QRCodeInfoResult CreateQRCode(int sceneId)
{
QRCodeInfo code = new QRCodeInfo()
{
IsForever = true,
SceneId = sceneId
};
return this.CreateQRCode(code);
}
/// <summary>
/// 根据指定二维码信息创建二维码
/// </summary>
/// <param name="codeInfo">二维码信息</param>
/// <exception cref="System.ArgumentNullException">当codeInfo参数为空或codeInfo.SceneId为空时</exception>
/// <exception cref="System.ArgumentNullException">当生成永久二维码时场景号不在1 - 100000范围内时</exception>
/// <returns></returns>
public virtual QRCodeInfoResult CreateQRCode(QRCodeInfo codeInfo)
{
if (codeInfo == null)
throw new ArgumentNullException("codeInfo", "创建的二维码信息不可为空");
if (!codeInfo.SceneId.HasValue)
{
throw new ArgumentNullException("SceneId", "二维码场景号不可为空");
}
if (codeInfo.IsForever && (codeInfo.SceneId <= 0 || codeInfo.SceneId > 100000))
{
throw new ArgumentOutOfRangeException("SceneId", "永久二维码场景号只可在1~100000范围内");
}
return this.PostWithAccessToken<QRCodeInfoResult>("https://api.weixin.qq.com/cgi-bin/qrcode/create", JsonConvert.SerializeObject(codeInfo));
}
3、通过ticket换取二维码
Bitmap qrImage = null;
/// <summary>
/// 二维码图片,请在获取该对象成功后尽快调用此属性来获取二维码图片
/// </summary>
[JsonIgnore]
public Bitmap QRImage
{
get
{
if (qrImage == null && !string.IsNullOrWhiteSpace(this.Ticket))
{
HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + System.Web.HttpUtility.UrlEncode(this.Ticket));
web.AllowAutoRedirect = false;
try
{
HttpWebResponse response = (HttpWebResponse)web.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
qrImage = new Bitmap(response.GetResponseStream());
}
}
catch (Exception) { }
}
return qrImage;
}
}
标签:
原文地址:http://www.cnblogs.com/l36i90u/p/5358407.html