|
@@ -0,0 +1,154 @@
|
|
|
+package com.redxun.knowledge.qywexin;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.redxun.cache.CacheUtil;
|
|
|
+import com.redxun.common.utils.ExceptionUtil;
|
|
|
+import com.redxun.common.utils.HttpClientUtil;
|
|
|
+import com.redxun.dto.user.OsUserDto;
|
|
|
+import com.redxun.feign.org.UserClient;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.client.entity.EntityBuilder;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 企业微信对接工具类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class QYWechatConn {
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(QYWechatConn.class);
|
|
|
+
|
|
|
+ @Value("${QYWechat.agent-id}")
|
|
|
+ private String agentId;
|
|
|
+
|
|
|
+ @Value("${QYWechat.corp-secret}")
|
|
|
+ private String corpSecret;
|
|
|
+
|
|
|
+ @Value("${QYWechat.corp-id}")
|
|
|
+ private String corpId;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserClient userClient;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 企业微信对接:获取access_token
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getAccessToken() {
|
|
|
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", corpId, corpSecret);
|
|
|
+ //String result = "{\"errcode\":0,\"errmsg\":\"ok\",\"access_token\":\"FzfiNCYEnoyk6qW0LrmDcCWMIRPwcMXw6dRQkmU5yZg9KD_kJsAW355CUYQmW_dPV_hxsQ6W_mlycnv5J8YzcmVvqCjDQG_rj76EtGGGE_Os73nm8G1JYqcr-3BYA6HAfQkB5NijRJpd88cE1q1xqb6SUzb0SSIoEP_U5-pTlkBxtobMjurrPjRKdR1dUpt1hxKyRcXU-3CxgwFgWHl7-A\",\"expires_in\":7200}";
|
|
|
+
|
|
|
+ try {
|
|
|
+ String accessToken = null;
|
|
|
+ if (!CacheUtil.isExist("default", "QYWechat")) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(HttpClientUtil.getFromUrl(url, null));
|
|
|
+ int errCode = jsonObject.getIntValue("errcode");
|
|
|
+ if (errCode == 0) {
|
|
|
+ accessToken = jsonObject.getString("access_token");
|
|
|
+ CacheUtil.set("default", "QYWechat", accessToken);
|
|
|
+ }
|
|
|
+ logger.info("get qi ye Wechat access token result : " + jsonObject.toJSONString());
|
|
|
+ } else {
|
|
|
+ accessToken = CacheUtil.get("default", "QYWechat").toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error(ExceptionUtil.getExceptionMessage(e));
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String postTextMsg(String content, String... toUsers) {
|
|
|
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s", getAccessToken());
|
|
|
+
|
|
|
+ JSONObject paramJson = new JSONObject();
|
|
|
+ paramJson.put("msgtype", "text");
|
|
|
+ paramJson.put("agentid", agentId);
|
|
|
+ paramJson.put("content", content);
|
|
|
+
|
|
|
+ try {
|
|
|
+ StringBuilder sbWxUser = new StringBuilder();
|
|
|
+ JSONArray wxUserJsons = JSONArray.parseArray(wechatUserTransfer(toUsers));
|
|
|
+ if (wxUserJsons != null) {
|
|
|
+ wxUserJsons.forEach(u -> {
|
|
|
+ if (sbWxUser.length() > 0) {
|
|
|
+ sbWxUser.append("|");
|
|
|
+ }
|
|
|
+ sbWxUser.append(u);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ paramJson.put("touser", sbWxUser.toString());
|
|
|
+
|
|
|
+ EntityBuilder builder = EntityBuilder.create();
|
|
|
+ builder.setContentEncoding("utf-8");
|
|
|
+ builder.setContentType(ContentType.APPLICATION_JSON);
|
|
|
+ builder.setText(paramJson.toJSONString());
|
|
|
+
|
|
|
+ logger.info("post qi ye Wechat message param : " + paramJson.toJSONString());
|
|
|
+
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(HttpClientUtil.post(url, builder.build()));
|
|
|
+ logger.info("post qi ye Wechat message result : " + jsonObject.toJSONString());
|
|
|
+ int errCode = jsonObject.getIntValue("errcode");
|
|
|
+ if (errCode == 0) {
|
|
|
+ return jsonObject.toJSONString();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error(ExceptionUtil.getExceptionMessage(e));
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String wechatUserTransfer(String... jpassUserIds) {
|
|
|
+ String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/batch/openuserid_to_userid?access_token=%s", getAccessToken());
|
|
|
+// {
|
|
|
+// "open_userid_list":["xxx", "yyy"],
|
|
|
+// "source_agentid":100001
|
|
|
+// }
|
|
|
+ JSONObject paramJson = new JSONObject();
|
|
|
+ paramJson.put("source_agentid", agentId);
|
|
|
+ JSONArray paramUsers = new JSONArray();
|
|
|
+ OsUserDto userDto = null;
|
|
|
+ for (String userId : jpassUserIds) {
|
|
|
+ userDto = userClient.findByUserId(userId);
|
|
|
+ if (userDto != null) {
|
|
|
+ paramUsers.add(userDto.getWxOpenId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+// paramUsers.add("0177150");
|
|
|
+ paramJson.put("open_userid_list", paramUsers);
|
|
|
+
|
|
|
+ try {
|
|
|
+ EntityBuilder builder = EntityBuilder.create();
|
|
|
+ builder.setContentEncoding("utf-8");
|
|
|
+ builder.setContentType(ContentType.APPLICATION_JSON);
|
|
|
+ builder.setText(paramJson.toJSONString());
|
|
|
+
|
|
|
+ logger.info("post qi ye Wechat user transfer param : " + paramJson.toJSONString());
|
|
|
+
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(HttpClientUtil.post(url, builder.build()));
|
|
|
+ logger.info("post qi ye Wechat user transfer result : " + jsonObject.toJSONString());
|
|
|
+
|
|
|
+ return jsonObject.getJSONArray("userid_list").toJSONString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ logger.error(ExceptionUtil.getExceptionMessage(e));
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|