微信配置:
基本配置–》服务器配置。服务器地址(URL)即事件触发时被通知的接口
@Override
public String getPubAccessToken() {
String pubAccessToken;
Object obj = redisTemplate.opsForValue().get("pubAccessToken");
if (obj != null) {
pubAccessToken = (String)obj;
return pubAccessToken;
} else {
log.info("开始获取pubAccessToken");
try {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxPubPayProperties.getAppId() + "&secret=" + wxPubPayProperties.getAppSecret();
ResponseEntity<Map> entity = restTemplate.exchange(url,HttpMethod.GET, null,Map.class);
Map<String,Object> map = entity.getBody();
pubAccessToken = map.get("access_token") != null ? String.valueOf(map.get("access_token")) : null;
if (StringUtils.isNotEmpty(pubAccessToken)) {
redisTemplate.opsForValue().set("pubAccessToken", pubAccessToken, 3600, TimeUnit.SECONDS);
return pubAccessToken;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
@Override
public AppResult createQrCode(String sceneStr) {
String pubAccessToken = getPubAccessToken();
String url ="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + pubAccessToken;
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-type", "application/json; charset=utf-8");
HttpEntity<String> requestEntity = new HttpEntity<>("{\"expire_seconds\": 3600, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": " + sceneStr +" }}}", requestHeaders);
Map<String,Object> map = restTemplate.postForObject(url,requestEntity,Map.class);
// map为响应参数 其中有ticket expire_seconds url
return AppResult.successReturnDate(map);
}
@PostMapping("weChatNotice")
public String weChatNotice(@RequestBody String xmlData,String signature,Long timestamp,String nonce, String echostr) {
wxService.weChatNotice(xmlData);
return echostr;
}
@Override
public void weChatNotice(String xmlData) {
// xml转map
Map<String, String> resultMap = WxPayKit.xmlToMap(xmlData);
String ticket = resultMap.get("Ticket");
if (StringUtils.isNotEmpty(ticket)) {
String openId = resultMap.get("FromUserName");
// 将ticket 和 openId 存储进redis
redisTemplate.opsForValue().set(ticket, openId, 3600, TimeUnit.SECONDS);
}
}
@Override
public AppResult wxLogin(String ticket) {
// 判断 用户是否已扫码关注
Map<String,Object> map = new HashMap<>();
Object obj = redisTemplate.opsForValue().get(ticket);
if (obj == null) {
return AppResult.errorReturn(10024, "用户还未扫码关注");
} else {
String openId = (String) obj;
// 用户已扫码登录,删除redis
redisTemplate.delete(ticket);
// 查询openId是否已绑定手机号注册
map.put("openId",openId);
Member member = memberMapper.getMemberOpenId(openId);
if (member == null) {
map.put("state",1);
return AppResult.successReturnDate(map, "用户已扫码,但未注册");
} else {
map.put("state",0);
return AppResult.successReturnDate(map, "用户已扫码,已注册");
}
}
}
public Map<String,Object> getWxUserInfo(String openId) {
String pubAccessToken = getPubAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + pubAccessToken + "&openid=" + openId + "&lang=zh_CN";
ResponseEntity<Map> entity = restTemplate.exchange(url,HttpMethod.GET, null,Map.class);
Map<String,Object> map = entity.getBody();
return map;
}