• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net生成微信小程序二维码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

asp.net生成微信小程序二维码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;

/// <summary>
///微信小程序
/// </summary>
public class wxapp
{
    //pages/login/login?key=
    /// <summary>
    /// 获取access_token        
    /// </summary>        
    /// <returns></returns>        
    public static string GetAccessToken(string AppID, string AppSecret)
    {
        string token = string.Empty;
        try
        {
            string url = "http://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + AppID + "&secret=" + AppSecret;
            WebClient wc = new WebClient();
            string result = wc.DownloadString(url);
            if (!string.IsNullOrWhiteSpace(result))
            {
                var jObject = JObject.Parse(result);
                token = jObject["access_token"].ToString();
            }
            return token;
        }
        catch (Exception ex)
        {
            return "";
        }
    }

    /// <summary>
    /// 获取小程序二维码
    /// </summary>
    /// <param name="AppID">小程序AppID</param>
    /// <param name="AppSecret">小程序AppSecret</param>
    /// <param name="page">小程序页面,格式:pages/index/index?id=1</param>
    /// <param name="dir">二维码保存目录,格式:/upload/</param>
    /// <returns>二维码网址</returns>
    public static string Getqrcode(string AppID, string AppSecret, string page, string dir)
    {
        //只能生成10万个
        //https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html
        //var page = "pages/index/index?id=1";
        string token = GetAccessToken(AppID, AppSecret);
        if (!string.IsNullOrEmpty(token))
        {
            try
            {
                //var url = string.Format("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={0}", token);
                var url = string.Format("https://api.weixin.qq.com/wxa/getwxacode?access_token={0}", token);         
                var DataJson = "{";
                //DataJson += string.Format("\"width\":\"{0}\",", "430");//大小,最小280px,最大1280px,默认430
                DataJson += string.Format("\"path\":\"{0}\"", page);//小程序地址,根路径前不要填加'/',最大128字节
                DataJson += "}";
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                byte[] payload;
                payload = System.Text.Encoding.UTF8.GetBytes(DataJson);
                request.ContentLength = payload.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.Stream stream;
                stream = response.GetResponseStream();

                List<byte> bytes = new List<byte>();
                int temp = stream.ReadByte();
                while (temp != -1)
                {
                    bytes.Add((byte)temp);
                    temp = stream.ReadByte();
                }
                byte[] result = bytes.ToArray();

                String dirPath = HttpContext.Current.Server.MapPath(dir);
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
                string filePath = dirPath + fileName;
                FileStream fs = new FileStream(filePath, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(result, 0, result.Length);
                bw.Close();
                fs.Close();
                return dir + fileName;
            }
            catch { }
        }
        return string.Empty;
    }
    public static string Getqrcode2(string AppID, string AppSecret, string page, string dir)
    {
        //只能生成10万个
        //https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html
        //var page = "pages/index/index?id=1";
        string token = GetAccessToken(AppID, AppSecret);
        if (!string.IsNullOrEmpty(token))
        {
            //try
            //{
                var url = string.Format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}", token);
                var DataJson = "{";
                //DataJson += string.Format("\"width\":\"{0}\",", "430");//大小,最小280px,最大1280px,默认430
                DataJson += string.Format("\"scene\":\"{0}\",", "key=a-b-cv");
                DataJson += string.Format("\"check_path\":\"{0}\",", "false");
                DataJson += string.Format("\"page\":\"{0}\"", page);//小程序地址,根路径前不要填加'/',最大128字节
                
                DataJson += "}";
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                byte[] payload;
                payload = System.Text.Encoding.UTF8.GetBytes(DataJson);
                request.ContentLength = payload.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.Stream stream;
                stream = response.GetResponseStream();

                List<byte> bytes = new List<byte>();
                int temp = stream.ReadByte();
                while (temp != -1)
                {
                    bytes.Add((byte)temp);
                    temp = stream.ReadByte();
                }
                byte[] result = bytes.ToArray();

                String dirPath = HttpContext.Current.Server.MapPath(dir);
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
                string filePath = dirPath + fileName;
                FileStream fs = new FileStream(filePath, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(result, 0, result.Length);
                bw.Close();
                fs.Close();
                return dir + fileName;
            //}
            //catch { }
        }
        return string.Empty;
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序wx:key中的关键字*this发布时间:2022-07-18
下一篇:
在微信小程序中使用less发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap