规范化并优化代码

This commit is contained in:
Binary Wang 2019-06-02 12:19:18 +08:00
parent 3465d53880
commit f67333a06a
30 changed files with 534 additions and 535 deletions

View File

@ -15,6 +15,9 @@ import java.util.List;
* @author <a href="https://github.com/huansinho">huansinho</a> * @author <a href="https://github.com/huansinho">huansinho</a>
*/ */
public interface WxCpAgentService { public interface WxCpAgentService {
String GET_AGENT = "https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=%d";
String AGENT_SET = "https://qyapi.weixin.qq.com/cgi-bin/agent/set";
String AGENT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/agent/list";
/** /**
* <pre> * <pre>

View File

@ -1,11 +1,11 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;
import java.util.List;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpAppChatMessage; import me.chanjar.weixin.cp.bean.WxCpAppChatMessage;
import me.chanjar.weixin.cp.bean.WxCpChat; import me.chanjar.weixin.cp.bean.WxCpChat;
import java.util.List;
/** /**
* 群聊服务. * 群聊服务.
* *
@ -15,6 +15,10 @@ public interface WxCpChatService {
String APPCHAT_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create"; String APPCHAT_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create";
String APPCHAT_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update"; String APPCHAT_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update";
String APPCHAT_GET_CHATID = "https://qyapi.weixin.qq.com/cgi-bin/appchat/get?chatid="; String APPCHAT_GET_CHATID = "https://qyapi.weixin.qq.com/cgi-bin/appchat/get?chatid=";
String APPCHAT_SEND = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send";
@Deprecated
String chatCreate(String name, String owner, List<String> users, String chatId) throws WxErrorException;
/** /**
* 创建群聊会话注意刚创建的群如果没有下发消息在企业微信不会出现该群. * 创建群聊会话注意刚创建的群如果没有下发消息在企业微信不会出现该群.
@ -24,15 +28,13 @@ public interface WxCpChatService {
* @param users 群成员id列表至少2人至多500人 * @param users 群成员id列表至少2人至多500人
* @param chatId 群聊的唯一标志不能与已有的群重复字符串类型最长32个字符只允许字符0-9及字母a-zA-Z如果不填系统会随机生成群id * @param chatId 群聊的唯一标志不能与已有的群重复字符串类型最长32个字符只允许字符0-9及字母a-zA-Z如果不填系统会随机生成群id
* @return 创建的群聊会话chatId * @return 创建的群聊会话chatId
* @throws WxErrorException 发生异常 * @throws WxErrorException 异常
*/
String chatCreate(String name, String owner, List<String> users, String chatId) throws WxErrorException;
/**
* chatCreate 同名方法
*/ */
String create(String name, String owner, List<String> users, String chatId) throws WxErrorException; String create(String name, String owner, List<String> users, String chatId) throws WxErrorException;
@Deprecated
void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException;
/** /**
* 修改群聊会话. * 修改群聊会话.
* *
@ -41,26 +43,19 @@ public interface WxCpChatService {
* @param owner 新群主的id若不需更新请忽略此参数null or empty * @param owner 新群主的id若不需更新请忽略此参数null or empty
* @param usersToAdd 添加成员的id列表若不需要更新则传递空对象或者空集合 * @param usersToAdd 添加成员的id列表若不需要更新则传递空对象或者空集合
* @param usersToDelete 踢出成员的id列表若不需要更新则传递空对象或者空集合 * @param usersToDelete 踢出成员的id列表若不需要更新则传递空对象或者空集合
* @throws WxErrorException 发生异常 * @throws WxErrorException 异常
*/
void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException;
/**
* chatUpdate 同名方法
*/ */
void update(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException; void update(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException;
@Deprecated
WxCpChat chatGet(String chatId) throws WxErrorException;
/** /**
* 获取群聊会话. * 获取群聊会话.
* *
* @param chatId 群聊编号 * @param chatId 群聊编号
* @return 群聊会话 * @return 群聊会话
* @throws WxErrorException 发生异常 * @throws WxErrorException 异常
*/
WxCpChat chatGet(String chatId) throws WxErrorException;
/**
* chatGet 同名方法
*/ */
WxCpChat get(String chatId) throws WxErrorException; WxCpChat get(String chatId) throws WxErrorException;
@ -71,6 +66,7 @@ public interface WxCpChatService {
* 文档地址https://work.weixin.qq.com/api/doc#90000/90135/90248 * 文档地址https://work.weixin.qq.com/api/doc#90000/90135/90248
* *
* @param message 要发送的消息内容对象 * @param message 要发送的消息内容对象
* @throws WxErrorException 异常
*/ */
void sendMsg(WxCpAppChatMessage message) throws WxErrorException; void sendMsg(WxCpAppChatMessage message) throws WxErrorException;

View File

@ -14,6 +14,10 @@ import me.chanjar.weixin.cp.bean.WxCpDepart;
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public interface WxCpDepartmentService { public interface WxCpDepartmentService {
String DEPARTMENT_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
String DEPARTMENT_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/department/update";
String DEPARTMENT_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=%d";
String DEPARTMENT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
/** /**
* <pre> * <pre>

View File

@ -12,6 +12,10 @@ import me.chanjar.weixin.common.error.WxErrorException;
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public interface WxCpMenuService { public interface WxCpMenuService {
String MENU_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=%d";
String MENU_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=%d";
String MENU_GET = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=%d";
/** /**
* <pre> * <pre>
* 自定义菜单创建接口 * 自定义菜单创建接口

View File

@ -1,66 +0,0 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpApprovalDataResult;
import me.chanjar.weixin.cp.bean.WxCpCheckinData;
import me.chanjar.weixin.cp.bean.WxCpCheckinOption;
import me.chanjar.weixin.cp.bean.WxCpDialRecord;
import java.util.Date;
import java.util.List;
/**
* @author Element
* @Package me.chanjar.weixin.cp.api
* @date 2019-04-06 10:52
* @Description: <pre>
* 企业微信OA相关接口
*
* </pre>
*/
public interface WxCpOAService {
/**
* <pre>
* 获取打卡数据
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/90262
* </pre>
*
* @param openCheckinDataType 打卡类型1上下班打卡2外出打卡3全部打卡
* @param starttime 获取打卡记录的开始时间
* @param endtime 获取打卡记录的结束时间
* @param userIdList 需要获取打卡记录的用户列表
*/
List<WxCpCheckinData> getCheckinData(Integer openCheckinDataType, Date starttime, Date endtime, List<String> userIdList) throws WxErrorException;
/**
* <pre>
* 获取打卡规则
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/90263
* </pre>
*
* @param datetime 需要获取规则的当天日期
* @param userIdList 需要获取打卡规则的用户列表
* @return
* @throws WxErrorException
*/
List<WxCpCheckinOption> getCheckinOption(Date datetime, List<String> userIdList) throws WxErrorException;
/**
* <pre>
* 获取审批数据
* 通过本接口来获取公司一段时间内的审批记录一次拉取调用最多拉取10000个审批记录可以通过多次拉取的方式来满足需求但调用频率不可超过600次/
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/91530
* </pre>
*
* @param starttime 获取审批记录的开始时间
* @param endtime 获取审批记录的结束时间
* @param nextSpnum 第一个拉取的审批单号不填从该时间段的第一个审批单拉取
* @return
* @throws WxErrorException
*/
WxCpApprovalDataResult getApprovalData(Date starttime, Date endtime, Long nextSpnum) throws WxErrorException;
List<WxCpDialRecord> getDialRecord(Date starttime, Date endtime, Integer offset, Integer limit) throws WxErrorException;
}

View File

@ -0,0 +1,68 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpApprovalDataResult;
import me.chanjar.weixin.cp.bean.WxCpCheckinData;
import me.chanjar.weixin.cp.bean.WxCpCheckinOption;
import me.chanjar.weixin.cp.bean.WxCpDialRecord;
import java.util.Date;
import java.util.List;
/**
* 企业微信OA相关接口.
*
* @author Element
* @date 2019-04-06 10:52
*/
public interface WxCpOaService {
String GET_CHECKIN_DATA = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata";
String GET_CHECKIN_OPTION = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckinoption";
String GET_APPROVAL_DATA = "https://qyapi.weixin.qq.com/cgi-bin/corp/getapprovaldata";
String GET_DIAL_RECORD = "https://qyapi.weixin.qq.com/cgi-bin/dial/get_dial_record";
/**
* <pre>
* 获取打卡数据
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/90262
* </pre>
*
* @param openCheckinDataType 打卡类型1上下班打卡2外出打卡3全部打卡
* @param startTime 获取打卡记录的开始时间
* @param endTime 获取打卡记录的结束时间
* @param userIdList 需要获取打卡记录的用户列表
* @return 打卡数据列表
* @throws WxErrorException 异常
*/
List<WxCpCheckinData> getCheckinData(Integer openCheckinDataType, Date startTime, Date endTime, List<String> userIdList) throws WxErrorException;
/**
* <pre>
* 获取打卡规则
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/90263
* </pre>
*
* @param datetime 需要获取规则的当天日期
* @param userIdList 需要获取打卡规则的用户列表
* @return 打卡规则列表
* @throws WxErrorException 异常
*/
List<WxCpCheckinOption> getCheckinOption(Date datetime, List<String> userIdList) throws WxErrorException;
/**
* <pre>
* 获取审批数据
* 通过本接口来获取公司一段时间内的审批记录一次拉取调用最多拉取10000个审批记录可以通过多次拉取的方式来满足需求但调用频率不可超过600次/
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/91530
* </pre>
*
* @param startTime 获取审批记录的开始时间
* @param endTime 获取审批记录的结束时间
* @param nextSpnum 第一个拉取的审批单号不填从该时间段的第一个审批单拉取
* @throws WxErrorException 异常
*/
WxCpApprovalDataResult getApprovalData(Date startTime, Date endTime, Long nextSpnum) throws WxErrorException;
List<WxCpDialRecord> getDialRecord(Date startTime, Date endTime, Integer offset, Integer limit) throws WxErrorException;
}

View File

@ -13,7 +13,7 @@ import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
/** /**
* 微信API的Service * 微信API的Service.
* @author chanjaster * @author chanjaster
*/ */
public interface WxCpService { public interface WxCpService {
@ -25,6 +25,7 @@ public interface WxCpService {
String BATCH_REPLACE_USER = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser"; String BATCH_REPLACE_USER = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser";
String BATCH_GET_RESULT = "https://qyapi.weixin.qq.com/cgi-bin/batch/getresult?jobid="; String BATCH_GET_RESULT = "https://qyapi.weixin.qq.com/cgi-bin/batch/getresult?jobid=";
String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session"; String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session";
String GET_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=%s&corpsecret=%s";
/** /**
* <pre> * <pre>
@ -310,7 +311,7 @@ public interface WxCpService {
WxCpAgentService getAgentService(); WxCpAgentService getAgentService();
WxCpOAService getOAService(); WxCpOaService getOAService();
/** /**
* http请求对象 * http请求对象

View File

@ -17,6 +17,14 @@ import java.util.List;
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public interface WxCpTagService { public interface WxCpTagService {
String TAG_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
String TAG_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/tag/update";
String TAG_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?tagid=%s";
String TAG_LIST = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
String TAG_GET = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=%s";
String TAG_ADDTAGUSERS = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers";
String TAG_DELTAGUSERS = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers";
/** /**
* 创建标签. * 创建标签.
* *
@ -51,6 +59,14 @@ public interface WxCpTagService {
*/ */
List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException; List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException;
/**
* 获取标签成员.
* 对应: http://qydev.weixin.qq.com/wiki/index.php?title=管理标签 中的get接口
*
* @param tagId 标签id
*/
WxCpTagGetResult get(String tagId) throws WxErrorException;
/** /**
* 增加标签成员. * 增加标签成员.
* *
@ -69,13 +85,4 @@ public interface WxCpTagService {
*/ */
WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException; WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException;
/**
* 获取标签成员.
* 对应: http://qydev.weixin.qq.com/wiki/index.php?title=管理标签 中的get接口
*
* @param tagId 标签id
*/
WxCpTagGetResult get(String tagId) throws WxErrorException;
} }

View File

@ -14,6 +14,8 @@ import java.util.List;
* @date 2019-05-16 * @date 2019-05-16
*/ */
public interface WxCpTaskCardService { public interface WxCpTaskCardService {
String MESSAGE_UPDATE_TASKCARD = "https://qyapi.weixin.qq.com/cgi-bin/message/update_taskcard";
/** /**
* <pre> * <pre>
* 更新任务卡片消息状态 * 更新任务卡片消息状态

View File

@ -1,169 +1,173 @@
package me.chanjar.weixin.cp.api; package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
import me.chanjar.weixin.cp.bean.WxCpTpCorp; import me.chanjar.weixin.cp.bean.WxCpTpCorp;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
/**
/** * 微信第三方应用API的Service.
* 微信第三方应用API的Service *
* @author zhenjun cai * @author zhenjun cai
*/ */
public interface WxCpTpService { public interface WxCpTpService {
String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/service/miniprogram/jscode2session"; String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/service/miniprogram/jscode2session";
String GET_CORP_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token"; String GET_CORP_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token";
String GET_PERMANENT_CODE = "https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code"; String GET_PERMANENT_CODE = "https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code";
/** String GET_SUITE_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token";
* <pre>
* 验证推送过来的消息的正确性 /**
* 详情请见: https://work.weixin.qq.com/api/doc#90000/90139/90968/消息体签名校验 * <pre>
* </pre> * 验证推送过来的消息的正确性
* * 详情请见: https://work.weixin.qq.com/api/doc#90000/90139/90968/消息体签名校验
* @param msgSignature 消息签名 * </pre>
* @param timestamp 时间戳 *
* @param nonce 随机数 * @param msgSignature 消息签名
* @param data 微信传输过来的数据有可能是echoStr有可能是xml消息 * @param timestamp 时间戳
*/ * @param nonce 随机数
boolean checkSignature(String msgSignature, String timestamp, String nonce, String data); * @param data 微信传输过来的数据有可能是echoStr有可能是xml消息
*/
/** boolean checkSignature(String msgSignature, String timestamp, String nonce, String data);
* 获取suite_access_token, 不强制刷新suite_access_token
* /**
* @see #getSuiteAccessToken(boolean) * 获取suite_access_token, 不强制刷新suite_access_token
*/ *
String getSuiteAccessToken() throws WxErrorException; * @see #getSuiteAccessToken(boolean)
*/
/** String getSuiteAccessToken() throws WxErrorException;
* <pre>
* 获取suite_access_token本方法线程安全 /**
* 且在多线程同时刷新时只刷新一次避免超出2000次/日的调用次数上限 * <pre>
* 本service的所有方法都会在suite_access_token过期是调用此方法 * 获取suite_access_token本方法线程安全
* 程序员在非必要情况下尽量不要主动调用此方法 * 且在多线程同时刷新时只刷新一次避免超出2000次/日的调用次数上限
* 详情请见: https://work.weixin.qq.com/api/doc#90001/90143/90600 * 本service的所有方法都会在suite_access_token过期是调用此方法
* </pre> * 程序员在非必要情况下尽量不要主动调用此方法
* * 详情请见: https://work.weixin.qq.com/api/doc#90001/90143/90600
* @param forceRefresh 强制刷新 * </pre>
*/ *
String getSuiteAccessToken(boolean forceRefresh) throws WxErrorException; * @param forceRefresh 强制刷新
*/
/** String getSuiteAccessToken(boolean forceRefresh) throws WxErrorException;
* 获得suite_ticket,不强制刷新suite_ticket
* /**
* @see #getSuiteTicket(boolean) * 获得suite_ticket,不强制刷新suite_ticket
*/ *
String getSuiteTicket() throws WxErrorException; * @see #getSuiteTicket(boolean)
*/
/** String getSuiteTicket() throws WxErrorException;
* <pre>
* 获得suite_ticket /**
* 由于suite_ticket是微信服务器定时推送每10分钟不能主动获取如果碰到过期只能抛异常 * <pre>
* * 获得suite_ticket
* 详情请见https://work.weixin.qq.com/api/doc#90001/90143/90628 * 由于suite_ticket是微信服务器定时推送每10分钟不能主动获取如果碰到过期只能抛异常
* </pre> *
* * 详情请见https://work.weixin.qq.com/api/doc#90001/90143/90628
* @param forceRefresh 强制刷新 * </pre>
*/ *
String getSuiteTicket(boolean forceRefresh) throws WxErrorException; * @param forceRefresh 强制刷新
*/
/** String getSuiteTicket(boolean forceRefresh) throws WxErrorException;
* 小程序登录凭证校验
* /**
* @param jsCode 登录时获取的 code * 小程序登录凭证校验
*/ *
WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException; * @param jsCode 登录时获取的 code
*/
/** WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException;
* 获取企业凭证
* @param authCorpid 授权方corpid /**
* @param permanentCode 永久授权码通过get_permanent_code获取 * 获取企业凭证
*/ *
WxAccessToken getCorpToken(String authCorpid, String permanentCode) throws WxErrorException; * @param authCorpid 授权方corpid
* @param permanentCode 永久授权码通过get_permanent_code获取
/** */
* 获取企业永久授权码 WxAccessToken getCorpToken(String authCorpid, String permanentCode) throws WxErrorException;
* @param authCode
* @return /**
*/ * 获取企业永久授权码
WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException; *
* @param authCode
/** * @return
* 当本Service没有实现某个API的时候可以用这个针对所有微信API中的GET请求 */
* WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException;
* @param url 接口地址
* @param queryParam 请求参数 /**
*/ * 当本Service没有实现某个API的时候可以用这个针对所有微信API中的GET请求
String get(String url, String queryParam) throws WxErrorException; *
* @param url 接口地址
/** * @param queryParam 请求参数
* 当本Service没有实现某个API的时候可以用这个针对所有微信API中的POST请求 */
* String get(String url, String queryParam) throws WxErrorException;
* @param url 接口地址
* @param postData 请求body字符串 /**
*/ * 当本Service没有实现某个API的时候可以用这个针对所有微信API中的POST请求
String post(String url, String postData) throws WxErrorException; *
* @param url 接口地址
/** * @param postData 请求body字符串
* <pre> */
* Service没有实现某个API的时候可以用这个 String post(String url, String postData) throws WxErrorException;
* {@link #get}{@link #post}方法更灵活可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型
* 可以参考{@link MediaUploadRequestExecutor}的实现方法 /**
* </pre> * <pre>
* * Service没有实现某个API的时候可以用这个
* @param executor 执行器 * {@link #get}{@link #post}方法更灵活可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型
* @param uri 请求地址 * 可以参考{@link MediaUploadRequestExecutor}的实现方法
* @param data 参数 * </pre>
* @param <T> 请求值类型 *
* @param <E> 返回值类型 * @param executor 执行器
*/ * @param uri 请求地址
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; * @param data 参数
* @param <T> 请求值类型
/** * @param <E> 返回值类型
* <pre> */
* 设置当微信系统响应系统繁忙时要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试 <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
* 默认1000ms
* </pre> /**
* * <pre>
* @param retrySleepMillis 重试休息时间 * 设置当微信系统响应系统繁忙时要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试
*/ * 默认1000ms
void setRetrySleepMillis(int retrySleepMillis); * </pre>
*
/** * @param retrySleepMillis 重试休息时间
* <pre> */
* 设置当微信系统响应系统繁忙时最大重试次数 void setRetrySleepMillis(int retrySleepMillis);
* 默认5次
* </pre> /**
* * <pre>
* @param maxRetryTimes 最大重试次数 * 设置当微信系统响应系统繁忙时最大重试次数
*/ * 默认5次
void setMaxRetryTimes(int maxRetryTimes); * </pre>
*
/** * @param maxRetryTimes 最大重试次数
* 初始化http请求对象 */
*/ void setMaxRetryTimes(int maxRetryTimes);
void initHttp();
/**
/** * 初始化http请求对象
* 获取WxMpConfigStorage 对象 */
* void initHttp();
* @return WxMpConfigStorage
*/ /**
WxCpTpConfigStorage getWxCpTpConfigStorage(); * 获取WxMpConfigStorage 对象
*
/** * @return WxMpConfigStorage
* 注入 {@link WxCpTpConfigStorage} 的实现 */
* WxCpTpConfigStorage getWxCpTpConfigStorage();
* @param wxConfigProvider 配置对象
*/ /**
void setWxCpTpConfigStorage(WxCpTpConfigStorage wxConfigProvider); * 注入 {@link WxCpTpConfigStorage} 的实现
*
/** * @param wxConfigProvider 配置对象
* http请求对象 */
*/ void setWxCpTpConfigStorage(WxCpTpConfigStorage wxConfigProvider);
RequestHttp<?, ?> getRequestHttp();
/**
} * http请求对象
*/
RequestHttp<?, ?> getRequestHttp();
}

View File

@ -17,6 +17,19 @@ import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo;
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public interface WxCpUserService { public interface WxCpUserService {
String URL_AUTHENTICATE = "https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?userid=";
String URL_USER_CREATE = "https://qyapi.weixin.qq.com/cgi-bin/user/create";
String URL_USER_UPDATE = "https://qyapi.weixin.qq.com/cgi-bin/user/update";
String URL_USER_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?userid=";
String URL_USER_BATCH_DELETE = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete";
String URL_USER_GET = "https://qyapi.weixin.qq.com/cgi-bin/user/get?userid=";
String URL_USER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/list?department_id=";
String URL_USER_SIMPLE_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=";
String URL_BATCH_INVITE = "https://qyapi.weixin.qq.com/cgi-bin/batch/invite";
String URL_CONVERT_TO_OPENID = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid";
String URL_CONVERT_TO_USERID = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid";
String URL_GET_EXTERNAL_CONTACT = "https://qyapi.weixin.qq.com/cgi-bin/crm/get_external_contact?external_userid=";
/** /**
* <pre> * <pre>
* 用在二次验证的时候. * 用在二次验证的时候.

View File

@ -45,7 +45,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this); private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this);
private WxCpTagService tagService = new WxCpTagServiceImpl(this); private WxCpTagService tagService = new WxCpTagServiceImpl(this);
private WxCpAgentService agentService = new WxCpAgentServiceImpl(this); private WxCpAgentService agentService = new WxCpAgentServiceImpl(this);
private WxCpOAService oaService = new WxCpOAServiceImpl(this); private WxCpOaService oaService = new WxCpOaServiceImpl(this);
private WxCpTaskCardService taskCardService = new WxCpTaskCardServiceImpl(this); private WxCpTaskCardService taskCardService = new WxCpTaskCardServiceImpl(this);
/** /**
@ -389,7 +389,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
} }
@Override @Override
public WxCpOAService getOAService() { public WxCpOaService getOAService() {
return oaService; return oaService;
} }

View File

@ -37,15 +37,13 @@ public class WxCpAgentServiceImpl implements WxCpAgentService {
throw new IllegalArgumentException("缺少agentid参数"); throw new IllegalArgumentException("缺少agentid参数");
} }
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=" + agentId; String responseContent = this.mainService.get(String.format(WxCpAgentService.GET_AGENT, agentId), null);
String responseContent = this.mainService.get(url, null);
return WxCpAgent.fromJson(responseContent); return WxCpAgent.fromJson(responseContent);
} }
@Override @Override
public void set(WxCpAgent agentInfo) throws WxErrorException { public void set(WxCpAgent agentInfo) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set"; String responseContent = this.mainService.post(WxCpAgentService.AGENT_SET, agentInfo.toJson());
String responseContent = this.mainService.post(url, agentInfo.toJson());
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode").getAsInt() != 0) { if (jsonObject.get("errcode").getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent)); throw new WxErrorException(WxError.fromJson(responseContent));
@ -54,8 +52,7 @@ public class WxCpAgentServiceImpl implements WxCpAgentService {
@Override @Override
public List<WxCpAgent> list() throws WxErrorException { public List<WxCpAgent> list() throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list"; String responseContent = this.mainService.get(WxCpAgentService.AGENT_LIST, null);
String responseContent = this.mainService.get(url, null);
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode").getAsInt() != 0) { if (jsonObject.get("errcode").getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent)); throw new WxErrorException(WxError.fromJson(responseContent));

View File

@ -98,7 +98,7 @@ public class WxCpChatServiceImpl implements WxCpChatService {
@Override @Override
public void sendMsg(WxCpAppChatMessage message) throws WxErrorException { public void sendMsg(WxCpAppChatMessage message) throws WxErrorException {
this.cpService.post("https://qyapi.weixin.qq.com/cgi-bin/appchat/send", message.toJson()); this.cpService.post(WxCpChatService.APPCHAT_SEND, message.toJson());
} }
} }

View File

@ -29,27 +29,25 @@ public class WxCpDepartmentServiceImpl implements WxCpDepartmentService {
@Override @Override
public Long create(WxCpDepart depart) throws WxErrorException { public Long create(WxCpDepart depart) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create"; String responseContent = this.mainService.post(WxCpDepartmentService.DEPARTMENT_CREATE, depart.toJson());
String responseContent = this.mainService.post(url, depart.toJson());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("id")); return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("id"));
} }
@Override @Override
public void update(WxCpDepart group) throws WxErrorException { public void update(WxCpDepart group) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/update"; this.mainService.post(WxCpDepartmentService.DEPARTMENT_UPDATE, group.toJson());
this.mainService.post(url, group.toJson());
} }
@Override @Override
public void delete(Long departId) throws WxErrorException { public void delete(Long departId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" + departId; String url = String.format(WxCpDepartmentService.DEPARTMENT_DELETE, departId);
this.mainService.get(url, null); this.mainService.get(url, null);
} }
@Override @Override
public List<WxCpDepart> list(Long id) throws WxErrorException { public List<WxCpDepart> list(Long id) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; String url = WxCpDepartmentService.DEPARTMENT_LIST;
if (id != null) { if (id != null) {
url += "?id=" + id; url += "?id=" + id;
} }

View File

@ -8,10 +8,11 @@ import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
/** /**
* <pre> * <pre>
* 菜单管理相关接口 * 菜单管理相关接口.
* Created by Binary Wang on 2017-6-25. * Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre> * </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public class WxCpMenuServiceImpl implements WxCpMenuService { public class WxCpMenuServiceImpl implements WxCpMenuService {
private WxCpService mainService; private WxCpService mainService;
@ -27,8 +28,7 @@ public class WxCpMenuServiceImpl implements WxCpMenuService {
@Override @Override
public void create(Integer agentId, WxMenu menu) throws WxErrorException { public void create(Integer agentId, WxMenu menu) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=" + agentId; this.mainService.post(String.format(WxCpMenuService.MENU_CREATE, agentId), menu.toJson());
this.mainService.post(url, menu.toJson());
} }
@Override @Override
@ -38,7 +38,7 @@ public class WxCpMenuServiceImpl implements WxCpMenuService {
@Override @Override
public void delete(Integer agentId) throws WxErrorException { public void delete(Integer agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=" + agentId; String url = String.format(WxCpMenuService.MENU_DELETE, agentId);
this.mainService.get(url, null); this.mainService.get(url, null);
} }
@ -49,7 +49,7 @@ public class WxCpMenuServiceImpl implements WxCpMenuService {
@Override @Override
public WxMenu get(Integer agentId) throws WxErrorException { public WxMenu get(Integer agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + agentId; String url = String.format(WxCpMenuService.MENU_GET, agentId);
try { try {
String resultContent = this.mainService.get(url, null); String resultContent = this.mainService.get(url, null);
return WxCpGsonBuilder.create().fromJson(resultContent, WxMenu.class); return WxCpGsonBuilder.create().fromJson(resultContent, WxMenu.class);

View File

@ -6,7 +6,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpOAService; import me.chanjar.weixin.cp.api.WxCpOaService;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpApprovalDataResult; import me.chanjar.weixin.cp.bean.WxCpApprovalDataResult;
import me.chanjar.weixin.cp.bean.WxCpCheckinData; import me.chanjar.weixin.cp.bean.WxCpCheckinData;
@ -19,22 +19,19 @@ import java.util.List;
/** /**
* @author Element * @author Element
* @Package me.chanjar.weixin.cp.api.impl
* @date 2019-04-06 11:20 * @date 2019-04-06 11:20
* @Description: TODO
*/ */
public class WxCpOAServiceImpl implements WxCpOAService { public class WxCpOaServiceImpl implements WxCpOaService {
private WxCpService mainService; private WxCpService mainService;
public WxCpOAServiceImpl(WxCpService mainService) { public WxCpOaServiceImpl(WxCpService mainService) {
this.mainService = mainService; this.mainService = mainService;
} }
@Override @Override
public List<WxCpCheckinData> getCheckinData(Integer openCheckinDataType, Date starttime, Date endtime, List<String> userIdList) throws WxErrorException { public List<WxCpCheckinData> getCheckinData(Integer openCheckinDataType, Date startTime, Date endTime, List<String> userIdList) throws WxErrorException {
if (starttime == null || endtime == null) { if (startTime == null || endTime == null) {
throw new RuntimeException("starttime and endtime can't be null"); throw new RuntimeException("starttime and endtime can't be null");
} }
@ -42,15 +39,13 @@ public class WxCpOAServiceImpl implements WxCpOAService {
throw new RuntimeException("用户列表不能为空不超过100个若用户超过100个请分批获取"); throw new RuntimeException("用户列表不能为空不超过100个若用户超过100个请分批获取");
} }
long endtimestamp = endtime.getTime() / 1000L; long endtimestamp = endTime.getTime() / 1000L;
long starttimestamp = starttime.getTime() / 1000L; long starttimestamp = startTime.getTime() / 1000L;
if (endtimestamp - starttimestamp < 0 || endtimestamp - starttimestamp >= 30 * 24 * 60 * 60) { if (endtimestamp - starttimestamp < 0 || endtimestamp - starttimestamp >= 30 * 24 * 60 * 60) {
throw new RuntimeException("获取记录时间跨度不超过一个月"); throw new RuntimeException("获取记录时间跨度不超过一个月");
} }
String url = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
@ -64,7 +59,7 @@ public class WxCpOAServiceImpl implements WxCpOAService {
jsonObject.add("useridlist", jsonArray); jsonObject.add("useridlist", jsonArray);
String responseContent = this.mainService.post(url, jsonObject.toString()); String responseContent = this.mainService.post(WxCpOaService.GET_CHECKIN_DATA, jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create() return WxCpGsonBuilder.create()
.fromJson( .fromJson(
@ -76,7 +71,6 @@ public class WxCpOAServiceImpl implements WxCpOAService {
@Override @Override
public List<WxCpCheckinOption> getCheckinOption(Date datetime, List<String> userIdList) throws WxErrorException { public List<WxCpCheckinOption> getCheckinOption(Date datetime, List<String> userIdList) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckinoption";
if (datetime == null) { if (datetime == null) {
throw new RuntimeException("datetime can't be null"); throw new RuntimeException("datetime can't be null");
} }
@ -94,7 +88,7 @@ public class WxCpOAServiceImpl implements WxCpOAService {
jsonObject.addProperty("datetime", datetime.getTime() / 1000L); jsonObject.addProperty("datetime", datetime.getTime() / 1000L);
jsonObject.add("useridlist", jsonArray); jsonObject.add("useridlist", jsonArray);
String responseContent = this.mainService.post(url, jsonObject.toString()); String responseContent = this.mainService.post(WxCpOaService.GET_CHECKIN_OPTION, jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create() return WxCpGsonBuilder.create()
@ -106,24 +100,20 @@ public class WxCpOAServiceImpl implements WxCpOAService {
} }
@Override @Override
public WxCpApprovalDataResult getApprovalData(Date starttime, Date endtime, Long nextSpnum) throws WxErrorException { public WxCpApprovalDataResult getApprovalData(Date startTime, Date endTime, Long nextSpnum) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/corp/getapprovaldata";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("starttime", starttime.getTime() / 1000L); jsonObject.addProperty("starttime", startTime.getTime() / 1000L);
jsonObject.addProperty("endtime", endtime.getTime() / 1000L); jsonObject.addProperty("endtime", endTime.getTime() / 1000L);
if (nextSpnum != null) { if (nextSpnum != null) {
jsonObject.addProperty("next_spnum", nextSpnum); jsonObject.addProperty("next_spnum", nextSpnum);
} }
String responseContent = this.mainService.post(url, jsonObject.toString()); String responseContent = this.mainService.post(WxCpOaService.GET_APPROVAL_DATA, jsonObject.toString());
return WxCpGsonBuilder.create().fromJson(responseContent, WxCpApprovalDataResult.class); return WxCpGsonBuilder.create().fromJson(responseContent, WxCpApprovalDataResult.class);
} }
@Override @Override
public List<WxCpDialRecord> getDialRecord(Date starttime, Date endtime, Integer offset, Integer limit) throws WxErrorException { public List<WxCpDialRecord> getDialRecord(Date startTime, Date endTime, Integer offset, Integer limit) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/dial/get_dial_record";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
if (offset == null) { if (offset == null) {
@ -137,10 +127,10 @@ public class WxCpOAServiceImpl implements WxCpOAService {
jsonObject.addProperty("offset", offset); jsonObject.addProperty("offset", offset);
jsonObject.addProperty("limit", limit); jsonObject.addProperty("limit", limit);
if (starttime != null && endtime != null) { if (startTime != null && endTime != null) {
long endtimestamp = endtime.getTime() / 1000L; long endtimestamp = endTime.getTime() / 1000L;
long starttimestamp = starttime.getTime() / 1000L; long starttimestamp = startTime.getTime() / 1000L;
if (endtimestamp - starttimestamp < 0 || endtimestamp - starttimestamp >= 30 * 24 * 60 * 60) { if (endtimestamp - starttimestamp < 0 || endtimestamp - starttimestamp >= 30 * 24 * 60 * 60) {
throw new RuntimeException("受限于网络传输起止时间的最大跨度为30天如超过30天则以结束时间为基准向前取30天进行查询"); throw new RuntimeException("受限于网络传输起止时间的最大跨度为30天如超过30天则以结束时间为基准向前取30天进行查询");
@ -148,11 +138,9 @@ public class WxCpOAServiceImpl implements WxCpOAService {
jsonObject.addProperty("start_time", starttimestamp); jsonObject.addProperty("start_time", starttimestamp);
jsonObject.addProperty("end_time", endtimestamp); jsonObject.addProperty("end_time", endtimestamp);
} }
String responseContent = this.mainService.post(url, jsonObject.toString()); String responseContent = this.mainService.post(WxCpOaService.GET_DIAL_RECORD, jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create() return WxCpGsonBuilder.create()

View File

@ -8,7 +8,7 @@ import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.cp.api.WxCpOAService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
@ -19,6 +19,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException; import java.io.IOException;
/**
* @author someone
*/
public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<CloseableHttpClient, HttpHost> { public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<CloseableHttpClient, HttpHost> {
protected CloseableHttpClient httpClient; protected CloseableHttpClient httpClient;
protected HttpHost httpProxy; protected HttpHost httpProxy;
@ -45,9 +48,7 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
} }
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" String url = String.format(WxCpService.GET_TOKEN, this.configStorage.getCorpId(), this.configStorage.getCorpSecret());
+ "&corpid=" + this.configStorage.getCorpId()
+ "&corpsecret=" + this.configStorage.getCorpSecret();
try { try {
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
if (this.httpProxy != null) { if (this.httpProxy != null) {
@ -56,8 +57,8 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
httpGet.setConfig(config); httpGet.setConfig(config);
} }
String resultContent; String resultContent;
try (CloseableHttpClient httpclient = getRequestHttpClient(); try (CloseableHttpClient httpClient = getRequestHttpClient();
CloseableHttpResponse response = httpclient.execute(httpGet)) { CloseableHttpResponse response = httpClient.execute(httpGet)) {
resultContent = new BasicResponseHandler().handleResponse(response); resultContent = new BasicResponseHandler().handleResponse(response);
} finally { } finally {
httpGet.releaseConnection(); httpGet.releaseConnection();

View File

@ -6,8 +6,12 @@ import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
/**
* @author someone
*/
public class WxCpServiceJoddHttpImpl extends BaseWxCpServiceImpl<HttpConnectionProvider, ProxyInfo> { public class WxCpServiceJoddHttpImpl extends BaseWxCpServiceImpl<HttpConnectionProvider, ProxyInfo> {
protected HttpConnectionProvider httpClient; protected HttpConnectionProvider httpClient;
protected ProxyInfo httpProxy; protected ProxyInfo httpProxy;
@ -35,11 +39,7 @@ public class WxCpServiceJoddHttpImpl extends BaseWxCpServiceImpl<HttpConnectionP
} }
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" HttpRequest request = HttpRequest.get(String.format(WxCpService.GET_TOKEN, this.configStorage.getCorpId(), this.configStorage.getCorpSecret()));
+ "&corpid=" + this.configStorage.getCorpId()
+ "&corpsecret=" + this.configStorage.getCorpSecret();
HttpRequest request = HttpRequest.get(url);
if (this.httpProxy != null) { if (this.httpProxy != null) {
httpClient.useProxy(this.httpProxy); httpClient.useProxy(this.httpProxy);
} }

View File

@ -6,15 +6,18 @@ import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import okhttp3.*; import okhttp3.*;
import java.io.IOException; import java.io.IOException;
/**
* @author someone
*/
public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkHttpProxyInfo> { public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkHttpProxyInfo> {
protected OkHttpClient httpClient; private OkHttpClient httpClient;
protected OkHttpProxyInfo httpProxy; private OkHttpProxyInfo httpProxy;
@Override @Override
public OkHttpClient getRequestHttpClient() { public OkHttpClient getRequestHttpClient() {
@ -38,28 +41,28 @@ public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkH
} }
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" //得到httpClient
+ "&corpid=" + this.configStorage.getCorpId() OkHttpClient client = getRequestHttpClient();
+ "&corpsecret=" + this.configStorage.getCorpSecret(); //请求的request
//得到httpClient Request request = new Request.Builder()
OkHttpClient client = getRequestHttpClient(); .url(String.format(WxCpService.GET_TOKEN, this.configStorage.getCorpId(), this.configStorage.getCorpSecret()))
//请求的request .get()
Request request = new Request.Builder().url(url).get().build(); .build();
String resultContent = null; String resultContent = null;
try { try {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
resultContent = response.body().string(); resultContent = response.body().string();
} catch (IOException e) { } catch (IOException e) {
this.log.error(e.getMessage(), e); this.log.error(e.getMessage(), e);
} }
WxError error = WxError.fromJson(resultContent, WxType.CP); WxError error = WxError.fromJson(resultContent, WxType.CP);
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.configStorage.updateAccessToken(accessToken.getAccessToken(), this.configStorage.updateAccessToken(accessToken.getAccessToken(),
accessToken.getExpiresIn()); accessToken.getExpiresIn());
} }
return this.configStorage.getAccessToken(); return this.configStorage.getAccessToken();
} }

View File

@ -1,12 +1,6 @@
package me.chanjar.weixin.cp.api.impl; package me.chanjar.weixin.cp.api.impl;
import java.util.List; import com.google.gson.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
@ -17,12 +11,15 @@ import me.chanjar.weixin.cp.bean.WxCpTagGetResult;
import me.chanjar.weixin.cp.bean.WxCpUser; import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.util.List;
/** /**
* <pre> * <pre>
* 标签管理接口 * 标签管理接口.
* Created by Binary Wang on 2017-6-25. * Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre> * </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public class WxCpTagServiceImpl implements WxCpTagService { public class WxCpTagServiceImpl implements WxCpTagService {
private WxCpService mainService; private WxCpService mainService;
@ -33,33 +30,29 @@ public class WxCpTagServiceImpl implements WxCpTagService {
@Override @Override
public String create(String tagName) throws WxErrorException { public String create(String tagName) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
JsonObject o = new JsonObject(); JsonObject o = new JsonObject();
o.addProperty("tagname", tagName); o.addProperty("tagname", tagName);
String responseContent = this.mainService.post(url, o.toString()); String responseContent = this.mainService.post(WxCpTagService.TAG_CREATE, o.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("tagid").getAsString(); return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
} }
@Override @Override
public void update(String tagId, String tagName) throws WxErrorException { public void update(String tagId, String tagName) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update";
JsonObject o = new JsonObject(); JsonObject o = new JsonObject();
o.addProperty("tagid", tagId); o.addProperty("tagid", tagId);
o.addProperty("tagname", tagName); o.addProperty("tagname", tagName);
this.mainService.post(url, o.toString()); this.mainService.post(WxCpTagService.TAG_UPDATE, o.toString());
} }
@Override @Override
public void delete(String tagId) throws WxErrorException { public void delete(String tagId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?tagid=" + tagId; this.mainService.get(String.format(WxCpTagService.TAG_DELETE, tagId), null);
this.mainService.get(url, null);
} }
@Override @Override
public List<WxCpTag> listAll() throws WxErrorException { public List<WxCpTag> listAll() throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"; String responseContent = this.mainService.get(WxCpTagService.TAG_LIST, null);
String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create() return WxCpGsonBuilder.create()
.fromJson( .fromJson(
@ -71,8 +64,7 @@ public class WxCpTagServiceImpl implements WxCpTagService {
@Override @Override
public List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException { public List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId; String responseContent = this.mainService.get(String.format(WxCpTagService.TAG_GET, tagId), null);
String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create() return WxCpGsonBuilder.create()
.fromJson( .fromJson(
@ -84,22 +76,20 @@ public class WxCpTagServiceImpl implements WxCpTagService {
@Override @Override
public WxCpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException { public WxCpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId); jsonObject.addProperty("tagid", tagId);
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject); this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);
return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString())); return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(WxCpTagService.TAG_ADDTAGUSERS, jsonObject.toString()));
} }
@Override @Override
public WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException { public WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId); jsonObject.addProperty("tagid", tagId);
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject); this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);
return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString())); return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(WxCpTagService.TAG_DELTAGUSERS, jsonObject.toString()));
} }
private void addUserIdsAndPartyIdsToJson(List<String> userIds, List<String> partyIds, JsonObject jsonObject) { private void addUserIdsAndPartyIdsToJson(List<String> userIds, List<String> partyIds, JsonObject jsonObject) {
@ -122,15 +112,11 @@ public class WxCpTagServiceImpl implements WxCpTagService {
@Override @Override
public WxCpTagGetResult get(String tagId) throws WxErrorException { public WxCpTagGetResult get(String tagId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get"; if (tagId == null) {
if (tagId != null) {
url += "?tagId=" + tagId;
} else {
throw new IllegalArgumentException("缺少tagId参数"); throw new IllegalArgumentException("缺少tagId参数");
} }
String responseContent = this.mainService.get(url, null); String responseContent = this.mainService.get(String.format(WxCpTagService.TAG_GET, tagId), null);
return WxCpTagGetResult.fromJson(responseContent); return WxCpTagGetResult.fromJson(responseContent);
} }
} }

View File

@ -33,7 +33,6 @@ public class WxCpTaskCardServiceImpl implements WxCpTaskCardService {
data.put("task_id", taskId); data.put("task_id", taskId);
data.put("clicked_key", clickedKey); data.put("clicked_key", clickedKey);
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/update_taskcard"; this.mainService.post(MESSAGE_UPDATE_TASKCARD, WxGsonBuilder.create().toJson(data));
this.mainService.post(url, WxGsonBuilder.create().toJson(data));
} }
} }

View File

@ -1,114 +1,114 @@
package me.chanjar.weixin.cp.api.impl; package me.chanjar.weixin.cp.api.impl;
import java.io.IOException; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.Consts; import me.chanjar.weixin.common.WxType;
import org.apache.http.HttpHost; import me.chanjar.weixin.common.error.WxError;
import org.apache.http.client.config.RequestConfig; import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.http.client.methods.CloseableHttpResponse; import me.chanjar.weixin.common.util.http.HttpType;
import org.apache.http.client.methods.HttpPost; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import org.apache.http.entity.StringEntity; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.http.impl.client.BasicResponseHandler; import me.chanjar.weixin.cp.api.WxCpTpService;
import org.apache.http.impl.client.CloseableHttpClient; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import org.apache.http.Consts;
import com.google.gson.JsonObject; import org.apache.http.HttpHost;
import com.google.gson.JsonParser; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import me.chanjar.weixin.common.WxType; import org.apache.http.client.methods.HttpPost;
import me.chanjar.weixin.common.error.WxError; import org.apache.http.entity.StringEntity;
import me.chanjar.weixin.common.error.WxErrorException; import org.apache.http.impl.client.BasicResponseHandler;
import me.chanjar.weixin.common.util.http.HttpType; import org.apache.http.impl.client.CloseableHttpClient;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import java.io.IOException;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
/**
public class WxCpTpServiceApacheHttpClientImpl extends BaseWxCpTpServiceImpl<CloseableHttpClient, HttpHost> { * @author someone
protected CloseableHttpClient httpClient; */
protected HttpHost httpProxy; public class WxCpTpServiceApacheHttpClientImpl extends BaseWxCpTpServiceImpl<CloseableHttpClient, HttpHost> {
private CloseableHttpClient httpClient;
@Override private HttpHost httpProxy;
public CloseableHttpClient getRequestHttpClient() {
return httpClient; @Override
} public CloseableHttpClient getRequestHttpClient() {
return httpClient;
@Override }
public HttpHost getRequestHttpProxy() {
return httpProxy; @Override
} public HttpHost getRequestHttpProxy() {
return httpProxy;
@Override }
public HttpType getRequestType() {
return HttpType.APACHE_HTTP; @Override
} public HttpType getRequestType() {
return HttpType.APACHE_HTTP;
@Override }
public String getSuiteAccessToken(boolean forceRefresh) throws WxErrorException {
if (!this.configStorage.isSuiteAccessTokenExpired() && !forceRefresh) { @Override
return this.configStorage.getSuiteAccessToken(); public String getSuiteAccessToken(boolean forceRefresh) throws WxErrorException {
} if (!this.configStorage.isSuiteAccessTokenExpired() && !forceRefresh) {
return this.configStorage.getSuiteAccessToken();
synchronized (this.globalSuiteAccessTokenRefreshLock) { }
String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token";
try { synchronized (this.globalSuiteAccessTokenRefreshLock) {
HttpPost httpPost = new HttpPost(url); try {
if (this.httpProxy != null) { HttpPost httpPost = new HttpPost(WxCpTpService.GET_SUITE_TOKEN);
RequestConfig config = RequestConfig.custom() if (this.httpProxy != null) {
.setProxy(this.httpProxy).build(); RequestConfig config = RequestConfig.custom()
httpPost.setConfig(config); .setProxy(this.httpProxy).build();
} httpPost.setConfig(config);
JsonObject jsonObject = new JsonObject(); }
jsonObject.addProperty("suite_id", this.configStorage.getSuiteId()); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("suite_secret", this.configStorage.getSuiteSecret()); jsonObject.addProperty("suite_id", this.configStorage.getSuiteId());
jsonObject.addProperty("suite_ticket", this.getSuiteTicket()); jsonObject.addProperty("suite_secret", this.configStorage.getSuiteSecret());
StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8); jsonObject.addProperty("suite_ticket", this.getSuiteTicket());
httpPost.setEntity(entity); StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
httpPost.setEntity(entity);
String resultContent;
try (CloseableHttpClient httpclient = getRequestHttpClient(); String resultContent;
CloseableHttpResponse response = httpclient.execute(httpPost)) { try (CloseableHttpClient httpclient = getRequestHttpClient();
resultContent = new BasicResponseHandler().handleResponse(response); CloseableHttpResponse response = httpclient.execute(httpPost)) {
} finally { resultContent = new BasicResponseHandler().handleResponse(response);
httpPost.releaseConnection(); } finally {
} httpPost.releaseConnection();
WxError error = WxError.fromJson(resultContent, WxType.CP); }
if (error.getErrorCode() != 0) { WxError error = WxError.fromJson(resultContent, WxType.CP);
throw new WxErrorException(error); if (error.getErrorCode() != 0) {
} throw new WxErrorException(error);
jsonObject = new JsonParser().parse(resultContent).getAsJsonObject(); }
String suiteAccussToken = jsonObject.get("suite_access_token").getAsString(); jsonObject = new JsonParser().parse(resultContent).getAsJsonObject();
Integer expiresIn = jsonObject.get("expires_in").getAsInt(); String suiteAccussToken = jsonObject.get("suite_access_token").getAsString();
this.configStorage.updateSuiteAccessToken(suiteAccussToken, expiresIn); Integer expiresIn = jsonObject.get("expires_in").getAsInt();
} catch (IOException e) { this.configStorage.updateSuiteAccessToken(suiteAccussToken, expiresIn);
throw new RuntimeException(e); } catch (IOException e) {
} throw new RuntimeException(e);
} }
return this.configStorage.getSuiteAccessToken(); }
} return this.configStorage.getSuiteAccessToken();
}
@Override
public void initHttp() { @Override
ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage public void initHttp() {
.getApacheHttpClientBuilder(); ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage.getApacheHttpClientBuilder();
if (null == apacheHttpClientBuilder) { if (null == apacheHttpClientBuilder) {
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get(); apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
} }
apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost()) apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost())
.httpProxyPort(this.configStorage.getHttpProxyPort()) .httpProxyPort(this.configStorage.getHttpProxyPort())
.httpProxyUsername(this.configStorage.getHttpProxyUsername()) .httpProxyUsername(this.configStorage.getHttpProxyUsername())
.httpProxyPassword(this.configStorage.getHttpProxyPassword()); .httpProxyPassword(this.configStorage.getHttpProxyPassword());
if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) { if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort()); this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());
} }
this.httpClient = apacheHttpClientBuilder.build(); this.httpClient = apacheHttpClientBuilder.build();
} }
@Override @Override
public WxCpTpConfigStorage getWxCpTpConfigStorage() { public WxCpTpConfigStorage getWxCpTpConfigStorage() {
return this.configStorage; return this.configStorage;
} }
} }

View File

@ -26,7 +26,7 @@ import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public class WxCpUserServiceImpl implements WxCpUserService { public class WxCpUserServiceImpl implements WxCpUserService {
private WxCpService mainService; private final WxCpService mainService;
public WxCpUserServiceImpl(WxCpService mainService) { public WxCpUserServiceImpl(WxCpService mainService) {
this.mainService = mainService; this.mainService = mainService;
@ -34,50 +34,44 @@ public class WxCpUserServiceImpl implements WxCpUserService {
@Override @Override
public void authenticate(String userId) throws WxErrorException { public void authenticate(String userId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?userid=" + userId; this.mainService.get(WxCpUserService.URL_AUTHENTICATE + userId, null);
this.mainService.get(url, null);
} }
@Override @Override
public void create(WxCpUser user) throws WxErrorException { public void create(WxCpUser user) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/create"; this.mainService.post(WxCpUserService.URL_USER_CREATE, user.toJson());
this.mainService.post(url, user.toJson());
} }
@Override @Override
public void update(WxCpUser user) throws WxErrorException { public void update(WxCpUser user) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/update"; this.mainService.post(WxCpUserService.URL_USER_UPDATE, user.toJson());
this.mainService.post(url, user.toJson());
} }
@Override @Override
public void delete(String... userIds) throws WxErrorException { public void delete(String... userIds) throws WxErrorException {
if (userIds.length == 1) { if (userIds.length == 1) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?userid=" + userIds[0]; this.mainService.get(WxCpUserService.URL_USER_DELETE + userIds[0], null);
this.mainService.get(url, null);
return; return;
} }
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
for (String userid : userIds) { for (String userId : userIds) {
jsonArray.add(new JsonPrimitive(userid)); jsonArray.add(new JsonPrimitive(userId));
} }
jsonObject.add("useridlist", jsonArray); jsonObject.add("useridlist", jsonArray);
this.mainService.post(url, jsonObject.toString()); this.mainService.post(WxCpUserService.URL_USER_BATCH_DELETE, jsonObject.toString());
} }
@Override @Override
public WxCpUser getById(String userid) throws WxErrorException { public WxCpUser getById(String userid) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?userid=" + userid; String responseContent = this.mainService.get(WxCpUserService.URL_USER_GET + userid, null);
String responseContent = this.mainService.get(url, null);
return WxCpUser.fromJson(responseContent); return WxCpUser.fromJson(responseContent);
} }
@Override @Override
public List<WxCpUser> listByDepartment(Long departId, Boolean fetchChild, Integer status) throws WxErrorException { public List<WxCpUser> listByDepartment(Long departId, Boolean fetchChild, Integer status) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?department_id=" + departId;
String params = ""; String params = "";
if (fetchChild != null) { if (fetchChild != null) {
params += "&fetch_child=" + (fetchChild ? "1" : "0"); params += "&fetch_child=" + (fetchChild ? "1" : "0");
@ -88,7 +82,7 @@ public class WxCpUserServiceImpl implements WxCpUserService {
params += "&status=0"; params += "&status=0";
} }
String responseContent = this.mainService.get(url, params); String responseContent = this.mainService.get(WxCpUserService.URL_USER_LIST + departId, params);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create() return WxCpGsonBuilder.create()
.fromJson(tmpJsonElement.getAsJsonObject().get("userlist"), .fromJson(tmpJsonElement.getAsJsonObject().get("userlist"),
@ -99,7 +93,6 @@ public class WxCpUserServiceImpl implements WxCpUserService {
@Override @Override
public List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status) throws WxErrorException { public List<WxCpUser> listSimpleByDepartment(Long departId, Boolean fetchChild, Integer status) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId;
String params = ""; String params = "";
if (fetchChild != null) { if (fetchChild != null) {
params += "&fetch_child=" + (fetchChild ? "1" : "0"); params += "&fetch_child=" + (fetchChild ? "1" : "0");
@ -110,7 +103,7 @@ public class WxCpUserServiceImpl implements WxCpUserService {
params += "&status=0"; params += "&status=0";
} }
String responseContent = this.mainService.get(url, params); String responseContent = this.mainService.get(WxCpUserService.URL_USER_SIMPLE_LIST + departId, params);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.create() return WxCpGsonBuilder.create()
.fromJson( .fromJson(
@ -122,7 +115,6 @@ public class WxCpUserServiceImpl implements WxCpUserService {
@Override @Override
public WxCpInviteResult invite(List<String> userIds, List<String> partyIds, List<String> tagIds) throws WxErrorException { public WxCpInviteResult invite(List<String> userIds, List<String> partyIds, List<String> tagIds) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/batch/invite";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
if (userIds != null) { if (userIds != null) {
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
@ -148,19 +140,18 @@ public class WxCpUserServiceImpl implements WxCpUserService {
jsonObject.add("tag", jsonArray); jsonObject.add("tag", jsonArray);
} }
return WxCpInviteResult.fromJson(this.mainService.post(url, jsonObject.toString())); return WxCpInviteResult.fromJson(this.mainService.post(WxCpUserService.URL_BATCH_INVITE, jsonObject.toString()));
} }
@Override @Override
public Map<String, String> userId2Openid(String userId, Integer agentId) throws WxErrorException { public Map<String, String> userId2Openid(String userId, Integer agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("userid", userId); jsonObject.addProperty("userid", userId);
if (agentId != null) { if (agentId != null) {
jsonObject.addProperty("agentid", agentId); jsonObject.addProperty("agentid", agentId);
} }
String responseContent = this.mainService.post(url, jsonObject.toString()); String responseContent = this.mainService.post(WxCpUserService.URL_CONVERT_TO_OPENID, jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
Map<String, String> result = Maps.newHashMap(); Map<String, String> result = Maps.newHashMap();
if (tmpJsonElement.getAsJsonObject().get("openid") != null) { if (tmpJsonElement.getAsJsonObject().get("openid") != null) {
@ -176,18 +167,16 @@ public class WxCpUserServiceImpl implements WxCpUserService {
@Override @Override
public String openid2UserId(String openid) throws WxErrorException { public String openid2UserId(String openid) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid";
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("openid", openid); jsonObject.addProperty("openid", openid);
String responseContent = this.mainService.post(url, jsonObject.toString()); String responseContent = this.mainService.post(WxCpUserService.URL_CONVERT_TO_USERID, jsonObject.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent); JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("userid").getAsString(); return tmpJsonElement.getAsJsonObject().get("userid").getAsString();
} }
@Override @Override
public WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException { public WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/crm/get_external_contact?external_userid=" + userId; String responseContent = this.mainService.get(WxCpUserService.URL_GET_EXTERNAL_CONTACT + userId, null);
String responseContent = this.mainService.get(url, null);
return WxCpUserExternalContactInfo.fromJson(responseContent); return WxCpUserExternalContactInfo.fromJson(responseContent);
} }
} }

View File

@ -29,19 +29,19 @@ public class WxCpTagGetResult implements Serializable {
private String errmsg; private String errmsg;
/** /**
* 用户列表 * 用户列表.
*/ */
@SerializedName("userlist") @SerializedName("userlist")
private List<WxCpUser> userlist; private List<WxCpUser> userlist;
/** /**
* 部门列表 * 部门列表.
*/ */
@SerializedName("partylist") @SerializedName("partylist")
private List<Integer> partylist; private List<Integer> partylist;
/** /**
* 标签名称 * 标签名称.
*/ */
@SerializedName("tagname") @SerializedName("tagname")
private String tagname; private String tagname;

View File

@ -6,7 +6,6 @@ import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpAgentService; import me.chanjar.weixin.cp.api.WxCpAgentService;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpAgent; import me.chanjar.weixin.cp.bean.WxCpAgent;
import org.testng.Assert;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -71,7 +70,7 @@ public class WxCpAgentServiceImplTest {
@Test @Test
public void testGet() throws Exception { public void testGet() throws Exception {
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}"; String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}";
when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson); when(wxService.get(String.format(WxCpAgentService.GET_AGENT, 9), null)).thenReturn(returnJson);
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService)); when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService));
WxCpAgentService wxAgentService = this.wxService.getAgentService(); WxCpAgentService wxAgentService = this.wxService.getAgentService();

View File

@ -24,7 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @date 2019-04-20 13:46 * @date 2019-04-20 13:46
*/ */
@Guice(modules = ApiTestModule.class) @Guice(modules = ApiTestModule.class)
public class WxCpOAServiceImplTest { public class WxCpOaServiceImplTest {
@Inject @Inject
protected WxCpService wxService; protected WxCpService wxService;

View File

@ -22,10 +22,10 @@ import static org.testng.Assert.assertNotEquals;
/** /**
* <pre> * <pre>
*
* Created by Binary Wang on 2017-6-25. * Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre> * </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
@Guice(modules = ApiTestModule.class) @Guice(modules = ApiTestModule.class)
public class WxCpTagServiceImplTest { public class WxCpTagServiceImplTest {
@ -35,7 +35,7 @@ public class WxCpTagServiceImplTest {
@Inject @Inject
protected ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage; protected ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage;
protected String tagId; private String tagId;
@Test @Test
public void testCreate() throws Exception { public void testCreate() throws Exception {
@ -83,7 +83,7 @@ public class WxCpTagServiceImplTest {
public void testGet() throws WxErrorException { public void testGet() throws WxErrorException {
String apiResultJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"userlist\": [{\"userid\": \"0124035\",\"name\": \"王五\"},{\"userid\": \"0114035\",\"name\": \"梦雪\"}],\"partylist\": [9576,9567,9566],\"tagname\": \"测试标签-001\"}"; String apiResultJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"userlist\": [{\"userid\": \"0124035\",\"name\": \"王五\"},{\"userid\": \"0114035\",\"name\": \"梦雪\"}],\"partylist\": [9576,9567,9566],\"tagname\": \"测试标签-001\"}";
WxCpService wxService = mock(WxCpService.class); WxCpService wxService = mock(WxCpService.class);
when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagId=150", null)).thenReturn(apiResultJson); when(wxService.get(String.format(WxCpTagService.TAG_GET, 150), null)).thenReturn(apiResultJson);
when(wxService.getTagService()).thenReturn(new WxCpTagServiceImpl(wxService)); when(wxService.getTagService()).thenReturn(new WxCpTagServiceImpl(wxService));
WxCpTagService wxCpTagService = wxService.getTagService(); WxCpTagService wxCpTagService = wxService.getTagService();
@ -96,7 +96,6 @@ public class WxCpTagServiceImplTest {
assertEquals(3, wxCpTagGetResult.getPartylist().size()); assertEquals(3, wxCpTagGetResult.getPartylist().size());
assertEquals("测试标签-001", wxCpTagGetResult.getTagname()); assertEquals("测试标签-001", wxCpTagGetResult.getTagname());
} }
} }

View File

@ -3,6 +3,7 @@ package com.github.binarywang.wxpay.bean.request;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.config.WxPayConfig;
@ -27,6 +28,7 @@ import static com.github.binarywang.wxpay.constant.WxPayConstants.SignType.ALL_S
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
@Data @Data
@Accessors(chain = true)
public abstract class BaseWxPayRequest implements Serializable { public abstract class BaseWxPayRequest implements Serializable {
private static final long serialVersionUID = -4766915659779847060L; private static final long serialVersionUID = -4766915659779847060L;

View File

@ -5,6 +5,7 @@ import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.*; import lombok.*;
import lombok.experimental.Accessors;
import me.chanjar.weixin.common.annotation.Required; import me.chanjar.weixin.common.annotation.Required;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -23,6 +24,7 @@ import org.apache.commons.lang3.StringUtils;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@XStreamAlias("xml") @XStreamAlias("xml")
@Accessors(chain = true)
public class WxPayUnifiedOrderRequest extends BaseWxPayRequest { public class WxPayUnifiedOrderRequest extends BaseWxPayRequest {
private static final long serialVersionUID = 4611350167813931828L; private static final long serialVersionUID = 4611350167813931828L;