微信公众号开发,获取token,获取公众号粉丝,获取Unionid,发送客户消息

public class WeChatUtil {

    /**
     * 获取token
     */
    private static String ACCESSTOKENURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appId}&secret={appSecret}";

    /**
     * 获取所有公众号的粉丝
     */
    private static String OPENIDLIST = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={token}&next_openid=";

    /**
     * 从页面微信授权 过来获取的Unionid
     */
    private static String WEBSITINFO = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={appId}&secret={appSecret}&code={code}&grant_type=authorization_code";

    /**
     * 发送客服消息
     */
    private static String SENDMSG = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";

    /**
     * 获取登陆的token
     *
     * @param appId
     * @param appSecret
     * @return
     * @throws Exception
     */
    public static String getAccessToken(String appId, String appSecret) {
        String url = ACCESSTOKENURL.replace("{appId}", appId).replace("{appSecret}", appSecret);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        if (StringUtils.isNotBlank(result)) {
            return JSONObject.parseObject(result).getString("access_token");
        }
        return null;
    }

    /**
     * 获取公众号所有粉丝openID
     *
     * @param token
     * @return
     */
    public static List getOpenidList(String token) {
        String url = OPENIDLIST.replace("{token}", token);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("data");
            List openids = (List) jsonObject.get("openid");
            return openids;
        }
        return null;
    }

    /**
     * 根据openid 获取Unionid
     *
     * @param token
     * @param openid
     * @return
     */
    public static String getUnionid(String token, String openid) {
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + token + "&openid=" + openid + "&lang=zh_CN";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        if (StringUtils.isNotBlank(result)) {
            return JSONObject.parseObject(result).getString("unionid");
        }
        return null;
    }

    /**
     * 从页面微信授权 过来获取的Unionid
     *
     * @param appId
     * @param appSecret
     * @param code
     * @return
     */
    public static String getPageUnionid(String appId, String appSecret, String code) {
        String url = WEBSITINFO.replace("{appId}", appId).replace("{appSecret}", appSecret).replace("{code}", code);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        if (StringUtils.isNotBlank(result)) {
            return JSONObject.parseObject(result).getString("unionid");
        }
        return null;
    }

    public static String sendMsg(String content, String touser, String token) {
        String url = SENDMSG + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        StringBuilder sb = new StringBuilder();
        sb.append("{").append("'touser'").append(":").append("'").append(touser).append("'").append(",").append("'msgtype'").append(":").append("'text'").append(",").append("'text'").append(":");
        sb.append("{").append("'content'").append(":").append("'").append(content).append("'").append("}").append("}");
        JSONObject jsonObj = JSONObject.parseObject(sb.toString());
        HttpEntity formEntity = new HttpEntity(jsonObj.toString(), headers);
        String result = restTemplate.postForObject(url, formEntity, String.class);
        if (StringUtils.isNotBlank(result)) {
            return result;
        }
        return null;
    }

你可能感兴趣的:(微信公众号开发,获取token,获取公众号粉丝,获取Unionid,发送客户消息)