微信调用扫一扫的步骤(ASP实现方式)

来源:互联网 发布:access数据库编辑器 编辑:程序博客网 时间:2024/06/05 19:54

1.微信服务号(或公众号)需要通过认证

2.点击“公众号设置”-“功能设置”,在“JS接口安全域名”处填写上域名,二级域名也可以的,注意该域名必须是备案过的,否则无效。
图例

3.开始写代码上传到上述设定的安全域名下面。例如设定的安全域名是test.bzzs.com,那么可以在test.bzzs.com的ftp下面放源代码文件,不一定在根目录下。微信会检测网址的域名是否是备案的,而和具体的目录和代码文件名无关。

4.比如我们的测试文件是test.htm,那么开始配置这个文件里的js代码。wx.config这个部分有几个参数需要设置正确的。
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: ”, // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: ”, // 必填,生成签名的随机串
signature: ”,// 必填,签名,见附录1
jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});

参考:
http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E6.AD.A5.E9.AA.A4.E4.B8.80.EF.BC.9A.E7.BB.91.E5.AE.9A.E5.9F.9F.E5.90.8D

appId在基本配置里可以找到,每个账号都不一样的。timestamp随便写没关系。nonceStr是自己程序随机生成的。signature最复杂,是前面几个综合进行sha1加密的结果。
主要就是上面这4个。
我用asp实现了一个接口,需要传递3个参数给它,分别是AppID、AppSecret、url:

<!--#include file="sha1.asp"--><%AppID=request("AppID")'"wx0cd565f70be8xxxx"AppSecret=request("AppSecret")'"2ad5303155fb4c0b0474b309c9d5xxxx"timestamp=ToUnixTime(now,+8)nonceStr=getRndNumber()url=request("url")'"http://"&Request.ServerVariables("server_name")&Request.ServerVariables("script_name") response.Charset="gb2312" '根据相应情况,可修改为utf-8Response.Expires = -9999Response.AddHeader "Pragma","no-cache"Response.AddHeader "cache-ctrol","no-cache"function getRndNumber()    keyword="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"    intKeyWordLength=len(keyword)    randomize    for i=1 to 16        s=s & mid(keyword,Int(Rnd * intKeyWordLength) + 1,1)    next    getRndNumber=send functionFunction ToUnixTime(strTime, intTimeZone)            If IsEmpty(strTime) or Not IsDate(strTime) Then strTime = Now            If IsEmpty(intTimeZone) or Not isNumeric(intTimeZone) Then intTimeZone = 0             ToUnixTime = DateAdd("h",-intTimeZone,strTime)             ToUnixTime = DateDiff("s","1970-1-1 0:0:0", ToUnixTime)        End FunctionFunction GetBody(weburl)    Dim ObjXMLHTTP    Set ObjXMLHTTP=Server.CreateObject("MSXML2.serverXMLHTTP")    ObjXMLHTTP.Open "GET",weburl,False    ObjXMLHTTP.send    While ObjXMLHTTP.readyState <> 4        ObjXMLHTTP.waitForResponse 10000    Wend    GetBody=ObjXMLHTTP.responseBody    Set ObjXMLHTTP=NothingEnd FunctionFunction BytesToBstr(body,Cset)    dim objstream    set objstream = Server.CreateObject("adodb.stream")    objstream.Type = 1    objstream.Mode =3    objstream.Open    objstream.Write body    objstream.Position = 0    objstream.Type = 2    objstream.Charset = Cset    BytesToBstr = objstream.ReadText    objstream.Close    set objstream = nothingEnd Function'([^\d]+\d+\*admin)function getMatch1Sub1(strData,strPattern)    if isnull(strData) then        getMatch1Sub1=""        exit function    end if    Set reg = CreateObject("vbscript.regExp")    reg.Global = True    reg.IgnoreCase = True    reg.MultiLine = True    reg.Pattern = strPattern'"====\r\n([^\r\n]*)\r\n[\s\S]*?2431682927627455"    Set matchs = reg.Execute(strData)    if matchs.Count>0 then getMatch1Sub1=matchs(0).SubMatches(0)End functioncaiurl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" & AppID & "&secret=" & AppSecretaccess_token=getMatch1Sub1(BytesToBstr(GetBody(caiurl),"gb2312"),"{""access_token"":""(.*?)""")caiurl="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" & access_token & "&type=jsapi"jsapi_ticket=getMatch1Sub1(BytesToBstr(GetBody(caiurl),"gb2312"),"""ticket"":""(.*?)""")string1="jsapi_ticket=" & jsapi_ticket & "&noncestr=" & noncestr & "&timestamp=" & timestamp & "&url=" & urlsignature=Sha1(string1)response.write "appId=" & AppID & "," & "nonceStr=" & nonceStr & "," & "timestamp=" & timestamp & "," & "signature=" & signature & ",datetime=" & now%>

也就是js文件执行之前需要配置的参数由动态语言在服务器生成好。这个是不允许纯js去生成的因为要保护appid等隐私信息,规定只能后台生成。注意签名signature的有效时间大概就2个小时,而又不能无限制的去生成签名,因为微信限制了一天只能几百次好像。所以当有请求过来的时候得检查当前签名是否用了2个小时了,如果超时了那么重新生成。

这看起来非常简单的一个功能,但是实际做起来还是挺复杂的。如果没有人指导纯粹靠自己摸索可能要好多天都不一定能搞定。

0 0
原创粉丝点击