|
|
@@ -1,13 +1,11 @@
|
|
|
package com.kym.common.utils.wx;
|
|
|
|
|
|
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.kym.common.exception.BusinessException;
|
|
|
import com.kym.common.utils.CommUtil;
|
|
|
import com.kym.common.utils.EncryptUtil;
|
|
|
import com.kym.common.utils.HttpUtil;
|
|
|
-import org.apache.http.Consts;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -23,8 +21,9 @@ import java.util.UUID;
|
|
|
* @author yaopeng
|
|
|
*/
|
|
|
public class WxPbUtil {
|
|
|
-
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(WxPbUtil.class);
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(WxPbUtil.class);
|
|
|
+ private static WxAccess config = null;
|
|
|
|
|
|
/**
|
|
|
* 工具类禁用公有构造方法
|
|
|
@@ -32,8 +31,6 @@ public class WxPbUtil {
|
|
|
private WxPbUtil() {
|
|
|
}
|
|
|
|
|
|
- private static WxAccess config = null;
|
|
|
-
|
|
|
/**
|
|
|
* 发送客服消息
|
|
|
*
|
|
|
@@ -50,10 +47,10 @@ public class WxPbUtil {
|
|
|
param.put("msgtype", "text");
|
|
|
Map<String, String> text = new HashMap<>(2);
|
|
|
if (CommUtil.isNotEmptyAndNull(miniPath)) {
|
|
|
- String xcxAppId =WxPbConfig.PUBLIC_APP_ID;
|
|
|
+ String xcxAppId = WxPbConfig.PUBLIC_APP_ID;
|
|
|
if (CommUtil.isNotEmptyAndNull(xcxAppId)) {
|
|
|
// 哈哈哈{appid},跳转
|
|
|
- content =content.replaceFirst("\\{}",xcxAppId);
|
|
|
+ content = content.replaceFirst("\\{}", xcxAppId);
|
|
|
// StringUtils.replaceOnce(content, "{}", xcxAppId);
|
|
|
}
|
|
|
}
|
|
|
@@ -166,45 +163,6 @@ public class WxPbUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取推广二维码链接(临时)<br>
|
|
|
- * 1.如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。<br>
|
|
|
- * 2.如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
|
|
|
- *
|
|
|
- * @param sceneId 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)
|
|
|
- */
|
|
|
- public String spreadQrCode(int sceneId) {
|
|
|
- //有效期最大30天
|
|
|
- int expireSeconds = 2592000;
|
|
|
- String accessToken = getAccessToken();
|
|
|
- Map<String, String> wrap = new HashMap<>(2);
|
|
|
- wrap.put("access_token", accessToken);
|
|
|
- Map<String, Object> actionWrap = new HashMap<>(4);
|
|
|
- actionWrap.put("expire_seconds", expireSeconds);
|
|
|
- actionWrap.put("action_name", "QR_SCENE");
|
|
|
- //
|
|
|
- Map<String, Object> actionInfo = new HashMap<>(2);
|
|
|
- Map<String, Object> sceneInfo = new HashMap<>(2);
|
|
|
- sceneInfo.put("scene_id", sceneId);
|
|
|
- actionInfo.put("scene", sceneInfo);
|
|
|
- actionWrap.put("action_info", actionInfo);
|
|
|
- //
|
|
|
- String data = JSON.toJSONString(actionWrap);
|
|
|
- String url = String.format(WxPbConfig.API_QR_CODE, accessToken);
|
|
|
- try {
|
|
|
- Map<String, Object> result = sslRequest(url, "post", data);
|
|
|
- if (!checkResp(result)) {
|
|
|
- logger.error("WxPbUtil.spreadQrCode error msg:{}", JSON.toJSONString(result));
|
|
|
- throw new BusinessException("服务异常");
|
|
|
- }
|
|
|
- return (String) result.get("url");
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("WxPbUtil.spreadQrCode error ", e);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取用户基本信息
|
|
|
*/
|
|
|
@@ -224,7 +182,6 @@ public class WxPbUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* <p>https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842</p>
|
|
|
* <p>
|
|
|
@@ -302,7 +259,7 @@ public class WxPbUtil {
|
|
|
accessToken.expire = (int) result.get("expires_in");
|
|
|
} catch (Exception e) {
|
|
|
logger.error("WxPbUtil.reqAccessToken error ", e);
|
|
|
- throw new IllegalStateException("can't get access token from weixin server",e);
|
|
|
+ throw new IllegalStateException("can't get access token from weixin server", e);
|
|
|
}
|
|
|
|
|
|
return accessToken;
|
|
|
@@ -354,7 +311,6 @@ public class WxPbUtil {
|
|
|
return config.accessToken;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private static Map<String, Object> sslRequest(String reqUrl, String method, String body) {
|
|
|
logger.debug("WxPbUtil.sslRequest req url:{},method:{},body:{}", reqUrl, method, body);
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
@@ -367,9 +323,9 @@ public class WxPbUtil {
|
|
|
}
|
|
|
logger.info("WxPbUtil.sslRequest resp cost:{}ms,content:{}", (System.currentTimeMillis() - startTime), result);
|
|
|
try {
|
|
|
- return JSON.parseObject(result, Map.class);
|
|
|
+ return JSON.parseObject(result, Map.class);
|
|
|
} catch (Exception e) {
|
|
|
- return Map.of("result",result);
|
|
|
+ return Map.of("result", result);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("WxPbUtil.sslRequest error,", e);
|
|
|
@@ -382,12 +338,11 @@ public class WxPbUtil {
|
|
|
return null != resp && "0".equals(resp.get("errcode"));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 校验公众接口签名
|
|
|
*/
|
|
|
public static boolean checkSign(String signature, String timestamp, String nonce) {
|
|
|
- logger.info("signature:{},timestamp:{},nonce:{}",signature,timestamp,nonce);
|
|
|
+ logger.info("signature:{},timestamp:{},nonce:{}", signature, timestamp, nonce);
|
|
|
String[] arr = new String[]{WxPbConfig.TOKEN, timestamp, nonce};
|
|
|
Arrays.sort(arr);
|
|
|
StringBuilder content = new StringBuilder();
|
|
|
@@ -403,4 +358,42 @@ public class WxPbUtil {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取推广二维码链接(临时)<br>
|
|
|
+ * 1.如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。<br>
|
|
|
+ * 2.如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
|
|
|
+ *
|
|
|
+ * @param sceneId 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)
|
|
|
+ */
|
|
|
+ public String spreadQrCode(int sceneId) {
|
|
|
+ //有效期最大30天
|
|
|
+ int expireSeconds = 2592000;
|
|
|
+ String accessToken = getAccessToken();
|
|
|
+ Map<String, String> wrap = new HashMap<>(2);
|
|
|
+ wrap.put("access_token", accessToken);
|
|
|
+ Map<String, Object> actionWrap = new HashMap<>(4);
|
|
|
+ actionWrap.put("expire_seconds", expireSeconds);
|
|
|
+ actionWrap.put("action_name", "QR_SCENE");
|
|
|
+ //
|
|
|
+ Map<String, Object> actionInfo = new HashMap<>(2);
|
|
|
+ Map<String, Object> sceneInfo = new HashMap<>(2);
|
|
|
+ sceneInfo.put("scene_id", sceneId);
|
|
|
+ actionInfo.put("scene", sceneInfo);
|
|
|
+ actionWrap.put("action_info", actionInfo);
|
|
|
+ //
|
|
|
+ String data = JSON.toJSONString(actionWrap);
|
|
|
+ String url = String.format(WxPbConfig.API_QR_CODE, accessToken);
|
|
|
+ try {
|
|
|
+ Map<String, Object> result = sslRequest(url, "post", data);
|
|
|
+ if (!checkResp(result)) {
|
|
|
+ logger.error("WxPbUtil.spreadQrCode error msg:{}", JSON.toJSONString(result));
|
|
|
+ throw new BusinessException("服务异常");
|
|
|
+ }
|
|
|
+ return (String) result.get("url");
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("WxPbUtil.spreadQrCode error ", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
}
|