c#后台生成指定頁面帶參數的小程序碼


出於安全考慮,小程序禁用了直接在小程序端調用api.weixin.qq.com的功能,只能通過后台來調用,以下是實現的過程。

這是官方的文檔https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html;

1.首先獲取accesstoken 

  public string GetAccessToken(string strAPPID, string strSecret)
        {
            string str = "";
            string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + strAPPID + "&secret=" + strSecret + "";
            System.Net.WebRequest wRequest = System.Net.WebRequest.Create(url);
            wRequest.Method = "GET";
            wRequest.ContentType = "text/html;charset=UTF-8";
            System.Net.WebResponse wResponse = wRequest.GetResponse();
            Stream stream = wResponse.GetResponseStream();
            StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
            str = reader.ReadToEnd();   //url返回的值  
            return str;
        }

我這里沒有對strAPPID和strSecret進行加密,如果安全性要求較高的可以加密,然后接口進行解密。

 

2.根據獲取的accesstoken調用接口保存小程序碼。返回小程序的地址

   public string GetQrcode(string strToken, string content)
        {
            string strResult = "";
            string url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + strToken;
            System.Net.WebRequest wRequest = System.Net.WebRequest.Create(url);
            wRequest.Method = "post";
            wRequest.ContentType = "application/x-www-form-urlencoded";
            #region 添加Post 參數
            byte[] data = Encoding.UTF8.GetBytes(content);
            wRequest.ContentLength = data.Length;
            using (Stream reqStream = wRequest.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion

            HttpWebResponse resp = (HttpWebResponse)wRequest.GetResponse();
            Stream stream = resp.GetResponseStream();
            ////獲取響應內容
            //using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            //{
            //    strResult = reader.ReadToEnd();
            //}

            Image img = Image.FromStream(stream);
        //服務器硬盤位置,在接口web.config中配置
        //
<add key="strMiniProgramUrl" value="D:\W-E-B\xcx_mls" />
string strImageWebUrl = ConfigurationManager.AppSettings["strMiniProgramUrl"];
            string str = DateTime.Now.ToString("yyyyMM");
            string str2 = DateTime.Now.ToString("yyyyMMdd");
            string path = strImageWebUrl + @"\uploads";
            string path2 = @"https://服務器域名/" + @"/uploads";
            if (!Directory.Exists(path + @"\audio\" + str))
            {
                Directory.CreateDirectory(path + @"\audio\" + str);
            }
            path = path + @"\qrcode\" + str;
            path2 = path2 + @"/qrcode/" + str;
            if (!Directory.Exists(path + @"\" + str2))
            {
                Directory.CreateDirectory(path + @"\" + str2);
            }
            path = path + @"\" + str2;
            path2 = path2 + @"/" + str2;
            int num = new Random().Next(0x2710);
            string str6 = DateTime.Now.ToString("yyyyMMddHHmmss") + num.ToString() + Guid.NewGuid().ToString() + ".jpg";
            string filename = path + @"\" + str6;
            path2 = path2 + @"/" + str6;
            try
            {
                img.Save(filename, ImageFormat.Jpeg);
                this.model.value = path2;
                this.model.code = "1000";
            }
            catch
            {
                this.model.code = "0001";
            }
            finally
            {
                if (img != null)
                {
                    img.Dispose();
                    img = null;
                }
            }
            return this.bllcommon.returnStr(JsonConvert.ExportToString(this.model)).Replace("暫無", "");
        }
path2為保存后的小程序碼圖片地址

實際開發中遇到以下情況
 Image img = Image.FromStream(stream);參數錯誤 原因是stream不是有效的圖片
生成小程序碼時,小程序的頁面必須是已經發布過的,不然會提示
"{\"errcode\":41030,\"errmsg\":\"invalid page hint: [WxuvGA05281511]\"}"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM