微信AirKiss 配网界面调起Demo

微信AirKiss 配网界面调起Demo

最近硬件大哥又提出新需求,想要在我们的硬件上加上微信的AirKiss,用这个来实现wifi设备配网,之前的我们的硬件都是采取的蓝牙配网,不过配网好像没有那么方便,使用了微信这种配网方式,发现挺方便,且可行性比较高,所以呢,楼主又得开始进行这方面开发了,楼主看着微信网页的开发文档还是撸出来了,下面具体讲一下

第一步:

先用微信公众号的appid和秘钥去获取微信的acctoken

 "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;

第二步:

用拿到的acctoken去获取ticket

"https://api.weixin.qq.com/cgi-bin/ticket/getticket?&type=jsapi&access_token=" + acctoken;

第三步:

用当前网址、获取到的ticket、随机生成字符串、当前时间戳去按照字母排序组合成字符串,这种格式的

	String key = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "&timestamp=" + timeStamp + "&url=" + localurl;

第四步:

对生成的字符串用sha1加密算法,拿到sign签名

Sign = hash(key, "SHA1");

第五步:

看全部代码


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%> 
<jsp:directive.page import="com.puxun.icac.utils.*" />
<jsp:directive.page import="net.sf.json.JSONObject" />
<jsp:directive.page import="java.io.UnsupportedEncodingException,java.net.URL,java.security.MessageDigest,java.security.NoSuchAlgorithmException" /> 
<%!
	String appid = "您的APPID";
	String appsecret = "您的APPSECRECT";
	String acctokenurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid
			+ "&secret=" + appsecret;
	String ticketurl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?&type=jsapi&access_token=";
	String localurl = "当前文件的访问地址";
	String timeStamp =  System.currentTimeMillis() / 1000 + "";
	String noncestr = "";
	String ticket = "";
	String Sign = ""; 
	 
	  
	 /**
     * 计算字符串的hash值
     * @param string    明文
     * @param algorithm 算法名
     * @return          字符串的hash值
     */
    public  String hash(String string, String algorithm) {
        if (string.isEmpty()) {
            return "";
        }
        MessageDigest hash = null;
        try {
            hash = MessageDigest.getInstance(algorithm);
            byte[] bytes = hash.digest(string.getBytes("UTF-8"));
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
 
	public  void getmethof() {
	
		JSONObject acctokenJson = HttpsUtils.httpsRequest(acctokenurl, "GET", null); 
		
		if (!acctokenJson.containsKey("access_token")) {
			return;
		}
		String acctoken = acctokenJson.getString("access_token");
		JSONObject ticketjson = HttpsUtils.httpsRequest(ticketurl + acctoken, "GET", null); 
		if (!ticketjson.containsKey("ticket")) {
			return;
		}
		 ticket = ticketjson.getString("ticket");
		 noncestr = RandCharsUtils.getRandomString(16); 
		  
		String key = "jsapi_ticket=" + ticket 
				+ "&noncestr=" + noncestr 
				+ "&timestamp=" + timeStamp 
				+ "&url=" + localurl;
		System.out.println(key); 
			Sign = hash(key, "SHA1");
			System.out.println(Sign);
		 
	} 
%>
<%
	getmethof();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<title>设备配网</title>
</head>
<body>
	 <h1 style="font-size:200%">正在扫描WIFI</h1>
</body>
<script type="text/javascript">
	
	var appid = "<%=appid %>";
	var timestamp = parseInt("<%=timeStamp %>");
	var nonceStr = "<%=noncestr %>";
	var signature = "<%=Sign %>"; 
	var ticket = "<%=ticket %>"; 
	console.log(timestamp);
	console.log(location.href);
    wx.config({
        beta : true,
        debug : false,
        appId : appid,
        timestamp : timestamp,
        nonceStr : nonceStr,
        signature : signature,
        jsApiList : ['configWXDeviceWiFi']  
    });
 
    wx.ready(function () {
        wx.checkJsApi({
            jsApiList: ['configWXDeviceWiFi'],
            success: function(res) {
                wx.invoke('configWXDeviceWiFi', {}, function(res){  
                    var err_msg = res.err_msg;
                    if(err_msg == 'configWXDeviceWiFi:ok') { 
                           window.location.href = 'http://www.puxuntech.com' 
                        return;
                    } else {
                       window.location.href = 'http://www.puxuntech.com' 
                    }
                });
            }
        });
    }); 
</script> 
</html>

第六步:

发布了36 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_41392105/article/details/88736568