.NET開發微信小程序-生成二維碼 - 轉
阿新 • • 發佈:2017-12-04
reat 開發 switch mage pre 二維碼 位置 itl toast
1.生成小程序二維碼功能
直接請求相應的鏈接。傳遞相應的參數
以生成商鋪的付款碼為例:
var shopsId = e.ShopsId //付款碼的參數 var codeModel = new function () { } codeModel.path = "pages/PageWxPay/PageWxPay?shopsId=" + shopsId codeModel.width = 430 codeModel.auto_color = false codeModel.line_color = { "r": "0", "g": "0", "b": "0" } var data = { shopsID: shopsId, data: JSON.stringify(codeModel) } console.log(data) api.RequestApiURL("Weixin/MyPaymentCode", data, function (codeData) { console.log(codeData) var obj = codeData.data.data if (obj.Key == "0") { that.setData({ payCodeUrl: app.globalData.apiurl + obj.Value }) wx.hideLoading() } else { wx.showToast({ title: obj.Value }) } })
後臺代碼處理
private static object obj = new object(); /// <summary> /// 創建二維碼 /// 接口A: 適用於需要的碼數量較少的業務場景 接口地址: /// 接口B:適用於需要的碼數量極多,或僅臨時使用的業務場景 /// 接口C:適用於需要的碼數量較少的業務場景 /// </summary> /// <param name="data">前臺傳遞的數據</param> /// <param name="path">圖片存儲位置</param> /// <param name="toKen"></param> /// <returns></returns> public static bool CreateWxaqrCode(Utils.QrCodeType nType, string data, string path, string toKen, out string ExcaptionMassage) { ExcaptionMassage = ""; bool msg = false; string url = string.Empty; switch (nType) { case Utils.QrCodeType.A: url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}"; break; case Utils.QrCodeType.B: url = "http://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}"; break; case Utils.QrCodeType.C: url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={0}"; break; } url = string.Format(url, toKen); lock (obj) { //判斷當前用戶是否生成二微碼 if (!System.IO.File.Exists(path)) { try { //獲取數據流 Stream str = Request.PostMoths(url, data); byte[] by = Utils.StreamToBytes(str); Utils.PreservationCodeImage(path, by); //保存該文件 msg = true; } catch(Exception e) { ExcaptionMassage= e.Message; msg = false;//出現異常 } } } return msg; }
註:PostMoths方法在小程序基礎配置裏面有
StreamToBytes方法和PreservationCodeImage方法在支付裏面有
.NET開發微信小程序-生成二維碼 - 轉