微信公众号JS-SDK获取用户位置信息

记录一下自己实现这个功能的步骤

微信开放文档地址:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html

 

公众号配置很重要

参考https://www.cnblogs.com/liuxinruif0/p/8652461.html的配置部分

1.配置接口ip白名单

2.配置授权域名

3.将文件上传至服务器根目录

4.开启接口权限

 

项目使用的JFinal,代码就不贴了,此处给出实现过程和思路(参照微信开发文档实现)

获取网页授权access_token

get请求访问https://api.weixin.qq.com/cgi-bin/token,参数如下:

Map<String, String> params = new HashMap<>();
params.put("grant_type", "client_credential");
params.put("appid", appId);
params.put("secret", appSecret);

后台缓存token防止过度调用

获取api_ticket

get请求访问https://api.weixin.qq.com/cgi-bin/ticket/getticket,参数如下:

Map<String, String> params = new HashMap<>();
params.put("access_token", token);
params.put("type", "jsapi");
生成签名

根据当前网页的URL、后台生成随机字符串、时间戳按照ASCII码正序生成参数字符串并进行sha1加密生成签名,将签名、随机字符串、时间戳返回给前端

大致如下:

// 生成签名
Long timeStamp = getSecondTimestamp(new Date());// 时间戳
String nonceStr = UUID.randomUUID().toString();
                
System.out.println("时间戳:" + timeStamp);
System.out.println("随机字符串:" + nonceStr);
String signStr = "jsapi_ticket="+ticket+"&noncestr=" + nonceStr + "&timestamp=" + timeStamp + "&url=" + url;
String signature = SHAUtils.SHA1(signStr);
System.out.println("签名:" + signature);

前端页面引入:<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

获取位置信息:

wx.config({
    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: appId, // 必填,公众号的唯一标识
    timestamp: timeStame, // 必填,生成签名的时间戳
    nonceStr: nonceStr, // 必填,生成签名的随机串
    signature: signature,// 必填,签名
    jsApiList: [
	    'getLocation'
    ] // 必填,需要使用的JS接口列表
});
wx.ready(function(){
    // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
    wx.getLocation({
        type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
        success: function (res) {
        	currLat = res.latitude; // 纬度,浮点数,范围为90 ~ -90
            currLon = res.longitude; // 经度,浮点数,范围为180 ~ -180。
        }
    });
});
wx.error(function(res){
    console.log(res);
    alert("获取当前位置失败!");
});

 

附SHA加密工具类

import java.security.MessageDigest;

public class SHAUtils implements Serializable {
	private static final long serialVersionUID = 1L;

	public static String SHA1(String str) {
		if (str == null || str.length() == 0) {
			return null;
		}
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

		try {
			MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
			mdTemp.update(str.getBytes("UTF-8"));

			byte[] md = mdTemp.digest();
			int j = md.length;
			char buf[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
				buf[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(buf);
		} catch (Exception e) {
			return null;
		}
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值