All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foxinmy.weixin4j.qy.WeixinProxy Maven / Gradle / Ivy

There is a newer version: 1.10.2
Show newest version
package com.foxinmy.weixin4j.qy;

import java.io.InputStream;
import java.util.List;

import com.foxinmy.weixin4j.cache.CacheStorager;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.model.Button;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.model.media.MediaCounter;
import com.foxinmy.weixin4j.model.media.MediaDownloadResult;
import com.foxinmy.weixin4j.model.media.MediaItem;
import com.foxinmy.weixin4j.model.media.MediaRecord;
import com.foxinmy.weixin4j.model.media.MediaUploadResult;
import com.foxinmy.weixin4j.model.paging.Pageable;
import com.foxinmy.weixin4j.qy.api.AgentApi;
import com.foxinmy.weixin4j.qy.api.BatchApi;
import com.foxinmy.weixin4j.qy.api.ChatApi;
import com.foxinmy.weixin4j.qy.api.HelperApi;
import com.foxinmy.weixin4j.qy.api.MediaApi;
import com.foxinmy.weixin4j.qy.api.MenuApi;
import com.foxinmy.weixin4j.qy.api.NotifyApi;
import com.foxinmy.weixin4j.qy.api.OauthApi;
import com.foxinmy.weixin4j.qy.api.PartyApi;
import com.foxinmy.weixin4j.qy.api.TagApi;
import com.foxinmy.weixin4j.qy.api.UserApi;
import com.foxinmy.weixin4j.qy.message.ChatMessage;
import com.foxinmy.weixin4j.qy.message.CustomeMessage;
import com.foxinmy.weixin4j.qy.message.NotifyMessage;
import com.foxinmy.weixin4j.qy.model.AgentInfo;
import com.foxinmy.weixin4j.qy.model.AgentOverview;
import com.foxinmy.weixin4j.qy.model.AgentSetter;
import com.foxinmy.weixin4j.qy.model.BatchResult;
import com.foxinmy.weixin4j.qy.model.Callback;
import com.foxinmy.weixin4j.qy.model.ChatInfo;
import com.foxinmy.weixin4j.qy.model.ChatMute;
import com.foxinmy.weixin4j.qy.model.Contacts;
import com.foxinmy.weixin4j.qy.model.IdParameter;
import com.foxinmy.weixin4j.qy.model.Party;
import com.foxinmy.weixin4j.qy.model.Tag;
import com.foxinmy.weixin4j.qy.model.User;
import com.foxinmy.weixin4j.qy.suite.WeixinTokenSuiteCreator;
import com.foxinmy.weixin4j.qy.token.WeixinTicketCreator;
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.qy.type.ChatType;
import com.foxinmy.weixin4j.qy.type.InviteType;
import com.foxinmy.weixin4j.qy.type.KfType;
import com.foxinmy.weixin4j.qy.type.UserStatus;
import com.foxinmy.weixin4j.token.PerTicketManager;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.tuple.MpArticle;
import com.foxinmy.weixin4j.type.MediaType;
import com.foxinmy.weixin4j.type.TicketType;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;

/**
 * 微信企业号接口实现
 *
 * @className WeixinProxy
 * @author jinyu([email protected])
 * @date 2014年11月19日
 * @since JDK 1.6
 * @see api文档
 */
public class WeixinProxy {
	/**
	 * 授权API
	 */
	private final OauthApi oauthApi;
	/**
	 * 媒体素材API
	 */
	private final MediaApi mediaApi;
	/**
	 * 菜单API
	 */
	private final MenuApi menuApi;
	/**
	 * 消息服务API
	 */
	private final NotifyApi notifyApi;
	/**
	 * 部门API
	 */
	private final PartyApi partyApi;
	/**
	 * 成员API
	 */
	private final UserApi userApi;
	/**
	 * 标签API
	 */
	private final TagApi tagApi;
	/**
	 * 辅助API
	 */
	private final HelperApi helperApi;
	/**
	 * 应用API
	 */
	private final AgentApi agentApi;
	/**
	 * 批量操作API
	 */
	private final BatchApi batchApi;
	/**
	 * 聊天服务API
	 */
	private final ChatApi chatApi;
	/**
	 * token管理
	 */
	private final TokenManager tokenManager;
	/**
	 * 账号信息
	 */
	private final WeixinAccount weixinAccount;
	/**
	 * token存储
	 */
	private final CacheStorager cacheStorager;

