me.chanjar.weixin.mp.bean.result.WxMpUser Java Examples

The following examples show how to use me.chanjar.weixin.mp.bean.result.WxMpUser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseWxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken token, String lang) throws WxErrorException {
  if (lang == null) {
    lang = "zh_CN";
  }

  String url = String.format(WxMpService.OAUTH2_USERINFO_URL, token.getAccessToken(), token.getOpenId(), lang);

  try {
    RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
    String responseText = executor.execute(url, null);
    return WxMpUser.fromJson(responseText);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #2
Source File: WxMpOAuth2Servlet.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");
  response.setStatus(HttpServletResponse.SC_OK);

  String code = request.getParameter("code");
  try {
    response.getWriter().println("<h1>code</h1>");
    response.getWriter().println(code);

    WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
    response.getWriter().println("<h1>access token</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

    WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
    response.getWriter().println("<h1>user info</h1>");
    response.getWriter().println(wxMpUser.toString());

    wxMpOAuth2AccessToken = wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
    response.getWriter().println("<h1>after refresh</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

  } catch (WxErrorException e) {
    e.printStackTrace();
  }

  response.getWriter().flush();
  response.getWriter().close();

}
 
Example #3
Source File: WxMpUserGsonAdapter.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public WxMpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  JsonObject o = json.getAsJsonObject();
  WxMpUser wxMpUser = new WxMpUser();
  Integer subscribe = GsonHelper.getInteger(o, "subscribe");
  if (subscribe != null) {
    wxMpUser.setSubscribe(new Integer(0).equals(subscribe) ? false : true);
  }
  wxMpUser.setCity(GsonHelper.getString(o, "city"));
  wxMpUser.setCountry(GsonHelper.getString(o, "country"));
  wxMpUser.setHeadImgUrl(GsonHelper.getString(o, "headimgurl"));
  wxMpUser.setLanguage(GsonHelper.getString(o, "language"));
  wxMpUser.setNickname(GsonHelper.getString(o, "nickname"));
  wxMpUser.setOpenId(GsonHelper.getString(o, "openid"));
  wxMpUser.setProvince(GsonHelper.getString(o, "province"));
  wxMpUser.setSubscribeTime(GsonHelper.getLong(o, "subscribe_time"));
  wxMpUser.setUnionId(GsonHelper.getString(o, "unionid"));
  Integer sexId = GsonHelper.getInteger(o, "sex");
  wxMpUser.setRemark(GsonHelper.getString(o, "remark"));
  wxMpUser.setGroupId(GsonHelper.getInteger(o, "groupid"));
  wxMpUser.setSexId(sexId);
  if(new Integer(1).equals(sexId)) {
    wxMpUser.setSex("男");
  } else if (new Integer(2).equals(sexId)) {
    wxMpUser.setSex("女");
  } else {
    wxMpUser.setSex("未知");
  }
  return wxMpUser;
}
 
Example #4
Source File: WxMpUserServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public WxMpUser userInfo(String openid, String lang) throws WxErrorException {
  String url = API_URL_PREFIX + "/info";
  lang = lang == null ? "zh_CN" : lang;
  String responseContent = this.wxMpService.get(url,
    "openid=" + openid + "&lang=" + lang);
  return WxMpUser.fromJson(responseContent);
}
 
Example #5
Source File: WxMpUserServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException {
  String url = API_URL_PREFIX + "/info/batchget";
  String responseContent = this.wxMpService.post(url,
    userQuery.toJsonString());
  return WxMpUser.fromJsonList(responseContent);
}
 
Example #6
Source File: WxRedirectController.java    From black-shop with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/greet")
public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {

	WxMpService mpService = WxMpConfiguration.getMpServices().get(appid);

	try {
		WxMpOAuth2AccessToken accessToken = mpService.oauth2getAccessToken(code);
		WxMpUser user = mpService.oauth2getUserInfo(accessToken, null);
		map.put("user", user);
	} catch (WxErrorException e) {
		e.printStackTrace();
	}

	return "greet_user";
}
 
Example #7
Source File: WxMpUserServiceImplTest.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public void testUserInfoListByWxMpUserQuery() throws WxErrorException {
  WxMpUserQuery query = new WxMpUserQuery();
  query.add(this.configProvider.getOpenid(), "zh_CN");
  List<WxMpUser> userList = this.wxService.getUserService()
    .userInfoList(query);
  Assert.assertEquals(userList.size(), 1);
  System.out.println(userList);
}
 
Example #8
Source File: WxMpOAuth2Servlet.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
  throws IOException {

  response.setContentType("text/html;charset=utf-8");
  response.setStatus(HttpServletResponse.SC_OK);

  String code = request.getParameter("code");
  try {
    response.getWriter().println("<h1>code</h1>");
    response.getWriter().println(code);

    WxMpOAuth2AccessToken wxMpOAuth2AccessToken = this.wxMpService.oauth2getAccessToken(code);
    response.getWriter().println("<h1>access token</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

    WxMpUser wxMpUser = this.wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
    response.getWriter().println("<h1>user info</h1>");
    response.getWriter().println(wxMpUser.toString());

    wxMpOAuth2AccessToken = this.wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
    response.getWriter().println("<h1>after refresh</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

  } catch (WxErrorException e) {
    e.printStackTrace();
  }

  response.getWriter().flush();
  response.getWriter().close();

}
 
Example #9
Source File: WxMpUserServiceImplTest.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public void testUserInfoList() throws WxErrorException {
  List<String> openids = new ArrayList<>();
  openids.add(this.configProvider.getOpenid());
  List<WxMpUser> userList = this.wxService.getUserService()
    .userInfoList(openids);
  Assert.assertEquals(userList.size(), 1);
  System.out.println(userList);
}
 
Example #10
Source File: WxMpUserGsonAdapter.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  JsonObject o = json.getAsJsonObject();
  WxMpUser user = new WxMpUser();
  Integer subscribe = GsonHelper.getInteger(o, "subscribe");
  if (subscribe != null) {
    user.setSubscribe(!new Integer(0).equals(subscribe));
  }
  user.setCity(GsonHelper.getString(o, "city"));
  user.setCountry(GsonHelper.getString(o, "country"));
  user.setHeadImgUrl(GsonHelper.getString(o, "headimgurl"));
  user.setLanguage(GsonHelper.getString(o, "language"));
  user.setNickname(GsonHelper.getString(o, "nickname"));
  user.setOpenId(GsonHelper.getString(o, "openid"));
  user.setProvince(GsonHelper.getString(o, "province"));
  user.setSubscribeTime(GsonHelper.getLong(o, "subscribe_time"));
  user.setUnionId(GsonHelper.getString(o, "unionid"));
  user.setRemark(GsonHelper.getString(o, "remark"));
  user.setGroupId(GsonHelper.getInteger(o, "groupid"));
  user.setTagIds(GsonHelper.getLongArray(o, "tagid_list"));
  user.setPrivileges(GsonHelper.getStringArray(o, "privilege"));
  user.setSubscribeScene(GsonHelper.getString(o, "subscribe_scene"));
  user.setQrScene(GsonHelper.getString(o, "qr_scene"));
  user.setQrSceneStr(GsonHelper.getString(o, "qr_scene_str"));

  Integer sex = GsonHelper.getInteger(o, "sex");
  if (sex != null) {
    user.setSex(sex);
    switch (sex) {
      case 1:
        user.setSexDesc("男");
        break;
      case 2:
        user.setSexDesc("女");
        break;
      default:
        user.setSexDesc("未知");
    }

  }
  return user;
}
 
Example #11
Source File: WxMpUserAPITest.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public void testUserInfo() throws WxErrorException  {
  ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
  WxMpUser user = wxService.userInfo(configProvider.getOpenId(), null);
  Assert.assertNotNull(user);
}
 
Example #12
Source File: WxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public WxMpUser userInfo(String openid, String lang) throws WxErrorException {
  String url = "https://api.weixin.qq.com/cgi-bin/user/info";
  lang = lang == null ? "zh_CN" : lang;
  String responseContent = execute(new SimpleGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang);
  return WxMpUser.fromJson(responseContent);
}
 
Example #13
Source File: WechatRabbitListener.java    From fw-cloud-framework with MIT License 4 votes vote down vote up
@RabbitHandler
public void receive(WechatNotifyBean wechatNotifyBean) {
	long start = System.currentTimeMillis();
	log.info("【wxauth.wechatRabbit】:exec receive start|" + start);
	WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wechatNotifyBean.getWxMpOAuth2AccessToken();
	if (null == wxMpOAuth2AccessToken || StringHelper.isBlank(wechatNotifyBean.getWechatId())
			|| StringHelper.isBlank(wxMpOAuth2AccessToken.getOpenId())) return;
	log.info("【wxauth.wechatRabbit】:exec start");
	try {
		WxMpUser wxMpUser = null;
		String openId = wxMpOAuth2AccessToken.getOpenId();
		AuthInfo authInfo = authInfoService.findByOpenIdAndWechatId(openId, wechatNotifyBean
				.getWechatId());
		if (null == authInfo // 大于2小时
				|| System.currentTimeMillis() - authInfo.getUpdateTime().getTime() > 7200000) {
			boolean isSopeBase = wechatNotifyBean.isSopeBase();
			if (isSopeBase) {
				log.info("【wxauth.openId】静默登录");
				wxMpUser = wxService.getUserService().userInfo(openId);
			} else {
				// refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
				String refreshToken = wxMpOAuth2AccessToken.getRefreshToken();
				wxMpOAuth2AccessToken = wxService.oauth2refreshAccessToken(refreshToken);
				log.info("【wxauth.openId】主动登录");
				// 拉取用户信息(需scope为 snsapi_userinfo)
				wxMpUser = wxService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
			}

			if (null != wxMpUser) {
				if (null == authInfo) authInfo = new AuthInfo();
				authInfo.setOpenId(openId);
				authInfo.setWechatId(wechatNotifyBean.getWechatId());
				authInfo.setNickName(WebUtils.buildURLEncoder(EmojiUtils.toHtml(wxMpUser
						.getNickname())));
				authInfo.setHeadImgUrl(wxMpUser.getHeadImgUrl());
				authInfo.setCity(wxMpUser.getCity());
				authInfo.setProvince(wxMpUser.getProvince());
				authInfo.setLanguage(wxMpUser.getLanguage());
				authInfo.setRemark(wxMpUser.getRemark());
				authInfo.setSexDesc(wxMpUser.getSexDesc());
				authInfo.setSex(wxMpUser.getSex());
				authInfo.setCountry(wxMpUser.getCountry());
				authInfo.setRefreshToken(wxMpOAuth2AccessToken.getRefreshToken());
				if (null == authInfo.getCreateTime()) authInfo.setCreateTime(new Date());
				authInfo.setUpdateTime(new Date());
				authInfoService.saveOrUpdate(authInfo);
			}
		}
		log.info("【wxauth.wechatRabbit】:openId|" + openId);
	} catch (WxErrorException ex) {
		ex.printStackTrace();
		log.info("【wxauth.wechatRabbit】exception:" + ex.getError().getErrorMsg());
	}
	log.info("【wxauth.wechatRabbit】:exec finished 耗时:" + (System.currentTimeMillis() - start));
}
 
Example #14
Source File: WxMpUserServiceImplTest.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public void testUserInfo() throws WxErrorException {
  WxMpUser user = this.wxService.getUserService()
    .userInfo(this.configProvider.getOpenid(), null);
  Assert.assertNotNull(user);
  System.out.println(user);
}
 
Example #15
Source File: WxMpUserServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public List<WxMpUser> userInfoList(List<String> openids)
  throws WxErrorException {
  return this.userInfoList(new WxMpUserQuery(openids));
}
 
Example #16
Source File: WxMpUserServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpUser userInfo(String openid) throws WxErrorException {
  return this.userInfo(openid, null);
}
 
Example #17
Source File: WxMpUserService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 获取用户基本信息(语言为默认的zh_CN 简体)
 * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN
 * http请求方式: GET
 * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
 * </pre>
 *
 * @param openid 用户openid
 */
WxMpUser userInfo(String openid) throws WxErrorException;
 
Example #18
Source File: WxMpService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以
 * </pre>
 *
 * @param lang zh_CN, zh_TW, en
 */
WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException;
 
Example #19
Source File: WxMpUserService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 获取用户基本信息列表
 * 开发者可通过该接口来批量获取用户基本信息。最多支持一次拉取100条。
 * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN
 * http请求方式: POST
 * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN
 * </pre>
 *
 * @param userQuery 详细查询参数
 */
List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException;
 
Example #20
Source File: WxMpUserService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 获取用户基本信息列表
 * 开发者可通过该接口来批量获取用户基本信息。最多支持一次拉取100条。
 * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN
 * http请求方式: POST
 * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN
 * </pre>
 *
 * @param openids 用户openid列表
 */
List<WxMpUser> userInfoList(List<String> openids) throws WxErrorException;
 
Example #21
Source File: WxMpUserService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 获取用户基本信息
 * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN
 * http请求方式: GET
 * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
 * </pre>
 *
 * @param openid 用户openid
 * @param lang   语言,zh_CN 简体(默认),zh_TW 繁体,en 英语
 */
WxMpUser userInfo(String openid, String lang) throws WxErrorException;