1. 程式人生 > >微信公眾號的授權及分享

微信公眾號的授權及分享

保存 用戶信息 win openid code suse log ken 需求

最近忙著做一個登錄公眾號獲取用戶信息及用戶分享後可以得到積分。

首先用戶點擊一個鏈接

window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=#&redirect_uri=#&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"

appid是你公眾號裏的

redirect_ur是調用微信返回數據,咱們系統接收code的方法,這個必須要是外網可以訪問的 http://www.....com/OhdWeixin/UserAction!getPersonal.action

response_type=code

cope=snsapi_userinfo

state=STATE#wechat_redirect"

1.獲取用戶信息de action;可以在這一步就獲取用戶的信息 放在自己的庫裏 ,我們需求是要在登錄的時候把登錄信息一起放在一個庫裏,

public String getPersonal() throws Exception {
String result="login";
BindUserFace bindUserFace = this.getBeanByBeanClass(BindUserFace.class);
String code = request.getParameter("code"); //用戶同意授權後,能獲取到code
if(!"authdeny".equals(code) && code!=null){
//獲取網頁授權accexx_token
WeixinOauth2TokenView weixinOauth2TokenView = AdvancedUtil.getOauth2AccessToken(CommonUtil.appId, CommonUtil.appSecret, code);
if(weixinOauth2TokenView!=null) {
//用戶標識
String openId = weixinOauth2TokenView.getOpenId();
//網頁授權接口訪問憑證
String accessToken = weixinOauth2TokenView.getAccessToken();
session.put("openid",openId);
session.put("accessToken",accessToken);
BindWxUser bindWxUser=bindUserFace.findBindWxUser(openId);//openId
if(bindWxUser!=null){
session.put("bindWxUser",bindWxUser);
getMallURL();
result="personal";//這是跳到個人中心頁面
}else{
result="login";//如果沒有
}
}
}
return result;
}

2.要獲取用戶的openid 來獲取用戶的頭像:

/**
* 登錄(註冊)
*/
public String login() throws Exception {
BindUserFace bindUserFace = this.getBeanByBeanClass(BindUserFace.class);
if(session.get("openid")==null && session.get("accessToken")==null) {
String code = request.getParameter("code");
//用戶同意授權
if(!"authdeny".equals(code) && code!=null){
String accessToken= (String) session.get("accessToken");
String openid= (String) session.get("openid");
BindWxUser binUs=bindUserFace.findBindWxUser(openid);//openId
if(binUs==null){
//獲取用戶信息
String tel=bindWxUser.getMobile();
bindWxUser=AdvancedUtil.getSNSUserInfo(accessToken,openid);
bindWxUser.setMobile(tel);
bindWxUser.setRecommender(recommender);
bindUserFace.addBindWxUser(bindWxUser);//保存用戶信息
session.put("bindWxUser",bindWxUser);
getMallURL();
}
}
}
return "personal";
}

微信公眾號的授權及分享