	/**
	 * 微信接口实现(使用weixin4j.properties配置的account账号信息,
	 * 使用FileCacheStorager文件方式缓存TOKEN)
	 */
	public WeixinProxy() {
		this(new FileCacheStorager());
	}

	/**
	 * 微信接口实现(使用weixin4j.properties配置的account账号信息)
	 * 
	 * @param cacheStorager
	 *            token管理
	 */
	public WeixinProxy(CacheStorager cacheStorager) {
		this(Weixin4jConfigUtil.getWeixinAccount(), cacheStorager);
	}

	/**
	 * 微信接口实现
	 * 
	 * @param weixinAccount
	 *            账号信息
	 * @param cacheStorager
	 *            token管理
	 */
	public WeixinProxy(WeixinAccount weixinAccount,
			CacheStorager cacheStorager) {
		this(weixinAccount, new WeixinTokenCreator(weixinAccount.getId(),
				weixinAccount.getSecret()), cacheStorager);
	}

	/**
	 * 第三方套件(永久授权码机制)
	 *
	 * @param perTicketManager
	 *            第三方套件永久授权码
	 *            {@link com.foxinmy.weixin4j.qy.api.SuiteApi#getPerCodeManager(String)}
	 * @param suiteTokenManager
	 *            第三方套件凭证token
	 *            {@link com.foxinmy.weixin4j.qy.api.SuiteApi#getTokenManager}
	 * @see com.foxinmy.weixin4j.qy.api.SuiteApi
	 * @see WeixinSuiteProxy#getWeixinProxy(String, String)
	 */
	public WeixinProxy(PerTicketManager perTicketManager,
			TokenManager suiteTokenManager) {
		this(
				new WeixinAccount(perTicketManager.getThirdId(),
						perTicketManager.getThirdSecret()),
				new WeixinTokenSuiteCreator(perTicketManager, suiteTokenManager),
				perTicketManager.getCacheStorager());
	}

	/**
	 * 微信接口实现
	 * 
	 * @param settings
	 *            配置信息
	 * @param tokenManager
	 *            token管理
	 */
	private WeixinProxy(WeixinAccount weixinAccount, TokenCreator tokenCreator,
			CacheStorager cacheStorager) {
		if (weixinAccount == null) {
			throw new IllegalArgumentException("weixinAccount must not be empty");
		}
		if (tokenCreator == null) {
			throw new IllegalArgumentException("tokenCreator must not be empty");
		}
		if (cacheStorager == null) {
			throw new IllegalArgumentException(
					"cacheStorager must not be empty");
		}
		this.tokenManager = new TokenManager(tokenCreator, cacheStorager);
		this.weixinAccount = weixinAccount;
		this.cacheStorager = cacheStorager;
		this.oauthApi = new OauthApi(weixinAccount);
		this.partyApi = new PartyApi(tokenManager);
		this.userApi = new UserApi(tokenManager);
		this.tagApi = new TagApi(tokenManager);
		this.helperApi = new HelperApi(tokenManager);
		this.agentApi = new AgentApi(tokenManager);
		this.batchApi = new BatchApi(tokenManager);
		this.notifyApi = new NotifyApi(tokenManager);
		this.menuApi = new MenuApi(tokenManager);
		this.mediaApi = new MediaApi(tokenManager);
		this.chatApi = new ChatApi(tokenManager);
	}

