手机版

JS调用微信扫码功能

时间:2021-09-06 来源:互联网 编辑:宝哥软件园 浏览:

1.第一步:

设置调用微信js安全域名,可以在该域名下调用微信js接口

这里写图片描述

2.第二步:

需要调用微信扫描的页面附上以下js,前提是需要介绍微信的js【】

这里写图片描述

3.第三步:

参考官方文件https://mp.weixin.qq.com/wiki?

通过后台获取js权限签名jsapi _ ticket参考文档:http://MP . weixin . QQ.com/wiki/7/AAA 137 b 55 FB 2e 0456 BF 8dd 9148 DD 613 f . html # . e9 . 99 . 84 . e 5 . BD . 951-js-SDK . E4。

在后台获取特征码:

包裹。com。新纪元。WAP。常见;导入Java。io。ioexception导入Java。io。unsupportdencodinegexception导入Java。保安。messagedigest导入Java。保安。nosuchalgorithm异常;导入Java。乌提尔。格式化程序;导入Java。乌提尔。HashMap导入Java。乌提尔。地图;导入Java。乌提尔。UUID;导入组织。阿帕奇。http。HTtpresponse导入组织。阿帕奇。http。客户。clientprotocolexception导入组织。阿帕奇。http。客户。方法。HttpGet导入组织。阿帕奇。http。impl。客户。httpclients导入组织。阿帕奇。http。乌提尔。entityutils导入com。新纪元。常见。utils。jsonutil/** * 官方给的使用射流研究…的验证工具* @ author Administrator * */public class JsSignUtil { public static String访问令牌=null公共静态映射字符串,对象符号(字符串URL){ 0字符串发送网址='https://api.weixin.qq.com/cgi-bin/token?grant _ type=client _ credential appid=* * appid * * secret=* * secret * * ';字符串结果=gethttpresslt(发送URl);对象访问结果=jsonutil。getvaluefromjson(结果、“access _ token”);字符串符号='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access _ token=' accessresult。tostring()“type=jsapi”;字符串结果符号=gethttpresslt(符号URL);对象票证=jsonutil。getvaluefromjson(结果符号,)票证');MapString,Object ret=new HashMapString,Object();string nonce _ str=create _ nonce _ str();字符串时间戳=create _ timestamp();字符串1;字符串签名=' ';//注意这里参数名必须全部小写,且必须有序字符串1=' jsapi _ ticket=' ticket ' non estr=' nonce _ str ' timestamp=' timestamp ' URL=' URL;尝试{ MessageDigest crypt=MessageDigest。getinstance(' SHA-1 ');地穴。reset();地穴。更新(字符串1。getbytes(' UTF-8 ');signature=Bytetohex(crypt。digest());} catch(nosuchalgorithm异常e){ e . print stack trace();} catch(unsupportdencodinegexception e){ e . printstacktrace();} ret.put('url ',URL);ret。put(' non estr ',nonce _ str);ret.put('时间戳,时间戳);ret.put('签名,签名);ret.put('jsapi_ticket ',ticket);ret.put('appId ',' wx 154 f 74654 AE 04891 ');返回ret} /** *随机加密* @ param hash * @ return */private static String byteToHex(最终字节[]哈希){ Formatter Formatter=new Formatter();用于(字节b : hash){ formatter.format('x',b);}字符串结果=格式化程序。tostring();格式化程序。close();返回结果;} /* * 获取访问地址链接返回值*/私有静态字符串getHttpResult(字符串URL){ 0字符串结果=' ';Http get Httprequest=new Http get(URL);请尝试{ Httpresponse Httpresponse=httpclients。创建默认值().执行(Httprequest);if (httpResponse.getStatusLine().getStatusCode()==200){ result=entity tils。tostring(httpresponse。getentity());} } catch(ClientProtocolException e){ e . print stack trace();结果=e.getMessage().toString();} catch(IOexception e){ e . print stack trace();结果=e.getMessage().toString();}返回结果;} /** * 产生随机串-由程序自己随机产生* @ return */private static String create _ nonce _ str(){ return uuid。randomuuid().toString();} /** * 由程序自己获取当前时间* @ return */private static String create _ timestamp(){ return long。tostring(系统。currentimemillis()/1000);} } 官方文档示例:

参考以下文档获取access_token(有效期7200秒,开发者必须在自己的服务全局缓存access_token):/15/54 ce 45 d8d 30 b 6 BF 6758 f 68 d2e公元前95年627年。超文本标记语言

用第一步拿到的访问令牌采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi _ ticket):https://API。微信。QQ。com/CGI-bin/ticket/getticket?ACCESS _ token=ACCESS _ token type=jsapi

四第四步:

前台进入页面:`

@请求映射(值='/跳马)公共线跳马(HttpServletRequest请求,GoodsVO商品)引发异常{字符串Url=请求。getrequesturl()“?”request.getQueryString().toString();MapString,Object resMap=new HashMapString,Object();resMap=jssignutil。符号(URl);请求。setattribute(' non estr ',resmap。get(' non estr ');请求。setattribute(' timestamp ',resmap。get('时间戳');请求。setattribute(' signature ',resmap。get(' signature ');request.setAttribute('appId ',resmap。get(' AppID ');request.setAttribute('url ',resmap。get(' URL ');请求。setAttribute(' jsapi _ ticket ',resmap。get(' jsapi _ ticket ');返回“索引/跳马";}`以上所述是小编给大家介绍的射流研究…调用微信扫一扫功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

版权声明:JS调用微信扫码功能是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。