原文鏈接https://blog.csdn.net/qq_33696345/article/details/80533067
掃描二維碼後,首先會跳轉至二維碼中的redirect_uri中的url,掃描二維碼跳後臺的方法可參考小編的另一篇博客《微信二維碼掃碼獲取openid》
url的後臺首先通過code 獲取openid,通過openid獲取Access_token,通過openid和Access_token獲取subscribe
當subscribe=1時,說明已關注,當subscribe = 0時,說明未關注 ,則重定向至微信關注jian。
/** 注 測試公衆不適用此方法,因爲當用戶沒有關注時,不會跳轉到url指定後臺,而是會在頁面提示該用戶沒有關注。個人的訂閱號一般也是不行的,因爲沒有網頁授權的那個接口的權限。小編測試的時候用的是公司的公衆號。
下面來看代碼:
public String queueInfo() throws Exception{
HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
String code = request.getParameter("code");
//通過code獲取openid;
net.sf.json.JSONObject wxUser = CoreService.getOpenid(code);
String openid = wxUser.getString("openid");
//獲取subscribe
net.sf.json.JSONObject userInfo = CoreService.getUserInfo(openid);
String subscribe = userInfo.getString("subscribe");
//未關注公衆號
if(!subscribe.equals("1")){
HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
response.sendRedirect("https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz="+CoreService.BIZ+"&scene=110#wechat_redirect");
return null;
}else{
return "waitpage";
}
}
public class CoreService {
public static String BIZ="abcdefg==";
public static String GETOPENID = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
public static String GET_USERINFO = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
public static JSONObject pushMessage(String path) throws IOException{
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
URL url = new URL(path);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setRequestMethod("POST");
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 將返回的輸入流轉換成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 釋放資源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
return jsonObject;
}
/*通過code獲取用戶openid*/
public static JSONObject getOpenid(String code) throws IOException{
JSONObject jsonObject = null;
String path = GETOPENID.replace("APPID", APPID).replace("SECRET", APPSECRET).replace("CODE", code);
jsonObject = pushMessage(path);
return jsonObject;
}
/*獲取用戶信息*/
public static JSONObject getUserInfo(String openid) throws Exception{
String token = getToken();
JSONObject jsonObject = null;
String path = GET_USERINFO.replace("OPENID", openid).replace("ACCESS_TOKEN", token);
jsonObject = pushMessage(path);
return jsonObject;
}
/*獲取token*/
public static String getToken() throws Exception{
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
JSONObject jsonObject = pushMessage(url);
String token = jsonObject.getString("ACCESS_TOKEN");
return token;
}
}
其實,在queueInfo()方法中,通過code獲取的wxUser這個json對象中,有一個Access_token值,那爲什麼不在這裏獲取Access_token,而要在CoreService類中另寫一個getToken方法去獲取,這是不是多此一舉了呢,其實不然。因爲Access_token是一個憑證,它的有效期特別短,如果在queueInfo()方法中獲取再傳到CoreService類中的getUserInfo()方法中,就會報錯:Access_token 不是最新的。因此要在getUserInfo()方法中再去重新獲取一次最新的。
在沒有關注公衆號的時候,讓程序重定向至關注頁面,重定向的url中有一個值 _biz是公衆號的一個值,該值類似於身份證,是唯一的。獲取的方法百度一下教程很多,在這裏稍作介紹。
打開公衆號->點右上角的小人圖標->查看歷史消息->點右上角的三個點->複製鏈接->在除微信外的其他地方複製。得到類似如下url:https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=abcdefg==&scene=124#wechat_redirect
藍色部分即爲要的biz的值。
/**注 不適用與測試公衆號,因爲沒有查看歷史消息那個選項。測試公衆號的獲取方法小編目前也沒有get到。
---------------------
作者:隨遇灬而安
來源:CSDN
原文:https://blog.csdn.net/qq_33696345/article/details/80533067
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!