wx-tools 用戶相關(guān)API

2023-03-22 17:20 更新

包括如下接口:
- createUserTag - 創(chuàng)建用戶標(biāo)簽
- deleteUserTag - 刪除用戶標(biāo)簽
- queryAllUserTag - 查詢所有用戶標(biāo)簽
- updateUserTagName - 更新用戶標(biāo)簽名字
- batchMovingUserToNewTag - 批量移動(dòng)用戶到某標(biāo)簽
- batchRemoveUserTag - 批量移除用戶到某標(biāo)簽
- updateUserRemark - 更新用戶備注
- getUserInfoByOpenId - 獲取用戶信息基本信息
- batchGetUserInfo - 批量查詢用戶信息
- batchGetUserOpenId - 批量查詢關(guān)注者openid
- oauth2buildAuthorizationUrl - Oauth2.0 認(rèn)證
- oauth2ToGetAccessToken- Oauth2.0 認(rèn)證
- oauth2ToGetUserInfo- Oauth2.0 認(rèn)證
- batchAddUserToBlackList- 批量把用戶添加到黑名單
- batchRemoveUserFromBlackList- 批量把用戶刪除黑名單
- batchGetUsersFromBlackList- 批量獲取黑名單用戶列表

3.2.1 createUserTag 創(chuàng)建用戶標(biāo)簽

    try {
        WxUserTagResult result = iService.createUserTag("標(biāo)簽名");
        System.out.println(result.getTag().getId());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.2 deleteUserTag 刪除用戶標(biāo)簽

    try {
        WxError result = iService.deleteUserTag(創(chuàng)建tag時(shí)返回的ID);
        System.out.println(result);
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.3 queryAllUserTag 查詢所有用戶標(biāo)簽

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

3.2.4 updateUserTagName 更新用戶標(biāo)簽名字

    try {
        //標(biāo)簽ID,新標(biāo)簽名
        iService.updateUserTagName(1, "new tag name");
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.2.5 batchMovingUserToNewTag 批量移動(dòng)用戶在某標(biāo)簽

    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 批量移除用戶在某標(biāo)簽 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 批量獲取關(guān)注者openid

    try {
        //第一個(gè)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 認(rèn)證獲取用戶信息

  • 第一步:構(gòu)造URL獲取Code

    try {
        String oauthUrl = iService.oauth2buildAuthorizationUrl("回調(diào)URL",WxConsts.OAUTH2_SCOPE_USER_INFO, "自定義攜帶參數(shù)");
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

  • "回調(diào)URL"是指當(dāng)用戶點(diǎn)擊授權(quán),允許獲取用戶信息之后,頁面跳轉(zhuǎn)的地方.一般填寫Controller或者Servlet等可以處理code的地方.
  • "自定義攜帶參數(shù)": stage, 微信允許開發(fā)者帶自定義參數(shù)去回調(diào)URL上.

3.2.12 oauth2ToGetAccessToken Oauth2.0 認(rèn)證獲取用戶信息

  • 第二步:拿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是微信服務(wù)器轉(zhuǎn)發(fā)到你的服務(wù)器自動(dòng)攜帶的參數(shù).用于獲取AccessToken.

3.2.13 oauth2ToGetUserInfo Oauth2.0 認(rèn)證獲取用戶信息

  • 第三步:拿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默認(rèn)從第一個(gè)開始獲取.
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)