基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼
應(yīng)用場(chǎng)景
在微信小程序管理后臺(tái),我們可以生成下載標(biāo)準(zhǔn)的小程序二維碼,提供主程序入口功能。在實(shí)際應(yīng)用開發(fā)中,小程序二維碼是可以攜帶參數(shù)的,可以動(dòng)態(tài)進(jìn)行生成,如如下場(chǎng)景:
1、不同參數(shù)決定的顯示界面不同。
2、不同參數(shù)決定的功能不同。
3、由于小程序?qū)徍藱C(jī)制,我們將不同的應(yīng)用集成在一個(gè)小程序里,通過(guò)不同的參數(shù)進(jìn)行入口控制。
關(guān)鍵代碼
操作界面
我們以一種驗(yàn)證、綁定手機(jī)的小程序功能為例,該小程序可以生成動(dòng)態(tài)校驗(yàn)碼,以實(shí)現(xiàn)實(shí)際業(yè)務(wù)應(yīng)用的其它場(chǎng)景。界面中我們?cè)O(shè)計(jì)了提示信息Label,生成按鈕 Button 和掃碼圖片 Image 等Asp.net控件。
示例界面如下,通過(guò)點(diǎn)擊按鈕,動(dòng)態(tài)生成二維碼圖片,該參數(shù)將引導(dǎo)用戶進(jìn)入動(dòng)態(tài)碼生成功能:

示例UI代碼如下:
<div class="user-box">
<span>驗(yàn)證手機(jī)</span><br>
<br>
<div class="query-box">
<label>
掃描二維碼獲取動(dòng)態(tài)校驗(yàn)碼</label>
<br>
<label>
建議您PC注冊(cè),如果微信,可常按并選擇前往圖中包含的小程序打開(某些手機(jī)系統(tǒng)可能不支持此操作)</label></div>
<br>
<br>
<asp:Button ID="createCode" Text="新注冊(cè)或換手機(jī)號(hào)點(diǎn)這里生成以獲取動(dòng)態(tài)校驗(yàn)碼" 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ù)端點(diǎn)擊事件
點(diǎn)擊按鈕,通過(guò)設(shè)置參數(shù)值,并訪問(wèn)騰訊API,生成小程序二維碼,轉(zhuǎn)換為圖片BASE64編碼,如果生成成功則顯示在Image控件里,點(diǎn)擊事件的,示例代碼如下:
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)圖片臨時(shí)路徑
//生成二維碼圖片文件
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); //刪除臨時(shí)文件
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ù)需要傳遞通過(guò)小程序Appid和AppSecret生成的合法令牌值;動(dòng)態(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é)
最初我們?cè)O(shè)計(jì)的目標(biāo)是用小程序?qū)崿F(xiàn)一對(duì)一視頻面試的功能,對(duì)于查詢出來(lái)的記錄,為考生和考官生成不同帶參數(shù)的小程序二維碼,并進(jìn)入不同的功能。后來(lái)由于集成了一些相關(guān)的功能應(yīng)用,通過(guò)動(dòng)態(tài)參數(shù)以決定不同的入口,以避免申請(qǐng)過(guò)多的小程序應(yīng)用,達(dá)到降低費(fèi)用成本、維護(hù)成本的目的。
以上就是基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼的詳細(xì)內(nèi)容,更多關(guān)于C#動(dòng)態(tài)生成帶參數(shù)二維碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
Unity實(shí)現(xiàn)簡(jiǎn)單的虛擬搖桿
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單的虛擬搖桿,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#學(xué)習(xí)進(jìn)階Hello World的17種寫法代碼分享
本文針對(duì)不同階段、不同程度的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

