Search in sources :

Example 1 with WxError

use of me.chanjar.weixin.common.error.WxError in project project by Ahaochan.

the class BlacklistTest method getOpenIdsByBlacklist.

/**
 * 获取公众号的黑名单列表, 每次最多 10000 个 openId
 * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1471422259_pJMWA">黑名单管理 获取公众号的黑名单列表</a>
 */
@Test
void getOpenIdsByBlacklist() {
    String nextOpenId = null;
    do {
        try {
            WxMpUserBlacklistGetResult result = blackListService.getBlacklist(nextOpenId);
            System.out.println("总拉黑用户数量:" + result.getTotal());
            System.out.println("本次拉取用户数量:" + result.getCount());
            System.out.println("本次拉取的用户openId:" + result.getOpenidList());
            System.out.println("-------------------------------------------------------");
            if (Objects.equals(nextOpenId, result.getNextOpenid())) {
                break;
            }
            nextOpenId = result.getNextOpenid();
        } catch (WxErrorException e) {
            WxError error = e.getError();
            switch(error.getErrorCode()) {
                case -1:
                    Assertions.fail(WxMpErrorMsgEnum.CODE_1.getMsg());
                    break;
                case 40003:
                    Assertions.fail(WxMpErrorMsgEnum.CODE_40003.getMsg());
                    break;
                case 49003:
                    Assertions.fail("传入的openid不属于此AppID");
                    break;
            }
            e.printStackTrace();
        }
    } while (StringUtils.isNotBlank(nextOpenId));
}
Also used : WxMpUserBlacklistGetResult(me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult) WxError(me.chanjar.weixin.common.error.WxError) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) BaseMpTest(moe.ahao.spring.boot.wechat.mp.BaseMpTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with WxError

use of me.chanjar.weixin.common.error.WxError in project project by Ahaochan.

the class UserTagTest method createTag.

/**
 * 创建用户标签, 一个公众号, 最多可以创建 100 个标签
 * @param tagName 标签名
 * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837">用户标签管理 标签管理 创建标签</a>
 */
@ParameterizedTest
@ValueSource(strings = { "测试标签" })
void createTag(String tagName) {
    try {
        WxUserTag tag = tagService.tagCreate(tagName);
        System.out.println("------------------------------------------------------");
        System.out.println("标签Id:" + tag.getId());
        System.out.println("标签名称:" + tag.getName());
    } catch (WxErrorException e) {
        WxError error = e.getError();
        switch(error.getErrorCode()) {
            case -1:
                Assertions.fail(WxMpErrorMsgEnum.CODE_1.getMsg());
                break;
            case 45157:
                Assertions.fail("标签名非法,请注意不能和其他标签重名");
                break;
            case 45158:
                Assertions.fail("标签名长度超过30个字节");
                break;
            case 45056:
                Assertions.fail("创建的标签数过多,请注意不能超过100个");
                break;
        }
        e.printStackTrace();
    }
}
Also used : WxError(me.chanjar.weixin.common.error.WxError) WxUserTag(me.chanjar.weixin.mp.bean.tag.WxUserTag) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with WxError

use of me.chanjar.weixin.common.error.WxError in project project by Ahaochan.

the class UserTest method selectOpenIdListByTagId.

/**
 * 获取标签下的粉丝 openId
 * @param tagId 标签Id
 * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837">用户标签管理 标签管理 获取标签下粉丝列表</a>
 */
@ParameterizedTest
@ValueSource(longs = { 0, 1, 2, 101 })
void selectOpenIdListByTagId(Long tagId) {
    String nextOpenId = null;
    do {
        try {
            WxTagListUser userList = tagService.tagListUser(tagId, nextOpenId);
            System.out.println("本次拉取该标签的粉丝数量:" + userList.getCount());
            WxTagListUser.WxTagListUserData data = userList.getData();
            if (data != null) {
                System.out.println("本次拉取该标签的用户openId:" + data.getOpenidList());
            }
            System.out.println("-------------------------------------------------------");
            nextOpenId = userList.getNextOpenid();
        } catch (WxErrorException e) {
            WxError error = e.getError();
            switch(error.getErrorCode()) {
                case -1:
                    Assertions.fail(WxMpErrorMsgEnum.CODE_1.getMsg());
                    break;
                case 40003:
                    Assertions.fail(WxMpErrorMsgEnum.CODE_40003.getMsg());
                    break;
                case 45159:
                    Assertions.fail("非法的tag_id");
                    break;
            }
            e.printStackTrace();
        }
    } while (StringUtils.isNotBlank(nextOpenId));
}
Also used : WxTagListUser(me.chanjar.weixin.mp.bean.tag.WxTagListUser) WxError(me.chanjar.weixin.common.error.WxError) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with WxError

use of me.chanjar.weixin.common.error.WxError in project starshop by beautautumn.

the class WxSessionCheckingClientAdapter method code2session.

@Override
public void code2session(String code, WxAuthInfo wxAuthInfo) throws WxLoginErrorException {
    final WxMaService wxService = WxConfiguration.getMaService();
    try {
        WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
        wxAuthInfo.setOpenid(session.getOpenid());
        wxAuthInfo.setSessionKey(session.getSessionKey());
    } catch (WxErrorException e) {
        WxError wxError = e.getError();
        throw new WxLoginErrorException(wxError.getErrorCode(), wxError.getErrorMsg());
    }
}
Also used : WxError(me.chanjar.weixin.common.error.WxError) WxLoginErrorException(com.stardata.starshop2.authcontext.domain.WxLoginErrorException) WxMaService(cn.binarywang.wx.miniapp.api.WxMaService) WxMaJscode2SessionResult(cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Example 5 with WxError

use of me.chanjar.weixin.common.error.WxError in project chao-cloud by chaojunzi.

the class WxAppUserInfoServiceImpl method getUserInfo.

@Override
public WxMaUserInfo getUserInfo(String encryptedData, String iv, String code) {
    try {
        // 获取session_key
        log.info("[encryptedData={}]", encryptedData);
        log.info("[iv={}]", iv);
        WxMaJscode2SessionResult sessionInfo = wxMaUserService.getSessionInfo(code);
        log.info("[sessionInfo={}]", sessionInfo);
        // 获取用户信息
        return wxMaUserService.getUserInfo(sessionInfo.getSessionKey(), getEncryptedData(encryptedData), iv);
    } catch (WxErrorException e) {
        WxError error = e.getError();
        throw new BusinessException(error.getErrorMsg());
    }
}
Also used : WxError(me.chanjar.weixin.common.error.WxError) BusinessException(com.chao.cloud.common.exception.BusinessException) WxMaJscode2SessionResult(cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult) WxErrorException(me.chanjar.weixin.common.error.WxErrorException)

Aggregations

WxError (me.chanjar.weixin.common.error.WxError)6 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)6 WxMaJscode2SessionResult (cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 BusinessException (com.chao.cloud.common.exception.BusinessException)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 WxMaService (cn.binarywang.wx.miniapp.api.WxMaService)1 WxLoginErrorException (com.stardata.starshop2.authcontext.domain.WxLoginErrorException)1 WxMpUserBlacklistGetResult (me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult)1 WxTagListUser (me.chanjar.weixin.mp.bean.tag.WxTagListUser)1 WxUserTag (me.chanjar.weixin.mp.bean.tag.WxUserTag)1 BaseMpTest (moe.ahao.spring.boot.wechat.mp.BaseMpTest)1 Test (org.junit.jupiter.api.Test)1