使用框架有什么好处?
形象一点说吧,假如你盖房子,你是自己一砖一瓦的盖简单呢,还是拿一个现成的架子往上面添东西简单呢?结果不言而喻吧,有一个半成品的架子,你只需要添上一些你自己额外需要加的东西就好了。这就是框架的好处。
反过来就有它的不足,就是不容易100%满足大家的需求,毕竟国内很多框架都是根据公司自己业务和需求制作的程序,所谓的框架就是从程序中提取出来的基本组件。
以前Opengl开发谈到过,市场上的游戏场景编辑器只有EA编辑器的40%-50%功能,我想如果现成的框架如果满足你的50%需求,已经很不错了。
原框架没有QYOauthAPI,我们现在又需要怎么办?
下面的代码我以OauthAPI为例,扩展和修改出我们需要的API——QYOauthAPI。
QYOauthGetTokenResponse 的结构和类型请参考疼讯的开发者文档。
package com.fastwixinextend;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.enums.OauthScope;
import com.github.sd4324530.fastweixin.api.response.BaseResponse;
import com.github.sd4324530.fastweixin.company.api.QYBaseAPI;
import com.github.sd4324530.fastweixin.company.api.config.QYAPIConfig;
import com.github.sd4324530.fastweixin.util.BeanUtil;
import com.github.sd4324530.fastweixin.util.JSONUtil;
import com.github.sd4324530.fastweixin.util.StrUtil;
public class QYOauthAPI extends QYBaseAPI {
private static final Logger LOG = LoggerFactory.getLogger(QYOauthAPI.class);
public QYOauthAPI(QYAPIConfig config) {
super(config);
}
/* 生成回调url,这个结果要求用户在微信中打开,即可获得token,并指向redirectUrl
*
* @param redirectUrl 用户自己设置的回调地址
* @param scope 授权作用域
* @param state 用户自带参数
* @return 回调url,用户在微信中打开即可开始授权
*/
public String getOauthPageUrl(String redirectUrl, OauthScope scope, String state) {
BeanUtil.requireNonNull(redirectUrl, "redirectUrl is null");
BeanUtil.requireNonNull(scope, "scope is null");
String userState = StrUtil.isBlank(state) ? "STATE" : state;
String url = null;
try {
url = URLEncoder.encode(redirectUrl, "UTF-8");
} catch (UnsupportedEncodingException e) {
LOG.error("异常", e);
}
StringBuilder stringBuilder = new StringBuilder("https://open.weixin.qq.com/connect/oauth2/authorize?");
stringBuilder.append("appid=").append(this.config.getCorpid())
.append("&redirect_uri=").append(url)
.append("&response_type=code&scope=").append(scope.toString())
.append("&state=")
.append(userState)
.append("#wechat_redirect");
return stringBuilder.toString();
}
/**
* http://qydev.weixin.qq.com/wiki/index.php?title=%E6%88%90%E5%91%98%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83
* 用户同意授权后在回调url中会得到code,调用此方法用code换token以及openid,所以如果仅仅是授权openid,到这步就结束了
*
* @param code 授权后得到的code
* @return token对象
*/
public QYOauthGetTokenResponse getQYOauthUserID(String code) {
BeanUtil.requireNonNull(code, "code is null");
QYOauthGetTokenResponse response = null;
String url = QYBaseAPI.BASE_API_URL + "cgi-bin/user/getuserinfo?access_token=" + config.getAccessToken() + "&code=" + code;
BaseResponse r = executeGet(url);
String resultJson = isSuccess(r.getErrcode()) ? r.getErrmsg() : r.toJsonString();
response = JSONUtil.toBean(resultJson, QYOauthGetTokenResponse.class);
return response;
}
package com.fastwixinextend;
import com.github.sd4324530.fastweixin.api.response.BaseResponse;
import com.github.sd4324530.fastweixin.api.response.OauthGetTokenResponse;
import com.github.sd4324530.fastweixin.util.JSONUtil;
public class QYOauthGetTokenResponse extends OauthGetTokenResponse {
/**
*
*/
private static final long serialVersionUID = -9000341304985119035L;
private String userid;
private String deviceid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getDeviceid() {
return deviceid;
}
public void setDeviceid(String deviceid) {
this.deviceid = deviceid;
}
public static void main(String[] arg) {
QYOauthGetTokenResponse response = null;
String resultJson = "{\"UserId\":\"lc\",\"DeviceId\":\"c79a18e903240ad8887df127f1af579d\"}";
response = JSONUtil.toBean(resultJson, QYOauthGetTokenResponse.class);
}
}