大概流程:
1.根据appId和appSecret获取access_token(使用凭证)
进入微信公众平台扫码登录测试号公众号:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
public static JSONObject getTokenTool(String appId,String appSecret){
String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
System.out.println(HttpRequestUtil.httpRequest(url, "GET", ""));
return HttpRequestUtil.httpRequest(url, "GET", "");
}
这是测试号获取access_token的接口,普通公众号接口地址请查看微信官方文档。
2.根据access_token获取jsapi_ticket
public static JSONObject getTicketTool(String access_token){
String url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+access_token+"&type=jsapi";
System.out.println(HttpRequestUtil.httpRequest(url, "GET", ""));
return HttpRequestUtil.httpRequest(url, "GET", "");
}
3.在微信获取签名算法(具体如下)
public static Map<String, String> sign(String url) {
String appId="wxcc42f613dbf7299f";
String appSecret="e59e10c74c70b1c042b5ecaf454052ac";
String access_token=getTokenTool(appId, appSecret).getString("access_token");
JSONObject ticketJson=getTicketTool(access_token);
System.out.println(ticketJson.toString());
String jsapi_ticket = ticketJson.getString("ticket");
Map<String, String> ret = new HashMap<String, String>();
//这里的jsapi_ticket是获取的jsapi_ticket。
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
String string1;
String signature = "";
System.out.println(2);
//注意这里参数名必须全部小写,且必须有序
string1 = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + nonce_str +
"×tamp=" + timestamp +
"&url=" + url;
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
System.out.println("crypt="+crypt.toString());
System.out.println("string1="+string1);
System.out.println("signature="+signature);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
ret.put("url", url);
ret.put("jsapi_ticket", jsapi_ticket);
ret.put("nonceStr", nonce_str);
ret.put("timestamp", timestamp);
ret.put("signature", signature);
return ret;
}
private static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
private static String create_nonce_str() {
return UUID.randomUUID().toString();
}
private static String create_timestamp() {
return Long.toString(System.currentTimeMillis() / 1000);
}
4.获取一个js安全域名接口(如:gstz.imwork.net)
一般使用花生壳进行内网穿透,使内网地址映射到一个花生壳提供的公网的地址,适合用来测试,花生壳内网穿透如何使用可到花生壳官网查看。
5.把域名和action的地址拼接起来成url,放入签名方法中
(如签名算法的action:https://gstz.imwork.net:8080/Weixin/weixin/getSign.do)
@Controller
@SuppressWarnings("serial")
public class WeixinAction{
/**
* 获取签名算法
*/
@RequestMapping("getSign")
@ResponseBody
public JSONObject getSign(){
JSONObject jsonObject=new JSONObject();
String url="http://17j62137x0.imwork.net/ssm_d1/index.jsp";
Map<String, String> ret =WxJSUtil.sign(url);
System.out.println("map="+ret.toString());
jsonObject.put("weixin", ret);
System.out.println("json="+jsonObject.toString());
return jsonObject;
}
}
6.在JSP页面初始化时异步获取签名action传来3个参数,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 src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/weixin.js"></script>
</head>
<body>
<input id="timestamp" type="hidden" value="${timestamp}" />
<input id="noncestr" type="hidden" value="${nonceStr}" />
<input id="signature" type="hidden" value="${signature}" />
<input id="id_securityCode_input">
<button id="scanQRCode" onclick="getWeixinResult()">扫码</button>
</body>
<script type="text/javascript">
</script>
</html>
7.通过签名算法获取wx.config的4个重要参数:jsapi_ticket、noncestr、timestamp、url
wx.config({ debug: true,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId:'',// 必填,公众号的唯一标识 timestamp: ,// 必填,生成签名的时间戳
nonceStr:'',// 必填,生成签名的随机串 signature:'',//
必填,签名,见附录1 jsApiList: []// 必填,需要使用的JS接口列表,所有JS接口列表见附录2});
8.告诉微信选择调用的接口名称
9.把获取的结果result进行处理getWeixinResultCallBack(param)
10.给按钮添加 getWeixinResult事件,就可以通过按钮调用扫一扫功能。(记得用微信浏览器点开)
这是DEMO的下载地址(博主本想免费分享这个资源的,无奈CSDN最少也要2点资源分才能下载):下载DEMO