张延森 3 years ago
parent
commit
ffc741e46a
1 changed files with 15 additions and 0 deletions
  1. 15
    0
      src/main/java/com/njyunzhi/h5/controller/WxMpController.java

+ 15
- 0
src/main/java/com/njyunzhi/h5/controller/WxMpController.java View File

@@ -4,6 +4,8 @@ import com.njyunzhi.h5.common.ResponseBean;
4 4
 import com.njyunzhi.h5.common.WxUtils;
5 5
 import lombok.extern.slf4j.Slf4j;
6 6
 import me.chanjar.weixin.common.bean.WxJsapiSignature;
7
+import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
8
+import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
7 9
 import org.springframework.beans.factory.annotation.Autowired;
8 10
 import org.springframework.web.bind.annotation.*;
9 11
 
@@ -56,4 +58,17 @@ public class WxMpController {
56 58
         return ResponseBean.success(jsapiSignature);
57 59
     }
58 60
 
61
+
62
+    @GetMapping({"/openid"})
63
+    public ResponseBean openid(@RequestParam String code) throws Exception {
64
+        WxOAuth2AccessToken accessToken = this.wxUtils.getMpService().getOAuth2Service().getAccessToken(code);
65
+        return ResponseBean.success(accessToken.getOpenId());
66
+    }
67
+
68
+    @GetMapping({"/userinfo"})
69
+    public ResponseBean userinfo(@RequestParam String code) throws Exception {
70
+        WxOAuth2AccessToken accessToken = this.wxUtils.getMpService().getOAuth2Service().getAccessToken(code);
71
+        WxOAuth2UserInfo wxMpUser = this.wxUtils.getMpService().getOAuth2Service().getUserInfo(accessToken, (String)null);
72
+        return ResponseBean.success(wxMpUser);
73
+    }
59 74
 }