|
|
@@ -44,10 +44,25 @@ public class WxLoginService
|
|
|
@Value("${wx.small.appsecret}")
|
|
|
private String appsrcret;
|
|
|
|
|
|
+
|
|
|
private String wxAuthUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
|
|
|
private String wxAuthParam = "appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
|
|
|
|
|
|
+ @Value("${wx.gzh.appid}")
|
|
|
+ private String gzh_appid;
|
|
|
+
|
|
|
+ @Value("${wx.gzh.appsecret}")
|
|
|
+ private String gzh_appsrcret;
|
|
|
+
|
|
|
+ private String gzh_wxAuthUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
|
|
|
+
|
|
|
+ private String gzh_wxAuthParam = "appid=%s&secret=%s&code=%s&grant_type=authorization_code";
|
|
|
+
|
|
|
+ private String gzh_wxUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo";
|
|
|
+
|
|
|
+ private String gzh_wxUserInfoParam = "access_token=%s&openid=%s&lang=zh_CN";
|
|
|
+
|
|
|
@Autowired
|
|
|
private IUserService iUserService;
|
|
|
|
|
|
@@ -82,6 +97,19 @@ public class WxLoginService
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ public Map<String,String> gzh_login(WxLoginBody loginBody) {
|
|
|
+ User user = getWxGzhUnionIdUser(loginBody);
|
|
|
+ if(user==null){
|
|
|
+ throw new CustomException("登录错误");
|
|
|
+ }
|
|
|
+ ClientLoginUser loginUser = new ClientLoginUser();
|
|
|
+ loginUser.setUser(user);
|
|
|
+ Map<String,String> map = new HashMap<>();
|
|
|
+ map.put(Constants.TOKEN,wxTokenService.createToken(loginUser));
|
|
|
+ map.put("union_id",loginUser.getUser().getUnionId());
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
public String refreshToken(String unionId) {
|
|
|
User user = iUserService.queryByUnionId(unionId);
|
|
|
if(user==null){
|
|
|
@@ -136,6 +164,7 @@ public class WxLoginService
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ //小程序获取用户信息
|
|
|
public User getWxUnionIdUser(WxLoginBody loginBody) {
|
|
|
String param = String.format(wxAuthParam, appid, appsrcret, loginBody.getCode());
|
|
|
String resultString = HttpUtils.sendGet(wxAuthUrl,param);
|
|
|
@@ -155,6 +184,28 @@ public class WxLoginService
|
|
|
return user;
|
|
|
}
|
|
|
|
|
|
+ //公众号获取用户信息
|
|
|
+ public User getWxGzhUnionIdUser(WxLoginBody loginBody) {
|
|
|
+ String param = String.format(gzh_wxAuthParam, gzh_appid, gzh_appsrcret, loginBody.getCode());
|
|
|
+ String resultString = HttpUtils.sendGet(gzh_wxAuthUrl,param);
|
|
|
+ System.out.println(resultString);
|
|
|
+ //解析json
|
|
|
+ JSONObject jsonObject = (JSONObject) JSONObject.parse(resultString);
|
|
|
+ String access_token = String.valueOf(jsonObject.get("access_token"));
|
|
|
+ String openId = String.valueOf(jsonObject.get("openid"));
|
|
|
+
|
|
|
+ String userInfoParam = String.format(gzh_wxUserInfoParam, access_token, openId);
|
|
|
+ String userInfoResultString = HttpUtils.sendGet(gzh_wxUserInfoUrl,userInfoParam);
|
|
|
+ //解析json
|
|
|
+ JSONObject jsonObject1 = (JSONObject) JSONObject.parse(userInfoResultString);
|
|
|
+ String unionId = String.valueOf(jsonObject1.get("unionid"));
|
|
|
+ User user = iUserService.queryByUnionId(unionId);
|
|
|
+ if(user==null){
|
|
|
+ throw new CustomException("您尚未注册,请前往小程序注册");
|
|
|
+ }
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
public String obtainWxPhone(String iv, String encryptedData,String session_key){
|
|
|
String userInfo = null;
|
|
|
JSONObject userInfoJSON = null;
|