|
|
@@ -9,10 +9,23 @@ import p2.pao.PaoRemote;
|
|
|
import p2.pao.PaoSetRemote;
|
|
|
import p2.util.P2Exception;
|
|
|
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
public class WechatDock_Enterprise extends BaseClass {
|
|
|
+ /**
|
|
|
+ * 参数连接符
|
|
|
+ */
|
|
|
+ public static final String QYWX_AND = "&";
|
|
|
+ public static final String QYWX_EQUAL = "=";
|
|
|
+ public static final String QYWX_QUERY = "?";
|
|
|
+ public static final String QYWX_GET_JSAPITICKET_URL_PARAM_TICKET = "jsapi_ticket";
|
|
|
+ public static final String QYWX_GET_JSAPITICKET_URL_PARAM_NONCESTR = "noncestr";
|
|
|
+ public static final String QYWX_GET_JSAPITICKET_URL_PARAM_TIMESTAMP = "timestamp";
|
|
|
+ public static final String QYWX_GET_JSAPITICKET_URL_PARAM_URL = "url";
|
|
|
+ public static final String QYWX_GET_JSAPITICKET_RETURN_SIGNATURE = "signature";
|
|
|
//企业ID
|
|
|
public static String corpid = "ww6e401e63011f9f59";
|
|
|
//应用的凭证密钥(通讯录)
|
|
|
@@ -63,30 +76,194 @@ public class WechatDock_Enterprise extends BaseClass {
|
|
|
return access_token;
|
|
|
}
|
|
|
|
|
|
+// /**
|
|
|
+// * 获取小程序token
|
|
|
+// *
|
|
|
+// * @param type
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// public static String getMiniAppAccessToken(int type) {
|
|
|
+// String corpsecret = "";
|
|
|
+// Object access_token = "";
|
|
|
+// if (type == 1) {
|
|
|
+// corpsecret = corpsecret1;
|
|
|
+// } else {
|
|
|
+// corpsecret = corpsecret2;
|
|
|
+// }
|
|
|
+//
|
|
|
+// String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
|
|
|
+// String res = new WebRequest().doGet(url);
|
|
|
+// JSONObject resobject = JSONObject.parseObject(res);
|
|
|
+// System.err.println(resobject);
|
|
|
+// if (resobject.containsKey("access_token")) {
|
|
|
+// access_token = resobject.get("access_token");
|
|
|
+// }
|
|
|
+//
|
|
|
+// return access_token.toString();
|
|
|
+// }
|
|
|
+
|
|
|
+ private static String access_token_zlzx = "";
|
|
|
+ private static long access_token_timelimit_zlzx = 0L;
|
|
|
+
|
|
|
/**
|
|
|
- * 获取小程序token
|
|
|
+ * 资料中心token
|
|
|
*
|
|
|
- * @param type
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getMiniAppAccessToken(int type) {
|
|
|
- String corpsecret = "";
|
|
|
- Object access_token = "";
|
|
|
- if (type == 1) {
|
|
|
- corpsecret = corpsecret1;
|
|
|
- } else {
|
|
|
- corpsecret = corpsecret2;
|
|
|
+ public static String getAccessToken_zlzx() {
|
|
|
+ long nowtime = Calendar.getInstance().getTimeInMillis();
|
|
|
+ if (access_token_timelimit_zlzx < nowtime) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret1;
|
|
|
+ String res = new WebRequest().doGet(url);
|
|
|
+ JSONObject resobject = JSONObject.parseObject(res);
|
|
|
+ System.err.println(resobject);
|
|
|
+ if (resobject.containsKey("access_token")) {
|
|
|
+ access_token_zlzx = resobject.get("access_token").toString();
|
|
|
+ access_token_timelimit_zlzx = nowtime + 1000 * resobject.getIntValue("expires_in");
|
|
|
+ }
|
|
|
}
|
|
|
+ return access_token_zlzx;
|
|
|
+ }
|
|
|
|
|
|
- String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
|
|
|
- String res = new WebRequest().doGet(url);
|
|
|
- JSONObject resobject = JSONObject.parseObject(res);
|
|
|
- System.err.println(resobject);
|
|
|
- if (resobject.containsKey("access_token")) {
|
|
|
- access_token = resobject.get("access_token");
|
|
|
+ private static String access_token_pxxx = "";
|
|
|
+ private static long access_token_timelimit_pxxx = 0L;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 培训学习token
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getAccessToken_pxxx() {
|
|
|
+ long nowtime = Calendar.getInstance().getTimeInMillis();
|
|
|
+ if (access_token_timelimit_pxxx < nowtime) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret2;
|
|
|
+ String res = new WebRequest().doGet(url);
|
|
|
+ JSONObject resobject = JSONObject.parseObject(res);
|
|
|
+ System.err.println(resobject);
|
|
|
+ if (resobject.containsKey("access_token")) {
|
|
|
+ access_token_pxxx = resobject.get("access_token").toString();
|
|
|
+ access_token_timelimit_pxxx = nowtime + 1000 * resobject.getIntValue("expires_in");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return access_token_pxxx;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String ticket_zlzx = "";
|
|
|
+ private static long ticket_timelimit_zlzx = 0L;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 资料中心ticket
|
|
|
+ */
|
|
|
+ public static String getTicket_zlzx() {
|
|
|
+ long nowtime = Calendar.getInstance().getTimeInMillis();
|
|
|
+ if (ticket_timelimit_zlzx < nowtime) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + getAccessToken_zlzx() + "&type=agent_config";
|
|
|
+ String res = new WebRequest().doGet(url);
|
|
|
+ JSONObject resobject = JSONObject.parseObject(res);
|
|
|
+ System.err.println(resobject);
|
|
|
+ if (resobject.containsKey("ticket")) {
|
|
|
+ ticket_zlzx = resobject.get("ticket").toString();
|
|
|
+ ticket_timelimit_zlzx = nowtime + 1000 * resobject.getIntValue("expires_in");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ticket_zlzx;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String ticket_pxxx = "";
|
|
|
+ private static long ticket_timelimit_pxxx = 0L;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 培训学习ticket
|
|
|
+ */
|
|
|
+ public static String getTicket_pxxx() {
|
|
|
+ long nowtime = Calendar.getInstance().getTimeInMillis();
|
|
|
+ if (ticket_timelimit_pxxx < nowtime) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + getAccessToken_pxxx() + "&type=agent_config";
|
|
|
+ String res = new WebRequest().doGet(url);
|
|
|
+ JSONObject resobject = JSONObject.parseObject(res);
|
|
|
+ System.err.println(resobject);
|
|
|
+ if (resobject.containsKey("ticket")) {
|
|
|
+ ticket_pxxx = resobject.get("ticket").toString();
|
|
|
+ ticket_timelimit_pxxx = nowtime + 1000 * resobject.getIntValue("expires_in");
|
|
|
+ }
|
|
|
}
|
|
|
+ return ticket_pxxx;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String ticket = "";
|
|
|
+ private static long ticket_timelimit = 0L;
|
|
|
|
|
|
- return access_token.toString();
|
|
|
+ public static String getTicket() {
|
|
|
+ long nowtime = Calendar.getInstance().getTimeInMillis();
|
|
|
+ if (ticket_timelimit < nowtime) {
|
|
|
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + getAccessToken();
|
|
|
+ String res = new WebRequest().doGet(url);
|
|
|
+ JSONObject resobject = JSONObject.parseObject(res);
|
|
|
+ System.err.println(resobject);
|
|
|
+ if (resobject.containsKey("ticket")) {
|
|
|
+ ticket = resobject.get("ticket").toString();
|
|
|
+ ticket_timelimit = nowtime + 1000 * resobject.getIntValue("expires_in");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ticket;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static JSONObject getSign(String ticket, String url) {
|
|
|
+ String noncestr = getRandomString(16);
|
|
|
+ String timestamp = (int) (System.currentTimeMillis() / 1000) + "";
|
|
|
+
|
|
|
+ JSONObject rul = new JSONObject();
|
|
|
+ String sign = "";
|
|
|
+ sign += QYWX_GET_JSAPITICKET_URL_PARAM_TICKET + QYWX_EQUAL + ticket + QYWX_AND + QYWX_GET_JSAPITICKET_URL_PARAM_NONCESTR + QYWX_EQUAL + noncestr + QYWX_AND + QYWX_GET_JSAPITICKET_URL_PARAM_TIMESTAMP + QYWX_EQUAL + timestamp + QYWX_AND + QYWX_GET_JSAPITICKET_URL_PARAM_URL + QYWX_EQUAL + url;
|
|
|
+ String signature = "";
|
|
|
+ try {
|
|
|
+ // 指定sha1算法
|
|
|
+ MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
|
|
+ digest.update(sign.getBytes());
|
|
|
+ // 获取字节数组
|
|
|
+ byte messageDigest[] = digest.digest();
|
|
|
+ // Create Hex String
|
|
|
+ StringBuffer hexString = new StringBuffer();
|
|
|
+ // 字节数组转换为 十六进制 数
|
|
|
+ for (int i = 0; i < messageDigest.length; i++) {
|
|
|
+ String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
|
|
|
+ if (shaHex.length() < 2) {
|
|
|
+ hexString.append(0);
|
|
|
+ }
|
|
|
+ hexString.append(shaHex);
|
|
|
+ }
|
|
|
+ signature = hexString.toString();
|
|
|
+
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ rul.put("signature", signature);
|
|
|
+ rul.put("noncestr", noncestr);
|
|
|
+ rul.put("timestamp", timestamp);
|
|
|
+ rul.put("ticket", ticket);
|
|
|
+
|
|
|
+ return rul;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param
|
|
|
+ * @return 对应长度的随机字符串
|
|
|
+ * @name 中文名称
|
|
|
+ * @description 获取指定位数的随机字符串(包含小写字母 、 大写字母 、 数字, 0 < length)
|
|
|
+ * @time 创建时间:2018年7月23日14:17:21
|
|
|
+ * @author 朱浩
|
|
|
+ * @history 修订历史(历次修订内容、修订人、修订时间等)
|
|
|
+ */
|
|
|
+ private static String getRandomString(int length) {
|
|
|
+ // 随机字符串的随机字符库
|
|
|
+ String KeyString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ int len = KeyString.length();
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
}
|
|
|
|
|
|
|