//controller
public Result 授权方法(String code, String encryptedData, String iv) throws Exception {
HashMap result = service.微信授权方法(code, encryptedData, iv, stateCode.getConsumerCode());
UserDTO userDTO = (UserDTO) result.get("userInfo");
String token = (String) result.get("token");
if (userDTO == null || token == null) {
return Result.success(null);
}
UserVO userVO = BeanUtil.copyProperties(userDTO, UserVO.class);
userVO.setUserId(token);
result.put("userInfo", userVO);
return Result.success(result);
}
}
public HashMap 微信授权方法(String code, String encryptedData, String iv, String consumerCode) throws Exception {
HashMap hashMap = new HashMap();
HashMap params = new HashMap();
//调用微信接口必要参数--appid,secret,js_code,grant_type
params.put("appid", wxProperties.getAppid());
params.put("secret", wxProperties.getSecret());
params.put("js_code", code);
params.put("grant_type", wxProperties.getAuthCode());
//调用微信接口获取用户的openid和sessionKey
String jsonResult = HttpUtil.sendPost(wxProperties.getUrl(), params);
SecretInfo secretInfo = JacksonUtil.parse(jsonResult, SecretInfo.class);
//再通过sessionKey解密用户基本信息
UserInfo userInfo = this.userService.getUserInfo(encryptedData, iv, secretInfo.getSessionKey());
//微信用户信息是否已存在判断省略
//hashmap包含用户信息对象和token
return hashMap;
}