官方地址:微信小程序码无限制
#region "获取小程序二维码"
/// <summary>
/// 获取小程序二维码
/// </summary>
/// <param name="scene">参数</param>
/// <param name="page">跳转的小程序页面(必须是已发布的)例如 pages/index/index, 根路径前不要填加 /,为空默认跳主页面</param>
/// <param name="width">二维码的宽度,单位 px,最小 280px,最大 1280px</param>
/// <returns></returns>
[HttpGet]
public ResultClass<string> GetMiniProgramQRCode(string scene= "", string page = "", int width = 430)
{
ResultClass<string> ret = new ResultClass<string>();
try
{
if (string.IsNullOrEmpty(scene)) { throw new Exception("参数空值!"); }
string strReturn = string.Empty;
string token = GetAccess_Token();
string url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
//https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
string temp = "{\"scene\":\"" + scene+ "\",\"page\":\"" + page + "\",\"width\":" + width + "}";
byte[] imgStream = GetResponseDataByte(temp, @url);
ret = new ImageUpload.ImageUploadController().UpLoadImage(imgStream);
}
catch (Exception ex)
{
ret.Result = false;
ret.ErrorMessage = ex.Message;
}
return ret;
}
/// <summary>
/// 返回字节数据
/// </summary>
/// <param name="JSONData">要处理的JSON数据</param>
/// <param name="Url">要提交的URL</param>
/// <returns></returns>
private byte[] GetResponseDataByte(string JSONData, string Url)
{
byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "json";
Stream reqstream = request.GetRequestStream();
reqstream.Write(bytes, 0, bytes.Length);
//声明一个HttpWebRequest请求
request.Timeout = 90000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
byte[] img = StreamToBytes(streamReceive);
streamReceive.Dispose();
return img;
}
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
private static byte[] StreamToBytes(Stream stream)
{
List<byte> bytes = new List<byte>();
int temp = stream.ReadByte();
while (temp != -1)
{
bytes.Add((byte)temp);
temp = stream.ReadByte();
}
return bytes.ToArray();
}
/// <summary>
/// 获取Access_Token
/// </summary>
/// <returns></returns>
private string GetAccess_Token()
{
string apiurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序的appid&secret=小程序的secret";
WebRequest request = WebRequest.Create(@apiurl);
request.Method = "POST";
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.UTF8;
StreamReader reader = new StreamReader(stream, encode);
string detail = reader.ReadToEnd();
var jd = JsonConvert.DeserializeObject<WXApi>(detail);
return (String)jd.access_token;
}
/// <summary>
/// 返回JSon数据
/// </summary>
/// <param name="JSONData">要处理的JSON数据</param>
/// <param name="Url">要提交的URL</param>
/// <returns>返回的JSON处理字符串</returns>
private string GetResponseData(string JSONData, string Url)
{
byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "json";
Stream reqstream = request.GetRequestStream();
reqstream.Write(bytes, 0, bytes.Length);
//声明一个HttpWebRequest请求
request.Timeout = 90000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.UTF8;
StreamReader streamReader = new StreamReader(streamReceive, encoding);
string strResult = streamReader.ReadToEnd();
streamReceive.Dispose();
streamReader.Dispose();
return strResult;
}
#endregion
#region "保存图片"
/// <summary>
/// 保存图片
/// </summary>
/// <param name="imgStream"></param>
/// <returns></returns>
[HttpPost]
public ResultClass<string> UpLoadImage(byte[] imgStream)
{
string serverurl = ConfigurationManager.AppSettings["ServerUrl"];
ResultClass<string> ret = new ResultClass<string>();
try
{
string LocalPath = GetPath();
string Name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + (new System.Random()).Next(10000, 99999).ToString();
if (System.IO.Directory.Exists(LocalPath) == false)
{
System.IO.Directory.CreateDirectory(LocalPath);
}
MemoryStream stream = new MemoryStream(imgStream);
Bitmap img = new Bitmap(stream);
string fullName = LocalPath + "\\" + Name + ".jpg";
img.Save(fullName);
stream.Close();
stream.Dispose();
stream = null;
img.Dispose();
img = null;
string surlPath = serverurl + "ClientImages/" + System.DateTime.Now.ToString("yyyyMMdd") + @"/" + Name + ".jpg";
ret.Result = true;
ret.ResultData = surlPath;
}
catch (Exception ex)
{
ret.Result = false;
ret.ErrorMessage = ex.Message;
}
return ret;
}
private string GetPath()
{
string path = System.Web.HttpContext.Current.Server.MapPath("~/ClientImages/");
string np = System.DateTime.Now.ToString("yyyyMMdd");
path = path + np + @"\";
if (System.IO.Directory.Exists(path) == false)
{
System.IO.Directory.CreateDirectory(path);
}
return path;
}
#endregion