删除AuthResult类,将BaseApi移入到weixin4j-base工程
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.foxinmy.weixin4j.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;
|
||||
|
||||
/**
|
||||
* 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://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();
|
||||
protected static ResourceBundle weixinBundle;
|
||||
static {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -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
|
||||
* (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_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="
|
||||
|
||||
Reference in New Issue
Block a user