// 點擊微信菜單獲取code
String code = request.getParameter("code");
if (org.apache.commons.lang.StringUtils.isNotBlank(code)) {//經過code判斷請求來源微信
log.info("___微信訪問___code:" + code);
// 獲取網頁受權access_token
if(null == (request.getSession().getAttribute("extSource"))){ //返回後不會二次解析code致使崩潰
WeiXinOauth2Token weixinOauth2Token = AdvancedUtil
.getOauth2AccessToken("APPID", "APPSECRET", code);
String openId = weixinOauth2Token.getOpenId();
request.getSession().setAttribute("wxopenId", openId);
request.getSession().setAttribute("extSource", 1);
}
}
if (request.getSession().getAttribute("wxopenId") != null) {
String openId = (String) request.getSession().getAttribute(
"wxopenId");
log.info("___微信訪問___openid:" + openId);
MemberExtRels meExtRels = new MemberExtRels();
meExtRels.setExtId(openId);
meExtRels = memberExtRelsMapper.selectMemberExtRels(meExtRels);
if (null != meExtRels) { //若表中有此openid,則獲取相關boid,從而獲取userinfo。
userInfo = this.getWeiXinUserInfo(meExtRels.getBoId(),
request);
log.info("___微信訪問___userinfo:" + userInfo);
} else if (null != userInfo) { //將經過openid和boid獲取的userinfo插入對應表中。
memberExtRelsService.addMemberExtRels(openId,
userInfo.getBoId());
}else{ //若沒有相關信息,則爲新用戶,發送至登陸頁面。
try {
response.sendRedirect(request.getContextPath()
+ mid + "/mobileLogin.do");
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}apache
public class WeiXinOauth2Token {
// 網頁受權接口調用憑證
private String accessToken;
// 憑證有效時長
private int expiresIn;
// 用於刷新憑證
private String refreshToken;
// 用戶標識
private String openId;
// 用戶受權做用域
private String scope;json
相應的get set方法。
api
}微信
/**
* 獲取網頁受權憑證
* 獲取openid
* @param appId
* 公衆帳號的惟一標識
* @param appSecret
* 公衆帳號的密鑰
* @param code
* @return WeixinAouth2Token
*/
public static WeiXinOauth2Token getOauth2AccessToken(String appId,
String appSecret, String code) {
WeiXinOauth2Token wat = null;
// 拼接請求地址
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
requestUrl = requestUrl.replace("APPID", WeiXinConstant.APPID);
requestUrl = requestUrl.replace("SECRET", WeiXinConstant.APPSECRET);
requestUrl = requestUrl.replace("CODE", code);
log.info("微信請求地址:"+requestUrl);
// 獲取網頁受權憑證
JSONObject jsonObject = CommonUtil
.httpsRequest(requestUrl, "GET", null);
if (null != jsonObject) {
try {
wat = new WeiXinOauth2Token();
wat.setAccessToken(jsonObject.getString("access_token"));
wat.setExpiresIn(jsonObject.getInt("expires_in"));
wat.setRefreshToken(jsonObject.getString("refresh_token"));
wat.setOpenId(jsonObject.getString("openid"));
wat.setScope(jsonObject.getString("scope"));
} catch (Exception e) {
wat = null;
int errorCode = jsonObject.getInt("errcode");
String errorMsg = jsonObject.getString("errmsg");
log.error("獲取網頁受權憑證失敗 errcode:{} errmsg:{}", errorCode,
errorMsg);
}
}
return wat;
}app
MemberExtRels表經過generator反編譯獲得。加入相應查詢方法。this
<select id="selectMemberExtRelsByExtId" resultMap="BaseResultMap"
parameterType="com.nxmall.weixin.entity.MemberExtRels">
select
<include refid="Base_Column_List" />
from nxmall_member_ext_rels
where ext_id = #{extId,jdbcType=VARCHAR}
</select>.net
public MemberExtRels selectMemberExtRels(MemberExtRels memberExtRels) {
return getSessionTemplate().selectOne(
getStatement("selectMemberExtRelsByExtId"), memberExtRels);
}code
public int addMemberExtRels(String openId, Long boId) {
MemberExtRels meExtRels = new MemberExtRels();
meExtRels.setExtId(openId);
meExtRels = memberExtRelsMapper.selectMemberExtRels(meExtRels);
if (meExtRels == null) {
MemberExtRels memberExtRels = new MemberExtRels();
memberExtRels.setExtId(openId);
memberExtRels.setBoId(boId);
memberExtRels.setExtSource(WeiXinConstant.EXTSOURCE);
return memberExtRelsMapper.insertSelective(memberExtRels);
}
return 0;
}
token