C#、.NET後端,B接口-微信小程序帶參數二維碼的生成

接口B:適用於需要的碼數量極多,或僅臨時使用的業務場景

接口地址:https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

注意:通過該接口生成的小程序碼,永久有效,數量暫無限制。用戶掃描該碼進入小程序後,開發者需在對應頁面獲取的碼中 scene 字段的值,再做處理邏輯。使用如下代碼可以獲取到二維碼中的 scene 字段的值。調試階段可以使用開發工具的條件編譯自定義參數 scene=xxxx 進行模擬,開發工具模擬時的 scene 的參數值需要進行 urlencode

// 這是首頁的 js
Page({
  onLoad: function(options) {
    // options 中的 scene 需要使用 decodeURIComponent 才能獲取到生成二維碼時傳入的 scene
    var scene = decodeURIComponent(options.scene)
  }
})

詳情可見小程序開發文檔:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
 /// <summary>
        /// B接口-微信小程序帶參數二維碼的生成
        /// </summary>
        /// <param name="MicroId"></param>
        /// <returns></returns>
        public static string CreateWxCode(int MicroId,int UserId,int CardLevel)
        {
            string ret = string.Empty;
            try
            {
                string DataJson = string.Empty;
                string url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=*****************";
                DataJson = "{";
                DataJson += string.Format("\"scene\":\"{0}\",", "所傳參數內容1,所傳參數內容2");//所要傳的參數用,分看
                DataJson += string.Format("\"width\":\"{0}\",", 124);
                DataJson += string.Format("\"page\":\"{0}\",", "pages/index/index");//掃碼所要跳轉的地址,根路徑前不要填加'/',不能攜帶參數(參數請放在scene字段裏),如果不填寫這個字段,默認跳主頁面
                DataJson += "\"line_color\":{";
                DataJson += string.Format("\"r\":\"{0}\",", "0");
                DataJson += string.Format("\"g\":\"{0}\",", "0");
                DataJson += string.Format("\"b\":\"{0}\"", "0");
                DataJson += "}";
                DataJson += "}";
//DataJson的配置見小程序開發文檔,B接口:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
                ret= PostMoths(url, DataJson);
                if(ret.Length>0)
                {
                    //對圖片進行存儲操作,下次可直接調用你存儲的圖片,不用再調用接口
                }
            }
            catch (Exception e)
            { ret = e.Message; }
            return ret;//返回圖片地址
        }
 //請求處理,返回二維碼圖片
public static string  PostMoths(string url, string param)
        {
            string strURL = url;
            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            string paraUrlCoded = param;
            byte[] payload;
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            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 s;
            s = response.GetResponseStream();//返回圖片數據流
            byte[] tt = StreamToBytes(s);//將數據流轉爲byte[]

            //在文件名前面加上時間,以防重名
            string imgName = DateTime.Now.ToString("yyyyMMddhhmmss")+".jpg";
            //文件存儲相對於當前應用目錄的虛擬目錄
            string path = "/image/";
            //獲取相對於應用的基目錄,創建目錄
            string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path;     //通過此對象獲取文件名
            StringHelper.CreateDirectory(imgPath);
            System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(path+imgName), tt);//講byte[]存儲爲圖片
            return "/image/" + imgName;
        }
 ///將數據流轉爲byte[]
        public 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();
        }
這個是返回的二維碼圖片






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章