JAva微信公衆番号の参照QRコード
6736 ワード
会社は業務の需要のため、すべての人が自分の専用のQRコードを持っている必要があります.このQRコードをスキャンすると、公衆番号に直接注目することができ、一人一人のQRコードをスキャンする回数を統計します.
まず最初のステップに着手しましょう.まずappidとsecretを取得し、次にこの2つのパラメータでaccessを取得します.tokenは、tokenが2時間に1回リフレッシュするので、定期的に取得してデータベースに格納し、使用するときにクエリーの比較を取り出します.おしゃべりは言わないで、コードをつけます.
一、get/post要求を含むutilツールクラスの作成
三、直接controller層で、サービス層を作成していません(ここでコードはまだ最適化されていません.access_tokenをデータベースに置いています.だから、私は取るたびに時間を比較する必要があります.タイマーを書くことができます)
まず最初のステップに着手しましょう.まずappidとsecretを取得し、次にこの2つのパラメータでaccessを取得します.tokenは、tokenが2時間に1回リフレッシュするので、定期的に取得してデータベースに格納し、使用するときにクエリーの比較を取り出します.おしゃべりは言わないで、コードをつけます.
一、get/post要求を含むutilツールクラスの作成
//post
public static String sendPost(String param, String url) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// URL
URLConnection conn = realUrl.openConnection();
//
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// POST
conn.setDoOutput(true);
conn.setDoInput(true);
// URLConnection
// out = new PrintWriter(conn.getOutputStream());
out = new PrintWriter(new OutputStreamWriter(
conn.getOutputStream(), "utf-8"));
//
out.print(param);
// flush
out.flush();
// BufferedReader URL
in = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(" POST !" + e);
e.printStackTrace();
}
// finally 、
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
/**
* URL GET
*
*/
public static String get(String url) {
BufferedReader in = null;
try {
URL realUrl = new URL(url);
// URL
URLConnection connection = realUrl.openConnection();
//
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//
connection.connect();
// BufferedReader URL
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
}
// finally
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
二、QRコードを作成する方法をもう一つ書きます(ここではscene_idをカスタマイズして識別します) /* */
public static String getTemporaryQR(String access_token,String scene_id){
//String access_token="7_JekEGh-c4Op13Z-mDkFs07C6dquttZu4maa6ntxz6kYS0mD5x6rNyv-brIVJ1a5xCjrnkPNs26H1I49inCcbeMwn-KvH1mouOJT_G_dURAgozHRLwOP9xSVCtKoM30lB1zbFlKFk9tKjnqilJWMaADAZMC";
// ( )
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+access_token+"";
//
String jsonStr = "{\"expire_seconds\": 2592000,\"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\" \": "+scene_id+"}}}";
// json
String response = sendPost(jsonStr, url);
return response.toString();
}
三、直接controller層で、サービス層を作成していません(ここでコードはまだ最適化されていません.access_tokenをデータベースに置いています.だから、私は取るたびに時間を比較する必要があります.タイマーを書くことができます)
@RequestMapping("/getImg")
@ResponseBody
public void getImg(String account,HttpServletRequest request,HttpServletResponse resp) throws Exception{
Wechattoken byId = imgService.getById(1);
String access_token = null;
if (byId==null) {
String string = wechatImg.get(wechatImg.GET_TOKEN_URL+wechatImg.grant_type+"&"+wechatImg.APP_ID+"&"+wechatImg.SECRET);
System.out.println(string);
JSONObject fromObject = JSONObject.fromObject(string);
access_token=fromObject.get("access_token").toString();
Wechattoken tok = new Wechattoken();
tok.setAccessToken(access_token);
tok.setTime(DateTool.getTime());
imgService.insert(tok);
}else{
String time1 = byId.getTime();
String time2=DateTool.getTime();
long minus = DateTool.getMinus(time1, time2);
if (minus>115) {
String string = wechatImg.get(wechatImg.GET_TOKEN_URL+wechatImg.grant_type+wechatImg.APP_ID+wechatImg.SECRET);
JSONObject fromObject = JSONObject.fromObject(string);
access_token=fromObject.get("access_token").toString();
byId.setAccessToken(access_token);
byId.setTime(DateTool.getTime());
imgService.update(byId);
}else{
access_token=byId.getAccessToken();
}
}
String scene_id=account;
String temporaryQR = wechatImg.getTemporaryQR(access_token,scene_id);
System.out.println(temporaryQR);
JSONObject fromObject = JSONObject.fromObject(temporaryQR);
String ticket = fromObject.get("ticket").toString();
System.out.println(ticket);
//wechatImg.downloadFile(wechatImg.DOWNLOADIMG+ticket, "F://image//"+account+"//"+account+".jpg");
//DownLoadUtii.downloadAmachment(wechatImg.DOWNLOADIMG+ticket, account+".jpg", request, resp);
json.put("account", account);
json.put("ticket", ticket);
json.put("errorMessage", errorMessage);
json.put("resultCode", resultCode);
ResponseTool.write(resp, json);
}