java调用微信扫一扫
步骤:
1,获取Accesstoken(参考我之前的文章)
2,获取jsapiticket(参考我之前的文章)
3,获取签名
4JSSDK使用步骤
步骤一:绑定域名(JS接口安全域名),。否则会报invalid url domain
步骤二:引入JS文件http://res.wx.qq.com/open/js/jweixin-1.2.0.js
步骤三:通过config接口注入权限验证配置
步骤四:通过ready接口处理成功验证
步骤五:通过error接口处理失败验证
5.调用扫一扫接口
controller
package controller; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import util.AccessTokenUtil; /**
* @author zhangyanan
* @todo 微信扫一扫
* @date 2018年7月23日
*/
@Controller
@RequestMapping("/scan")
public class ScanController {
Logger logger=LoggerFactory.getLogger(ScanController.class); /**
* @todo 扫一扫准备操作
* @author zhangyanan
* @date 2018年7月31日
*/
@RequestMapping("/preScan")
public String preScan(HttpServletRequest req){
Long timestamp = System.currentTimeMillis() / 1000;
String nonceStr =UUID.randomUUID().toString();
//AccessTokenUtil.getJsApiTicket()是获取jsapi_ticket
String sign = getSign(AccessTokenUtil.getJsApiTicket(),nonceStr,timestamp,req.getRequestURL().toString());
req.setAttribute("timestamp", timestamp);
req.setAttribute("nonceStr", nonceStr);
req.setAttribute("sign", sign);
return "scan";
} /**
* @todo 获取签名 注意这里参数名必须全部小写,且必须有序
* @author zhangyanan
* @date 2018年7月31日
*/
private String getSign(String jsapi_ticket, String noncestr, Long timestamp, String url){
try {
String shaStr = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url="+ url;
logger.info(shaStr); MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(shaStr.getBytes());
StringBuffer signature = new StringBuffer();
for (int i = 0; i < result.length; i++) {
signature.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return signature.toString();
} catch (NoSuchAlgorithmException e) {
logger.error("获取微信签名异常",e);
return null;
}
}
}
scan.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>微信扫一扫</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.2.1.1.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<!-- <script type="text/javascript" src="js/weixin.js"></script> -->
</head>
<body>
<button id="scanQRCode" onclick="scanCode()">扫码</button>
<input id="timestamp" type="hidden" value="${timestamp}" />
<input id="noncestr" type="hidden" value="${nonceStr}" />
<input id="signature" type="hidden" value="${sign}" />
</body>
<!-- JSSDK使用步骤
步骤一:绑定域名
步骤二:引入JS文件http://res.wx.qq.com/open/js/jweixin-1.2.0.js
步骤三:通过config接口注入权限验证配置
步骤四:通过ready接口处理成功验证
步骤五:通过error接口处理失败验证 -->
<script type="text/javascript">
$(function(){
wxConfig($("#timestamp").val(),$("#noncestr").val(),$("#signature").val());
});
function wxConfig(_timestamp, _nonceStr, _signature) {
//alert('获取数据:'+_timestamp+'\n'+_nonceStr+'\n'+_signature);
console.log('获取数据:' + _timestamp + '\n' + _nonceStr + '\n' + _signature);
wx.config({
debug : true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId : "wx867cb83bc6a97559", // 必填,公众号的唯一标识
timestamp : _timestamp, // 必填,生成签名的时间戳
nonceStr : _nonceStr, // 必填,生成签名的随机串
signature : _signature,// 必填,签名,见附录1
jsApiList : ['scanQRCode' ]// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
alert("config完成");
});
wx.error(function(res){
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
alert("config失败");
});
}
function scanCode() {
wx.scanQRCode({
needResult : 1,
scanType : [ "qrCode", "barCode" ],
success : function(res) {
console.log(res)
alert(JSON.stringify(res));
var result = res.resultStr;
},
fail : function(res) {
console.log(res)
alert(JSON.stringify(res)); }
});
}
</script>
</html>
参考文章:
官网api:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
文章1:https://blog.csdn.net/ricky73999/article/details/78588722
文章2:https://blog.csdn.net/hfhwfw/article/details/76038923
java调用微信扫一扫的更多相关文章
- .Net 调用微信公众号扫一扫
1.绑定域名 去微信公众号平台中设置js接口安全域名,要注意的是不填写http://, 只填写域名即可,如 www.baidu.com. 一个月只能修改三次,要谨慎填写. 2.引入JS文件 在页面中引 ...
- java实现微信支付之扫码支付
本文直接从代码调用微信扫码支付讲起.账号配置,参数生成等请参考官方文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1 微信 ...
- JAVA调用微信接口实现页面分享功能(分享到朋友圈显示图片,分享给朋友)
钉钉提供的内网穿透之HTTP穿透:https://www.cnblogs.com/pxblog/p/13862376.html 网页分享到微信中如何显示标题图,如果自定义标题图,描述,显示效果如下 官 ...
- [实例]JAVA调用微信接口发送图文消息,不用跳到详情页
package com.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStrea ...
- java 调用微信截图工具
- JAVA实现调用微信js-sdk扫一扫
喜欢的朋友可以关注下. 已经很久没有给大家分享一片技术文章了,今天抽了点时间来,给大家说一说如何调用微信提供的扫一扫接口. 前提: 需要申请一个公众号:申请公众号需要的资料我就不说了,去申请微信会提示 ...
- 微信JS-SDK使用步骤(以微信扫一扫为例)
概述: 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用 ...
- DWR实现扫一扫登录功能
前言 <DWR实现后台推送消息到Web页面>一文中已对DWR作了简介,并列出了集成步骤.本文中再一次使用到DWR,用以实现扫一扫登录功能. 业务场景 web端首页点击"登陆&qu ...
- 调用微信的扫一扫功能详解说明---(java 排坑版)
最近碰到了这么一个需求,说是在前端页面调用手机本地的相机,扫描二维码这么一个需求,对于我一个后端来说, 这实在是难,难于上青天,但是决不能说一个不字.我说可以使用微信的扫码工具吗,这样可以方便一点,. ...
随机推荐
- linux shell 杂
1. 字符串截取 var=http://www.baidu.com/index.html echo ${var} var=http://www.baidu.com/index.html /*从左到右第 ...
- 使用ansible安装docker以及docker-compose
转自:https://www.cnblogs.com/jsonhc/p/7879028.html 环境三台centos7主机: master:192.168.101.14,node1:192.168. ...
- php装curl拓展出错
错误: checking for cURL in default path... not found checking for cURL 7.10.5 or greater... ./configur ...
- P、NP、NPC和NP-Hard相关概念的图形和解释
P.NP.NPC和NP-Hard相关概念的图形和解释 http://blog.csdn.net/huang1024rui/article/details/49154507 一.相关概念 P: 能在多项 ...
- npm run dev 报错:missing script:dev
一.问题: 今天在运行vue项目时,在mac终端输入npm run dev,结果报错: 翻译是: npm错误:缺少script:dev npm错误:完整路径见:users/mymac/ .npm/_l ...
- springboot 整合redis redis工具类
一步 : pom中引入相关依赖 <!-- 引入 redis 依赖 --> <dependency> <groupId>org.springframework.boo ...
- Linux简介及Linux学习路线图
一.Linux 为何物 Linux 就是一个操作系统,就像你多少已经了解的 Windows(xp,7,8)和 Max OS ,至于操作系统是什么,就不用过多解释了,如果你学习过前面的入门课程,应该会有 ...
- Real Time Rendering 1
[Real Time Rendering 1] 1.RTR是一本导论.官网:http://www.realtimerendering.com. 2.At around 6 fps, a sense o ...
- python+selenium的环境配置
以前写过关于python和selenium加myeclipse的环境配置,但是myeclipse启动时过于费时,虽然myeclipse有很好的提示功能,但是作为初学者,我还是直接用python的idl ...
- IDEA中配置JUnit单元测试
参考安装教程:https://www.jianshu.com/p/c37753b6dbd6 如果想用junit4的话,需要在pom.xml中配置. 需要安装JUnitGenerator V2.0插件, ...