1:小程序接口调用(获取openid)
private String openidUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
private String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
public String getOpenid(String jsCode) {
String appid = coreProperties.getMiniapp().getAppid();
String secret = coreProperties.getMiniapp().getAppsecret();
String url = String.format(openidUrl, appid, secret, jsCode);
String result = Https.get(url);
MiniOpenid mini = Jsons.toObject(result, MiniOpenid.class);
if (mini.getErrcode() != null && !SUCCESS_CODE.equals(mini.getErrcode())) {
throw new ServiceException("获取openid失败");
}
return mini.getOpenid();
}
public String getAccessToken() {
String accessToken = redisCache.get("accessToken");
if (StringUtils.isNotBlank(accessToken)) {
return accessToken;
}
String appid = coreProperties.getMiniapp().getAppid();
String secret = coreProperties.getMiniapp().getAppsecret();
String url = String.format(accessTokenUrl, appid, secret);
String result = Https.get(url);
accessToken = Jsons.getValue(result, "access_token");
if (!StringUtils.isNotBlank(accessToken)) {
throw new ServiceException("获取accessToken失败");
}
redisCache.set("accessToken", accessToken, 7000L, TimeUnit.SECONDS);
return accessToken;
}
2:获取微信公众号的openid
/**
* 公众号授权
*/
public void accessSign(String code,String state){
// 前面为业务逻辑
ShipOwnerRegister shipOwnerRegister = shipOwnerRegisterService.getByToken(state);
String phone = shipOwnerRegister.getPhone();
String appid = coreproperties.getWechat().getAppid();
String appsecret = coreproperties.getWechat().getAppsecret();
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid
+ "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";
String resp = Https.get(url);
log.info("wechat-resp:" + resp);
String openid = Jsons.getValue(resp, "openid");
log.info("wechat-openid:" + openid);
redisCache.set(ACCESS_TOKEN, getAccessToken(), 7200L, TimeUnit.SECONDS);
// 保存openid到ship_owner_register中去
shipOwnerRegister.setWechatOpenId(openid);
shipOwnerRegisterService.updateEX(shipOwnerRegister);
}
/**
* 获得公众号的access_token
*/
private String getAccessToken() {
/*if (StringUtils.isNotBlank(redisCache.get("driverApi:ACCESS_TOKEN"))) {
return redisCache.get("driverApi:ACCESS_TOKEN");
}*/
String appid = coreProperties.getWechat().getAppid();
String appsecret = coreProperties.getWechat().getAppsecret();
log.info("appid:" + appid + "----appsecret:" + appsecret);
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid
+ "&secret=" + appsecret;
String resp = Https.get(url);
log.info("accessToken-resp:" + resp);
String accessToken = Jsons.getValue(resp, "access_token");
log.info("accessToken-token:" + accessToken);
redisCache.set("driverApi:ACCESS_TOKEN", accessToken, 7200L, TimeUnit.SECONDS);
return accessToken;
}
3:发送微信模板消息
/**
* 发送模板消息
*/
public void sendToWeChat(BigDecimal price,String cargoName,BigDecimal capacity,String startPort,String endPort,String remarks,String weopenId){
if (StringUtils.isBlank(weopenId))
return;
String access_token = getAccessToken();
String priceStr = price!=null?price.toString():"";
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
Map<String, Object> params = new HashMap<String, Object>();
params.put("touser",weopenId);
params.put("template_id", coreProperties.getWechat().getTemplate());
params.put("first",MessageData.builder().value("运费单价:"+priceStr+"元").color("#173177").build());
params.put("keyword1",MessageData.builder().value(cargoName).color("#173177").build());
params.put("keyword2",MessageData.builder().value(capacity!=null?capacity.toString():"").color("#173177").build());
params.put("keyword3",MessageData.builder().value(startPort).color("#173177").build());
params.put("keyword4",MessageData.builder().value(endPort).color("#173177").build());
params.put("keyword5",MessageData.builder().value("").color("#173177").build());
params.put("remark",MessageData.builder().value("备注:"+remarks).color("#173177").build());
// 将map转成json
String response = Https.postJson(url,params);
log.info("微信返回信息:"+response);
}
@Data
public static class MessageData {
private String value;
private String color;
}
4:根据公众号openid获得用户信息