java获取微信小程序用户信息代码
时间: 2023-09-05 20:12:08 浏览: 165
java实现微信小程序-用户安全等级
以下是基于微信官方提供的 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` 方法用于查询微信支付订单信息,需要传入商户订单号。
阅读全文