删除AuthResult类,将BaseApi移入到weixin4j-base工程

This commit is contained in:
jy.hu 2014-11-27 22:29:23 +08:00
parent 18954cfde6
commit 008c5ce4a8
26 changed files with 109 additions and 178 deletions

View File

@ -147,6 +147,10 @@ https://github.com/foxinmy/weixin4j/releases
+ **weixin4j-qy**: 新增netty服务与消息分发
* 2014-11-27
+ **weixin-base**: 将BaseApi移入
接下来
------
* 微信小店

View File

@ -1,4 +1,4 @@
package com.foxinmy.weixin4j.mp.api;
package com.foxinmy.weixin4j.api;
import java.util.Map;
import java.util.ResourceBundle;
@ -12,20 +12,19 @@ import com.thoughtworks.xstream.core.ClassLoaderReference;
import com.thoughtworks.xstream.mapper.DefaultMapper;
/**
*
* API基础
* @className BaseApi
* @author jy.hu
* @date 2014年9月26日
* @since JDK 1.7
* @see <a href="http://mp.weixin.qq.com/wiki/index.php">api文档</a>
* @see <a href="http://mp.weixin.qq.com/wiki/index.php">微信公众平台API文档</a>
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php">微信企业号API文档</a>
*/
public class BaseApi {
protected final HttpRequest request = new HttpRequest();
protected final static XStream mapXstream = XStream.get();
private final static ResourceBundle weixinBundle;
protected static ResourceBundle weixinBundle;
static {
weixinBundle = ResourceBundle
.getBundle("com/foxinmy/weixin4j/mp/api/weixin");
mapXstream.alias("xml", Map.class);
mapXstream.registerConverter(new Map2ObjectConverter(new DefaultMapper(
new ClassLoaderReference(XStream.class.getClassLoader()))));

View File

@ -12,6 +12,7 @@ public final class Consts {
public static final String PROTOCOL_FILE = "file";
public static final String PROTOCOL_JAR = "jar";
public static final String OAUTH_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
public static final String MP_ASSESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
public static final String QY_ASSESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";

View File

@ -1,6 +1,10 @@
package com.foxinmy.weixin4j.model;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.lang3.StringUtils;
/**
* 微信账号信息
@ -64,6 +68,39 @@ public abstract class WeixinAccount implements Serializable {
this.encodingAesKey = encodingAesKey;
}
/**
* 拼接授权URL
*
* @param redirectUri
* 授权后重定向的回调链接地址
* @param scope
* 应用授权作用域snsapi_base
* 不弹出授权页面直接跳转只能获取用户openidsnsapi_userinfo
* 弹出授权页面可通过openid拿到昵称性别所在地并且即使在未关注的情况下只要用户授权也能获取其信息
* @param state
* 重定向后会带上state参数开发者可以填写a-zA-Z0-9的参数值
* @return 授权URL
*/
public String getOauthAuthorizeUrl(String redirectUri, String scope,
String state) {
if (StringUtils.isBlank(scope)) {
scope = "snsapi_base";
}
if (StringUtils.isBlank(state)) {
state = "STATE";
}
try {
return String.format(
Consts.OAUTH_AUTHORIZE_URL,
URLEncoder.encode(redirectUri,
org.apache.http.Consts.UTF_8.name()), id, scope,
state);
} catch (UnsupportedEncodingException ignore) {
;
}
return "";
}
@Override
public String toString() {
return "WeixinAccount [id=" + id + ", secret=" + secret + ", token="

View File

@ -21,7 +21,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @since JDK 1.7
* @see <a href="http://dkf.qq.com">多客服说明</a>
*/
public class CustomApi extends BaseApi {
public class CustomApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -21,7 +21,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* BB%84%E7%AE%A1%E7%90%86%E6%8E%A5%E5%8F%A3">分组接口</a>
* @see com.foxinmy.weixin4j.mp.model.Group
*/
public class GroupApi extends BaseApi {
public class GroupApi extends MpApi {
private final TokenHolder tokenHolder;
public GroupApi(TokenHolder tokenHolder) {

View File

@ -22,7 +22,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @since JDK 1.7
* @see
*/
public class HelperApi extends BaseApi {
public class HelperApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -29,7 +29,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E9%AB%98%E7%BA%A7%E7%BE%A4%E5%8F%91%E6%8E%A5%E5%8F%A3">群发接口</a>
* @see com.foxinmy.weixin4j.msg.model.MpArticle
*/
public class MassApi extends BaseApi {
public class MassApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -29,7 +29,7 @@ import com.foxinmy.weixin4j.util.IOUtil;
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD%E5%A4%9A%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6">上传多媒体文件</a>
* @see com.foxinmy.weixin4j.type.MediaType
*/
public class MediaApi extends BaseApi {
public class MediaApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @since JDK 1.7
* @see com.foxinmy.weixin4j.model.Button
*/
public class MenuApi extends BaseApi {
public class MenuApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -0,0 +1,22 @@
package com.foxinmy.weixin4j.mp.api;
import java.util.ResourceBundle;
import com.foxinmy.weixin4j.api.BaseApi;
/**
* 微信公众平台API
*
* @className MpApi
* @author jy.hu
* @date 2014年9月26日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.api.BaseApi
* @see <a href="http://mp.weixin.qq.com/wiki/index.php">api文档</a>
*/
public class MpApi extends BaseApi {
static {
weixinBundle = ResourceBundle
.getBundle("com/foxinmy/weixin4j/mp/api/weixin");
}
}

View File

@ -19,7 +19,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E5%AE%A2%E6%9C%8D%E6%B6%88%E6%81%AF">客服消息</a>
* @see com.foxinmy.weixin4j.mp.message.NotifyMessage
*/
public class NotifyApi extends BaseApi {
public class NotifyApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -68,7 +68,7 @@ import com.foxinmy.weixin4j.util.RandomUtil;
* @since JDK 1.7
* @see
*/
public class PayApi extends BaseApi {
public class PayApi extends MpApi {
private final TokenHolder tokenHolder;
private final WeixinMpAccount weixinAccount;

View File

@ -22,7 +22,7 @@ import com.foxinmy.weixin4j.util.ConfigUtil;
* @see <a
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E7%94%9F%E6%88%90%E5%B8%A6%E5%8F%82%E6%95%B0%E7%9A%84%E4%BA%8C%E7%BB%B4%E7%A0%81">二维码支持</a>
*/
public class QrApi extends BaseApi {
public class QrApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -17,7 +17,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @since JDK 1.7
* @see
*/
public class TmplApi extends BaseApi {
public class TmplApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -25,7 +25,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @since JDK 1.7
* @see com.foxinmy.weixin4j.mp.model.User
*/
public class UserApi extends BaseApi {
public class UserApi extends MpApi {
private final TokenHolder tokenHolder;

View File

@ -1,98 +0,0 @@
package com.foxinmy.weixin4j.mp.model;
import java.io.Serializable;
/**
* 网页授权结果
*
* @className AuthResult
* @author jy.hu
* @date 2014年4月8日
* @since JDK 1.7
* @see <a
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E7%BD%91%E9%A1%B5%E6%8E%88%E6%9D%83%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E5%9F%BA%E6%9C%AC%E4%BF%A1%E6%81%AF">网页授权获取用户基本资料</a>
*/
public class AuthResult implements Serializable {
private static final long serialVersionUID = 654855396163854805L;
/**
* 网页授权获取code
*
* @className AuthScope
* @author jy.hu
* @date 2014年4月8日
* @since JDK 1.7
* @see <a
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E7%BD%91%E9%A1%B5%E6%8E%88%E6%9D%83%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E5%9F%BA%E6%9C%AC%E4%BF%A1%E6%81%AF#.E7.AC.AC.E4.B8.80.E6.AD.A5.EF.BC.9A.E7.94.A8.E6.88.B7.E5.90.8C.E6.84.8F.E6.8E.88.E6.9D.83.EF.BC.8C.E8.8E.B7.E5.8F.96code">获取code</a>
*/
public enum AuthScope {
BASE("snsapi_base"), USERINFO("snsapi_userinfo");
private String name;
AuthScope(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public enum AuthCode {
OK, FAILED, REDIRECT
}
private AuthCode authCode;
private String location;
private OauthToken accessToken;
public AuthResult(String location) {
this.location = location;
this.authCode = AuthCode.REDIRECT;
}
public AuthResult(OauthToken accessToken) {
this.authCode = AuthCode.OK;
this.accessToken = accessToken;
}
public AuthResult(String location, AuthCode authCode) {
this.location = location;
this.authCode = authCode;
}
public AuthCode getAuthCode() {
return authCode;
}
public void setAuthCode(AuthCode authCode) {
this.authCode = authCode;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public OauthToken getAccessToken() {
return accessToken;
}
public void setAccessToken(OauthToken accessToken) {
this.accessToken = accessToken;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[AuthResult authCode=").append(authCode);
sb.append(", location=").append(location);
sb.append(", accessToken=").append(accessToken).append("]");
return sb.toString();
}
}

View File

@ -32,6 +32,7 @@ public class UserTest extends TokenTest {
User user = userApi.getUser("owGBft_vbBbOaQOmpEUE4xDLeRSU");
Assert.assertNotNull(user);
System.out.println(user);
following();
}
@Test

View File

@ -1,57 +0,0 @@
package com.foxinmy.weixin4j.qy.api;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.foxinmy.weixin4j.http.HttpRequest;
import com.foxinmy.weixin4j.xml.Map2ObjectConverter;
import com.foxinmy.weixin4j.xml.XStream;
import com.thoughtworks.xstream.core.ClassLoaderReference;
import com.thoughtworks.xstream.mapper.DefaultMapper;
/**
*
* @className BaseApi
* @author jy.hu
* @date 2014年11月18日
* @since JDK 1.7
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php">api文档</a>
*/
public class BaseApi {
protected final HttpRequest request = new HttpRequest();
protected final static XStream mapXstream = XStream.get();
private final static ResourceBundle weixinBundle;
static {
weixinBundle = ResourceBundle
.getBundle("com/foxinmy/weixin4j/qy/api/weixin");
mapXstream.alias("xml", Map.class);
mapXstream.registerConverter(new Map2ObjectConverter(new DefaultMapper(
new ClassLoaderReference(XStream.class.getClassLoader()))));
}
protected String map2xml(Map<?, ?> map) {
return mapXstream.toXML(map).replaceAll("__", "_");
}
@SuppressWarnings("unchecked")
protected Map<String, String> xml2map(String xml) {
return mapXstream.fromXML(xml, Map.class);
}
protected String getRequestUri(String key) {
String url = weixinBundle.getString(key);
Pattern p = Pattern.compile("(\\{[^\\}]*\\})");
Matcher m = p.matcher(url);
StringBuffer sb = new StringBuffer();
String sub = null;
while (m.find()) {
sub = m.group();
m.appendReplacement(sb,
getRequestUri(sub.substring(1, sub.length() - 1)));
}
m.appendTail(sb);
return sb.toString();
}
}

View File

@ -22,7 +22,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8">管理部门说明</a>
*/
public class DepartApi extends BaseApi {
public class DepartApi extends QyApi {
private final TokenHolder tokenHolder;
public DepartApi(TokenHolder tokenHolder) {

View File

@ -29,7 +29,7 @@ import com.foxinmy.weixin4j.util.IOUtil;
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E5%A4%9A%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6">管理多媒体文件</a>
* @see com.foxinmy.weixin4j.type.MediaType
*/
public class MediaApi extends BaseApi {
public class MediaApi extends QyApi {
private final TokenHolder tokenHolder;

View File

@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @since JDK 1.7
* @see com.foxinmy.weixin4j.model.Button
*/
public class MenuApi extends BaseApi {
public class MenuApi extends QyApi {
private final TokenHolder tokenHolder;

View File

@ -26,7 +26,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @see com.foxinmy.weixin4j.msg.model.News
* @see com.foxinmy.weixin4j.msg.model.MpNews
*/
public class NotifyApi extends BaseApi {
public class NotifyApi extends QyApi {
private final TokenHolder tokenHolder;

View File

@ -0,0 +1,22 @@
package com.foxinmy.weixin4j.qy.api;
import java.util.ResourceBundle;
import com.foxinmy.weixin4j.api.BaseApi;
/**
* 微信企业号API
*
* @className QyApi
* @author jy.hu
* @date 2014年11月18日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.api.BaseApi
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php">api文档</a>
*/
public class QyApi extends BaseApi {
static {
weixinBundle = ResourceBundle
.getBundle("com/foxinmy/weixin4j/qy/api/weixin");
}
}

View File

@ -22,7 +22,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE">管理标签</a>
*/
public class TagApi extends BaseApi {
public class TagApi extends QyApi {
private final TokenHolder tokenHolder;
public TagApi(TokenHolder tokenHolder) {

View File

@ -23,7 +23,7 @@ import com.foxinmy.weixin4j.token.TokenHolder;
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98">管理成员说明</a>
*/
public class UserApi extends BaseApi {
public class UserApi extends QyApi {
private final TokenHolder tokenHolder;
public UserApi(TokenHolder tokenHolder) {