package com.hx.mp.util;
|
|
import com.hx.util.corp.entity.AppLetInfo;
|
import com.hx.exception.TipsException;
|
import com.hx.util.HttpMethodUtil;
|
import com.hx.util.StringUtils;
|
import net.sf.json.JSONException;
|
import net.sf.json.JSONObject;
|
import org.apache.commons.io.IOUtils;
|
|
import java.io.OutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.text.MessageFormat;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 企业微信工具类
|
*/
|
public class CorpMpUtil {
|
|
/**链接-获取应用accessToken*/
|
public static final String URL_GET_ACCESS_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
|
/**链接-获取session信息*/
|
public static final String URL_CODE_2_SESSION = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token={0}&js_code={1}&grant_type=authorization_code";
|
/**链接-添加标签*/
|
public static final String URL_ADD_LABEL = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token={0}";
|
/**链接-添加部门*/
|
public static final String URL_ADD_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={0}";
|
/**链接-修改部门*/
|
public static final String URL_UPDATE_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={0}";
|
/**链接-删除部门*/
|
public static final String URL_DELETE_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={0}&id={1}";
|
/**链接-添加成员*/
|
public static final String URL_ADD_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={0}";
|
/**链接-更新成员*/
|
public static final String URL_UPDATE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={0}";
|
/**链接-删除成员*/
|
public static final String URL_DELETE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={0}&userid={1}";
|
/**链接-成员详情*/
|
public static final String URL_INFO_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}";
|
/**发送消息*/
|
public static final String URL_MESSAGE_SEND = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
|
|
|
/**
|
* 添加工作人员
|
* @param at 访问at
|
* @param userId 用户id
|
* @param name 名称
|
* @param depId 部门id
|
* @param position 职位
|
* @param tel 电话
|
* @param email 邮箱
|
* @return 返回
|
*/
|
public static int addUser(String at, String userId, String name, int depId
|
, String position,String tel,String email) {
|
|
if(StringUtils.isEmpty(tel)&&StringUtils.isEmpty(email)){
|
throw new TipsException("添加企业微信手机号和邮箱不能都为空!");
|
}
|
|
JSONObject obj = new JSONObject();
|
obj.put("userid", userId);
|
obj.put("name", name);
|
obj.put("department", depId);
|
obj.put("position", position);
|
obj.put("mobile", tel);
|
obj.put("email", email);
|
obj = HttpURLUtil(MessageFormat.format(URL_ADD_USER, at), obj.toString());
|
if(obj != null) {
|
return obj.optInt("errcode", -1);
|
}
|
return -1;
|
}
|
|
/**
|
* 添加工作人员
|
* @param at 访问at
|
* @param userId 用户id
|
* @param name 名称
|
* @param depId 部门id
|
* @param position 职位
|
* @param tel 电话
|
* @param email 邮箱
|
* @return 返回
|
*/
|
public static JSONObject addUserObj(String at, String userId, String name, int depId
|
, String position,String tel,String email) {
|
|
if(StringUtils.isEmpty(tel)&&StringUtils.isEmpty(email)){
|
throw new TipsException("添加企业微信手机号和邮箱不能都为空!");
|
}
|
|
JSONObject obj = new JSONObject();
|
obj.put("userid", userId);
|
obj.put("name", name);
|
obj.put("department", depId);
|
obj.put("position", position);
|
obj.put("mobile", tel);
|
obj.put("email", email);
|
obj = HttpURLUtil(MessageFormat.format(URL_ADD_USER, at), obj.toString());
|
|
if(obj == null){
|
obj = new JSONObject();
|
}
|
|
return obj;
|
|
}
|
|
/**
|
* 更新工作人员
|
* @param at 访问at
|
* @param userId 用户id
|
* @param name 名称
|
* @param depId 部门id
|
* @param position 职位
|
* @param tel 电话
|
* @param email 邮箱
|
* @return 返回
|
*/
|
public static int updateUser(String at, String userId, String name, int depId
|
, String position,String tel,String email) {
|
JSONObject obj = new JSONObject();
|
obj.put("userid", userId);
|
obj.put("name", name);
|
obj.put("department", depId);
|
obj.put("position", position);
|
obj.put("mobile", tel);
|
obj.put("email", email);
|
|
obj = HttpURLUtil(MessageFormat.format(URL_UPDATE_USER, at), obj.toString());
|
if(obj != null && obj.optInt("errcode", -1) == 0) {
|
return obj.optInt("errcode", -1);
|
}
|
|
return -1;
|
}
|
|
/**
|
* 删除工作人员
|
* @param at 访问at
|
* @param userId 企业用户id
|
* @return 返回
|
*/
|
public static int deleteUser(String at, String userId) {
|
JSONObject obj = new JSONObject();
|
obj.put("userid", userId);
|
|
obj = HttpURLUtil(MessageFormat.format(URL_DELETE_USER, at,userId), obj.toString());
|
if(obj != null) {
|
return obj.optInt("errcode", -1);
|
}
|
return -1;
|
}
|
|
/**
|
* 工作人员判断是否存在企业微信
|
* @param at 访问at
|
* @param userId 用户id
|
* @return 返回
|
*/
|
public static int infoUser(String at, String userId) {
|
JSONObject obj = new JSONObject();
|
obj.put("userid", userId);
|
|
obj = HttpURLUtil(MessageFormat.format(URL_INFO_USER, at,userId), obj.toString());
|
|
if(obj != null) {
|
return obj.optInt("errcode", -1);
|
}
|
return -1;
|
}
|
|
/**
|
* 获取企业用户信息
|
* @param at 访问at
|
* @param userId 用户id
|
* @return 返回
|
*/
|
public static JSONObject userData(String at, String userId) {
|
JSONObject obj = new JSONObject();
|
obj.put("userid", userId);
|
|
return obj = HttpURLUtil(MessageFormat.format(URL_INFO_USER, at,userId), obj.toString());
|
}
|
|
|
/**
|
* 添加部门
|
* @param name 部门名称
|
* @return id
|
*/
|
public static int addDepartment(String at, String name, int parentId) {
|
JSONObject obj = new JSONObject();
|
obj.put("name", name);
|
obj.put("parentid", parentId);
|
|
obj = HttpURLUtil(MessageFormat.format(URL_ADD_DEPARTMENT, at), obj.toString());
|
|
if(obj != null && obj.optInt("errcode", -1) == 0) {
|
return obj.optInt("id", -1);
|
}
|
return -1;
|
}
|
|
/**
|
* 更新部门
|
* @param name 部门名称
|
* @param deaprtId 部门id
|
* @param parentid 父类部门id,可以为空
|
* @return 状态,0成功
|
*/
|
public static int updateDepartment(String at, String name, int deaprtId,int parentid) {
|
JSONObject obj = new JSONObject();
|
obj.put("name", name);
|
obj.put("id", deaprtId);
|
obj.put("parentid", parentid);
|
|
obj = HttpURLUtil(MessageFormat.format(URL_UPDATE_DEPARTMENT, at), obj.toString());
|
return obj.optInt("errcode", -1);
|
}
|
|
/**删除部门
|
* 删除的部门不能是根部门且没有成员
|
* @param deaprtId 部门id
|
* @return 状态,0成功
|
*/
|
public static int deleteDepartment(String at,int deaprtId) {
|
JSONObject obj = new JSONObject();
|
obj.put("id", deaprtId);
|
|
obj = HttpURLUtil(MessageFormat.format(URL_DELETE_DEPARTMENT,at,deaprtId), obj.toString());
|
System.out.println("删除部门:"+obj.toString());
|
return obj.optInt("errcode", -1);
|
}
|
|
/**发送企业应用消息
|
* @param accessToken 企业accessToken
|
* @param data 发送数据结构
|
* @return
|
*/
|
public static JSONObject messageSend(String accessToken,String data){
|
return HttpURLUtil(URL_MESSAGE_SEND+accessToken,data);
|
}
|
|
/**添加用户标签,标签名称不能重复
|
* @param at 访问at
|
* @param label 标签名称
|
* @return 返回 -1失败,0成功,1已存在
|
*/
|
public static int addLabel(String at, String label) {
|
String url = MessageFormat.format(URL_ADD_LABEL, at);
|
JSONObject obj = new JSONObject();
|
obj.put("tagname", label);
|
obj = HttpURLUtil(url, obj.toString());
|
System.out.println("obj....:"+obj.toString());
|
if(obj != null ) {
|
int errcode = obj.optInt("errcode", -1);
|
if( errcode== 0){
|
//上传成功
|
return obj.optInt("tagid", -1);
|
}if(errcode == 40071){
|
//标签已经存在
|
return 1;
|
}
|
}
|
return -1;
|
}
|
|
|
/**
|
* 获取应用accessToken
|
* @param corpId 企业id
|
* @param appSecret 应用密钥
|
* @return
|
* @throws Exception
|
*/
|
public static JSONObject getApplicationAccessToken(String corpId, String appSecret) {
|
String url = MessageFormat.format(URL_GET_ACCESS_TOKEN, corpId, appSecret);
|
JSONObject obj = HttpURLUtil(url, null);
|
return obj;
|
}
|
|
/**
|
* 使用临时登录凭证code获取 session_key、用户userId以及用户所在企业的corpId等信息
|
* @param accessToken 访问token
|
* @param code 临时code
|
* @return
|
*/
|
public static JSONObject code2Session(String accessToken, String code) {
|
String url = MessageFormat.format(URL_CODE_2_SESSION, accessToken, code);
|
JSONObject obj = HttpURLUtil(url, null);
|
return obj;
|
}
|
|
|
|
/** 请求http协议 获取信息工具 **/
|
public static JSONObject HttpURLUtil(String url, String data) {
|
HttpURLConnection con = null;
|
URL u = null;
|
String wxMsgXml = null;
|
JSONObject obj = null;
|
try {
|
u = new URL(url);
|
con = (HttpURLConnection) u.openConnection();
|
con.setRequestMethod("POST");
|
con.setDoOutput(true);
|
con.setDoInput(true);
|
con.setUseCaches(false);
|
con.setReadTimeout(5000);
|
con.setRequestProperty("Charset", "UTF-8");
|
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
if (data != null) {
|
OutputStream os = con.getOutputStream();
|
os.write(data.getBytes("utf-8"));
|
}
|
|
if (con.getResponseCode() != 200)
|
throw new RuntimeException("请求url失败");
|
// 读取返回内容
|
wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
|
obj = JSONObject.fromObject(wxMsgXml);
|
// //System.out.println("HttpURLUtil:"+wxMsgXml);
|
} catch (Exception e) {
|
e.printStackTrace();
|
obj = new JSONObject();
|
try {
|
obj.put("status", 1);
|
obj.put("errMsg", e.getMessage());
|
} catch (JSONException e1) {
|
e1.printStackTrace();
|
}
|
} finally {
|
if (con != null) {
|
con.disconnect();
|
}
|
}
|
return obj;
|
}
|
|
}
|