微信服务号下可以主动给用户推送小程序卡片,但是得用户授权接受消息才可以,小程序卡片点击即可跳转到小程序中对应的页面
前提是服务号要与小程序绑定,且是同一主体.
1.发送卡片工具类
@Component
public class SendAppletsCard {
private static final Logger logger = Logger.getLogger(SendAppletsCard.class.getName());
public Integer sendCard(String appletsAppid,String GZHappid,String GZHsecret,String title,String touser,String pagepath,String mediaid){
try {
Map<String, Object> params = new HashMap<>();
params.put("touser", touser);
params.put("msgtype", "miniprogrampage");
Map<String, Object> miniprogrampageMap = new HashMap<>();
miniprogrampageMap.put("title", title);
miniprogrampageMap.put("appid", appletsAppid);
miniprogrampageMap.put("thumb_media_id", mediaid);
miniprogrampageMap.put("pagepath", pagepath);
params.put("miniprogrampage", miniprogrampageMap);
String token = WechatUtil.getToken(GZHappid, GZHsecret).getAccessToken();
logger.info("token:--------"+token);
String PUSH_APPLET_URL = AppletsConstant.PUSH_APPLET_URL + token;
JSONObject result = WX_HttpsUtil.httpsRequest(PUSH_APPLET_URL, "POST", JSON.toJSONString(params));
logger.info(JSON.toJSONString(params)+"jsonString---------------");
JSONObject resultJson = new JSONObject(result);
logger.info("发送小程序卡片结果:" + resultJson.toString());
if(resultJson.getInteger("errcode")==40003){
//获取新的token并保存到redis中 重新推消息
token = WechatUtil.getToken2(GZHappid, GZHsecret).getAccessToken();
logger.info("新token:--------"+token);
Map<String, Object> params2 = new HashMap<>();
params2.put("touser", touser);
params2.put("msgtype", "miniprogrampage");
Map<String, Object> miniprogrampageMap2 = new HashMap<>();
miniprogrampageMap2.put("title", title);
miniprogrampageMap2.put("appid", appletsAppid);
miniprogrampageMap2.put("thumb_media_id", mediaid);
miniprogrampageMap2.put("pagepath", pagepath);
params.put("miniprogrampage", miniprogrampageMap2);
String PUSH_APPLET_URL2 = AppletsConstant.PUSH_APPLET_URL + token;
JSONObject result2 = WX_HttpsUtil.httpsRequest(PUSH_APPLET_URL2, "POST", JSON.toJSONString(params2));
JSONObject resultJson2 = new JSONObject(result2);
logger.info("发送小程序卡片结果2:" + resultJson2.toString());
return resultJson2.getInteger("errcode");
}
return resultJson.getInteger("errcode");
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
2.获取素材列表工具类
public class TestMediaList {
public String getMediaId(String appid,String secret){
String token = WechatUtil.getToken(appid, secret).getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token="+token;
WXMediaListUtil media = new WXMediaListUtil();
media.setOffset(0);
media.setCount(10);
media.setType("image");
JSONObject json = (JSONObject) JSONObject.toJSON(media);
JSONObject result = WX_HttpsUtil.httpsRequest(url,"POST",json.toJSONString());
JSONArray arr = result.getJSONArray("item");
JSONObject jsonObj = arr.getJSONObject(0);
String media_id = jsonObj.get("media_id").toString();
return media_id;
}
}
3.获取token工具类
public static Token getToken(String appid, String appsecret) {
Token token = null;
String redis_token=redisUtil.getData(wechat_token_redis_name+":"+appid);
if (null==redis_token){
String requestUrl = token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
// 发起GET请求获取凭证
JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);
if (null != jsonObject) {
try {
token = new Token();
token.setAccessToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getInt("expires_in"));
//将微信token保存到redis中,失效时间与token失效时间相同
redisUtil.setData(wechat_token_redis_name+":"+appid, token.getAccessToken(), token.getExpiresIn());
} catch (JSONException e) {
token = null;
// 获取token失败
log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
}else{
token=new Token();
token.setAccessToken(redis_token);
token.setExpiresIn(7200);
}
return token;
}
代码都是亲测可用的,若有任何疑问请关注"老秦的快乐生活"公众号,获取我的联系方式