View Javadoc
1   package com.foxinmy.weixin4j.qy.api;
2   
3   import com.alibaba.fastjson.JSON;
4   import com.alibaba.fastjson.JSONObject;
5   import com.foxinmy.weixin4j.cache.CacheStorager;
6   import com.foxinmy.weixin4j.exception.WeixinException;
7   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
8   import com.foxinmy.weixin4j.model.Token;
9   import com.foxinmy.weixin4j.qy.model.OUserInfo;
10  import com.foxinmy.weixin4j.qy.type.LoginTargetType;
11  import com.foxinmy.weixin4j.token.TokenCreator;
12  import com.foxinmy.weixin4j.token.TokenManager;
13  import com.foxinmy.weixin4j.util.StringUtil;
14  
15  /**
16   * 服务商相关API
17   *
18   * @className ProviderApi
19   * @author jinyu(foxinmy@gmail.com)
20   * @date 2015年12月30日
21   * @since JDK 1.6
22   * @see <a
23   *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E5%8F%B7%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83">企业号登录授权说明</a>
24   */
25  public class ProviderApi extends QyApi {
26  	private final TokenManager providerTokenManager;
27  	private final CacheStorager<Token> cacheStorager;
28  
29  	public ProviderApi(TokenManager providerTokenManager,
30  			CacheStorager<Token> cacheStorager) {
31  		this.providerTokenManager = providerTokenManager;
32  		this.cacheStorager = cacheStorager;
33  	}
34  
35  	/**
36  	 * 第三方套件获取企业号管理员登录信息
37  	 *
38  	 * @param authCode
39  	 *            oauth2.0授权企业号管理员登录产生的code
40  	 * @return 登陆信息
41  	 * @see <a
42  	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E4%BC%81%E4%B8%9A%E7%AE%A1%E7%90%86%E5%91%98%E7%99%BB%E5%BD%95%E4%BF%A1%E6%81%AF">授权获取企业号管理员登录信息</a>
43  	 * @see com.foxinmy.weixin4j.qy.model.OUserInfo
44  	 * @throws WeixinException
45  	 */
46  	public OUserInfo getOUserInfoByCode(String authCode) throws WeixinException {
47  		String oauth_thirdinfo_uri = getRequestUri("oauth_logininfo_uri");
48  		WeixinResponse response = weixinExecutor.post(
49  				String.format(oauth_thirdinfo_uri,
50  						providerTokenManager.getAccessToken()),
51  				String.format("{\"auth_code\":\"%s\"}", authCode));
52  		JSONObject obj = response.getAsJson();
53  		OUserInfo oUser = JSON.toJavaObject(obj, OUserInfo.class);
54  		obj = obj.getJSONObject("redirect_login_info");
55  		Token loginInfo = new Token(obj.getString("login_ticket"),
56  				obj.getLongValue("expires_in") * 1000l);
57  		oUser.setRedirectLoginInfo(loginInfo);
58  		cacheStorager.caching(getLoginTicketCacheKey(oUser.getCorpInfo()
59  				.getCorpId()), oUser.getRedirectLoginInfo());
60  		return oUser;
61  	}
62  
63  	private String getLoginTicketCacheKey(String coprId) {
64  		return String.format("%sqy_provider_ticket_%s",
65  				TokenCreator.CACHEKEY_PREFIX, coprId);
66  	}
67  
68  	/**
69  	 * 获取登录企业号官网的url
70  	 *
71  	 * @param corpId
72  	 *            <font color="red">oauth授权的corpid</font>
73  	 * @param targetType
74  	 *            登录跳转到企业号后台的目标页面
75  	 * @param agentId
76  	 *            授权方应用id 小余1时则不传递
77  	 * @return 登陆URL
78  	 * @see <a
79  	 *      href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%AE%98%E7%BD%91%E7%9A%84url">获取登录企业号官网的url</a>
80  	 * @throws WeixinException
81  	 */
82  	public String getLoginUrl(String corpId, LoginTargetType targetType,
83  			int agentId) throws WeixinException {
84  		Token token = cacheStorager.lookup(getLoginTicketCacheKey(corpId));
85  		if (token == null || StringUtil.isBlank(token.getAccessToken())) {
86  			throw new WeixinException("maybe oauth first?");
87  		}
88  		String oauth_loginurl_uri = getRequestUri("oauth_loginurl_uri");
89  		JSONObject obj = new JSONObject();
90  		obj.put("login_ticket", token.getAccessToken());
91  		obj.put("target", targetType.name());
92  		if (agentId > 0) {
93  			obj.put("agentid", agentId);
94  		}
95  		WeixinResponse response = weixinExecutor.post(
96  				String.format(oauth_loginurl_uri,
97  						providerTokenManager.getAccessToken()),
98  				obj.toJSONString());
99  		return response.getAsJson().getString("login_url");
100 	}
101 }