1. 程式人生 > >微信公眾號開發,獲取token,獲取公眾號粉絲,獲取Unionid,傳送客戶訊息

微信公眾號開發,獲取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<String> 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<String> openids = (List<String>) 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<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
        String result = restTemplate.postForObject(url, formEntity, String.class);
        if (StringUtils.isNotBlank(result)) {
            return result;
        }
        return null;
    }