	/**
	 * token获取
	 *
	 * @return
	 */
	public TokenManager getTokenManager() {
		return this.tokenManager;
	}

	/**
	 * 获取oauth授权API
	 * 
	 * @see com.foxinmy.weixin4j.qy.api.OauthApi
	 * @return
	 */
	public OauthApi getOauthApi() {
		return oauthApi;
	}

	/**
	 * 获取微信账号信息
	 *
	 * @return
	 */
	public WeixinAccount getWeixinAccount() {
		return weixinAccount;
	}

	/**
	 * 获取JSSDK Ticket的tokenManager
	 *
	 * @param ticketType
	 *            票据类型
	 * @return
	 */
	public TokenManager getTicketManager(TicketType ticketType) {
		return new TokenManager(new WeixinTicketCreator(weixinAccount.getId(),
				ticketType, this.tokenManager), cacheStorager);
	}

	/**
	 * 发送消息提醒(需要管理员对应用有使用权限,对收件人touser、toparty、totag有查看权限,否则本次调用失败)
	 * 

* 1) 发送人员列表存在错误的userid:执行发送,开发者需注意返回结果说明
* 2)发送人员不在通讯录权限范围内:不执行发送任务,返回首个出错的userid
* 3)发送人员不在应用可见范围内:不执行发送任务,返回首个出错的userid
*

* * @param message * 消息对象 * @return 如果对应用或收件人、部门、标签任何一个无权限,则本次发送失败;如果收件人、部门或标签不存在,发送仍然执行,但返回无效的部分 *
{ "errcode": 0, "errmsg": "ok", "invaliduser": "UserID1", * "invalidparty":"PartyID1", "invalidtag":"TagID1" } * @throws WeixinException * @see com.foxinmy.weixin4j.qy.api.NotifyApi * @see * 发送接口说明 * @see * 发送格式说明 * @see com.foxinmy.weixin4j.tuple.Text * @see com.foxinmy.weixin4j.tuple.Image * @see com.foxinmy.weixin4j.tuple.Voice * @see com.foxinmy.weixin4j.tuple.Video * @see com.foxinmy.weixin4j.tuple.File * @see com.foxinmy.weixin4j.tuple.News * @see com.foxinmy.weixin4j.tuple.MpNews * @see com.foxinmy.weixin4j.qy.model.IdParameter */ public IdParameter sendNotifyMessage(NotifyMessage message) throws WeixinException { return notifyApi.sendNotifyMessage(message); } /** * 发送客服消息 * * @param message * 消息对象 * @return 发送结果 * @see * 客服接口说明 * @see com.foxinmy.weixin4j.qy.api.NotifyApi * @see com.foxinmy.weixin4j.tuple.Text * @see com.foxinmy.weixin4j.tuple.Image * @see com.foxinmy.weixin4j.tuple.Voice * @see com.foxinmy.weixin4j.tuple.Video * @see com.foxinmy.weixin4j.tuple.File * @see com.foxinmy.weixin4j.qy.message.CustomeMessage * @throws WeixinException */ public ApiResult sendCustomeMessage(CustomeMessage message) throws WeixinException { return notifyApi.sendCustomeMessage(message); } /** * 获取客服列表 * * @param kfType * 客服类型 为空时返回全部类型的客服 * @return 第一个元素为内部客服(internal),第二个参数为外部客服(external) * @see com.foxinmy.weixin4j.qy.api.NotifyApi * @see com.foxinmy.weixin4j.qy.model.IdParameter * @see * 客服列表 * @throws WeixinException */ public IdParameter[] getKfList(KfType kfType) throws WeixinException { return notifyApi.getKfList(kfType); } /** * 自定义菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式) * * @param agentid * 应用ID * * @param buttons * 菜单列表 * @throws WeixinException * @see com.foxinmy.weixin4j.qy.api.MenuApi * @see * 创建自定义菜单 * @see com.foxinmy.weixin4j.model.Button */ public ApiResult createMenu(int agentid, List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy