基于C#動態(tài)生成帶參數(shù)的小程序二維碼
應(yīng)用場景
在微信小程序管理后臺,我們可以生成下載標(biāo)準(zhǔn)的小程序二維碼,提供主程序入口功能。在實際應(yīng)用開發(fā)中,小程序二維碼是可以攜帶參數(shù)的,可以動態(tài)進(jìn)行生成,如如下場景:
1、不同參數(shù)決定的顯示界面不同。
2、不同參數(shù)決定的功能不同。
3、由于小程序?qū)徍藱C(jī)制,我們將不同的應(yīng)用集成在一個小程序里,通過不同的參數(shù)進(jìn)行入口控制。
關(guān)鍵代碼
操作界面
我們以一種驗證、綁定手機(jī)的小程序功能為例,該小程序可以生成動態(tài)校驗碼,以實現(xiàn)實際業(yè)務(wù)應(yīng)用的其它場景。界面中我們設(shè)計了提示信息Label,生成按鈕 Button 和掃碼圖片 Image 等Asp.net控件。
示例界面如下,通過點擊按鈕,動態(tài)生成二維碼圖片,該參數(shù)將引導(dǎo)用戶進(jìn)入動態(tài)碼生成功能:
示例UI代碼如下:
<div class="user-box"> <span>驗證手機(jī)</span><br> <br> <div class="query-box"> <label> 掃描二維碼獲取動態(tài)校驗碼</label> <br> <label> 建議您PC注冊,如果微信,可常按并選擇前往圖中包含的小程序打開(某些手機(jī)系統(tǒng)可能不支持此操作)</label></div> <br> <br> <asp:Button ID="createCode" Text="新注冊或換手機(jī)號點這里生成以獲取動態(tài)校驗碼" Visible="true" CssClass="form-control" runat="server" OnClick="createCode_Click"></asp:Button> </div> <div class="user-box" id="ecodepanel" align="center" visible="false" runat="server"> <br /> <asp:Image ID="ecode" Width="200px" Height="200px" Visible="false" runat="server"></asp:Image> <br> <span style='color: silver'></span> <br> <br> </div>
服務(wù)端點擊事件
點擊按鈕,通過設(shè)置參數(shù)值,并訪問騰訊API,生成小程序二維碼,轉(zhuǎn)換為圖片BASE64編碼,如果生成成功則顯示在Image控件里,點擊事件的,示例代碼如下:
protected void createCode_Click(object sender, EventArgs e) { string paras = "reqvmobile"; //參數(shù)值設(shè)置 //可以更改小程序二維碼的色系 int r = 4; int g = 128; int b = 188; ecode.ImageUrl = getBase64(255, r, g, b); //將生成成功的BASE64編碼賦值給Image控件 ecode.Visible = true; ecodepanel.Visible = true; } //獲取小程序二維碼圖片的 Base64值 string getBase64(string paras, int width, int linecolor_R, int linecolor_G, int linecolor_B) { string desimg = Request.PhysicalApplicationPath + "\\app_data\\" + System.Guid.NewGuid().ToString() + ".jpg"; //目標(biāo)圖片臨時路徑 //生成二維碼圖片文件 System.IO.File.WriteAllBytes(desimg, getMpBuffer(wxmp_accesstoken, paras, width, linecolor_R, linecolor_G, linecolor_B)); string base64 = ImgToBase64String(desimg, true); System.IO.File.Delete(desimg); //刪除臨時文件 return base64; } //圖片轉(zhuǎn)BASE64方法 public string ImgToBase64String(string Imagefilename,bool outFullString=false) { try { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Imagefilename); MemoryStream ms = new MemoryStream(); // bmp.Save(ms,ImageFormat.Jpeg) System.Drawing.Imaging.ImageFormat iformat = System.Drawing.Imaging.ImageFormat.Jpeg; string extension = System.IO.Path.GetExtension(Imagefilename).Replace(".", "").ToLower(); if (extension == "bmp") { iformat = System.Drawing.Imaging.ImageFormat.Bmp; } else if (extension == "emf") { iformat = System.Drawing.Imaging.ImageFormat.Emf; } else if (extension == "exif") { iformat = System.Drawing.Imaging.ImageFormat.Exif; } else if (extension == "gif") { iformat = System.Drawing.Imaging.ImageFormat.Gif; } else if (extension == "icon") { iformat = System.Drawing.Imaging.ImageFormat.Icon; } else if (extension == "png") { iformat = System.Drawing.Imaging.ImageFormat.Png; } else if (extension == "tiff") { iformat = System.Drawing.Imaging.ImageFormat.Tiff; } else if (extension == "wmf") { iformat = System.Drawing.Imaging.ImageFormat.Wmf; } bmp.Save(ms, iformat); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); bmp.Dispose(); string rv=Convert.ToBase64String(arr); if (outFullString == true) { rv = "data:image/" + extension + ";base64," + rv; } return rv; } catch (Exception ex) { return null; } }
生成小程序二維碼
getMpBuffer(wxmp_accesstoken, paras, width, linecolor_R, linecolor_G, linecolor_B)方法返回byte[]類型,參數(shù)需要傳遞通過小程序Appid和AppSecret生成的合法令牌值;動態(tài)參數(shù)值;圖像寬度;R/G/B的色系值。
方法代碼如下:
public byte[] getMpBuffer(string access_token, string scene, int width, int linecolor_R, int linecolor_G, int linecolor_B) { var url = string.Format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}", access_token); var postData = "{\"scene\":\"" + scene + "\",\"width\":" + width.ToString() + ",\"line_color\":{\"r\":" + linecolor_R.ToString() + ",\"g\":" + linecolor_G.ToString() + ",\"b\":" + linecolor_B.ToString() + "}}"; 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(postData); 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(); return result; }
小結(jié)
最初我們設(shè)計的目標(biāo)是用小程序?qū)崿F(xiàn)一對一視頻面試的功能,對于查詢出來的記錄,為考生和考官生成不同帶參數(shù)的小程序二維碼,并進(jìn)入不同的功能。后來由于集成了一些相關(guān)的功能應(yīng)用,通過動態(tài)參數(shù)以決定不同的入口,以避免申請過多的小程序應(yīng)用,達(dá)到降低費(fèi)用成本、維護(hù)成本的目的。
以上就是基于C#動態(tài)生成帶參數(shù)的小程序二維碼的詳細(xì)內(nèi)容,更多關(guān)于C#動態(tài)生成帶參數(shù)二維碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#學(xué)習(xí)進(jìn)階Hello World的17種寫法代碼分享
本文針對不同階段、不同程度的C#學(xué)習(xí)者,介紹了C# Hello World的17種不同寫法,C# Hello World寫法入門、C# Hello World寫法進(jìn)階、C# Hello World的特別寫法三種角度進(jìn)行推進(jìn)2013-12-12常用.NET工具(包括.NET可再發(fā)行包2.0)下載
常用.NET工具(包括.NET可再發(fā)行包2.0)下載...2007-03-03