标签:
專案中選用大名鼎鼎的 Senparc 微信開發套件
獲取臨時票證處理常式的程式碼 (GetgVXinInfo.ashx)
using Senparc.Weixin; using Senparc.Weixin.MP; using Senparc.Weixin.MP.Entities; using Senparc.Weixin.MP.CommonAPIs; using ShouJia.BO; using ShouJia.Facades; using ShouJia.Debugger; using System; using System.IO; using System.Net; using System.Text; using System.Web; namespace ShouJia.VXinWeb.User.ashx { /// <summary> /// VXinInfo 的摘要描述 /// </summary> public class GetVXinInfo : IHttpHandler { public void ProcessRequest(HttpContext c) { //string GarageID = c.Request.QueryString["xlc_id"]; string GarageID = "43"; string Url = c.Request.UrlReferrer.ToString(); WebLogs.Debug("Start for GetVXinInfo ", "Start"); WebLogs.Debug("Get GarageID", GarageID); WebLogs.Debug("Get URL", Url); SendVXinInfo(GarageID, Url, c); WebLogs.Debug("End for GetVXinInfo", "End"); } private void SendVXinInfo(string garageID, string url, HttpContext c) { VXConfigInfo vxi = WeixinBehaviors.GetVXinInfoByGarage(int.Parse(garageID)); AccessTokenContainer.TryGetAccessToken(vxi.AppID, vxi.AppSecret); string access_token = AccessTokenContainer.GetAccessToken(vxi.AppID); WebLogs.Debug("Get AccessToken", access_token); WebLogs.Debug("Get AppID", vxi.AppID); StringBuilder ticketUrl = new StringBuilder(); ticketUrl.AppendFormat("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token); WebLogs.Debug("Get ticketUrl", ticketUrl.ToString()); string ticket = JsApiTicketContainer.GetJsApiTicket(vxi.AppID); string oragiralStr = "jsapi_ticket=" + ticket + "&noncestr=" + GetRandomString() + "×tamp=" + GetTimeTrap() + "&url=" + url; string signature = SHA1(oragiralStr).ToLower(); WebLogs.Debug("Get ticket", ticket); WebLogs.Debug("Get oragiralStr", oragiralStr); WebLogs.Debug("Get signature", signature); c.Response.Write("{\"appid\":\"" + vxi.AppID + "\",\"timestamp\":" + GetTimeTrap() + ",\"nonceStr\":\"" + GetRandomString() + "\",\"signature\":\"" + signature + "\"}"); } private int GetTimeTrap() { return (int)DateTime.Now.Ticks; } private string GetRandomString() { string rString = null; Random ran = new Random((int)DateTime.Now.Ticks); for (int i = 0; i < 10; i++) rString += (char)ran.Next((int)‘a‘, (int)‘z‘); return rString; } private string SHA1(string src) { byte[] cleanBytes = Encoding.Default.GetBytes(src); byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes); return BitConverter.ToString(hashedBytes).Replace("-", ""); } public bool IsReusable { get { return false; } } } }
前端如何呼叫獲取票證處理常式 (以下為前端頁面的 js 程式碼片段,僅摘錄重點)
$.ajax({ url: "/user/ashx/GetVXinInfo.ashx?xlc_id=" + xlcId, success: function (data) { if (data != "error") { var wxInfo = $.parseJSON(data); wx.config({ debug: false, // 羲?覃彸耀宒,覃蚚腔垀衄api腔殿隙硉頗婓諦誧傷alert堤懂ㄛ?猁脤艘換?腔統杅ㄛ褫眕婓pc傷湖羲ㄛ統杅陓洘頗籵徹log湖堤ㄛ躺婓pc傷奀符頗湖荂 appId: wxInfo.appid, // 斛沓ㄛ鼠笲瘍腔峔珨梓妎 timestamp: wxInfo.timestamp, // 斛沓ㄛ汜傖?靡腔奀潔期 nonceStr: wxInfo.nonceStr, // 斛沓ㄛ汜傖?靡腔呴儂揹 signature: wxInfo.signature,// 斛沓ㄛ?靡ㄛ獗蜇翹1 jsApiList: [‘getLocation‘, ‘closeWindow‘] // 斛沓ㄛ剒猁妏蚚腔JS諉諳蹈桶ㄛ垀衄JS諉諳蹈桶獗蜇翹2 }); wx.ready(function () { wx.getLocation({ type: ‘wgs84‘, success: function (res) { $.get( "/user/ashx/GetLocation.ashx", { "Location_x": res.latitude, "Location_y": res.longitude }, function (data) { GetXlcList($("#L_xlc")[0], data); } ); } }); }); } else { GetXlcList($("#L_xlc")[0], "") } } });
标签:
原文地址:http://www.cnblogs.com/hsw1976/p/5478557.html