From f0753e3b608ba47deaaf4619924bb8765003fd1d Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期五, 08 十二月 2023 17:25:12 +0800
Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/PlayPai
---
cloud-server-account/src/main/java/com/dsh/account/util/weChat/WeChatUtil.java | 98 ++++++++++++++++++++++++++----------------------
1 files changed, 53 insertions(+), 45 deletions(-)
diff --git a/cloud-server-account/src/main/java/com/dsh/account/util/weChat/WeChatUtil.java b/cloud-server-account/src/main/java/com/dsh/account/util/weChat/WeChatUtil.java
index 4c7d129..85280d3 100644
--- a/cloud-server-account/src/main/java/com/dsh/account/util/weChat/WeChatUtil.java
+++ b/cloud-server-account/src/main/java/com/dsh/account/util/weChat/WeChatUtil.java
@@ -32,7 +32,7 @@
@Value("${wx.appletsAppSecret}")
private String wxAppletsAppSecret;
-// @Value("${wx.officialAccountAppid}")
+ // @Value("${wx.officialAccountAppid}")
private String officialAccountAppid;
@Value("{wx.officialAccountAppSecret}")
@@ -50,35 +50,36 @@
/**
* 小程序使用jscode获取openid
+ *
* @param jscode
* @return
*/
- public Map<String, Object> code2Session(String jscode) throws Exception{
+ public Map<String, Object> code2Session(String jscode) throws Exception {
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret
+ "&js_code=" + jscode + "&grant_type=authorization_code";
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "form");
- if(null == httpResult || httpResult.getCode() != 200){
+ if (null == httpResult || httpResult.getCode() != 200) {
return null;
}
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
int errcode = jsonObject.getIntValue("errcode");
Map<String, Object> map = new HashMap<>();
map.put("errcode", errcode);
- if(errcode == 0){//成功
+ 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){//系统繁忙,此时请开发者稍候再试
+ if (errcode == -1) {//系统繁忙,此时请开发者稍候再试
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
- if(errcode == 40029){//code 无效
+ if (errcode == 40029) {//code 无效
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
- if(errcode == 45011){//频率限制,每个用户每分钟100次
+ if (errcode == 45011) {//频率限制,每个用户每分钟100次
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
@@ -88,12 +89,13 @@
/**
* 获取微信小程序token
+ *
* @return
*/
- public String getWxAppletsAccessToken() throws Exception{
+ public String getWxAppletsAccessToken() throws Exception {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret;
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "form");
- if(httpResult.getCode() != 200){
+ if (httpResult.getCode() != 200) {
return "";
}
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
@@ -103,34 +105,35 @@
/**
* 网站应用登录
+ *
* @param code
* @return
*/
- public Map<String, String> webAccessToken(String code) throws Exception{
+ 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";
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "form");
- if(httpResult.getCode() != 200){
+ if (httpResult.getCode() != 200) {
return null;
}
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
int errcode = jsonObject.getIntValue("errcode");
Map<String, String> map = new HashMap<>();
- if(errcode == 0){//成功
+ 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){//系统繁忙,此时请开发者稍候再试
+ if (errcode == -1) {//系统繁忙,此时请开发者稍候再试
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
- if(errcode == 40029){//code 无效
+ if (errcode == 40029) {//code 无效
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
- if(errcode == 45011){//频率限制,每个用户每分钟100次
+ if (errcode == 45011) {//频率限制,每个用户每分钟100次
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
@@ -140,35 +143,36 @@
/**
* 获取微信个人信息
+ *
* @param access_token
* @param openid
* @return
*/
- public Map<String, Object> getUserInfo(String access_token, String openid) throws Exception{
+ 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;
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "form");
- if(httpResult.getCode() != 200){
+ if (httpResult.getCode() != 200) {
return null;
}
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
int errcode = jsonObject.getIntValue("errcode");
Map<String, Object> map = new HashMap<>();
- if(errcode == 0){//成功
+ 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){//系统繁忙,此时请开发者稍候再试
+ if (errcode == -1) {//系统繁忙,此时请开发者稍候再试
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
- if(errcode == 40029){//code 无效
+ if (errcode == 40029) {//code 无效
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
- if(errcode == 45011){//频率限制,每个用户每分钟100次
+ if (errcode == 45011) {//频率限制,每个用户每分钟100次
map.put("msg", jsonObject.getString("errmsg"));
return map;
}
@@ -178,18 +182,19 @@
/**
* 公众号获取openid
+ *
* @param code
* @return
*/
- public Map<String,Object> getOpenId(String code) throws Exception{
+ public Map<String, Object> getOpenId(String code) throws Exception {
if (code == null || code.length() == 0) {
return null;
}
String grantType = "authorization_code";
String params = "appid=" + officialAccountAppid + "&secret=" + officialAccountAppSecret + "&code=" + code + "&grant_type=" + grantType;
- System.out.println("sssss"+params);
+ System.out.println("sssss" + params);
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", "https://api.weixin.qq.com/sns/oauth2/access_token?" + params, null, null, "form");
- if(httpResult.getCode() != 200){
+ if (httpResult.getCode() != 200) {
return null;
}
JSONObject json = JSON.parseObject(httpResult.getData());
@@ -199,9 +204,9 @@
Integer expiresIn = json.getInteger("expires_in");
String refresh_token = json.getString("refresh_token");
String unionid = json.getString("unionid");
- Map<String,Object> map=new HashMap<>();
- map.put("openId",openId);
- map.put("accessToken",accessToken);
+ Map<String, Object> map = new HashMap<>();
+ map.put("openId", openId);
+ map.put("accessToken", accessToken);
map.put("expiresIn", expiresIn);
map.put("refreshToken", refresh_token);
map.put("unionid", unionid);
@@ -214,11 +219,11 @@
* 来源www.vxzsk.com
* @return
*/
- public String getAccessToken() throws Exception{
+ public String getAccessToken() throws Exception {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + officialAccountAppid
+ "&secret=" + officialAccountAppSecret;
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "form");
- if(httpResult.getCode() != 200){
+ if (httpResult.getCode() != 200) {
return null;
}
String accessToken = JSONObject.parseObject(httpResult.getData()).getString("access_token");
@@ -230,25 +235,26 @@
* 来源 www.vxzsk.com
* @return
*/
- public String getJSApiTicket() throws Exception{
+ public String getJSApiTicket() throws Exception {
//获取token
- String acess_token= this.getAccessToken();
+ String acess_token = this.getAccessToken();
String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + acess_token + "&type=jsapi";
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", urlStr, null, null, "form");
- if(httpResult.getCode() != 200){
+ if (httpResult.getCode() != 200) {
return null;
}
System.out.println(httpResult.getData());
String ticket = JSONObject.parseObject(httpResult.getData()).getString("ticket");
- return ticket;
+ return ticket;
}
/**
* 通过config接口注入权限验证配置(公众号)
* 附录1-JS-SDK使用权限签名算法,
+ *
* @return
*/
- public Map<String,Object> getSignatureConfig(String url) throws Exception{
+ public Map<String, Object> getSignatureConfig(String url) throws Exception {
//获取token
try {
url = URLDecoder.decode(url, "UTF-8");
@@ -260,25 +266,26 @@
Long timestamp = System.currentTimeMillis();
String content = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url;
String signature = DigestUtils.sha1Hex(content);
- Map<String,Object> map=new HashMap<>();
+ Map<String, Object> map = new HashMap<>();
map.put("appId", officialAccountAppid);
map.put("timestamp", timestamp);
map.put("nonceStr", noncestr);
map.put("signature", signature);
- return map;
+ return map;
}
/**
* 公众号获取用户个人信息
+ *
* @param access_token
* @param openid
* @return
*/
- public Map<String, Object> queryUserInfo(String access_token, String openid) throws Exception{
+ public Map<String, Object> queryUserInfo(String access_token, String openid) throws Exception {
String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "form");
- if(httpResult.getCode() != 200){
+ if (httpResult.getCode() != 200) {
return null;
}
System.err.println(httpResult.getData());
@@ -294,13 +301,14 @@
/**
* 获取小程序二维码
- * @param page 跳转页 例如 pages/index/index
- * @param scene 参数 a=1&b=2
+ *
+ * @param page 跳转页 例如 pages/index/index
+ * @param scene 参数 a=1&b=2
*/
- public InputStream getwxacodeunlimit(String page, String scene){
+ public InputStream getwxacodeunlimit(String page, String scene) {
try {
String token = getWxAppletsAccessToken();
- if(ToolUtil.isEmpty(token)){
+ if (ToolUtil.isEmpty(token)) {
System.err.println("获取接口调用凭证失败");
}
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
@@ -308,17 +316,17 @@
param.put("scene", scene);
param.put("page", page);
HttpHeaders httpHeaders = new HttpHeaders();
- MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8");
+ 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]);
+ 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){
+ } catch (Exception e) {
e.printStackTrace();
}
return null;
--
Gitblit v1.7.1