wx-tools 用户相关API

2023-03-22 17:20 更新

包括如下接口:
- createUserTag - 创建用户标签
- deleteUserTag - 删除用户标签
- queryAllUserTag - 查询所有用户标签
- updateUserTagName - 更新用户标签名字
- batchMovingUserToNewTag - 批量移动用户到某标签
- batchRemoveUserTag - 批量移除用户到某标签
- updateUserRemark - 更新用户备注
- getUserInfoByOpenId - 获取用户信息基本信息
- batchGetUserInfo - 批量查询用户信息
- batchGetUserOpenId - 批量查询关注者openid
- oauth2buildAuthorizationUrl - Oauth2.0 认证
- oauth2ToGetAccessToken- Oauth2.0 认证
- oauth2ToGetUserInfo- Oauth2.0 认证
- batchAddUserToBlackList- 批量把用户添加到黑名单
- batchRemoveUserFromBlackList- 批量把用户删除黑名单
- batchGetUsersFromBlackList- 批量获取黑名单用户列表

3.2.1 createUserTag 创建用户标签

    try {
        WxUserTagResult result = iService.createUserTag("标签名");
        System.out.println(result.getTag().getId());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.2 deleteUserTag 删除用户标签

    try {
        WxError result = iService.deleteUserTag(创建tag时返回的ID);
        System.out.println(result);
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.3 queryAllUserTag 查询所有用户标签

    try {
        WxUserTagResult result = iService.queryAllUserTag();
        System.out.println(result);
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.4 updateUserTagName 更新用户标签名字

    try {
        //标签ID,新标签名
        iService.updateUserTagName(1, "new tag name");
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.5 batchMovingUserToNewTag 批量移动用户在某标签

    List<String> openidList = new ArrayList<>();
    openidList.add("openid1");
    openidList.add("openid2");
    try {
        //用户Openid列表, tagID
        iService.batchMovingUserToNewTag(openidList, 2);
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.6 batchRemoveUserTag 批量移除用户在某标签 batchRemoveUserTag

    List<String> openidList = new ArrayList<>();
    openidList.add("openid1");
    openidList.add("openid2");
    try {
        //用户Openid列表, tagID
        iService.batchRemoveUserTag(openidList, 2);
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.7 updateUserRemark 修改用户备注

    try {
        iService.updateUserRemark("openid", "备注名");
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.8 getUserInfoByOpenId 获取用户基本信息

    try {
        WxUser user = iService.getUserInfoByOpenId(new WxUserGet("openid", WxConsts.LANG_CHINA));
        System.out.println(user.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.9 batchGetUserInfo 批量获取用户信息

    List<WxUserGet> list = new ArrayList<>();
    WxUserGet userGet1 = new WxUserGet("openid", WxConsts.LANG_CHINA);
    WxUserGet userGet2 = new WxUserGet("openid", WxConsts.LANG_CHINA);
    list.add(userGet1);
    list.add(userGet2);
    try {
        WxUserList userList = iService.batchGetUserInfo(list);
        System.out.println(userList.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.10 batchGetUserOpenId 批量获取关注者openid

    try {
        //第一个openid之后拉取
        WxUserListResult result = iService.batchGetUserOpenId("next openid");
        System.out.println(result.getNext_openid());
        System.out.println(result.getData());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.11. oauth2buildAuthorizationUrl Oauth2.0 认证获取用户信息

  • 第一步:构造URL获取Code

    try {
        String oauthUrl = iService.oauth2buildAuthorizationUrl("回调URL",WxConsts.OAUTH2_SCOPE_USER_INFO, "自定义携带参数");
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

  • "回调URL"是指当用户点击授权,允许获取用户信息之后,页面跳转的地方.一般填写Controller或者Servlet等可以处理code的地方.
  • "自定义携带参数": stage, 微信允许开发者带自定义参数去回调URL上.

3.2.12 oauth2ToGetAccessToken Oauth2.0 认证获取用户信息

  • 第二步:拿code换token和openid

    try {
        WxOAuth2AccessTokenResult result = iService.oauth2ToGetAccessToken("code");
        System.out.println(result.getAccess_token());
        System.out.println(result.getOpenid());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

  • code是微信服务器转发到你的服务器自动携带的参数.用于获取AccessToken.

3.2.13 oauth2ToGetUserInfo Oauth2.0 认证获取用户信息

  • 第三步:拿token换用户信息

    try {
        WxUser user = iService.oauth2ToGetUserInfo("token", new WxUserGet("openid", WxConsts.LANG_CHINA));
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

  • token就是拿code换取回来的AccessToken

3.2.14 batchAddUserToBlackList 批量把用户添加到黑名单

WxError result = iService.batchAddUserToBlackList(Arrays.asList("openid"));
System.out.println(result);

3.2.15 batchRemoveUserFromBlackList 批量把用户删除黑名单

WxError result = iService.batchRemoveUserFromBlackList(Arrays.asList("openid"));
System.out.println(result);

3.2.16 batchGetUsersFromBlackList 批量获取黑名单用户列表

WxUserListResult result = iService.batchGetUsersFromBlackList("next_openid");
System.out.println(result);

  • next_openid 可以为null,如果为null默认从第一个开始获取.
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号