@At
public String getCode(@Param("code")String code) {
PropertiesProxy conf =new PropertiesProxy();
logger.info(code);
conf.setPaths("/custom");
String appid = conf.get("weixin.appid");
String appsecret = conf.get("weixin.appsecret");
String fmt = "https://api.weixin.qq.com/sns/oauth2/access_token"
+ "?appid=%s"
+ "&secret=%s"
+ "&code=%s"
+ "&grant_type=authorization_code";
String url = String.format(fmt,appid,appsecret,code);
logger.info(url);
String json = Http.get(url).getContent();
logger.info("resp"+json);
NutMap map = Json.fromJson(NutMap.class, json);
logger.info("map"+map);
String openid = map.getString("openid");
logger.info("用户的标识"+openid);
return openid;
}
问答
微信用户鉴权获取openid,code可以得到Openid为空
标签:
无
9 回复
@At("/?/back")
@Ok("re")
@Filters
public String back(String wxid, @Param("code") String code, HttpServletRequest req, HttpSession session) throws Exception {
String wechat_goto_url = Strings.sNull(session.getAttribute("wechat_goto_url"));
Wx_config config = wxConfigService.fetch(wxid);
String fmt = "https://api.weixin.qq.com/sns/oauth2/access_token"
+ "?appid=%s"
+ "&secret=%s"
+ "&code=%s"
+ "&grant_type=authorization_code";
String url = String.format(fmt, config.getAppid(), config.getAppsecret(), code);
String json = Http.get(url).getContent();
NutMap map = Json.fromJson(NutMap.class, json);
String openid = map.getString("openid");
WxApi2 wxApi2 = wxService.getWxApi2(wxid);
Wx_user usr = wxUserService.fetch(Cnd.where("openid", "=", openid).and("wxid", "=", wxid));
WxResp resp = wxApi2.user_info(openid, "zh_CN");
if (usr == null) {
usr = Json.fromJson(Wx_user.class, Json.toJson(resp.user()));
if (usr != null && usr.getNickname() != null) {
usr.setNickname(Strings.sNull(EmojiParser.parseToAliases(usr.getNickname(), EmojiParser.FitzpatrickAction.REMOVE)));
usr.setSubscribeAt(resp.user().getSubscribe_time());
usr.setWxid(wxid);
wxUserService.insert(usr);
}
} else {
String id = usr.getId();
usr = Json.fromJson(Wx_user.class, Json.toJson(resp.user()));
usr.setNickname(EmojiParser.parseToAliases(usr.getNickname(), EmojiParser.FitzpatrickAction.REMOVE));
usr.setOpAt(Times.getTS());
usr.setWxid(wxid);
usr.setId(id);
wxUserService.updateIgnoreNull(usr);
}
session.setAttribute("wxid", wxid);
session.setAttribute("openid", usr == null ? "" : usr.getOpenid());
return "redirect:" + wechat_goto_url;
}
添加回复
请先登陆