專案中選用大名鼎鼎的 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 = "";
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() + "&timestamp=" + 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 = ; i < ; 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) {
GetGarages($("#L_xlc")[], data); // 根據地理位置座標篩選附近廠家,並將篩選結果填入前端頁面的下拉選單
}
);
}
});
});
}
else
{
GetGarages($("#L_xlc")[], "")
}
}
});

實際案例: 獲取臨時票証 (JsApi Ticket)的更多相关文章

  1. 實際案例: 已知要獲取臨時票証 (JsApi Ticket) 才能調用的接口

    需獲取票証才能調用的接口,簡單列示如下: 一.基礎類 1. wx.checkJsApi (當前客戶端是否支持指定JS) 二.分享類 1.wx.onMenuShareTimeline (分享到朋友圈)2 ...

  2. Android Training精要(五)讀取Bitmap對象實際的尺寸和類型

    讀取Bitmap對象實際的尺寸和類型 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecode ...

  3. [心得] 如何利用liquibase進行資料庫版本控制 - 實際練習

    透過上一篇的基本觀念介紹,希望大家應該有一點點感覺了! 這篇我們就來做個簡單的版本演練,加深印象吧! 我使用的環境如下 System : Windows 7 Database : SQL Server ...

  4. 自動獲取外網IP并發郵件

    問題: 公司有一Web系統需開放給香港Office公司查詢資料,但最近動態域名需實名認證, 因系統較小型,非公開大範圍使用,所以再認證一域名也沒多大必要, 所以想定時生成一封能查詢外網IP的郵件發送給 ...

  5. C#-Windows服務以LocalSystem賬戶安裝的話無法獲取我的文檔路徑

    如圖,如果Window服務以上圖 Account安裝運行,則無法獲取到 以下路徑: System.Environment.GetFolderPath(Environment.SpecialFolder ...

  6. jquery html 獲取或設置

    jquery提供操作html元素的屬性和內容的強大方法. DOM就是獨立于平台和語言的界面,允許程序和腳本動態訪問和改變DOM的內容,結構和樣式. 獲取內容:text(),html(),val(),a ...

  7. Oracle臨時表空間過大問題解決

    查詢資料庫伺服器時,發現資料庫伺服器磁片使用空間達到了98%,分析總共的資料檔案也不可能達到如此大,經過查詢發現原來臨時表空間的使用方式達到了 32G,導致磁碟空間使用緊張.搜索了相應的文檔與資料後, ...

  8. 在windows平臺下使用cygwin獲取root用戶權限

    最近在使用cygwin時發現一個問題,當我要使用root用戶權限時,竟然創建不了root賬戶.最後在網上找了下後,暫時衹找到了通過更改當前用戶權限獲得root權限的方法,具體如下: 实际环境:win1 ...

  9. python 爬虫入门----案例爬取上海租房图片

    前言 对于一个net开发这爬虫真真的以前没有写过.这段时间学习python爬虫,今天周末无聊写了一段代码爬取上海租房图片,其实很简短就是利用爬虫的第三方库Requests与BeautifulSoup. ...

随机推荐

  1. Spark Streaming源码解读之Executor容错安全性

    本期内容 : Executor的WAL 消息重放 数据安全的角度来考虑整个Spark Streaming : 1. Spark Streaming会不断次序的接收数据并不断的产生Job ,不断的提交J ...

  2. SQL Server Reporting Services (SSRS): Reporting Services in SQL Server 2012 (codename "Denali") will support XLSX, DOCX formats. Bye bye 65536 rows limit in XLS files ;)

    当SSRS报表的时候,若相应EXCEL是2003以下,在行数超过65536的时候报表会报错 "Microsoft.ReportingServices.ReportProcessing.Han ...

  3. 使用yum安装应用程序时候,报错:[Errno 14] PYCURL ERROR 7 - "Failed to connect to 2001:da8:8000:6023::230: 网络不可达"

    使用yum安装应用程序时候,报错:[Errno 14] PYCURL ERROR 7 - "Failed to connect to 2001:da8:8000:6023::230: 网络不 ...

  4. eclipse内嵌jetty(run-jetty-run插件) 配置jndi数据源

    运行环境 java 6,eclipse juno,ssh(spring,hibernate,springmvc ) 1.离线安装 下载地址:http://pan.baidu.com/s/1qX67wO ...

  5. 在sql server使用链接服务器中访问mysql

    ----创建ODBC链接EXEC sp_addlinkedserver @server = 'MySQL', @srvproduct='MySql' , @provider = 'MSDASQL', ...

  6. MySQL数据库(表)的导入导出(备份和还原)

    一)在同一个数据库服务器上面进行数据表间的数据导入导出: 1. 如果表tb1和tb2的结构是完全一样的,则使用以下的命令就可以将表tb1中的数据导入到表tb2中: insert into db2.tb ...

  7. asp.net mvc4 Html.BeginForm表单提交

    默认是get提交,如果是post提交需要在控制器ActionResult上加:[AcceptVerbs(HttpVerbs.Post)] 举例: 在HelpController中,会定义如下的Acti ...

  8. ajax 对象创建 兼容各个浏览器

    <script> function createAjax(){ var request=false; //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE ...

  9. char wchar 互转 多字符 宽字符 的N种方式

    1:  用 CString  如果没有mfc 可以用 ATL 中的 CString  #include <atlstr.h>     CStringA v1 = "111&quo ...

  10. Linux解压和打包jar

    linux 中解压jarunzip XXX.jar -d app 打jar 进入到解压目录里面(app)jar cvfm0 MR-XDR-JMR-NEW.jar META-INF/MANIFEST.M ...