package com.ruoyi.account.util.weChat;
|
|
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpResponse;
|
import cn.hutool.http.HttpUtil;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 微信工具类
|
*/
|
@Slf4j
|
@Component
|
public class WeChatUtil {
|
|
@Value("${wx.appletsAppid}")
|
private String wxAppletsAppid;
|
|
@Value("${wx.appletsAppSecret}")
|
private String wxAppletsAppSecret;
|
|
@Value("{wx.officialAccountAppSecret}")
|
private String officialAccountAppSecret;
|
|
@Value("${wx.appid}")
|
private String webAppId;
|
|
@Value("${wx.appSecret}")
|
private String webAppSecret;
|
|
|
/**
|
* 小程序使用jscode获取openid
|
* @param jscode
|
* @return
|
*/
|
public Map<String, Object> code2Session(String jscode) {
|
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret
|
+ "&js_code=" + jscode + "&grant_type=authorization_code";
|
HttpRequest get = HttpUtil.createGet(url);
|
HttpResponse response = get.execute();
|
int status = response.getStatus();
|
if(200 != status){
|
throw new RuntimeException(response.body());
|
}
|
JSONObject jsonObject = JSON.parseObject(response.body());
|
int errcode = jsonObject.getIntValue("errcode");
|
Map<String, Object> map = new HashMap<>();
|
map.put("errcode", errcode);
|
if(errcode == 0){//成功
|
map.put("openid", jsonObject.getString("openid"));
|
map.put("sessionKey", jsonObject.getString("session_key"));
|
map.put("unionid", jsonObject.getString("unionid"));
|
return map;
|
}
|
if(errcode == -1){//系统繁忙,此时请开发者稍候再试
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 40029){//code 无效
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 45011){//频率限制,每个用户每分钟100次
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
return null;
|
}
|
|
|
/**
|
* 获取微信小程序token
|
* @return
|
*/
|
public String getWxAppletsAccessToken() throws Exception{
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret;
|
HttpRequest get = HttpUtil.createGet(url);
|
HttpResponse response = get.execute();
|
if(response.getStatus() != 200){
|
return "";
|
}
|
JSONObject jsonObject = JSON.parseObject(response.body());
|
return jsonObject.getString("access_token");
|
}
|
|
|
/**
|
* 网站应用登录
|
* @param code
|
* @return
|
*/
|
public Map<String, String> webAccessToken(String code) throws Exception{
|
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + webAppId + "&secret=" + webAppSecret + "&code=" + code + "&grant_type=authorization_code";
|
HttpRequest get = HttpUtil.createGet(url);
|
HttpResponse response = get.execute();
|
if(response.getStatus() != 200){
|
return null;
|
}
|
JSONObject jsonObject = JSON.parseObject(response.body());
|
int errcode = jsonObject.getIntValue("errcode");
|
Map<String, String> map = new HashMap<>();
|
if(errcode == 0){//成功
|
map.put("access_token", jsonObject.getString("access_token"));
|
map.put("openid", jsonObject.getString("openid"));
|
map.put("refresh_token", jsonObject.getString("refresh_token"));
|
map.put("unionid", jsonObject.getString("unionid"));
|
return map;
|
}
|
if(errcode == -1){//系统繁忙,此时请开发者稍候再试
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 40029){//code 无效
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 45011){//频率限制,每个用户每分钟100次
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
return map;
|
}
|
|
|
/**
|
* 获取微信个人信息
|
* @param access_token
|
* @param openid
|
* @return
|
*/
|
public Map<String, Object> getUserInfo(String access_token, String openid) throws Exception{
|
String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid;
|
HttpRequest get = HttpUtil.createGet(url);
|
HttpResponse response = get.execute();
|
if(response.getStatus() != 200){
|
return null;
|
}
|
JSONObject jsonObject = JSON.parseObject(response.body());
|
int errcode = jsonObject.getIntValue("errcode");
|
Map<String, Object> map = new HashMap<>();
|
if(errcode == 0){//成功
|
map.put("nickname", jsonObject.getString("nickname"));
|
map.put("openid", jsonObject.getString("openid"));
|
map.put("sex", jsonObject.getString("sex"));
|
map.put("headimgurl", jsonObject.getString("headimgurl"));
|
return map;
|
}
|
if(errcode == -1){//系统繁忙,此时请开发者稍候再试
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 40029){//code 无效
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 45011){//频率限制,每个用户每分钟100次
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
return map;
|
}
|
|
|
|
|
// /**
|
// * 获取小程序二维码
|
// * @param page 跳转页 例如 pages/index/index
|
// * @param scene 参数 a=1&b=2
|
// */
|
// public InputStream getwxacodeunlimit(String page, String scene){
|
// try {
|
// String token = getWxAppletsAccessToken();
|
// if(StringUtils.isEmpty(token)){
|
// System.err.println("获取接口调用凭证失败");
|
// }
|
// String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
|
// Map<String, Object> param = new HashMap<>();
|
// param.put("scene", scene);
|
// param.put("page", page);
|
// HttpHeaders httpHeaders = new HttpHeaders();
|
// MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
|
// httpHeaders.setContentType(type);
|
// HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(param, httpHeaders);
|
// ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, new Object[0]);
|
// String body1 = exchange.getBody();
|
//// System.err.println(body1);
|
// ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
|
// byte[] body = entity.getBody();
|
//// System.err.println(Base64.encodeBase64String(body));
|
// return new ByteArrayInputStream(body);
|
// }catch (Exception e){
|
// e.printStackTrace();
|
// }
|
// return null;
|
// }
|
}
|