微信小程序中通过‘http://ip-api.com/json/‘获取地址存在不准确问题

学习过程中,发现通过'http://ip-api.com/json/'提供的IP地址查询位置信息存在误差,经纬度有误差,city也不正确。

微信小程序wx.request得到结果与在浏览器中输入下面网址结果一样

http://ip-api.com/json/106.117.233.21 ​​​​​​​ icon-default.png?t=N7T8http://ip-api.com/json/106.117.233.21结果为:

{"status":"success","country":"China","countryCode":"CN","region":"HE","regionName":"Hebei","city":"Zhoutou","zip":"","lat":37.8957,"lon":114.904,"timezone":"Asia/Shanghai","isp":"Chinanet","org":"Chinanet HE","as":"AS4134 CHINANET-BACKBONE","query":"106.117.233.21"}

我的地址为河北省唐山市,下面是通过https://www.ipshudi.com/106.117.233.21.htm查询的结果:

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是基于微信官方提供的 Java SDK,获取微信小程序用户信息的示例代码: ```java import com.alibaba.fastjson.JSONObject; import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayConfig; import com.github.wxpay.sdk.WXPayConstants; import com.github.wxpay.sdk.WXPayUtil; import java.util.HashMap; import java.util.Map; public class WechatMiniProgramUserInfo { // 小程序 appId private static final String APP_ID = "your_app_id"; // 小程序 appSecret private static final String APP_SECRET = "your_app_secret"; // 微信支付商户号 private static final String MCH_ID = "your_mch_id"; // 微信支付商户密钥 private static final String KEY = "your_key"; // 微信支付统一下单接口 URL private static final String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // 微信支付查询订单接口 URL private static final String ORDER_QUERY_URL = "https://api.mch.weixin.qq.com/pay/orderquery"; /** * 获取小程序用户信息 * * @param code 用户登录凭证 code * @return 用户信息 JSON 对象 * @throws Exception */ public static JSONObject getUserInfo(String code) throws Exception { // 发送请求,获取 openid 和 session_key String url = "https://api.weixin.qq.com/sns/jscode2session"; Map<String, String> requestParams = new HashMap<>(); requestParams.put("appid", APP_ID); requestParams.put("secret", APP_SECRET); requestParams.put("js_code", code); requestParams.put("grant_type", "authorization_code"); String response = HttpRequestUtil.doGet(url, requestParams); JSONObject responseJson = JSONObject.parseObject(response); String openid = responseJson.getString("openid"); String sessionKey = responseJson.getString("session_key"); // 解密用户信息 String encryptedData = "your_encrypted_data"; // 用户信息密文,需从小程序获取 String iv = "your_iv"; // 加密算法的初始向量,需从小程序获取 String decryptedData = WechatMiniProgramUtil.decryptData(encryptedData, sessionKey, iv); JSONObject userInfoJson = JSONObject.parseObject(decryptedData); // 封装用户信息 JSONObject result = new JSONObject(); result.put("openid", openid); result.put("nickname", userInfoJson.getString("nickName")); result.put("avatar_url", userInfoJson.getString("avatarUrl")); result.put("gender", userInfoJson.getInteger("gender")); result.put("province", userInfoJson.getString("province")); result.put("city", userInfoJson.getString("city")); result.put("country", userInfoJson.getString("country")); return result; } /** * 微信支付统一下单接口 * * @param openid 用户 openid * @param body 商品描述 * @param outTradeNo 商户订单号 * @param totalFee 订单总金额(单位为分) * @param ip 用户 IP 地址 * @return 预支付交易会话标识 prepay_id * @throws Exception */ public static String unifiedOrder(String openid, String body, String outTradeNo, int totalFee, String ip) throws Exception { WXPayConfig config = new WXPayConfig() { @Override public String getAppID() { return APP_ID; } @Override public String getMchID() { return MCH_ID; } @Override public String getKey() { return KEY; } @Override public InputStream getCertStream() { return null; } @Override public int getHttpConnectTimeoutMs() { return 8000; } @Override public int getHttpReadTimeoutMs() { return 10000; } @Override public IWXPayDomain getWXPayDomain() { return WXPayDomainSimpleImpl.instance(); } }; WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5); Map<String, String> data = new HashMap<>(); data.put("openid", openid); data.put("body", body); data.put("out_trade_no", outTradeNo); data.put("total_fee", String.valueOf(totalFee)); data.put("spbill_create_ip", ip); data.put("notify_url", "your_notify_url"); data.put("trade_type", "JSAPI"); Map<String, String> response = wxpay.unifiedOrder(data); String returnCode = response.get("return_code"); String resultCode = response.get("result_code"); if ("SUCCESS".equals(returnCode) && "SUCCESS".equals(resultCode)) { return response.get("prepay_id"); } else { throw new Exception("微信支付统一下单失败:" + response.get("return_msg")); } } /** * 微信支付查询订单接口 * * @param outTradeNo 商户订单号 * @return 订单信息 JSON 对象 * @throws Exception */ public static JSONObject orderQuery(String outTradeNo) throws Exception { WXPayConfig config = new WXPayConfig() { @Override public String getAppID() { return APP_ID; } @Override public String getMchID() { return MCH_ID; } @Override public String getKey() { return KEY; } @Override public InputStream getCertStream() { return null; } @Override public int getHttpConnectTimeoutMs() { return 8000; } @Override public int getHttpReadTimeoutMs() { return 10000; } @Override public IWXPayDomain getWXPayDomain() { return WXPayDomainSimpleImpl.instance(); } }; WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5); Map<String, String> data = new HashMap<>(); data.put("out_trade_no", outTradeNo); Map<String, String> response = wxpay.orderQuery(data); String returnCode = response.get("return_code"); String resultCode = response.get("result_code"); if ("SUCCESS".equals(returnCode) && "SUCCESS".equals(resultCode)) { JSONObject result = new JSONObject(); result.put("trade_state", response.get("trade_state")); result.put("total_fee", response.get("total_fee")); result.put("transaction_id", response.get("transaction_id")); return result; } else { throw new Exception("微信支付查询订单失败:" + response.get("return_msg")); } } } ``` 其,`getUserInfo` 方法用于获取用户信息,需要传入用户登录凭证 `code`、用户信息密文和加密算法的初始向量。`unifiedOrder` 方法用于发起微信支付统一下单,需要传入用户 openid、商品描述、商户订单号、订单总金额和用户 IP 地址。`orderQuery` 方法用于查询微信支付订单信息,需要传入商户订单号。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值