|
@@ -0,0 +1,382 @@
|
|
|
+package com.xzl.web.utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.collections.MapUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import javax.net.ssl.SSLSocketFactory;
|
|
|
+import javax.net.ssl.TrustManager;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class WorkWxUtils {
|
|
|
+
|
|
|
+ private static final Logger LOG = LoggerFactory.getLogger(WorkWxUtils.class);
|
|
|
+
|
|
|
+ public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr, Map<String, String> headers) {
|
|
|
+ JSONObject jsonObject = null;
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ try {
|
|
|
+ TrustManager[] tm = {new MyX509TrustManager()};
|
|
|
+ SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
+ sslContext.init(null, tm, new java.security.SecureRandom());
|
|
|
+ SSLSocketFactory ssf = sslContext.getSocketFactory();
|
|
|
+ URL url = new URL(requestUrl);
|
|
|
+ HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
+ httpUrlConn.setDoOutput(true);
|
|
|
+ httpUrlConn.setDoInput(true);
|
|
|
+ httpUrlConn.setUseCaches(false);
|
|
|
+ httpUrlConn.setRequestMethod(requestMethod.toUpperCase());
|
|
|
+
|
|
|
+ if (headers != null) {
|
|
|
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ httpUrlConn.setRequestProperty(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("GET".equalsIgnoreCase(requestMethod)) httpUrlConn.connect();
|
|
|
+ if ("POST".equalsIgnoreCase(requestMethod)) {
|
|
|
+ httpUrlConn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
|
+ httpUrlConn.setRequestProperty("accept", "application/json");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null != outputStr) {
|
|
|
+ OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ InputStream inputStream = httpUrlConn.getInputStream();
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+ String str = null;
|
|
|
+ while ((str = bufferedReader.readLine()) != null) {
|
|
|
+ buffer.append(str);
|
|
|
+ }
|
|
|
+ if (LOG.isDebugEnabled()) {
|
|
|
+ LOG.debug(buffer.toString());
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ inputStreamReader.close();
|
|
|
+ inputStream.close();
|
|
|
+ inputStream = null;
|
|
|
+ httpUrlConn.disconnect();
|
|
|
+ jsonObject = JSON.parseObject(buffer.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("", e);
|
|
|
+ Object o = JSON.parse(buffer.toString());
|
|
|
+ if (o instanceof JSONArray) {
|
|
|
+ jsonObject = new JSONObject(ConstructUtils.map("items", JSON.parseArray(buffer.toString())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法名:httpRequest</br>
|
|
|
+ * 详述:发送http请求</br>
|
|
|
+ *
|
|
|
+ * @param requestUrl
|
|
|
+ * @param requestMethod
|
|
|
+ * @param outputStr
|
|
|
+ * @return 说明返回值含义
|
|
|
+ */
|
|
|
+ public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
|
|
|
+ return httpRequest(requestUrl, requestMethod, outputStr, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getAccessToken() {
|
|
|
+ return getAccessToken(Constants.WORK_WX.CORP_ID, Constants.WORK_WX.APP_SECRET);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getContactAccessToken() {
|
|
|
+ return getAccessToken(Constants.WORK_WX.CORP_ID, Constants.WORK_WX.APP_CONTACT_SYNC_SECRET);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String getAccessToken(String corpId, String secret) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + secret;
|
|
|
+ JSONObject json = httpRequest(url, "GET", null);
|
|
|
+ if (json != null) {
|
|
|
+ return json.getString("access_token");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Map getUserInfo(String code) {
|
|
|
+ String accessToken = getAccessToken();
|
|
|
+ return getUserInfo(code, accessToken);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Map getUserInfo(String code, String accessToken) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" + accessToken + "&code=" + code;
|
|
|
+ JSONObject json = httpRequest(url, "GET", null);
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置工作台显示样式
|
|
|
+ *
|
|
|
+ * @param accessToken
|
|
|
+ */
|
|
|
+ public static void setWorkBench(String accessToken) {
|
|
|
+ Map data = ConstructUtils.map("agentid", Constants.WORK_WX.AGENT_ID, "type", "keydata");
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_template?access_token=" + accessToken;
|
|
|
+ httpRequest(url, "POST", JSON.toJSONString(data));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置工作台数据(定时任务调用,每 10分钟执行一次,到个人)
|
|
|
+ *
|
|
|
+ * @param accessToken
|
|
|
+ */
|
|
|
+ public static void setWorkBenchData(String accessToken, String userId) {
|
|
|
+ List<Map> menus = new ArrayList<>();
|
|
|
+
|
|
|
+ Map m1 = ConstructUtils.map("key", "终端选点申请", "data", "0", "jump_url", "http://www.qq.com");
|
|
|
+ Map m2 = ConstructUtils.map("key", "终端打标验收", "data", "0", "jump_url", "http://www.baidu.com");
|
|
|
+ Map m3 = ConstructUtils.map("key", "设备报修", "data", "0", "jump_url", "http://www.schoolwisdoms.com");
|
|
|
+ //Map m4 = ConstructUtils.map("key", "菜单4", "data", "2", "jump_url", "https://demo.schoolwisdoms.com/schoolbaby/api/wx/index");
|
|
|
+
|
|
|
+ menus.add(m1);
|
|
|
+ menus.add(m2);
|
|
|
+ menus.add(m3);
|
|
|
+ //menus.add(m4);
|
|
|
+
|
|
|
+ Map keydata = ConstructUtils.map("items", menus);
|
|
|
+ Map data = ConstructUtils.map("agentid", Constants.WORK_WX.AGENT_ID, "userid", userId, "type", "keydata", "keydata", keydata);
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_data?access_token=" + accessToken;
|
|
|
+ httpRequest(url, "POST", JSON.toJSONString(data));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getJsapiTicket(String accessToken) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + accessToken;
|
|
|
+ JSONObject json = httpRequest(url, "GET", null);
|
|
|
+ if (json != null) {
|
|
|
+ return json.getString("ticket");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getAgentJsapiTicket(String accessToken) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + accessToken + "&type=agent_config";
|
|
|
+ JSONObject json = httpRequest(url, "GET", null);
|
|
|
+ if (json != null) {
|
|
|
+ return json.getString("ticket");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建通员工,需要 通讯录管理secret。在“管理工具”-“通讯录同步”里面查看(需开启“API接口同步”);
|
|
|
+ *
|
|
|
+ * @param accessToken
|
|
|
+ * @param employee
|
|
|
+ */
|
|
|
+ public static void createEmployee(String accessToken, Map employee) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=" + accessToken;
|
|
|
+ httpRequest(url, "POST", JSON.toJSONString(employee));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 先创建,再邀请员工
|
|
|
+ *
|
|
|
+ * @param accessToken
|
|
|
+ * @param userId
|
|
|
+ */
|
|
|
+ public static void inviteEmployee(String accessToken, String userId) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/batch/invite?access_token=" + accessToken;
|
|
|
+ String[] users = new String[]{userId};
|
|
|
+ Map data = ConstructUtils.map("user", users);
|
|
|
+ httpRequest(url, "POST", JSON.toJSONString(data));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void deleteEmployee(String accessToken, String userId) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=" + accessToken + "&userid=" + userId;
|
|
|
+ httpRequest(url, "GET", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map getEmployee(String accessToken, String userId) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&userid=" + userId;
|
|
|
+ return httpRequest(url, "GET", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map getEmployee(String userId) {
|
|
|
+ return getEmployee(getContactAccessToken(), userId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建通部门,需要 通讯录管理secret。在“管理工具”-“通讯录同步”里面查看(需开启“API接口同步”);
|
|
|
+ *
|
|
|
+ * @param accessToken
|
|
|
+ * @param department
|
|
|
+ */
|
|
|
+ public static void createDepartment(String accessToken, Map department) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=" + accessToken;
|
|
|
+ httpRequest(url, "POST", JSON.toJSONString(department));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void deleteDepartment(String accessToken, String id) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=" + accessToken + "&id=" + id;
|
|
|
+ httpRequest(url, "GET", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getUrl(HttpServletRequest request) {
|
|
|
+ StringBuilder url = new StringBuilder(request.getRequestURL().toString());
|
|
|
+ String queryString = request.getQueryString();
|
|
|
+ if (queryString != null) {
|
|
|
+ url.append("?").append(queryString);
|
|
|
+ }
|
|
|
+ return url.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法名:getWxConfig</br>
|
|
|
+ * 详述:获取微信的配置信息 </br>
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return 说明返回值含义
|
|
|
+ */
|
|
|
+ public static Map<String, Object> getWxConfig(HttpServletRequest request) {
|
|
|
+ Map<String, Object> ret = new HashMap<String, Object>();
|
|
|
+ String access_token = getAccessToken();
|
|
|
+ String jsapi_ticket = null;
|
|
|
+ if (access_token != null) {
|
|
|
+ jsapi_ticket = getJsapiTicket(access_token);
|
|
|
+ ret.put("jsapi_ticket", jsapi_ticket);
|
|
|
+ }
|
|
|
+ String requestUrl = getUrl(request);
|
|
|
+ // 阿里云对于 Nigix 无法配置,导致 tomcat 获取https的协议仍未http
|
|
|
+ requestUrl = requestUrl.replaceAll("http:", "https:");
|
|
|
+ String timestamp = Long.toString(System.currentTimeMillis() / 1000); // 必填,生成签名的时间戳
|
|
|
+ String nonceStr = UUID.randomUUID().toString(); // 必填,生成签名的随机串
|
|
|
+ String signature = null;
|
|
|
+ // 注意这里参数名必须全部小写,且必须有序
|
|
|
+ String sign = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url="
|
|
|
+ + requestUrl;
|
|
|
+ ret.put("sign", sign);
|
|
|
+ try {
|
|
|
+ MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
|
|
+ crypt.reset();
|
|
|
+ crypt.update(sign.getBytes("UTF-8"));
|
|
|
+ signature = byteToHex(crypt.digest());
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ ret.put("appId", Constants.WORK_WX.CORP_ID);
|
|
|
+ ret.put("timestamp", timestamp);
|
|
|
+ ret.put("nonceStr", nonceStr);
|
|
|
+ ret.put("signature", signature);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Map<String, Object> getWxAgentConfig(HttpServletRequest request) {
|
|
|
+ Map<String, Object> ret = new HashMap<String, Object>();
|
|
|
+ String access_token = getAccessToken();
|
|
|
+ String jsapi_ticket = null;
|
|
|
+ if (access_token != null) {
|
|
|
+ jsapi_ticket = getAgentJsapiTicket(access_token);
|
|
|
+ ret.put("jsapi_ticket", jsapi_ticket);
|
|
|
+ }
|
|
|
+ String requestUrl = getUrl(request);
|
|
|
+ // 阿里云对于 Nigix 无法配置,导致 tomcat 获取https的协议仍未http
|
|
|
+ requestUrl = requestUrl.replaceAll("http:", "https:");
|
|
|
+ String timestamp = Long.toString(System.currentTimeMillis() / 1000); // 必填,生成签名的时间戳
|
|
|
+ String nonceStr = UUID.randomUUID().toString(); // 必填,生成签名的随机串
|
|
|
+ String signature = null;
|
|
|
+ // 注意这里参数名必须全部小写,且必须有序
|
|
|
+ String sign = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url="
|
|
|
+ + requestUrl;
|
|
|
+ ret.put("sign", sign);
|
|
|
+ try {
|
|
|
+ MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
|
|
+ crypt.reset();
|
|
|
+ crypt.update(sign.getBytes("UTF-8"));
|
|
|
+ signature = byteToHex(crypt.digest());
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ ret.put("appId", Constants.WORK_WX.CORP_ID);
|
|
|
+ ret.put("agentId", Constants.WORK_WX.AGENT_ID);
|
|
|
+ ret.put("timestamp", timestamp);
|
|
|
+ ret.put("nonceStr", nonceStr);
|
|
|
+ ret.put("signature", signature);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String byteToHex(final byte[] hash) {
|
|
|
+ Formatter formatter = new Formatter();
|
|
|
+ for (byte b : hash) {
|
|
|
+ formatter.format("%02x", b);
|
|
|
+ }
|
|
|
+ String result = formatter.toString();
|
|
|
+ formatter.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void sendTextCardMessage(String accessToken, String userId, String title, String content, String pageUrl) {
|
|
|
+
|
|
|
+ sendMessage(accessToken, "textcard", userId, title, content, pageUrl);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void sendMessage(String accessToken, String msgType, String userId, String title, String content, String pageUrl) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
|
|
|
+ Map textcard = ConstructUtils.map("title", title, "description", content, "url", pageUrl);
|
|
|
+ Map data = ConstructUtils.map("touser", userId, "msgtype", msgType, "agentid", Constants.WORK_WX.AGENT_ID, "textcard", textcard);
|
|
|
+ System.out.println(JSON.toJSONString(data));
|
|
|
+ httpRequest(url, "POST", JSON.toJSONString(data));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // String accessToken = getAccessToken();
|
|
|
+ // setWorkBench(accessToken);
|
|
|
+ // setWorkBenchData(accessToken, "ZhangShuLing");
|
|
|
+
|
|
|
+ // sendTextCardMessage(accessToken, "ZhangShuling", "审核title", "sdsdsdsdsdsd", "http://www.baidu.com");
|
|
|
+
|
|
|
+ String accessToken = getContactAccessToken();
|
|
|
+
|
|
|
+ // Map department = ConstructUtils.map("name", "信息部", "name_en", "xxb", "parentid", 1, "order", 1, "id", 10001);
|
|
|
+ // createDepartment(accessToken, department);
|
|
|
+ // deleteDepartment(accessToken, "2");
|
|
|
+
|
|
|
+// int[] deptIds = new int[]{10001};
|
|
|
+// Map employee = ConstructUtils.map("userid", "12345678891", "name", "张三", "department", deptIds, "mobile", "15671668917");
|
|
|
+// createEmployee(accessToken, employee);
|
|
|
+
|
|
|
+ // inviteEmployee(accessToken, "ZhangShuLing");
|
|
|
+
|
|
|
+ // deleteEmployee(accessToken, "0cd8605f59204532963dbb5a75109a97");
|
|
|
+ Map result = getEmployee(accessToken, "114200900908");
|
|
|
+ System.out.println(JSON.toJSONString(result));
|
|
|
+ System.out.println(MapUtils.getString(result, "mobile"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|