Skip to content

Commit d9f2eff

Browse files
ettingshausenbinarywang
authored andcommitted
docker support & 添加获取用户信息菜单 (binarywang#19)
* fix issue binarywang#17 * add configuration metadata * add docker support * 添加获取用户信息菜单 * 添加获取用户信息菜单
1 parent 3162735 commit d9f2eff

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
2626
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2727
<project.build.locales>zh_CN</project.build.locales>
28+
<docker.image.prefix>wechat-mp-demo</docker.image.prefix>
2829
</properties>
2930

3031
<dependencies>
@@ -81,6 +82,23 @@
8182
<groupId>org.springframework.boot</groupId>
8283
<artifactId>spring-boot-maven-plugin</artifactId>
8384
</plugin>
85+
86+
<plugin>
87+
<groupId>com.spotify</groupId>
88+
<artifactId>docker-maven-plugin</artifactId>
89+
<version>1.0.0</version>
90+
<configuration>
91+
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
92+
<dockerDirectory>src/main/docker</dockerDirectory>
93+
<resources>
94+
<resource>
95+
<targetPath>/</targetPath>
96+
<directory>${project.build.directory}</directory>
97+
<include>${project.build.finalName}.jar</include>
98+
</resource>
99+
</resources>
100+
</configuration>
101+
</plugin>
84102
</plugins>
85103
</build>
86104

src/main/docker/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM openjdk:8-jdk-alpine
2+
VOLUME /tmp
3+
ADD weixin-java-mp-demo-springboot-1.0.0-SNAPSHOT.jar app.jar
4+
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

src/main/java/com/github/binarywang/demo/wx/mp/controller/WxMenuController.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package com.github.binarywang.demo.wx.mp.controller;
22

33
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
4+
import me.chanjar.weixin.common.api.WxConsts;
45
import me.chanjar.weixin.common.bean.menu.WxMenu;
56
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
67
import me.chanjar.weixin.common.error.WxErrorException;
78
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
89
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
910
import org.springframework.web.bind.annotation.*;
11+
import org.springframework.web.context.request.RequestContextHolder;
12+
import org.springframework.web.context.request.ServletRequestAttributes;
13+
14+
import javax.servlet.http.HttpServletRequest;
15+
16+
import java.net.MalformedURLException;
17+
import java.net.URL;
1018

1119
import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
1220

@@ -33,7 +41,7 @@ public String menuCreate(@PathVariable String appid, @RequestBody WxMenu menu) t
3341
}
3442

3543
@GetMapping("/create")
36-
public String menuCreateSample(@PathVariable String appid) throws WxErrorException {
44+
public String menuCreateSample(@PathVariable String appid) throws WxErrorException, MalformedURLException {
3745
WxMenu menu = new WxMenu();
3846
WxMenuButton button1 = new WxMenuButton();
3947
button1.setType(MenuButtonType.CLICK);
@@ -69,9 +77,26 @@ public String menuCreateSample(@PathVariable String appid) throws WxErrorExcepti
6977
button33.setName("赞一下我们");
7078
button33.setKey("V1001_GOOD");
7179

80+
WxMenuButton button34 = new WxMenuButton();
81+
button34.setType(MenuButtonType.VIEW);
82+
button34.setName("获取用户信息");
83+
84+
ServletRequestAttributes servletRequestAttributes =
85+
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
86+
if (servletRequestAttributes != null) {
87+
HttpServletRequest request = servletRequestAttributes.getRequest();
88+
URL requestURL = new URL(request.getRequestURL().toString());
89+
String url = WxMpConfiguration.getMpServices().get(appid)
90+
.oauth2buildAuthorizationUrl(
91+
String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appid),
92+
WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
93+
button34.setUrl(url);
94+
}
95+
7296
button3.getSubButtons().add(button31);
7397
button3.getSubButtons().add(button32);
7498
button3.getSubButtons().add(button33);
99+
button3.getSubButtons().add(button34);
75100

76101
return WxMpConfiguration.getMpServices().get(appid).getMenuService().menuCreate(menu);
77102
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.binarywang.demo.wx.mp.controller;
2+
3+
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
import me.chanjar.weixin.mp.api.WxMpService;
6+
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
7+
import me.chanjar.weixin.mp.bean.result.WxMpUser;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.ModelMap;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
14+
/**
15+
* @author Edward
16+
*/
17+
@Controller
18+
@RequestMapping("/wx/redirect/{appid}")
19+
public class WxRedirectController {
20+
21+
22+
@RequestMapping("/greet")
23+
public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {
24+
25+
WxMpService mpService = WxMpConfiguration.getMpServices().get(appid);
26+
27+
try {
28+
WxMpOAuth2AccessToken accessToken = mpService.oauth2getAccessToken(code);
29+
WxMpUser user = mpService.oauth2getUserInfo(accessToken, null);
30+
map.put("user", user);
31+
} catch (WxErrorException e) {
32+
e.printStackTrace();
33+
}
34+
35+
return "greet_user";
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Hello</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
7+
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap-grid.min.css" rel="stylesheet">
8+
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap-reboot.min.css" rel="stylesheet">
9+
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
10+
<script src="https://cdn.bootcss.com/bootstrap/4.1.1/js/bootstrap.bundle.min.js"></script>
11+
<script src="https://cdn.bootcss.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
12+
</head>
13+
<body>
14+
Hello, <span th:text="${user.nickname}"></span>!
15+
<br>
16+
<br>
17+
<br>
18+
<div class="card" style="width: 10rem;">
19+
<img class="card-img-top" th:src="${user.headImgUrl}" alt="Card image cap">
20+
<div class="card-body">
21+
<h5 class="card-title" th:text="${user.nickname}"></h5>
22+
<p class="card-text"> 性别: <span th:text="${user.sexDesc}"></span></p>
23+
<p class="card-text">城市: <span th:text="${user.city}"></span></p>
24+
</div>
25+
</div>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)