二维码的生成

  • 在com.webchat.util目录的SetTemplate类里增加获取二维码的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//获取二维码的信息
@Test
public void testGetQrCodeTicket(){
String ticket = getQrCodeTicket();
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
try {
URL urlObj = new URL(url);
//开连接
URLConnection conn = urlObj.openConnection();
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() +
" ,Value : " + entry.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}

//获取带参数二维码的ticket
public static String getQrCodeTicket(){
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+WxService.getOutToken();
String data = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}";
String result = GetAndPost.post(url,data);
JSONObject jsonObject = JSONObject.fromObject(result);
String ticket = jsonObject.getString("ticket");
return ticket;
}
  • 在web目录下创建index.jsp,并输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%--
Created by IntelliJ IDEA.
User: raowe
Date: 2019/12/9
Time: 10:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("button").click(function () {
$.get("GetTicket",function (ticket) {
var src = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
$("img").attr("src",src);
})
})
});

</script>
<body>
<button>生成二维码</button><br>
<img src="" alt="">
</body>
</html>
  • 在com.webchat.servlet目录下创建QrCodeServlet.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.webchat.servlet;
import com.webchat.util.SetTemplate;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/GetTicket")
public class QrCodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String ticket = SetTemplate.getQrCodeTicket();
out.print(ticket);
out.flush();
out.close();
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doGet(request,response);
}
}

扫描二维码,如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者(xml数据包)。

获取已关注用户的基本信息

  • 根在com.webchat.util目录的SetTemplate类里增加获取已关注用户信息的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 //执行获取用户的信息
@Test
public void testGetUesrInfo(){
String openid = "oN04-06h0y9BUvfC87XYPvuDzlKY";
String result = getUesrInfo(openid);
System.out.println(result);
}

//获取用户的基本信息
public static String getUesrInfo(String openid){
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
url = url.replace("ACCESS_TOKEN",WxService.getOutToken()).replace("OPENID",openid);
String result = GetAndPost.get(url);
return result;
}

网页授权

  • 在com.webchat.util的wxService里的dealTextMessage方法添加文字判断
1
2
3
4
5
if(msg.equals("登录")){
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe6774713a448fb6f&redirect_uri=http://www.weixin.itrwj.com/getUserInfo&response_type=code&scope=snsapi_userinfo#wechat_redirect";
TextMessage tm = new TextMessage(requestMap,"点击<a href=\""+url+"\">这里</a>登录");
return tm;
}
  • 在com.webchat.util下,创建GetUserInfoServlet类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.webchat.util;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/getUserInfo")
public class GetUserInfoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取code
String code = request.getParameter("code");
//通过code获取access_token
//获取access_token的地址
String url= "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
url = url.replace("APPID","wxe6774713a448fb6f").replace("SECRET","1fec63da4f2a53c3224e6adf6dd4876e").replace("CODE",code);
String result = GetAndPost.get(url);
String access_token = JSONObject.fromObject(result).getString("access_token");
String openid = JSONObject.fromObject(result).getString("openid");
//拉取用户的基本信息
url= "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
url = url.replace("ACCESS_TOKEN",access_token).replace("OPENID",openid);
result = GetAndPost.get(url);
System.out.println(result);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}