1. 程式人生 > >C#、.NET後端,B介面-微信小程式帶引數二維碼的生成

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();
        }
這個是返回的二維碼圖片