企业微信-回调配置(一)

时间:2025-03-10 09:23:31
  • package ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import org.;
  • import org.;
  • import ;
  • import ;
  • import ;
  • public class WechatUtils {
  • private static final Logger logger = ();
  • /**
  • * 获取微信token
  • * @return
  • * @throws Exception
  • * @param
  • */
  • public static JSONObject getToken(String type) throws Exception {
  • String url = "";
  • if ("corp".equals(type)) {
  • url = WechatConstants.GETTOKEN_URL.replace("{corpid}", WechatConstants.CORP_ID).replace("{corpsecret}", WechatConstants.CORP_SECRET);
  • }else if("contacts".equals(type)){
  • url = WechatConstants.GETTOKEN_URL.replace("{corpid}", WechatConstants.CORP_ID).replace("{corpsecret}", WechatConstants.CONTACTS_SECRET);
  • }
  • JSONObject jsonObject = (url, "", "json", "UTF-8", false);
  • // JSONObject jsonObject = (jsons);
  • // String access_token = ("access_token");
  • ("\n=====1.获取token请求:"+());
  • return jsonObject;
  • }
  • /**
  • * 根据code,获取微信用户id
  • * @param token
  • * @param code
  • * @return
  • * @throws Exception
  • */
  • public static JSONObject getUserId(String token,String code) throws Exception {
  • String url = WechatConstants.GETUSERINFO_URL.replace("{access_token}", token).replace("{code}", code);
  • JSONObject jsonObject = (url, "", "json", "UTF-8", false);
  • // JSONObject jsonObject = (jsons);
  • // String userId = ("UserId");
  • ("\n=====2.获取用户id请求:"+());
  • return jsonObject;
  • }
  • /**
  • * 获取单个微信用户信息
  • * @param token
  • * @param userid
  • * @return
  • * @throws Exception
  • */
  • public static JSONObject getUserByUserId(String token,String userid) throws Exception {
  • String url = WechatConstants.USER_GET_URL.replace("{access_token}", token).replace("{userid}", userid);
  • JSONObject jsonObject = (url, "", "json", "UTF-8", false);
  • // JSONObject jsonObject = (jsons);
  • ("\n=====3.获取用户信息:"+());
  • return jsonObject;
  • }
  • /**
  • * 获取某部门用户列表
  • */
  • public static JSONObject getSimpleList(String token,String departmentId,String fetchChild) throws Exception {
  • String url = WechatConstants.USER_SIMPLELIST_URL.replace("{access_token}", token).replace("{department_id}", departmentId).replace("{fetch_child}",fetchChild);
  • JSONObject jsonObject = (url, "", "json", "UTF-8", false);
  • // byte[] jsons = (url, "", "json", "UTF-8", false);
  • // JSONObject jsonObject = (jsons);
  • ("\n=====4.获取用户列表:"+());
  • return jsonObject;
  • }
  • /**
  • * 获取某部门列表
  • */
  • public static JSONObject getDepartmentList(String token,String departmentId) throws Exception {
  • String url = WechatConstants.DEPARTMENT_LIST_URL.replace("{access_token}", token).replace("{id}", departmentId);
  • JSONObject jsonObject = (url, "", "json", "UTF-8", false);
  • ("\n=====5.获取部门列表:"+());
  • return jsonObject;
  • }
  • /**
  • * 获取企业的jsapi_ticket
  • */
  • public static JSONObject getJsapiTicket(String token) throws Exception {
  • String url = WechatConstants.GET_JSAPI_TICKET.replace("{access_token}", token);
  • JSONObject jsonObject = (url, "", "json", "UTF-8", false);
  • ("\n=====6.获取JsapiTicket(:"+());
  • return jsonObject;
  • }
  • /**
  • * 获取指定长度随机字符串
  • *
  • * @param length
  • * @return
  • */
  • public static String getRandomString(int length) {
  • Random random = new Random();
  • StringBuffer sb = new StringBuffer();
  • for (int i = 0; i < length; i++) {
  • int number = (3);
  • long result = 0;
  • switch (number) {
  • case 0:
  • result = (() * 25 + 65);
  • (((char) result));
  • break;
  • case 1:
  • result = (() * 25 + 97);
  • (((char) result));
  • break;
  • case 2:
  • ((new Random().nextInt(10)));
  • break;
  • }
  • }
  • return ();
  • }
  • /**
  • * 拼接字符串
  • */
  • public static String getSignatureStr(JSONObject userJson){
  • StringBuffer jsapi_ticketStr = new StringBuffer();
  • jsapi_ticketStr.append("jsapi_ticket=").append(WechatScheduled.jsapi_ticket).append("&")
  • .append("noncestr=").append(("nonceStr")).append("&")
  • .append("timestamp=").append(("timestamp")).append("&")
  • .append("url=").append(("url"));
  • return jsapi_ticketStr.toString();
  • }
  • /**
  • * 获取sha1加密字符串
  • * @param str
  • * @return
  • */
  • public static String getSha1(String str) {
  • // return DigestUtils.sha1Hex(inStr);
  • char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 'a', 'b', 'c', 'd', 'e', 'f' };
  • try {
  • MessageDigest mdTemp = ("SHA1");
  • (("UTF-8"));
  • byte[] md = ();
  • int j = ;
  • char buf[] = new char[j * 2];
  • int k = 0;
  • for (int i = 0; i < j; i++) {
  • byte byte0 = md[i];
  • buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
  • buf[k++] = hexDigits[byte0 & 0xf];
  • }
  • return new String(buf);
  • } catch (Exception e) {
  • return null;
  • }
  • }
  • /**
  • * 推送消息到企业微信
  • */
  • public static JSONObject sendMessage(String token,byte[] body) throws Exception {
  • String url = WechatConstants.SEND_MESSAGE_URL.replace("{access_token}", token);
  • JSONObject jsonObject = (url, "", body,"json", "UTF-8", false);
  • ("\n=====7.推送message:"+());
  • return jsonObject;
  • }
  • }