该类存放接口所用到的企业微信的接口地址
package com.common;
public final class QyapiUrl {
/**
* 请求nginx转发服务器地址
*/
public static String RELAY_IP = "http://127.0.0.1:8081";
/**
* 获取access_token的url
*/
public static String GETTOKEN = RELAY_IP + "/cgi-bin/gettoken";
/**
* 获取应用信息
*/
public static String AGENT_GET = RELAY_IP + "/cgi-bin/agent/get";
/**
* 获取标签信息
*/
public static String TAG_GET = RELAY_IP + "/cgi-bin/tag/list";
/**
* 获取部门信息
*/
public static String DEPARTMENT_GET = RELAY_IP + "/cgi-bin/department/list";
/**
* 创建菜单
*/
public static String CREATEWECHAT_POST = RELAY_IP + "/cgi-bin/menu/create";
/**
* 删除菜单
*/
public static String DELETEWECHAT_GET=RELAY_IP+"/cgi-bin/menu/delete";
/**
* 发送信息
*/
public static String MESSAGE_SEND=RELAY_IP+"/cgi-bin/message/send";
/**
* 上传图片到企业微信
*/
public static String MEDIA_UPLOAD=RELAY_IP+"/cgi-bin/media/upload";
}
企业微信消息推送接口
package com.platform.service.WxCp;
import com.alibaba.fastjson.JSONObject;
import com.base.entity.sitepermissions.SitePermissionsEntity;
import com.base.entity.temp.ArticleEntity;
import com.base.entity.temp.ContentsEntity;
import com.base.entity.wxCp.WxCpEntity;
import com.base.entity.wxCp.WxCpMessage;
import com.core.service.CommonService;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;
/**
* 企业微信管理接口
*/
public interface WxCpServiceI extends CommonService {
/**
* 建立连接
* @param corpid 公司id
* @param corpsecret 企业应用私钥
* return access_tocken
*/
public JSONObject getAccessToken(String corpid, String corpsecret) throws IOException;
/**
* 请求企业微信服务器
* @param url 请求地址
* return JsonObject
*/
public JSONObject wxCpUtil(String url) throws IOException;
/**
* 获取列表通过参数
* @param enterprise_wechat_name 企业微信名称
* @param enterprise_wechat_apply 企业应用名称
* return map
*/
public Map getWxCpParam(String enterprise_wechat_name,String enterprise_wechat_apply,Integer pageSize,Integer pageNo,String siteId);
/**
* 获取企业应用名称
* @param access_token 调用接口凭证
* @param agentid 应用id
* return jsonObject
*/
public JSONObject getWxCpAgent(String access_token,String agentid) throws IOException;
/**
* 获取企业应用对象
* @param enterprise_wechat_id
* @param enterprise_wechat_applyId
* return List
*/
public List
企业微信消息推送接口实现类
package com.platform.service.impl.WxCp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.entity.sitepermissions.SitePermissionsEntity;
import com.base.entity.temp.ArticleEntity;
import com.base.entity.temp.ContentsEntity;
import com.base.entity.wxCp.WxCpEntity;
import com.base.entity.wxCp.WxCpMessage;
import com.common.QyapiUrl;
import com.common.hibernate.qbc.CriteriaQuery;
import com.common.hibernate.qbc.PageList;
import com.core.service.CommonService;
import com.core.service.impl.CommonServiceImpl;
import com.core.util.StringUtils;
import com.platform.service.WxCp.WxCpServiceI;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author
*/
@Service("wxCpService")
@Transactional
public class WxCpServiceImpI extends CommonServiceImpl implements WxCpServiceI{
private static final Logger logger = Logger.getLogger(WxCpServiceImpI.class);
@Autowired
private CommonService commonService;
/**
* 建立连接
* @param corpid 公司id
* @param corpsecret 企业应用私钥
* return JsonObject
*/
@Override
public JSONObject getAccessToken(String corpid,String corpsecret) throws IOException {
String url = QyapiUrl.GETTOKEN + "?corpid=" + corpid + "&corpsecret=" + corpsecret;
JSONObject jsonObject = this.wxCpUtil(url);
return jsonObject;
}
/**
* 获取标签集合
* @param access_token 调用接口凭证
* return jsonObject
*/
@Override
public JSONObject getWxCpTagList(String access_token) throws IOException{
String url = QyapiUrl.TAG_GET + "?access_token=" + access_token;
JSONObject jsonObject = this.wxCpUtil(url);
return jsonObject;
}
/**
* 获取部门集合
* @param access_token 调用接口凭证
* return jsonObject
*/
@Override
public JSONObject getWxCpDepartmentList(String access_token) throws IOException{
String url = QyapiUrl.DEPARTMENT_GET + "?access_token=" + access_token;
JSONObject jsonObject = this.wxCpUtil(url);
return jsonObject;
}
/**
* 上传临时图片到企业微信服务器
* @param access_token 调用接口凭证
* @param filePath 图片路径
* return JsonObject
*/
@Override
public JSONObject uploadTemp(String access_token,String filePath) throws IOException{
String url = QyapiUrl.MEDIA_UPLOAD + "?access_token=" + access_token +"&type=image";
JSONObject jsonObject = new JSONObject();
HttpPost post = new HttpPost(url);
File file = new File(filePath);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpEntity entity = null;
HttpResponse response = null;
String BoundaryStr = "------------7da2e536604c8";
post.addHeader("Connection", "keep-alive");
post.addHeader("Accept", "*/*");
post.addHeader("Content-Type", "multipart/form-data;boundary=" + BoundaryStr);
post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
meb.setBoundary(BoundaryStr).setCharset(Charset.forName("utf-8")).setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
meb.addBinaryBody("media", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
entity = meb.build();
post.setEntity(entity);
response = httpclient.execute(post);
entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);// 关闭流
System.out.println(result);
jsonObject = JSONObject.parseObject(result);
System.out.println(jsonObject.toString());
return jsonObject;
}
/**
* httpclient请求企业微信服务器
* @param url 请求地址
* return JsonObject
*/
@Override
public JSONObject wxCpUtil(String url){
JSONObject jsonResult = null;
try{
url = URLDecoder.decode(url, "UTF-8");
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(response.getEntity());
jsonResult = JSONObject.parseObject(strResult);
}else{
logger.error("请求提交失败:"+url);
}
}catch(Exception e){
logger.error("请求提交失败:"+url,e);
}
/*URL u = new URL(url);
HttpsURLConnection huconn = (HttpsURLConnection) u.openConnection();
BufferedReader in = null;
StringBuilder result = new StringBuilder();
huconn.connect();
in = new BufferedReader(new InputStreamReader(huconn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
if (in != null) {
in.close();
}
JSONObject myJsonObject = new JSONObject();
myJsonObject = JSONObject.parseObject(result.toString());
return myJsonObject;*/
return jsonResult;
}
/**
* 获取参数
* @param enterprise_wechat_name 企业微信名称
* @param enterprise_wechat_apply 企业应用名称
* return map
*/
@Override
public Map getWxCpParam(String enterprise_wechat_name, String enterprise_wechat_apply, Integer pageSize, Integer pageNo,String siteId){
CriteriaQuery cq = new CriteriaQuery(WxCpEntity.class, pageSize, pageNo, "", "");
if(StringUtils.isNotBlank(enterprise_wechat_name)){
cq.like("enterprise_wechat_name", "%"+enterprise_wechat_name+"%");//微信名称查询
}
if(StringUtils.isNotBlank(enterprise_wechat_apply)){
cq.like("enterprise_wechat_apply", "%"+enterprise_wechat_apply+"%");//微信应用名称查询
}
/*if(StringUtils.isNotBlank(wxcp_status)){
cq.eq("wxcp_status",Integer.valueOf(wxcp_status));
}*/
if(StringUtils.isNotBlank(siteId)){
//创建关联站点表引用
cq.createAlias("siteEntity","se");
cq.eq("se.id",siteId);
}
cq.add();
PageList pageList = this.getPageList(cq, true);
int pageCount = (int) Math.ceil((double) pageList.getCount() / (double) pageSize);
if (pageCount <= 0) {
pageCount = 1;
}
List wxCpList = pageList.getResultList();
Map map = new HashedMap();
map.put("wxCpList",wxCpList);
map.put("pageCount",pageCount);
return map;
}
/**
* 获取企业应用信息
* @param access_token 调用接口凭证
* @param agentid 应用id
* return jsonObject
*/
@Override
public JSONObject getWxCpAgent(String access_token,String agentid) throws IOException{
String url = QyapiUrl.AGENT_GET + "?access_token="+access_token+"&agentid="+agentid;
JSONObject jsonObject = this.wxCpUtil(url);
return jsonObject;
}
/**
* 接收发送消息返回结果
* @param access_token 调用接口凭证
* @param message 消息实体类
*/
@Override
public JSONObject getSendResponse(String access_token, WxCpMessage message) throws IOException{
String url = QyapiUrl.MESSAGE_SEND + "?access_token="+access_token;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(message.toJson(), "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httpPost);
String resp;
JSONObject jsonObject = new JSONObject();
try {
HttpEntity entity = response.getEntity();
resp = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity);
} finally {
response.close();
}
jsonObject = JSONObject.parseObject(resp);
return jsonObject;
}
/**
* 获取企业应用对象
* @param enterprise_wechat_id
* @param enterprise_wechat_applyId
* return List
*/
@Override
public List