11package com .github .binarywang .demo .wx .miniapp .controller ;
22
3- import org .apache .commons .lang3 .StringUtils ;
4- import org .slf4j .Logger ;
5- import org .slf4j .LoggerFactory ;
6- import org .springframework .web .bind .annotation .GetMapping ;
7- import org .springframework .web .bind .annotation .PathVariable ;
8- import org .springframework .web .bind .annotation .RequestMapping ;
9- import org .springframework .web .bind .annotation .RestController ;
10-
113import cn .binarywang .wx .miniapp .api .WxMaService ;
124import cn .binarywang .wx .miniapp .bean .WxMaJscode2SessionResult ;
135import cn .binarywang .wx .miniapp .bean .WxMaPhoneNumberInfo ;
146import cn .binarywang .wx .miniapp .bean .WxMaUserInfo ;
15- import com . github . binarywang .demo . wx .miniapp .config . WxMaConfiguration ;
7+ import cn . binarywang .wx .miniapp .util . WxMaConfigHolder ;
168import com .github .binarywang .demo .wx .miniapp .utils .JsonUtils ;
9+ import lombok .AllArgsConstructor ;
10+ import lombok .extern .slf4j .Slf4j ;
1711import me .chanjar .weixin .common .error .WxErrorException ;
12+ import org .apache .commons .lang3 .StringUtils ;
13+ import org .springframework .web .bind .annotation .GetMapping ;
14+ import org .springframework .web .bind .annotation .PathVariable ;
15+ import org .springframework .web .bind .annotation .RequestMapping ;
16+ import org .springframework .web .bind .annotation .RestController ;
1817
1918/**
2019 * 微信小程序用户接口
2120 *
2221 * @author <a href="https://github.com/binarywang">Binary Wang</a>
2322 */
2423@ RestController
24+ @ AllArgsConstructor
25+ @ Slf4j
2526@ RequestMapping ("/wx/user/{appid}" )
2627public class WxMaUserController {
27- private final Logger logger = LoggerFactory . getLogger ( this . getClass ()) ;
28+ private final WxMaService wxMaService ;
2829
2930 /**
3031 * 登陆接口
@@ -35,17 +36,21 @@ public String login(@PathVariable String appid, String code) {
3536 return "empty jscode" ;
3637 }
3738
38- final WxMaService wxService = WxMaConfiguration .getMaService (appid );
39+ if (!wxMaService .switchover (appid )) {
40+ throw new IllegalArgumentException (String .format ("未找到对应appid=[%s]的配置,请核实!" , appid ));
41+ }
3942
4043 try {
41- WxMaJscode2SessionResult session = wxService .getUserService ().getSessionInfo (code );
42- this . logger .info (session .getSessionKey ());
43- this . logger .info (session .getOpenid ());
44+ WxMaJscode2SessionResult session = wxMaService .getUserService ().getSessionInfo (code );
45+ log .info (session .getSessionKey ());
46+ log .info (session .getOpenid ());
4447 //TODO 可以增加自己的逻辑,关联业务相关数据
4548 return JsonUtils .toJson (session );
4649 } catch (WxErrorException e ) {
47- this . logger .error (e .getMessage (), e );
50+ log .error (e .getMessage (), e );
4851 return e .toString ();
52+ } finally {
53+ WxMaConfigHolder .remove ();//清理ThreadLocal
4954 }
5055 }
5156
@@ -57,16 +62,19 @@ public String login(@PathVariable String appid, String code) {
5762 @ GetMapping ("/info" )
5863 public String info (@ PathVariable String appid , String sessionKey ,
5964 String signature , String rawData , String encryptedData , String iv ) {
60- final WxMaService wxService = WxMaConfiguration .getMaService (appid );
65+ if (!wxMaService .switchover (appid )) {
66+ throw new IllegalArgumentException (String .format ("未找到对应appid=[%s]的配置,请核实!" , appid ));
67+ }
6168
6269 // 用户信息校验
63- if (!wxService .getUserService ().checkUserInfo (sessionKey , rawData , signature )) {
70+ if (!wxMaService .getUserService ().checkUserInfo (sessionKey , rawData , signature )) {
71+ WxMaConfigHolder .remove ();//清理ThreadLocal
6472 return "user check failed" ;
6573 }
6674
6775 // 解密用户信息
68- WxMaUserInfo userInfo = wxService .getUserService ().getUserInfo (sessionKey , encryptedData , iv );
69-
76+ WxMaUserInfo userInfo = wxMaService .getUserService ().getUserInfo (sessionKey , encryptedData , iv );
77+ WxMaConfigHolder . remove (); //清理ThreadLocal
7078 return JsonUtils .toJson (userInfo );
7179 }
7280
@@ -78,16 +86,19 @@ public String info(@PathVariable String appid, String sessionKey,
7886 @ GetMapping ("/phone" )
7987 public String phone (@ PathVariable String appid , String sessionKey , String signature ,
8088 String rawData , String encryptedData , String iv ) {
81- final WxMaService wxService = WxMaConfiguration .getMaService (appid );
89+ if (!wxMaService .switchover (appid )) {
90+ throw new IllegalArgumentException (String .format ("未找到对应appid=[%s]的配置,请核实!" , appid ));
91+ }
8292
8393 // 用户信息校验
84- if (!wxService .getUserService ().checkUserInfo (sessionKey , rawData , signature )) {
94+ if (!wxMaService .getUserService ().checkUserInfo (sessionKey , rawData , signature )) {
95+ WxMaConfigHolder .remove ();//清理ThreadLocal
8596 return "user check failed" ;
8697 }
8798
8899 // 解密
89- WxMaPhoneNumberInfo phoneNoInfo = wxService .getUserService ().getPhoneNoInfo (sessionKey , encryptedData , iv );
90-
100+ WxMaPhoneNumberInfo phoneNoInfo = wxMaService .getUserService ().getPhoneNoInfo (sessionKey , encryptedData , iv );
101+ WxMaConfigHolder . remove (); //清理ThreadLocal
91102 return JsonUtils .toJson (phoneNoInfo );
92103 }
93104
0 commit comments