123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- #region Apache License Version 2.0
- #endregion Apache License Version 2.0
- using System;
- using System.IO;
- using System.Threading.Tasks;
- using Senparc.CO2NET.Extensions;
- using Senparc.NeuChar;
- using Senparc.Weixin.CommonAPIs;
- using Senparc.CO2NET.HttpUtility;
- using Senparc.Weixin.MP.AdvancedAPIs.QrCode;
- using Senparc.Weixin.MP.CommonAPIs;
- namespace Senparc.Weixin.MP.AdvancedAPIs
- {
-
-
-
- public static class QrCodeApi
- {
- #region 同步方法
-
-
-
-
-
-
-
-
-
-
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "QrCodeApi.Create", true)]
- public static CreateQrCodeResult Create(string accessTokenOrAppId, int expireSeconds, int sceneId, QrCode_ActionName actionName, string sceneStr = null, int timeOut = Config.TIME_OUT)
- {
- return ApiHandlerWapper.TryCommonApi(accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/qrcode/create?access_token={0}";
- object data = null;
- switch (actionName)
- {
- case QrCode_ActionName.QR_SCENE:
- data = new
- {
- expire_seconds = expireSeconds,
- action_name = "QR_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_id = sceneId
- }
- }
- };
- break;
- case QrCode_ActionName.QR_LIMIT_SCENE:
- data = new
- {
- action_name = "QR_LIMIT_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_id = sceneId
- }
- }
- };
- break;
- case QrCode_ActionName.QR_LIMIT_STR_SCENE:
- data = new
- {
- action_name = "QR_LIMIT_STR_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_str = sceneStr
- }
- }
- };
- break;
- case QrCode_ActionName.QR_STR_SCENE:
- data = new
- {
- expire_seconds = expireSeconds,
- action_name = "QR_STR_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_str = sceneStr
- }
- }
- };
- break;
- default:
-
- throw new ArgumentOutOfRangeException(actionName.GetType().Name, actionName, null);
- }
- return CommonJsonSend.Send<CreateQrCodeResult>(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "QrCodeApi.GetShowQrCodeUrl", true)]
- public static string GetShowQrCodeUrl(string ticket)
- {
- var urlFormat = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}";
- return string.Format(urlFormat, ticket.AsUrlData());
- }
-
-
-
-
-
-
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "QrCodeApi.ShowQrCode", true)]
- public static void ShowQrCode(string ticket, Stream stream)
- {
- var url = GetShowQrCodeUrl(ticket);
- Get.Download(url, stream);
- }
- #endregion
- #if !NET35 && !NET40
- #region 异步方法
-
-
-
-
-
-
-
-
-
-
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "QrCodeApi.CreateAsync", true)]
- public static async Task<CreateQrCodeResult> CreateAsync(string accessTokenOrAppId, int expireSeconds, int sceneId, QrCode_ActionName actionName, string sceneStr = null, int timeOut = Config.TIME_OUT)
- {
- return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
- {
- var urlFormat = Config.ApiMpHost + "/cgi-bin/qrcode/create?access_token={0}";
- object data = null;
- switch (actionName)
- {
- case QrCode_ActionName.QR_SCENE:
- data = new
- {
- expire_seconds = expireSeconds,
- action_name = "QR_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_id = sceneId
- }
- }
- };
- break;
- case QrCode_ActionName.QR_LIMIT_SCENE:
- data = new
- {
- action_name = "QR_LIMIT_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_id = sceneId
- }
- }
- };
- break;
- case QrCode_ActionName.QR_LIMIT_STR_SCENE:
- data = new
- {
- action_name = "QR_LIMIT_STR_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_str = sceneStr
- }
- }
- };
- break;
- case QrCode_ActionName.QR_STR_SCENE:
- data = new
- {
- expire_seconds = expireSeconds,
- action_name = "QR_STR_SCENE",
- action_info = new
- {
- scene = new
- {
- scene_str = sceneStr
- }
- }
- };
- break;
- default:
-
- throw new ArgumentOutOfRangeException(actionName.GetType().Name, actionName, null);
- }
- return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<CreateQrCodeResult>(accessToken, urlFormat, data, timeOut: timeOut);
- }, accessTokenOrAppId);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "QrCodeApi.ShowQrCodeAsync", true)]
- public static async Task ShowQrCodeAsync(string ticket, Stream stream)
- {
- var url = GetShowQrCodeUrl(ticket);
- await Get.DownloadAsync(url, stream);
- }
- #endregion
- #endif
- }
- }
|