获取授权用户信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dushanlitian/article/details/72522829
 //授权页面
//private String secret="36499a20d720ddee483954f21cc28766";
//private String state="STATE";
//private String authoUrl =getMenuOauthUrl(appId, url, state);
//引导用户授权获取用户基本信息后,跳转到首页去,根据不同appid来生成不同关注首页,appid需要从request里取出来
@RequestMapping("/author")
public String author(HttpServletRequest request,
HttpServletResponse response, Map<String, Object> modelMap) throws IOException{
HttpSession session=request.getSession();


// 用户同意授权后并获取的code
if (session.getAttribute("appid") != null) {
session.removeAttribute("appid");// 清空session,不同系统相同用户的appid不同
}
if (session.getAttribute("openid") != null) {
session.removeAttribute("openid");// 清空session,不同系统相同用户的openid不同
}
//session.setAttribute("openid","111");//281提交前去掉设置
//session.setAttribute("appid", "wxdcc8625c52014d3f");//
String appid = request.getParameter("appid");
session.setAttribute("appid", appid);
String openid = (String) session.getAttribute("openid");


CompanyEntity company = companyModel.getByAppid(appid);
String indexPhotoURL = company.getIndexPhotoURL();
if (company != null && indexPhotoURL != null
&& indexPhotoURL.contains("http")) {
// 公司首页图片
} else {
company.setIndexPhotoURL("");
}
session.setAttribute("companyTitle", company.getName());
String servername = request.getServerName();// http://www.b-union.net/
String source = ("http://" + servername + company.getRedirect_uri());
String url = CommonUtil.urlEncodeUTF8(source);
String secret = company.getSecret();
String returnPage = null;
// 获取openid
if (openid == null) {
String code = request.getParameter("code");
if (code == null) {
// 重定向微信获取code
String rurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ url
+ "&response_type=code"
+ "&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect";
log.info("url=" + url);
// response.sendRedirect(rurl);
return "redirect:" + rurl;
} else {
// 通过code 获取openid
Map map = WeiXinUtils.getMapByCodeS(code, appid, secret);
if (map == null) {
log.info("1url=" + url);
String u = url;
int i = u.indexOf("?");
if (i > 0) {
u = u.substring(0, i);
}
String rurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ url
+ "&response_type=code"
+ "&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect";
return "redirect:" + rurl;
}
openid = map.get("openid").toString();
session.setAttribute("openid", openid);
session.setAttribute("appid", appid);
   }
}


WechatAccessUserEntity wechatAccessUserEntity = wechatAccessUserModel
.getByOpenid(openid);
if (wechatAccessUserEntity != null) {
// 判断是否已经退出登录了
if (wechatAccessUserEntity.getLogoutStatus() == 0) {
if (company.getSupport() == 1 && company.getUseCoupon() == 1) {
modelMap.put("company", company);
return "/weixin/index/index11";
} else if (company.getSupport() == 0
&& company.getUseCoupon() == 0) {
modelMap.put("company", company);
return "/weixin/index/index00";
} else if (company.getSupport() == 0
&& company.getUseCoupon() == 1) {
modelMap.put("company", company);
return "/weixin/index/index01";
}
} else if (wechatAccessUserEntity.getLogoutStatus() == 1) {
// 重新获取验证码进行登录
session.setAttribute("appid", appid);
session.setAttribute("openid", openid);
return "/weixin/index/login";
}
}else{
return "/weixin/index/login";
}
return "/weixin/index/login";
}

猜你喜欢

转载自blog.csdn.net/dushanlitian/article/details/72522829