java 判断用户是否关注了公众号
生活随笔
收集整理的這篇文章主要介紹了
java 判断用户是否关注了公众号
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.獲取token
public String getToken(){try {HttpClient client = HttpClients.createDefault();String tokenUrl = MessageFormat.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", APP_ID, APP_SECRET);HttpGet request = new HttpGet(tokenUrl);HttpResponse response = client.execute(request);JSONObject object = getResponseJson(response);if (object == null) {return null;}return ObjectUtils.isEmpty(object) ? null : object.getString("access_token");}catch (Exception e){ log.info(e.getMessage()); }return null;}2.獲取用戶openId
// code前端傳;type:1.公眾號 2.小程序public UserOpenIdDto getUserOpenId(String code,Integer type ) {String resultStr;UserOpenIdDto userOpenIdDto = new UserOpenIdDto();try {if(OpenIdTypeEnum.TYPE_PUBLIC.getCode().equals(type)){//公眾號發送請求resultStr = wxUtils.executeHttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", publicappid, publicSecret, code);}else {//小程序發送請求resultStr = wxUtils.executeHttpGet("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code", uappid, uSecret, code);}//解析響應JSONObject jsonObject = JSONObject.parseObject(resultStr);if (ObjectUtils.isEmpty(jsonObject)){throw new CommonException(ErrorCodeEnum.LOGIN_GET_THIRD_PARTY_USER_INFO_FAIL);}userOpenIdDto.setOpenId(jsonObject.getString("openid"));userOpenIdDto.setUnionId(jsonObject.getString("unionid"));} catch (IOException e) {throw new CommonException(ErrorCodeEnum.LOGIN_GET_THIRD_PARTY_USER_INFO_FAIL);}return userOpenIdDto;}2-1 執行get url
public String executeHttpGet(String url,String appId,String secret,String code) throws IOException {String formatUrl = MessageFormat.format(url,appId,secret,code);HttpClient client = HttpClients.createDefault();HttpGet request = new HttpGet(formatUrl);HttpResponse response = client.execute(request);return readResponse(response);}3.查詢用戶是否關注了公眾號
/*** 查看用戶是否關注了公眾號* @param token token* @param openid 微信用戶openId* @return true 關注了;false 沒關注*/public boolean userIsFollowPublic(String token,String openid){Integer subscribe = 0;String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN";url = MessageFormat.format(url,token,openid);try {URL urlGet = new URL(url);HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();http.setRequestMethod("GET"); // 必須是get方式請求http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");http.setDoOutput(true);http.setDoInput(true);http.connect();InputStream is = http.getInputStream();int size = is.available();byte[] jsonBytes = new byte[size];is.read(jsonBytes);String message = new String(jsonBytes, "UTF-8");JSONObject demoJson = JSONObject.parseObject(message);subscribe = demoJson.getIntValue("subscribe"); // 此字段為關注字段 關注為1 未關注為0is.close();} catch (Exception e) {e.printStackTrace();}return 1 == subscribe;}==========分隔符==========
json工具類
package com.xxx.global.utils;import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;/*** @author maomo* @date 2021-12-16* @since 1.0.0**/ public class JsonUtils {/*** response轉String* @param response 響應* @return string* @throws IOException 報錯*/public static String getResponseStr(HttpResponse response) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuilder result = new StringBuilder();String line;while ((line = in.readLine()) != null) {result.append(line);}return result.toString();}/*** response轉json* @param response 響應* @return json* @throws IOException 報錯*/public static JSONObject getResponseJson(HttpResponse response) throws IOException {JSONObject json = null;HttpEntity entity = response.getEntity();if(entity!=null){String result = EntityUtils.toString(entity,"UTF-8");json = JSONObject.parseObject(result);}if(ObjectUtils.isEmpty(json)){return null;}return json;} }總結
以上是生活随笔為你收集整理的java 判断用户是否关注了公众号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言小案例_小程序学习(三)
- 下一篇: 刚晋升为部门经理,我要如何领导下属影响上